spellingbee 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +20 -0
  4. data/README +40 -0
  5. data/Rakefile +2 -0
  6. data/dict/default.txt +2 -0
  7. data/doc/Gemfile.html +98 -0
  8. data/doc/LICENSE.html +117 -0
  9. data/doc/README.html +146 -0
  10. data/doc/Rakefile.html +95 -0
  11. data/doc/SpellingBee.html +586 -0
  12. data/doc/created.rid +8 -0
  13. data/doc/dict/default_txt.html +97 -0
  14. data/doc/images/brick.png +0 -0
  15. data/doc/images/brick_link.png +0 -0
  16. data/doc/images/bug.png +0 -0
  17. data/doc/images/bullet_black.png +0 -0
  18. data/doc/images/bullet_toggle_minus.png +0 -0
  19. data/doc/images/bullet_toggle_plus.png +0 -0
  20. data/doc/images/date.png +0 -0
  21. data/doc/images/find.png +0 -0
  22. data/doc/images/loadingAnimation.gif +0 -0
  23. data/doc/images/macFFBgHack.png +0 -0
  24. data/doc/images/package.png +0 -0
  25. data/doc/images/page_green.png +0 -0
  26. data/doc/images/page_white_text.png +0 -0
  27. data/doc/images/page_white_width.png +0 -0
  28. data/doc/images/plugin.png +0 -0
  29. data/doc/images/ruby.png +0 -0
  30. data/doc/images/tag_green.png +0 -0
  31. data/doc/images/wrench.png +0 -0
  32. data/doc/images/wrench_orange.png +0 -0
  33. data/doc/images/zoom.png +0 -0
  34. data/doc/index.html +80 -0
  35. data/doc/js/darkfish.js +116 -0
  36. data/doc/js/jquery.js +32 -0
  37. data/doc/js/quicksearch.js +114 -0
  38. data/doc/js/thickbox-compressed.js +10 -0
  39. data/doc/lib/spellingbee/version_rb.html +52 -0
  40. data/doc/lib/spellingbee_rb.html +52 -0
  41. data/doc/rdoc.css +706 -0
  42. data/lib/spellingbee.rb +94 -0
  43. data/lib/spellingbee/version.rb +3 -0
  44. data/spellingbee.gemspec +21 -0
  45. metadata +108 -0
