kkroesch
10/18/2016 - 3:37 PM

A primitive search for keywords in a text.

A primitive search for keywords in a text.

import string
from collections import Counter

george_takei_tweet = "When the media gave him millions in free air time, Trump loved them. Now when they do their job and ask questions, it's a global conspiracy."
tweet = george_takei_tweet.lower()
tweet = ''.join(ch for ch in tweet if ch not in set(string.punctuation))
contains = Counter(tweet.split(' '))

eval("contains['trump'] and contains['conspiracy']")
# ==> 1

eval("contains['trump'] and not contains['conspiracy']")
# ==> False