-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletter_frequency.rb
More file actions
46 lines (39 loc) · 894 Bytes
/
letter_frequency.rb
File metadata and controls
46 lines (39 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
load 'utils.rb'
word_list = read_file("ordliste.txt").force_encoding('UTF-8').split(/\n/)
left_table = Hash.new(0)
right_table = Hash.new(0)
def analyze_word(word, table, direction)
word.each_char_with_index do |c, i|
if i > 3
next
end
w = word[0..i]
if direction
w = w.reverse!
end
table[w] = table[w] + 1
end
end
word_list.each do |word|
if !word.include? '-'
next
end
left, right = word.split(/-/)
analyze_word(left.reverse, left_table, true)
analyze_word(right, right_table, false)
end
puts "Writing LEFT TABLE\n"
File.open('left.txt', 'w') do |file|
left_table.sort_by {|k, v| v}.reverse.each do |w, k|
file.write "#{k} #{w}\n"
end
end
puts "Writing RIGHT TABLE\n"
File.open('right.txt', 'w') do |file|
right_table.sort_by {|k, v| v}.reverse.each do |w, k|
file.write "#{k} #{w}\n"
end
end