2013-02-06 20:09:31 -05:00
|
|
|
#
|
2013-04-10 05:00:50 -04:00
|
|
|
# Given a string, tell us whether or not is acceptable.
|
2013-02-06 20:09:31 -05:00
|
|
|
#
|
|
|
|
class TextSentinel
|
|
|
|
|
|
|
|
attr_accessor :text
|
|
|
|
|
2013-04-10 05:00:50 -04:00
|
|
|
def initialize(text, opts=nil)
|
|
|
|
@opts = opts || {}
|
2013-05-23 00:52:12 -04:00
|
|
|
@text = text.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '')
|
2013-04-10 05:00:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.body_sentinel(text)
|
|
|
|
TextSentinel.new(text, min_entropy: SiteSetting.body_min_entropy)
|
2013-02-06 20:09:31 -05:00
|
|
|
end
|
|
|
|
|
2013-02-08 16:55:40 -05:00
|
|
|
def self.title_sentinel(text)
|
2013-02-25 11:42:20 -05:00
|
|
|
TextSentinel.new(text,
|
2013-02-08 16:55:40 -05:00
|
|
|
min_entropy: SiteSetting.title_min_entropy,
|
2013-04-10 05:00:50 -04:00
|
|
|
max_word_length: SiteSetting.max_word_length)
|
2013-02-08 16:55:40 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
# Entropy is a number of how many unique characters the string needs.
|
2013-06-07 14:47:07 -04:00
|
|
|
# Non-ASCII characters are weighted heavier since they contain more "information"
|
2013-02-06 20:09:31 -05:00
|
|
|
def entropy
|
2013-06-07 14:47:07 -04:00
|
|
|
chars = @text.to_s.strip.split('')
|
|
|
|
@entropy ||= chars.pack('M*'*chars.size).gsub("\n",'').split('=').uniq.size
|
2013-02-06 20:09:31 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
def valid?
|
2013-05-23 00:52:12 -04:00
|
|
|
@text.present? &&
|
2013-05-23 13:48:37 -04:00
|
|
|
seems_meaningful? &&
|
|
|
|
seems_pronounceable? &&
|
|
|
|
seems_unpretentious? &&
|
|
|
|
seems_quiet? &&
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2013-02-06 20:09:31 -05:00
|
|
|
|
2013-05-23 13:48:37 -04:00
|
|
|
def symbols_regex
|
|
|
|
/[\ -\/\[-\`\:-\@\{-\~]/m
|
|
|
|
end
|
2013-02-06 20:09:31 -05:00
|
|
|
|
2013-05-23 13:48:37 -04:00
|
|
|
def seems_meaningful?
|
2013-05-23 00:52:12 -04:00
|
|
|
# Minimum entropy if entropy check required
|
2013-05-23 13:48:37 -04:00
|
|
|
@opts[:min_entropy].blank? || (entropy >= @opts[:min_entropy])
|
|
|
|
end
|
2013-02-06 20:09:31 -05:00
|
|
|
|
2013-05-23 13:48:37 -04:00
|
|
|
def seems_pronounceable?
|
2013-05-23 00:52:12 -04:00
|
|
|
# At least some non-symbol characters
|
|
|
|
# (We don't have a comprehensive list of symbols, but this will eliminate some noise)
|
2013-05-23 13:48:37 -04:00
|
|
|
@text.gsub(symbols_regex, '').size > 0
|
|
|
|
end
|
2013-02-06 20:09:31 -05:00
|
|
|
|
2013-05-23 13:48:37 -04:00
|
|
|
def seems_unpretentious?
|
2013-05-23 00:52:12 -04:00
|
|
|
# Don't allow super long words if there is a word length maximum
|
2013-05-27 10:56:55 -04:00
|
|
|
@opts[:max_word_length].blank? || @text.split(/\s/).map(&:size).max <= @opts[:max_word_length]
|
2013-05-23 13:48:37 -04:00
|
|
|
end
|
2013-02-06 20:09:31 -05:00
|
|
|
|
|
|
|
|
2013-05-23 13:48:37 -04:00
|
|
|
def seems_quiet?
|
2013-02-14 16:09:57 -05:00
|
|
|
# We don't allow all upper case content in english
|
2013-05-23 13:48:37 -04:00
|
|
|
not((@text =~ /[A-Z]+/) && (@text == @text.upcase))
|
2013-02-06 20:09:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|