Skip to content

Instantly share code, notes, and snippets.

@paultopia
Created October 24, 2016 16:39
Show Gist options
  • Select an option

  • Save paultopia/cacd3c0c7a6c7aaa9daef086a6c9fbc5 to your computer and use it in GitHub Desktop.

Select an option

Save paultopia/cacd3c0c7a6c7aaa9daef086a6c9fbc5 to your computer and use it in GitHub Desktop.
python 2 script to extract trump insults from NYT article
# you can extract the text from NYT article in javascript console with document.documentElement.outerText
# nyt article is here: http://www.nytimes.com/interactive/2016/01/28/upshot/donald-trump-twitter-insults.html
# first open the file. I saved it as trumpinsults2.txt on my hard drive
# I also changed non-ascii quotes to quotes and got rid of other non-ascii stuff using sublime text, and some code I
# wrote for the purpose from here: https://github.com/paultopia/ascii_punctuation
with open("trumpinsults2.txt") as t:
insults = t.readlines()
# looking at the file, one thing we notice is that labels are not quotes, but trump's actual text is quoted,
# and followed by a newline. that's easy to grab.
def idquotes(line):
if line[0] == '"' and line[-2] == '"':
return True
return False
# insults are separated by quotes rather than spaces, so replace quotes with spaces, and then strip other punctuation
# + strip duplicative whitespace + make all same case
import string
def stripjunk(line):
newline = []
for c in line:
if c == '"':
newline.append(' ')
else:
newline.append(c)
return ' '.join(''.join(newline).split()).translate(string.maketrans("",""), string.punctuation).lower()
# now it's a one-liner:
cleaninsults = ' '.join([stripjunk(l) for l in insults if idquotes(l)])
# save to file, then plug into a wordcloud generator like this one: https://www.jasondavies.com/wordcloud/
with open("cleaninsults.txt", "w") as ifile:
ifile.write(cleaninsults)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment