-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellchecker.rb
88 lines (77 loc) · 2.1 KB
/
spellchecker.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
class SpellChecker
def initialize(dictionary)
@dictionary = Dictionary.new(dictionary)
@counter = 0
@cursor = @dictionary.root
end
def suggest_words(prefix)
@prefix_node = get_node_at_end_of_prefix(prefix)
@cursor = @prefix_node
@chars_in_current_word = [prefix]
get_word(@prefix_node)
puts("#{@counter} words have prefix #{prefix}")
end
def get_word(node)
if node.children.nil?
@chars_in_current_word.pop
return
end
node.children.each_with_index do |child_node, idx|
next if child_node.nil?
@cursor = child_node
@chars_in_current_word << get_char(idx)
if child_node.is_word == true
@counter += 1
File.open('words_with_prefix.txt', 'a') { |f| f.puts @chars_in_current_word.join }
end
get_word(@cursor)
end
@chars_in_current_word.pop
end
def get_char(idx)
return "'" if idx == 26
(idx + 97).chr
end
def get_node_at_end_of_prefix(prefix)
prefix.downcase.each_char do |char|
if @cursor.nil? || @cursor.children.nil? || @cursor.children[get_dict_index(char)].nil?
puts("Prefix #{prefix} not present in dictionary")
exit
else
@cursor = @cursor.children[get_dict_index(char)]
end
end
@cursor
end
def spellcheck_lines(text)
IO.foreach(text) do |line|
split_into_words(line).each do |word|
next if word.chomp == ''
if word_in_dictionary?(word.chomp) == false
@counter += 1
File.open('misspelled_words.txt', 'a') { |f| f.puts(word) }
end
end
end
puts("# of misspelt words: #{@counter}")
end
def split_into_words(line)
line.scan(/[a-zA-Z']*/)
end
private
def word_in_dictionary?(word)
@cursor = @dictionary.root
word.downcase.each_char do |char|
if @cursor.nil? || @cursor.children.nil? || @cursor.children[get_dict_index(char)].nil?
return false
else
@cursor = @cursor.children[get_dict_index(char)]
end
end
@cursor.is_word
end
def get_dict_index(char)
return 26 if char == "'"
char.ord - 97
end
end