Class: Corrector::Words

Inherits:
Array
  • Object
show all
Defined in:
app/services/corrector/words.rb

Overview

Breaks source string to words.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = []) ⇒ Words

Initializes the array containing words from the source array or string.

Params:

value

An array of words or a string to be splitted to words. Default value: [].

Returns the array of recognized words from the initial string. Both the map and to_s methods of the array are redefined.

Examples:

Words.new                  # => #<Words []>
Words.new "М В С2"         # => #<Words ["М", "В", "С", "2"]>
Words.new ["М", "В", "С2"] # => #<Words ["М", "В", "С2"]>


33
34
35
36
# File 'app/services/corrector/words.rb', line 33

def initialize(value = [])
  @source = value
  value.is_a?(Array) ? super(value) : super(words)
end

Instance Attribute Details

#sourceObject (readonly)

The source array or string for the current words list.

@example:

words = Words.new "М В С2" # => #< Words ["М", "В", "С", "2"]>
words.source # => "М В С2"

words = Wrods.new ["М", "В", "С2"] # => #< Words ["М", "В", "С2"]>
words.source # => ["М", "В", "С2"]

Returns either array or string.



18
19
20
# File 'app/services/corrector/words.rb', line 18

def source
  @source
end

Instance Method Details

#mapObject

Maps Words class object.

Returns the mapped Words object.

Examples:

words =     Words.new(%w(РАЗ ДВА)).map { |i| i }
words.is_a? Words # => true


45
46
47
# File 'app/services/corrector/words.rb', line 45

def map
  Words.new super.flatten
end

#to_sObject

Converts words list to a string. Joins words divided by the ^ symbol and removes spaces around the slash.

Returns a converted string with source method to check the source.

Examples:

words = Words.new %w(К ^ М / C ^ 2)
words.to_s # => "КМ/С2"


57
58
59
# File 'app/services/corrector/words.rb', line 57

def to_s
  map(&:strip).join(" ").gsub(/\s*\^\s*/, "").gsub(/\s*\/\s*/, "/")
end