!pip -q install wordcloud
Collecting wordcloud
Downloading wordcloud-1.8.1-cp37-cp37m-manylinux1_x86_64.whl (366 kB)
|████████████████████████████████| 366 kB 8.7 MB/s
Requirement already satisfied: numpy>=1.6.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from wordcloud) (1.19.5)
Requirement already satisfied: pillow in /shared-libs/python3.7/py/lib/python3.7/site-packages (from wordcloud) (8.3.2)
Requirement already satisfied: matplotlib in /shared-libs/python3.7/py/lib/python3.7/site-packages (from wordcloud) (3.4.3)
Requirement already satisfied: cycler>=0.10 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->wordcloud) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /shared-libs/python3.7/py/lib/python3.7/site-packages (from matplotlib->wordcloud) (1.3.2)
Requirement already satisfied: pyparsing>=2.2.1 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from matplotlib->wordcloud) (2.4.7)
Requirement already satisfied: python-dateutil>=2.7 in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from matplotlib->wordcloud) (2.8.2)
Requirement already satisfied: six in /shared-libs/python3.7/py-core/lib/python3.7/site-packages (from cycler>=0.10->matplotlib->wordcloud) (1.16.0)
Installing collected packages: wordcloud
Successfully installed wordcloud-1.8.1
# Some few necessary imports
from tqdm import tqdm, trange
from textblob import TextBlob
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
%matplotlib inline
import random
# Define book text file globally
BOOK_PATH = "/work/How to Win Friends and Influence People - Dale Carnegie.txt"
book = open(BOOK_PATH, 'r')
# First few lines
book.readlines()[:100]
# Get random story chunk
for i, l in enumerate(open(BOOK_PATH, 'r').readlines()[6000:6060]):
if l != '\n':
print(l)
Lee was so saddened, so shocked, that he sent in his resignation and
asked Jefferson Davis, the president of the Confederacy, to appoint
"a younger and abler man." If Lee had wanted to blame the
disastrous failure of Pickett's charge on someone else, he could have
found a score of alibis. Some of his division commanders had failed
him. The cavalry hadn't arrived in time to support the infantry attack.
This had gone wrong and that had gone awry.
But Lee was far too noble to blame others. As Pickett's beaten and
bloody troops struggled back to the Confederate lines, Robert E. Lee
rode out to meet them all alone and greeted them with a self-
condemnation that was little short of sublime. "All this has been my
fault," he confessed. "I and I alone have lost this battle."
Few generals in all history have had the courage and character to
admit that.
Michael Cheung, who teaches our course in Hong Kong, told of how
the Chinese culture presents some special problems and how
sometimes it is necessary to recognize that the benefit of applying a
principle may be more advantageous than maintaining an old
tradition. He had one middle-aged class member who had been
estranged from his son for many years. The father had been an
opium addict, but was now cured. In Chinese tradition an older
person cannot take the first step. The father felt that it was up to his
son to take the initiative toward a reconciliation. In an early session,
he told the class about the grandchildren he had never seen and how
much he desired to be reunited with his son. His classmates, all
Chinese, understood his conflict between his desire and long-
established tradition. The father felt that young people should have
respect for their elders and that he was right in not giving in to his
desire, but to wait for his son to come to him.
Toward the end of the course the father again addressed his class. "I
have pondered this problem," he said. "Dale Carnegie says, 'If you
are wrong, admit it quickly and emphatically.' It is too late for me to
admit it quickly, but I can admit it emphatically. I wronged my son.
He was right in not wanting to see me and to expel me from his life.
I may lose face by asking a younger person's forgiveness, but I was
at fault and it is my responsibility to admit this." The class applauded
and gave him their full support. At the next class he told how he
went to his son's house, asked for and received forgiveness and was
now embarked on a new relationship with his son, his daughter-in-
law and the grandchildren he had at last met.
Elbert Hubbard was one of the most original authors who ever stirred
up a nation, and his stinging sentences often aroused fierce
resentment. But Hubbard with his rare skill for handling people
frequently turned his enemies into friends.
For example, when some irritated reader wrote in to say that he
didn't agree with such and such an article and ended by calling
Hubbard this and that, Elbert Hubbard would answer like this:
Come to think it over, I don't entirely agree with it myself. Not
everything I wrote yesterday appeals to me today. I am glad to learn
for i, l in enumerate(open(BOOK_PATH, 'r').readlines()[:100]):
# remove extra new lines
if len(l) > 3:# or l.isupper():
print(l.split(' \n')[0])
How To Win Friends And Influence People
By
Dale Carnegie
Copyright - 1936 / 1964 / 1981 (Revised Edition)
Library of Congress Catalog Number - 17-19-20-18
ISBN - 0-671-42517-X
Scan Version : v 1.0
Format: Text with cover pictures.
Date Scanned: Unknown
Posted to (Newsgroup): alt.binaries.e-book
Scan/Edit Note: I have made minor changes to this work, including a
contents page, covers etc. I did not scan this work (I only have the
1964 version) but decided to edit it since I am working on Dale's
other book "How To Stop Worrying and Start Living" and thought it
best to make minor improvements. Parts 5 and 6 were scanned and
added to this version by me, they were not included (for some
reason) in the version which appeared on alt.binaries.e-book.
-Salmun
Contents:
Eight Things This Book Will Help You Achieve
Preface to Revised Edition
How This Book Was Written-And Why
Nine Suggestions on How to Get the Most Out of This Book
A Shortcut to Distinction
Part 1 - Fundamental Techniques In Handling People
• 1 - "If You Want to Gather Honey, Don't Kick Over the Beehive"
• 2 - The Big Secret of Dealing with People
• 3 - "He Who Can Do This Has the Whole World with Him. He Who
Cannot, Walks a Lonely Way"
• Eight Suggestions On How To Get The Most Out Of This Book
Part 2 - Six Ways To Make People Like You
• 1 - Do This and You'll Be Welcome Anywhere
• 2 - A Simple Way to Make a Good Impression
• 3 - If You Don't Do This, You Are Headed for Trouble
• 4 - An Easy Way to Become a Good Conversationalist
• 5 - How to Interest People
• 6 - How To Make People Like You Instantly
• In A Nutshell
Part 3 - Twelve Ways To Win People To Your Way Of Thinking
• 1 - You Can't Win an Argument
• 2 - A Sure Way of Making Enemies—and How to Avoid It
• 3 - If You're Wrong, Admit It
• 4 - The High Road to a Man's Reason
• 5 - The Secret of Socrates
• 6 - The Safety Valve in Handling Complaints
• 7 - How to Get Co-operation
• 8 - A Formula That Will Work Wonders for You
• 9 - What Everybody Wants
• 10 - An Appeal That Everybody Likes
• 11 - The Movies Do It. Radio Does It. Why Don't You Do It?
• 12 - When Nothing Else Works, Try This
• In A Nutshell
lines = []
for i, l in tqdm(enumerate(open(BOOK_PATH, 'r').readlines())):
# remove extra new lines
if len(l) > 3:
lines.append(l.split(' \n')[0])
# A preview of lines
print("A preview of first few(10) lines:\n" + '='*40 + f"\n{lines[:10]}\n")
# lines into single story-like corpus
joined_lines = ' '.join(lines)
# A preview of the first few lines of the story like text corpus
print("A preview of the story-like corpus:\n" + '='*40 + f"\n{joined_lines[:200]}\n")
# Designed sentences
sentences = joined_lines.split('.')
# A preview of the first 10 sentences
print("A preview of the first ten sentences:\n" + '='*40 + f"\n{[sent for sent in sentences[:10]]}")
10926it [00:00, 393131.67it/s]A preview of first few(10) lines:
========================================
['How To Win Friends And Influence People', 'By', 'Dale Carnegie', 'Copyright - 1936 / 1964 / 1981 (Revised Edition)', 'Library of Congress Catalog Number - 17-19-20-18', 'ISBN - 0-671-42517-X', 'Scan Version : v 1.0', 'Format: Text with cover pictures.', 'Date Scanned: Unknown', 'Posted to (Newsgroup): alt.binaries.e-book']
A preview of the story-like corpus:
========================================
How To Win Friends And Influence People By Dale Carnegie Copyright - 1936 / 1964 / 1981 (Revised Edition) Library of Congress Catalog Number - 17-19-20-18 ISBN - 0-671-42517-X Scan Version : v 1.0 For
A preview of the first ten sentences:
========================================
['How To Win Friends And Influence People By Dale Carnegie Copyright - 1936 / 1964 / 1981 (Revised Edition) Library of Congress Catalog Number - 17-19-20-18 ISBN - 0-671-42517-X Scan Version : v 1', '0 Format: Text with cover pictures', ' Date Scanned: Unknown Posted to (Newsgroup): alt', 'binaries', 'e-book Scan/Edit Note: I have made minor changes to this work, including a contents page, covers etc', ' I did not scan this work (I only have the 1964 version) but decided to edit it since I am working on Dale\'s other book "How To Stop Worrying and Start Living" and thought it best to make minor improvements', ' Parts 5 and 6 were scanned and added to this version by me, they were not included (for some reason) in the version which appeared on alt', 'binaries', 'e-book', ' -Salmun Contents: Eight Things This Book Will Help You Achieve Preface to Revised Edition How This Book Was Written-And Why Nine Suggestions on How to Get the Most Out of This Book A Shortcut to Distinction Part 1 - Fundamental Techniques In Handling People • 1 - "If You Want to Gather Honey, Don\'t Kick Over the Beehive" • 2 - The Big Secret of Dealing with People • 3 - "He Who Can Do This Has the Whole World with Him']
# Total number of sentences
len(sentences)
import nltk
nltk.download('punkt')
[nltk_data] Downloading package punkt to /root/nltk_data...
[nltk_data] Unzipping tokenizers/punkt.zip.
words = []
for sent in tqdm(sentences):
for word in TextBlob(sent).words:
words.append(word)
# Preview random words
print(words[23], words[42], words[1000])
100%|██████████| 5314/5314 [00:03<00:00, 1712.86it/s]0-671-42517-X e-book make
len(words)
lst = []
for l in open(BOOK_PATH, 'r').readlines():
lst.append(l)
# Random paragraph
' '.join(lst).split('. \n \n ')[100] + '.'
paragraphs = ' '.join(lst).split('. \n \n ')
# Add a fullstop at the end of each paragraph
paragraphs = [p+'.' for p in paragraphs]
# Remove unnecessary new lines in paragraphs
paragraphs = [''.join(' '.join(p.split('\n')).split(' ')) for p in paragraphs]
paragraphs[100]
len(paragraphs)
# get random paragragh
random.choice(paragraphs)
stopwords = set(STOPWORDS)
wc = WordCloud(background_color="red", max_words=20000,
stopwords=stopwords, contour_width=3, contour_color='steelblue',
relative_scaling=0, max_font_size=500)
wc.generate(' '.join(paragraphs))
plt.figure(figsize=(15,12))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")