Module: Vangrail::Dojo::Adapt

Defined in:
lib/vangrail/dojo.rb

Overview

Substitutes other words from the same concept. Not a model: the attacker who read the lexicon.

Class Method Summary collapse

Class Method Details

.rewrites(text, limit: 12) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vangrail/dojo.rb', line 106

def rewrites(text, limit: 12)
  found = [text]
  NLP::CONCEPTS.each_value do |lang|
    lang.each_value do |words|
      words.each do |word|
        next unless text.match?(/\b#{Regexp.escape(word)}\b/i)

        words.each do |alt|
          next if alt.casecmp?(word)

          found << text.sub(/\b#{Regexp.escape(word)}\b/i, alt)
          return found.uniq if found.uniq.size >= limit
        end
      end
    end
  end
  found.uniq
end