Class: TextSentinel
- Inherits:
-
Object
- Object
- TextSentinel
- Defined in:
- lib/text_sentinel.rb
Constant Summary collapse
- ENTROPY_SCALE =
0.7
Instance Attribute Summary collapse
-
#text ⇒ Object
Returns the value of attribute text.
Class Method Summary collapse
Instance Method Summary collapse
-
#entropy ⇒ Object
Number of unique bytes.
-
#initialize(text, opts = nil) ⇒ TextSentinel
constructor
A new instance of TextSentinel.
-
#seems_meaningful? ⇒ Boolean
Ensure minumum entropy.
-
#seems_pronounceable? ⇒ Boolean
At least one non-symbol character.
-
#seems_quiet? ⇒ Boolean
Ensure at least one lowercase letter.
-
#seems_unpretentious? ⇒ Boolean
Ensure maximum word length.
- #valid? ⇒ Boolean
Constructor Details
#initialize(text, opts = nil) ⇒ TextSentinel
Returns a new instance of TextSentinel.
8 9 10 11 |
# File 'lib/text_sentinel.rb', line 8 def initialize(text, opts = nil) @opts = opts || {} @text = text.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "") end |
Instance Attribute Details
#text ⇒ Object
Returns the value of attribute text.
4 5 6 |
# File 'lib/text_sentinel.rb', line 4 def text @text end |
Class Method Details
.body_sentinel(text, opts = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/text_sentinel.rb', line 13 def self.body_sentinel(text, opts = {}) entropy = SiteSetting.body_min_entropy if opts[:private_message] scale_entropy = SiteSetting..to_f / SiteSetting.min_post_length.to_f entropy = (entropy * scale_entropy).to_i entropy = (SiteSetting..to_f * ENTROPY_SCALE).to_i if entropy > SiteSetting. else entropy = (SiteSetting.min_post_length.to_f * ENTROPY_SCALE).to_i if entropy > SiteSetting.min_post_length end TextSentinel.new(text, min_entropy: entropy) end |
.title_sentinel(text) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/text_sentinel.rb', line 29 def self.title_sentinel(text) entropy = if SiteSetting.min_topic_title_length > SiteSetting.title_min_entropy SiteSetting.title_min_entropy else (SiteSetting.min_topic_title_length.to_f * ENTROPY_SCALE).to_i end TextSentinel.new(text, min_entropy: entropy, max_word_length: SiteSetting.title_max_word_length) end |
Instance Method Details
#entropy ⇒ Object
Number of unique bytes
40 41 42 |
# File 'lib/text_sentinel.rb', line 40 def entropy @entropy ||= @text.strip.bytes.uniq.size end |
#seems_meaningful? ⇒ Boolean
Ensure minumum entropy
50 51 52 |
# File 'lib/text_sentinel.rb', line 50 def seems_meaningful? @opts[:min_entropy].nil? || entropy >= @opts[:min_entropy] end |
#seems_pronounceable? ⇒ Boolean
At least one non-symbol character
55 56 57 |
# File 'lib/text_sentinel.rb', line 55 def seems_pronounceable? @text.match?(/\p{Alnum}/) end |
#seems_quiet? ⇒ Boolean
Ensure at least one lowercase letter
66 67 68 69 |
# File 'lib/text_sentinel.rb', line 66 def seems_quiet? SiteSetting.allow_uppercase_posts || @text.match?(/\p{Lowercase_Letter}|\p{Other_Letter}/) || !@text.match?(/\p{Letter}/) end |
#seems_unpretentious? ⇒ Boolean
Ensure maximum word length
60 61 62 63 |
# File 'lib/text_sentinel.rb', line 60 def seems_unpretentious? skipped_locales.include?(SiteSetting.default_locale) || @opts[:max_word_length].nil? || !@text.match?(/\p{Alnum}{#{@opts[:max_word_length] + 1},}/) end |
#valid? ⇒ Boolean
44 45 46 47 |
# File 'lib/text_sentinel.rb', line 44 def valid? @text.present? && seems_meaningful? && seems_pronounceable? && seems_unpretentious? && seems_quiet? end |