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

rules[R]

The typo substitution rules.

@return [Array<(Regexp, String)>]

Public Class Methods

[](*rules) click to toggle source

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']
]
# File lib/ronin/support/text/typo/generator.rb, line 67
def self.[](*rules)
  new(rules)
end
new(rules) click to toggle source

Initializes the typo substitution rules.

@param [Array<(Regexp, String)>] rules

The typo pattern patterns and substition strings for the
generator.
# File lib/ronin/support/text/typo/generator.rb, line 46
def initialize(rules)
  @rules = rules
end

Public Instance Methods

+(other) click to toggle source

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.
# File lib/ronin/support/text/typo/generator.rb, line 81
def +(other)
  Generator.new(@rules + other.rules)
end
each_substitution(word) { |new_string| ... } click to toggle source

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.
# 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
substitute(word) click to toggle source

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.
# 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
to_a() click to toggle source

Converts the generator into the Array of substitution rules.

@return [Array<(Regexp, String)>]

The Array of patterns and substitution strings.
# File lib/ronin/support/text/typo/generator.rb, line 153
def to_a
  @rules
end