Module: Ronin::Support::Text::Typo

Defined in:
lib/ronin/support/text/typo.rb,
lib/ronin/support/text/typo/generator.rb,
lib/ronin/support/text/typo/exceptions.rb

Overview

Generates typos in words.

Core-Ext Methods

Since:

  • 1.0.0

Defined Under Namespace

Classes: Generator, NoTypoPossible

Constant Summary collapse

OMIT_CHARS =

Typo generator that repeats characters.

Since:

  • 1.0.0

Generator[
[/(?<=\w)ae(?=\w)/, 'e']
REPEAT_CHARS =

Typo generator that repeats characters.

Since:

  • 1.0.0

SWAP_CHARS =

Typo generator that swaps neighboring characters.

Since:

  • 1.0.0

SWAP_SYMBOLS =

Typo generator that swaps different symbols.

Since:

  • 1.0.0

CHANGE_SUFFIX =

Typo generator that changes the suffix of words.

Since:

  • 1.0.0

DEFAULT =
Note:

Does not include the SWAP_SYMBOLS typo rules.

Default typo generator.

Since:

  • 1.0.0

Generator.new(
  OMIT_CHARS.rules + REPEAT_CHARS.rules + SWAP_CHARS.rules +
  CHANGE_SUFFIX.rules
)

Class Method Summary collapse

Class Method Details

.change_suffixGenerator

Typo generator that changes the suffix of words.

Returns:

See Also:

Since:

  • 1.0.0



158
159
160
# File 'lib/ronin/support/text/typo.rb', line 158

def self.change_suffix
  CHANGE_SUFFIX
end

.each_substitution(word, **kwargs) {|typoed| ... } ⇒ Enumerator

Enumerates over every typo mistake for the given word.

Parameters:

  • word (String)

    The given String.

  • kwargs (Hash{Symbol => Boolean})

    Additional keyword arguments.

Options Hash (**kwargs):

  • omit (Boolean)

    Enables/disables omission of repeated characters.

  • repeat (Boolean)

    Enables/disables repeatition of single characters.

  • swap (Boolean)

    Enables/disables swapping of certain common character pairs.

  • suffix (Boolean)

    Enables/disables changing the suffixes of words.

Yields:

  • (typoed)

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

Yield Parameters:

  • A (String)

    modified version of the original String.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

See Also:

Since:

  • 1.0.0



263
264
265
# File 'lib/ronin/support/text/typo.rb', line 263

def self.each_substitution(word,**kwargs,&block)
  generator(**kwargs).each_substitution(word,&block)
end

.generator(**kwargs) ⇒ Generator

Builds a set of typo substitution rules.

Parameters:

  • kwargs (Hash{Symbol => Boolean})

    Additional typo options.

Options Hash (**kwargs):

  • :omit_chars (Boolean)

    Whether to enable/disable omission of repeated characters.

  • :repeat_chars (Boolean)

    Whether to enable/disable repeatition of single characters.

  • :swap_chars (Boolean)

    Whether to enable/disable swapping of certain common character pairs.

  • :change_suffix (Boolean)

    Whether to enable/disable changing the suffixes of words.

Returns:

Since:

  • 1.0.0



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ronin/support/text/typo.rb', line 184

def self.generator(**kwargs)
  if kwargs.empty?
    DEFAULT
  else
    rules = []
    rules.concat(OMIT_CHARS.rules)    if kwargs[:omit_chars]
    rules.concat(REPEAT_CHARS.rules)  if kwargs[:repeat_chars]
    rules.concat(SWAP_CHARS.rules)    if kwargs[:swap_chars]
    rules.concat(SWAP_SYMBOLS.rules)  if kwargs[:swap_symbols]
    rules.concat(CHANGE_SUFFIX.rules) if kwargs[:change_suffix]

    Generator.new(rules)
  end
end

.omit_charsGenerator

Typo generator that repeats characters.

Returns:

See Also:

Since:

  • 1.0.0



114
115
116
# File 'lib/ronin/support/text/typo.rb', line 114

def self.omit_chars
  OMIT_CHARS
end

.repeat_charsGenerator

Typo generator that repeats characters.

Returns:

See Also:

Since:

  • 1.0.0



125
126
127
# File 'lib/ronin/support/text/typo.rb', line 125

def self.repeat_chars
  REPEAT_CHARS
end

.substitute(word, **kwargs) ⇒ String

Returns a random typo substitution for the given word.

Parameters:

  • word (String)

    The given String.

  • kwargs (Hash{Symbol => Boolean})

    Additional keyword arguments.

Options Hash (**kwargs):

  • omit (Boolean)

    Enables/disables omission of repeated characters.

  • repeat (Boolean)

    Enables/disables repeatition of single characters.

  • swap (Boolean)

    Enables/disables swapping of certain common character pairs.

  • suffix (Boolean)

    Enables/disables changing the suffixes of words.

Returns:

  • (String)

    A random typo of the given word.

See Also:

Since:

  • 1.0.0



225
226
227
# File 'lib/ronin/support/text/typo.rb', line 225

def self.substitute(word,**kwargs)
  generator(**kwargs).substitute(word)
end

.swap_charsGenerator

Typo generator that swaps neighboring characters.

Returns:

See Also:

Since:

  • 1.0.0



136
137
138
# File 'lib/ronin/support/text/typo.rb', line 136

def self.swap_chars
  SWAP_CHARS
end

.swap_symbolsGenerator

Typo generator that swaps different symbols.

Returns:

See Also:

Since:

  • 1.0.0



147
148
149
# File 'lib/ronin/support/text/typo.rb', line 147

def self.swap_symbols
  SWAP_SYMBOLS
end