Class: GentleBrute::CPAAnalyzer
- Inherits:
-
Object
- Object
- GentleBrute::CPAAnalyzer
- Defined in:
- lib/gentle_brute/cpa_analyzer.rb
Constant Summary collapse
- NEIGHBORS_PATH =
File.('../resources/neighbors.json', __FILE__)
Instance Method Summary collapse
-
#build_cpa_tables(dictionary_path = File.expand_path("../resources/dictionary", __FILE__)) ⇒ Object
Analyze a dictionary word list (‘lib/gentle_brute/resources/dictionary’) for repetition patterns.
- #get_ender_neighbor_score(char, char2) ⇒ Object
- #get_neighbor_score(char, char2) ⇒ Object
- #get_starter_neighbor_score(char, char2) ⇒ Object
-
#initialize ⇒ CPAAnalyzer
constructor
A new instance of CPAAnalyzer.
Constructor Details
#initialize ⇒ CPAAnalyzer
Returns a new instance of CPAAnalyzer.
5 6 7 8 9 10 11 |
# File 'lib/gentle_brute/cpa_analyzer.rb', line 5 def initialize build_cpa_tables if not File.exists? NEIGHBORS_PATH lattices = JSON.parse File.read(NEIGHBORS_PATH) @starters = lattices["starters"] @neighbors = lattices["neighbors"] @enders = lattices["enders"] end |
Instance Method Details
#build_cpa_tables(dictionary_path = File.expand_path("../resources/dictionary", __FILE__)) ⇒ Object
Analyze a dictionary word list (‘lib/gentle_brute/resources/dictionary’) for repetition patterns
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gentle_brute/cpa_analyzer.rb', line 15 def build_cpa_tables(dictionary_path=File.("../resources/dictionary", __FILE__)) # Create empty pattern occurce lattices lattice = Hash.new { |h, k| h[k] = {} } starter_lattice = Hash.new { |h, k| h[k] = {} } ender_lattice = Hash.new { |h, k| h[k] = {} } chars = ('a'..'z').to_a chars.each do | char | chars.each do | char2 | lattice[char][char2] = [0, 0] starter_lattice[char][char2] = [0, 0] ender_lattice[char][char2] = [0, 0] end end # Analyze each wrod in dictionary list words = File.read(dictionary_path) words.each_line do | word | word.chomp! word.downcase! for i in 0..word.length char = word[i] # current char char_r = word[i+1] # char one index to the right of the current char char_rr = word[i+2] # char two indicies to the right of the current char begin starter_lattice[char][char_r][0] += 1 if char_r and i == 0 starter_lattice[char][char_rr][1] += 1 if char_rr and i == 0 ender_lattice[char][char_r][0] += 1 if char_r and i == word.length-3 ender_lattice[char][char_rr][1] += 1 if char_rr and i == word.length-3 lattice[char][char_r][0] += 1 if char_r lattice[char][char_rr][1] += 1 if char_rr rescue break end end end # Write neighbors file output_lattice = {"starters" => starter_lattice, "neighbors" => lattice, "enders" => ender_lattice} File.open(NEIGHBORS_PATH, "w") {|f| f.write output_lattice.to_json } end |
#get_ender_neighbor_score(char, char2) ⇒ Object
66 67 68 |
# File 'lib/gentle_brute/cpa_analyzer.rb', line 66 def get_ender_neighbor_score(char, char2) @enders[char][char2] end |
#get_neighbor_score(char, char2) ⇒ Object
62 63 64 |
# File 'lib/gentle_brute/cpa_analyzer.rb', line 62 def get_neighbor_score(char, char2) @neighbors[char][char2] end |
#get_starter_neighbor_score(char, char2) ⇒ Object
58 59 60 |
# File 'lib/gentle_brute/cpa_analyzer.rb', line 58 def get_starter_neighbor_score(char, char2) @starters[char][char2] end |