Skip to content

Instantly share code, notes, and snippets.

@bernardo-cs
Last active August 30, 2016 07:54
Show Gist options
  • Save bernardo-cs/2d52410618f86d6f594a to your computer and use it in GitHub Desktop.
Save bernardo-cs/2d52410618f86d6f594a to your computer and use it in GitHub Desktop.
Clickbait identifier by Word or Regex
# Class used to identify if a string it considered clickbait
class Spotlight::Examiner
attr_accessor :text, :filters
def initialize(text: '', filters: [ WordFilter.new, RegexFilter.new])
@text = text || ''
@filters = filters
end
def self.clickbait?(text, filters=[ WordFilter.new, RegexFilter.new])
new(text: text, filters: filters).clickbait?
end
def clickbait?
binding.pry if filters.flatten.size !=2
filters.map{|f| f.match(text)}.reduce(:|)
end
private
class WordFilter
BLACKLIST_WORDS = [
'GIVEAWAY',
'WIN'
]
def match(text)
!!text.match(words_regex)
end
private
def words_regex
/#{BLACKLIST_WORDS.join('|')}/
end
end
class RegexFilter
BLACKLIST_REGEX = /follow.*RT| RT |^RT/
def match(text)
!!text.match(BLACKLIST_REGEX)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment