Last active
August 30, 2016 07:54
-
-
Save bernardo-cs/2d52410618f86d6f594a to your computer and use it in GitHub Desktop.
Clickbait identifier by Word or Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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