-
Notifications
You must be signed in to change notification settings - Fork 0
/
spell_correct.rb
106 lines (87 loc) · 2.44 KB
/
spell_correct.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
Nwords = Hash.new(0)
# extract words from given text
def words(text)
text.scan(/\w+/)
end
# builds a word to frequency hash using the given word
def train(the_words)
the_words.each do |word|
Nwords[word] += 1
end
end
def known(words)
words.select{|word| Nwords[word] > 0 }
end
Alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
# All words at an edit distance of 1 from the given word
def edits1(word)
_ = deletes(word) +
transposes(word) +
replaces(word) +
inserts(word)
_.uniq
end
# All words obtained by removing a letter at a time
def deletes(word)
#(0...word.size).map do |i|
# j = i+1
# word[0..i] + word[j..-1]
#end
#splits(word).map{|(p1, p2)| p1 + p2[1..-1] if !p2.empty? }.compact
splits(word)
.select {|(p1, p2)| p2.size > 0 }
.map{|(p1, p2)| p1 + p2[1..-1] }
end
# All words obtained by transposing adjacent letters
def transposes(word)
#(0...word.size).map do |i|
# w = word.dup
# t = w[i-1]; w[i-1] = w[i]; w[i] = t
# w
#end
#splits(word).map{|(p1, p2)| p1.chop + p2[0].to_s + p1[-1].to_s + p2[1..-1].to_s if !p1.empty? && !p2.empty? }.compact
splits(word)
.select{|(p1, p2)| p1.size > 0 && p2.size > 0 }
.map{|(p1, p2)| p1.chop + p2[0].to_s + p1[-1] + p2[1..-1] }
end
# All words obtained by changing one letter at a time with another letter from alphabets
def replaces(word)
#ws = []
#(0...word.size).each do |i|
# w = word.dup
# ws += Alphabets.map{|al| w[i]=al; w}
#end
#ws.uniq
#splits(word).map{|(p1, p2)| Alphabets.map{|al| p1 + al + p2[1..-1].to_s }}.flatten.uniq
splits(word)
.select{|(p1, p2)| p2.size > 0 }
.map{|(p1, p2)| Alphabets.map{|al| p1 + al + p2[1..-1]}}.flatten.uniq
end
# All words obtained by adding one letter of the alphabets at a time
def inserts(word)
splits(word).map{|(p1, p2)| Alphabets.map{|al| p1 + al + p2 } }.flatten.uniq
end
def splits(word)
(0..word.size).map{|i| [word[0...i], word[(i..-1)]] }
end
# known words among edits2
def known_edits2(word)
known(edits2(word))
end
# All words at an edit distance of 2
# Call edit1 on each result of edit1(word)
def edits2(word)
edits1(word).map{|e1_word| edits1(e1_word)}.flatten.uniq
end
def setup
train(words(File.read('./big.txt')))
nil
end
def correct(word)
candidates = known([word]) +
known(edits1(word)) +
known_edits2(word) +
[word]
p candidates
candidates.max_by{|candidate| Nwords[candidate] }
end