Class: AnagramSolver::Permutator
- Inherits:
-
Object
- Object
- AnagramSolver::Permutator
- Defined in:
- lib/anagram_solver/permutator.rb
Instance Attribute Summary collapse
-
#bg_process ⇒ Object
readonly
Returns the value of attribute bg_process.
-
#precomputed_list ⇒ Object
readonly
Returns the value of attribute precomputed_list.
-
#precomputed_list_old ⇒ Object
readonly
Returns the value of attribute precomputed_list_old.
-
#word_list ⇒ Object
readonly
Returns the value of attribute word_list.
Instance Method Summary collapse
-
#initialize(word_list) ⇒ Permutator
constructor
Initializes word_list instance variable, precomputed_list assigning it a hash whose default values is an empty array.
-
#precompute ⇒ Object
Generates a hash whose keys are sorted alphabetically and values (i.e rhd ) are words in its normal order.
-
#precompute_old ⇒ Object
Used for benchmarking.
-
#sort!(word) ⇒ Object
Orders any given word in alphabetical order by unpacking them (i.e spliting ) sort and packing them again ( i.e joining ).
Constructor Details
#initialize(word_list) ⇒ Permutator
Initializes word_list instance variable, precomputed_list assigning it a hash whose default values is an empty array.
Precomputes word_list in underneath the hood. (i.e A in-process thread that AsyncConsumer offers. )
46 47 48 49 50 51 |
# File 'lib/anagram_solver/permutator.rb', line 46 def initialize(word_list) @word_list = word_list @precomputed_list = Hash.new([]) @bg_process = AsyncConsumer.bg_process(self) { |s| s.precompute } @precomputed_list_old = Hash.new([]) end |
Instance Attribute Details
#bg_process ⇒ Object (readonly)
Returns the value of attribute bg_process.
37 38 39 |
# File 'lib/anagram_solver/permutator.rb', line 37 def bg_process @bg_process end |
#precomputed_list ⇒ Object (readonly)
Returns the value of attribute precomputed_list.
36 37 38 |
# File 'lib/anagram_solver/permutator.rb', line 36 def precomputed_list @precomputed_list end |
#precomputed_list_old ⇒ Object (readonly)
Returns the value of attribute precomputed_list_old.
37 38 39 |
# File 'lib/anagram_solver/permutator.rb', line 37 def precomputed_list_old @precomputed_list_old end |
#word_list ⇒ Object (readonly)
Returns the value of attribute word_list.
36 37 38 |
# File 'lib/anagram_solver/permutator.rb', line 36 def word_list @word_list end |
Instance Method Details
#precompute ⇒ Object
Generates a hash whose keys are sorted alphabetically and values (i.e rhd ) are words in its normal order.
Given a list pots, stop, opts When I precompute it Then I should have a hash like: { “opst” => [ “pots”, “stop”, “opts” ] }
NOTE: precompute only gets the first word it ‘slices’ off a line. Therefore if a line was: “pots, opts” It would get the first word, which is pots
.
However if it was: “, Deviation’s, pots”; It would slice Deviation’s
It is also slices words with accents, such as: émigré‘s Ångström
If you’re on UNIX-like there might exist a dict word in see /usr/share/dict/words
Want to learn more about Regexp? Fear not: open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1500.html
95 96 97 98 99 100 101 |
# File 'lib/anagram_solver/permutator.rb', line 95 def precompute word_list.each do |line| # word = line.chomp.slice(/\b['\wÀ-ÖÙ-Üà-öù-ü]+/i) word = line.chomp precomputed_list[sort!(word)] += [word] end end |
#precompute_old ⇒ Object
Used for benchmarking
56 57 58 59 60 61 62 |
# File 'lib/anagram_solver/permutator.rb', line 56 def precompute_old warn "This method should not be used, please use precompute instead." word_list.rewind.each do |line| word = line.chomp precomputed_list_old[word.split('').sort.join.freeze] += [word] end end |
#sort!(word) ⇒ Object
Orders any given word in alphabetical order by unpacking them (i.e spliting ) sort and packing them again ( i.e joining ). This facilitates when trying to find combinations for words, as we only have to sort once and return results by calling a hash with a sorted key.
ruby-doc.org/core-2.0.0/Array.html#method-i-pack See: en.wikipedia.org/wiki/UTF-8 Unpacks a string using: Integer UTF-8 character, as it can represent every character in the Unicode char set. Such as émigré‘s
-
means all remaining arrays elements will be
converted.
Freezes string, otherwise Ruby would generate another String object instead of keeping the one already supplied. So that we would have two instances of the same thing in memory.
125 126 127 |
# File 'lib/anagram_solver/permutator.rb', line 125 def sort!(word) word.unpack("U*").sort.pack("U*").freeze end |