-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
111 lines (82 loc) · 2.45 KB
/
main.rb
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/env ruby1.9
# encoding: utf-8
require 'rubygems'
require 'fuzzystringmatch'
require 'text/hyphen'
require 'active_support'
require_relative 'greeklish_converter'
def find_best(word, array)
jarow = FuzzyStringMatch::JaroWinkler.create( :native )
max = 0
max_word = ''
array.each do |runner|
score = jarow.getDistance(word, runner)
if score > max
max = score
max_word = runner
end
end
return max_word
end
my_word = ''
while true
if File.exist? "./toruby.txt"
my_word = File.open("./toruby.txt", "r").lines.first.greeklish
File.delete("./toruby.txt")
# my_word = ARGV[0].greeklish
# puts "My word is: " + my_word
index = 0
index_end = 0
top_list = []
top_top_list = []
hh = Text::Hyphen.new(:language => "it", :left => 0, :right => 0)
my_word = hh.visualize(my_word," ").split(" ")
# my_word = my_word.split(//)
remaining_parts_of_my_word = true
try = 0
output = ''
while(remaining_parts_of_my_word)
File.open("./dictionary.txt", "r") do |file_handle|
file_handle.each_line do |line|
word = line.split[0]
search = my_word[index..index_end].join("")
prev_end = index_end
if word.include? search
while (word.include? search) && index_end < my_word.length()
index_end += 1
search = my_word[index..index_end].join("")
end
index_end -= 1
if prev_end < index_end
top_list = []
end
top_list << word
end
end
end
# p top_list
# p my_word[index..index_end].join("")
try += 1
# p top_list
if index_end >= (my_word.length-1) #|| try > 2
remaining_parts_of_my_word = false
best_word = find_best(my_word[index..index_end].join(""), top_list)
top_top_list << best_word
output << my_word[index..index_end].join("") + " " + best_word
else
best_word = find_best(my_word[index..index_end].join(""), top_list)
top_top_list << best_word
output << my_word[index..index_end].join("") + " " + best_word + "|"
index_end += 1
index = index_end
top_list = []
end
end
# p top_top_list
# p index_end
# p output
File.open("./fromruby.txt", 'w') {|f| f.write(output) }
else
sleep(1)
end
end