Obscure banned terms (comic book style)

This commit is contained in:
Joel McCoy
2014-12-29 13:38:09 -05:00
parent 003b17fd44
commit 73ab3c8a06
2 changed files with 38 additions and 18 deletions

View File

@@ -18,6 +18,24 @@ module Ebooks::Boodoo
value value
end end
def obscure_curse(len)
s = []
c = ['!', '@', '$', '%', '^', '&', '*']
len.times do
s << c.sample
end
s.join('')
end
def obscure_curses(tweet)
# TODO: Ignore banned terms that are part of @-mentions
$banned_terms.each do |term|
re = Regexp.new("\\b#{term}\\b", "i")
tweet.gsub!(re, Ebooks::Boodoo.obscure_curse(term.size))
end
tweet
end
def parse_array(value, array_splitter=nil) def parse_array(value, array_splitter=nil)
array_splitter ||= / *[,;]+ */ array_splitter ||= / *[,;]+ */
value.split(array_splitter).map(&:strip) value.split(array_splitter).map(&:strip)
@@ -40,17 +58,6 @@ class Ebooks::TweetMeta
end end
end end
class Ebooks::Boodoo::Model < Ebooks::Model
def valid_tweet?(tokens, limit)
tweet = NLP.reconstruct(tokens)
found_banned = $banned_words.any? do |word|
re = Regexp.new("\\b#{word}\\b", "i")
re.match tweet
end
tweet.length <= limit && !found_banned && !NLP.unmatched_enclosers?(tweet)
end
end
class Ebooks::Boodoo::BoodooBot < Ebooks::Bot class Ebooks::Boodoo::BoodooBot < Ebooks::Bot
@required_fields = ['consumer_key', 'consumer_secret', @required_fields = ['consumer_key', 'consumer_secret',
'access_token', 'access_token_secret', 'access_token', 'access_token_secret',
@@ -122,9 +129,9 @@ class Ebooks::Boodoo::BoodooBot < Ebooks::Bot
def make_model! def make_model!
log "Updating model: #{@model_path}" log "Updating model: #{@model_path}"
Ebooks::Boodoo::Model.consume(@archive_path).save(@model_path) Ebooks::Model.consume(@archive_path).save(@model_path)
log "Loading model..." log "Loading model..."
@model = Ebooks::Boodoo::Model.load(@model_path) @model = Ebooks::Model.load(@model_path)
end end
def can_run? def can_run?

23
bots.rb
View File

@@ -22,8 +22,9 @@ class UserInfo
end end
end end
class CloneBot < BoodooBot class BoodooBot
attr_accessor :original, :model, :model_path, :auth_name, :archive_path, :archive attr_accessor :original, :model, :model_path, :auth_name, :archive_path, :archive
attr_accessor :followers, :following
# alias_method :oauth_token, :access_token # alias_method :oauth_token, :access_token
# alias_method :oauth_token_secret, :access_token_secret # alias_method :oauth_token_secret, :access_token_secret
def configure def configure
@@ -69,8 +70,8 @@ class CloneBot < BoodooBot
# @have_talked = {} # @have_talked = {}
if can_run? if can_run?
get_archive! unless has_archive? get_archive!
make_model! unless has_model? make_model!
else else
missing_fields.each {|missing| missing_fields.each {|missing|
log "Can't run without #{missing}" log "Can't run without #{missing}"
@@ -211,6 +212,18 @@ class CloneBot < BoodooBot
end end
end end
# Prefilter for banned terms before tweeting
def tweet(text, *args)
text = obscure_curses(text)
super(text, *args)
end
# Prefilter for banned terms before replying
def reply(ev, text, opts={})
text = obscure_curses(text)
super(ev, text, opts)
end
private private
def load_model! def load_model!
return if @model return if @model
@@ -222,7 +235,7 @@ class CloneBot < BoodooBot
end end
end end
CloneBot.new(SETTINGS['BOT_NAME']) do |bot| BoodooBot.new(SETTINGS['BOT_NAME']) do |bot|
# CloneBot#configure does everything! # BoodooBot#configure does everything!
bot bot
end end