Class: Ronin::Support::Text::Typo::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/text/typo/generator.rb

Overview

Geneerates one or more typos based on a series of substitution rules.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ Generator

Initializes the typo substitution rules.

Parameters:

  • rules (Array<(Regexp, String)>)

    The typo pattern patterns and substition strings for the generator.

Since:

  • 1.0.0



46
47
48
# File 'lib/ronin/support/text/typo/generator.rb', line 46

def initialize(rules)
  @rules = rules
end

Instance Attribute Details

#rulesArray<(Regexp, String)> (readonly)

The typo substitution rules.

Returns:

Since:

  • 1.0.0



37
38
39
# File 'lib/ronin/support/text/typo/generator.rb', line 37

def rules
  @rules
end

Class Method Details

.[](*rules) ⇒ Generator

Creates a new generator.

Examples:

Text::Typo::Generator[
  [/(?<=\w)o(?=\w)/, 'oo'],
  [/(?<=\w)l(?=\w)/, 'll'],
  [/(?<=\w)s(?=\w)/, 'ss']
]

Parameters:

  • rules (Array<(Regexp, String)>)

    The typo pattern patterns and substition strings for the generator.

Returns:

  • (Generator)

    The newly created typo generator.

Since:

  • 1.0.0



67
68
69
# File 'lib/ronin/support/text/typo/generator.rb', line 67

def self.[](*rules)
  new(rules)
end

Instance Method Details

#+(other) ⇒ Generator

Combines the typo generator's rules with another typo generator's rules.

Parameters:

  • other (Generator)

    The other typo generator.

Returns:

  • (Generator)

    The new typo generator object.

Since:

  • 1.0.0



81
82
83
# File 'lib/ronin/support/text/typo/generator.rb', line 81

def +(other)
  Generator.new(@rules + other.rules)
end

#each_substitution(word) {|typo_word| ... } ⇒ Enumerator

Enumerates over every possible typo substition for the given word.

Parameters:

  • word (String)

    The original word to typo.

Yields:

  • (typo_word)

    If a block is given, it will be passed each typo variation of the original word.

Yield Parameters:

  • typo_word (String)

    One of the typoed variations of the original word.

Returns:

  • (Enumerator)

    If no block is given, an Enumrator object will be returned.

Since:

  • 1.0.0



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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) ⇒ String

Performs a random typo substitution of the given word.

Parameters:

  • word (String)

    The original word.

Returns:

  • (String)

    The random typoed version of the original word.

Raises:

  • (NoTypoPossible)

    No possible typo substitutions were found in the word.

Since:

  • 1.0.0



97
98
99
100
101
102
103
104
105
106
107
108
109
# 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_aArray<(Regexp, String)>

Converts the generator into the Array of substitution rules.

Returns:

Since:

  • 1.0.0



153
154
155
# File 'lib/ronin/support/text/typo/generator.rb', line 153

def to_a
  @rules
end