spell_checker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,6 @@
1
+
2
+ h1 Spell Checker
3
+
4
+ Simple spell checker using word stemming. A basic word list is used to match words against. Word stemming is used to reduce the size of the list and be a bit smarter about the way we match the words.
5
+
6
+
File without changes
@@ -0,0 +1,64 @@
1
+
2
+ class Dictionary
3
+
4
+ end
5
+
6
+
7
+ class MysqlDictionary < Dictionary
8
+
9
+ end
10
+
11
+ class TextDictionary < Dictionary
12
+ require 'ftools'
13
+
14
+ def initialize(params={})
15
+ @params = params
16
+ if @params[:base_dictionary].nil?
17
+ @base_file = File.expand_path(File.dirname(__FILE__) + "/dictionary.txt")
18
+ else
19
+ @base_file = @params[:base_dictionary]
20
+ raise "Unable to find base dictionary '#{@params[:base_dictionary]}'" unless File.exist?(@params[:base_dictionary])
21
+ end
22
+ if @params[:custom_dictionary].nil?
23
+ @custom_file = File.expand_path(File.dirname(__FILE__) + "/custom.txt")
24
+ else
25
+ @custom_file = @params[:custom_dictionary]
26
+ raise "Unable to find base dictionary '#{@params[:custom_dictionary]}'" unless File.exist?(@params[:custom_dictionary])
27
+ end
28
+ end
29
+
30
+ def word_exists?( word )
31
+ unles
32
+ File.open(@custom_file, "w") unless File.exists?(@custom_file)
33
+ File.open(@base_file, "w") unless File.exists?(@base_file)
34
+
35
+ File.open( @custom_file ) do |io|
36
+ io.each {|line| line.chomp! ; return true if line == word}
37
+ end
38
+ File.open( @base_file ) do |io|
39
+ io.each {|line| line.chomp! ; return true if line == word}
40
+ end
41
+ false
42
+ end
43
+
44
+ def filter_stemmed_words
45
+ dictionary = []
46
+ puts "loading stemmed list"
47
+ File.move (@base_file, File.expand_path(File.dirname(@base_file) + "/original_dictionary.txt"))
48
+ File.open( File.expand_path(File.dirname(__FILE__) + "/original_dictionary.txt") ) do |io|
49
+ io.each {|line| puts "loading #{line}"; line.chomp! ; dictionary << line.stem}
50
+ end
51
+
52
+ dictionary.uniq!
53
+ puts "#{dictionary.length} stemmed words found"
54
+
55
+ puts "writing to #{@base_file}"
56
+ File.open(@base_file, "w") do |io|
57
+ dictionary.each do |word|
58
+ io.write(word + "\n")
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ end