Skip to content

Instantly share code, notes, and snippets.

@kkroesch
Created October 18, 2016 15:37
Show Gist options
  • Save kkroesch/29ac39d9b891f10786e838c5e24aabb4 to your computer and use it in GitHub Desktop.
Save kkroesch/29ac39d9b891f10786e838c5e24aabb4 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment