class Ronin::Support::Text::Typo::Generator
Geneerates one or more typos based on a series of substitution rules.
@api public
@since 1.0.0
Attributes
Public Class Methods
Source
# File lib/ronin/support/text/typo/generator.rb, line 67 def self.[](*rules) new(rules) end
Creates a new generator.
@param [Array<(Regexp
, String
)>] rules
The typo pattern patterns and substition strings for the generator.
@return [Generator]
The newly created typo generator.
@example
Text::Typo::Generator[ [/(?<=\w)o(?=\w)/, 'oo'], [/(?<=\w)l(?=\w)/, 'll'], [/(?<=\w)s(?=\w)/, 'ss'] ]
Source
Public Instance Methods
Source
# File lib/ronin/support/text/typo/generator.rb, line 81 def +(other) Generator.new(@rules + other.rules) end
Combines the typo generator’s rules with another typo generator’s rules.
@param [Generator] other
The other typo generator.
@return [Generator]
The new typo generator object.
Source
# File lib/ronin/support/text/typo/generator.rb, line 127 def each_substitution(word) return enum_for(__method__,word) unless block_given? @rules.each do |regexp,replace| offset = 0 while (match = word.match(regexp,offset)) start, stop = match.offset(0) new_string = word.dup new_string[start...stop] = replace yield new_string offset = stop end end return nil end
Enumerates over every possible typo substition for the given word.
@param [String] word
The original word to typo.
@yield [typo_word]
If a block is given, it will be passed each typo variation of the original word.
@yieldparam [String] typo_word
One of the typoed variations of the original word.
@return [Enumerator]
If no block is given, an Enumrator object will be returned.
Source
# File lib/ronin/support/text/typo/generator.rb, line 97 def substitute(word) matching_rules = @rules.select do |regexp,replace| word =~ regexp end if matching_rules.empty? raise(NoTypoPossible,"no possible typo substitution found in word: #{word.inspect}") end regexp, replace = matching_rules.sample return word.sub(regexp,replace) end
Performs a random typo substitution of the given word.
@param [String] word
The original word.
@return [String]
The random typoed version of the original word.
@raise [NoTypoPossible]
No possible typo substitutions were found in the word.