@@ -0,0 +1,94 @@
1
+
2
+ class SpellingBee
3
+ attr_accessor :dict_words, :dict_frequency
4
+
5
+ DEFAULT_DICT = File.join(File.dirname(__FILE__), '..', 'dict', 'default.txt')
6
+
7
+ def initialize(opts = {})
8
+ options = { :source_text => DEFAULT_DICT }.merge opts
9
+ @dict_frequency = Hash.new(1)
10
+ @dict_words = File.new(options[:source_text]).read.downcase.scan(/[a-z]+/)
11
+ @dict_words.each { |word| @dict_frequency[word] += 1 }
12
+ end
13
+
14
+ # Returns all the combinations of the word with one character removed
15
+ #
16
+ # Example:
17
+ # deletions("word") #=> ["ord", "wrd", "wod", "wor"]
18
+ #
19
+ def deletions word
20
+ n = word.length
21
+ deletion = (0...n).collect {|i| word[0...i]+word[i+1..-1] }
22
+ end
23
+
24
+ # Returns all combinations of the word with adjacent words transposed.
25
+ #
26
+ # Example:
27
+ # transpositions("word") #=> ["owrd", "wrod", "wodr"]
28
+ #
29
+ def transpositions word
30
+ n = word.length
31
+ (0...n-1).collect {|i| word[0...i]+word[i+1,1]+word[i,1]+word[i+2..-1] }
32
+ end
33
+
34
+ # Returns all variations of the word with each character replaced by all
35
+ # characters from 'a' to 'z'.
36
+ #
37
+ # Example:
38
+ # replacements("word") #=> ["aord", "bord", "cord", ... "ward", "wbrd" ...]
39
+ #
40
+ def replacements word
41
+ n = word.length
42
+ new_words = []
43
+ n.times {|i| ('a'..'z').each {|l| new_words << word[0...i]+l.chr+word[i+1..-1] } }
44
+ new_words
45
+ end
46
+
47
+ # Returns all variations of the word with an alphabet inserted between the
48
+ # characters.
49
+ #
50
+ # Example:
51
+ # insertions("word") #=> ["aword", "bword", "cword" ... "waord", "wbord" ... ]
52
+ #
53
+ def insertions word
54
+ n = word.length
55
+ new_words = []
56
+ (n+1).times {|i| ('a'..'z').each {|l| new_words << word[0...i]+l.chr+word[i..-1] } }
57
+ new_words
58
+ end
59
+
60
+ # Returns the variations of the given spelling. This set will contain all the
61
+ # possible variations of the spelling havind edit distance = 1. (Edit
62
+ # distance is the number of edits it takes to obtain the second word from
63
+ # the first.)
64
+ def variation_words word
65
+ ( deletions(word) + transpositions(word) + replacements(word) + insertions(word) ).uniq
66
+ end
67
+
68
+ # Selects the words from the list that are present in the dictionary.
69
+ #
70
+ # Example:
71
+ #
72
+ # known ["asdfgh", "known", "word", "qqqq"] #=> ["known", "word"]
73
+ #
74
+ def known words
75
+ known_words = words.find_all { |w| @dict_frequency.has_key? w }
76
+ known_words.empty? ? nil : known_words
77
+ end
78
+
79
+ # Returns the suggestion for the correction. If the word is found in the
80
+ # dictionary, it is returned, otherwise the word closest to it is returned.
81
+ # If there are no spelling suggestions, the same word is returned.
82
+ #
83
+ # Example:
84
+ #
85
+ # s = Spellingbee.new
86
+ # s.correct "spelling" #=> ["spelling"] -> word from dictionary
87
+ # s.correct "speling" #=> ["spelling"] -> corrected spelling
88
+ # s.correct "qqqqqq" #=> ["qqqqqq"] -> no suggestions
89
+ #
90
+ def correct word
91
+ known [word] or known(variation_words word) or [word]
92
+ end
93
+
94
+ end
@@ -0,0 +1,3 @@
1
+ class SpellingBee
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "spellingbee/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "spellingbee"
7
+ s.version = SpellingBee::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nithin Bekal"]
10
+ s.email = ["nithinbekal@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/spellingbee"
12
+ s.summary = %q{A spelling correction tool for ruby.}
13
+ s.description = %q{Suggests corrections for mis-spelled words.}
14
+
15
+ s.rubyforge_project = "spellingbee"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spellingbee
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Nithin Bekal
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-01 00:00:00 +05:30
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Suggests corrections for mis-spelled words.
22
+ email:
23
+ - nithinbekal@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - LICENSE
34
+ - README
35
+ - Rakefile
36
+ - dict/default.txt
37
+ - doc/Gemfile.html
38
+ - doc/LICENSE.html
39
+ - doc/README.html
40
+ - doc/Rakefile.html
41
+ - doc/SpellingBee.html
42
+ - doc/created.rid
43
+ - doc/dict/default_txt.html
44
+ - doc/images/brick.png
45
+ - doc/images/brick_link.png
46
+ - doc/images/bug.png
47
+ - doc/images/bullet_black.png
48
+ - doc/images/bullet_toggle_minus.png
49
+ - doc/images/bullet_toggle_plus.png
50
+ - doc/images/date.png
51
+ - doc/images/find.png
52
+ - doc/images/loadingAnimation.gif
53
+ - doc/images/macFFBgHack.png
54
+ - doc/images/package.png
55
+ - doc/images/page_green.png
56
+ - doc/images/page_white_text.png
57
+ - doc/images/page_white_width.png
58
+ - doc/images/plugin.png
59
+ - doc/images/ruby.png
60
+ - doc/images/tag_green.png
61
+ - doc/images/wrench.png
62
+ - doc/images/wrench_orange.png
63
+ - doc/images/zoom.png
64
+ - doc/index.html
65
+ - doc/js/darkfish.js
66
+ - doc/js/jquery.js
67
+ - doc/js/quicksearch.js
68
+ - doc/js/thickbox-compressed.js
69
+ - doc/lib/spellingbee/version_rb.html
70
+ - doc/lib/spellingbee_rb.html
71
+ - doc/rdoc.css
72
+ - lib/spellingbee.rb
73
+ - lib/spellingbee/version.rb
74
+ - spellingbee.gemspec
75
+ has_rdoc: true
76
+ homepage: http://rubygems.org/gems/spellingbee
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project: spellingbee
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A spelling correction tool for ruby.
107
+ test_files: []
108
+