ruby on rails - Tweets exclusion -
let's pretend have site users create topics , write threads on fruit.
to keep users informed of fruit conversations on entire web, collect tweets related specific topic , create threads based on contents of tweet.
it's important tweets relevant topic, obviously. let's user creates topic called apples , oranges. pull tweets contains keywords apples an/or oranges.
the problem i'm having twitter users write tweet includes keywords apples, oranges, pears, example, , gets collected , posted thread apples , oranges discussion topic. makes users angry!
so need way filter out tweet includes fruit words other apples and/or oranges.
for example, if twitter user writes "i love apples, oranges, pears, , grapes" tweet should not included.
now can make twitter search query sophisticated. exclusion logic have performed in ruby after tweets collected.
programmatically, how go solving this?
determine words related topic name. pears, grapes, etc. can exclude tweets use these related words.
one way using google sets.
note: in unfortunate position of not condoning own solution due service not having official api (as awesome be!). though if going use strategy suggest storing google set result.
require 'google_set' twitter_search_terms = ['apples', 'oranges'] # mocked twitter search method tweets = search_twitter(twitter_search_terms) # returns ["both apples , oranges great!", "i love apples, oranges, pears, , grapes."] related_words = googleset.for(*twitter_search_terms) # returns ["apples", "oranges", "bananas", "peaches", "pears", "grapes", "strawberries", "plums", ...] related_words = (related_words - twitter_search_terms).each(&:downcase) good_tweets = [] bad_tweets = [] tweets.each |tweet| tweet_words = tweet.downcase.split # remove non-word characters tweet_words = tweet_words.map { |word| word.gsub(/\w+/, '') }.compact if (tweet_words - related_words).size == tweet_words.size good_tweets << tweet else bad_tweets << tweet end end p good_tweets # returns ["both apples , oranges great!"] p bad_tweets # returns ["i love apples, oranges, pears, , grapes."]
Comments
Post a Comment