spell 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/spell.rb +2 -0
- data/lib/spell/spell.rb +100 -0
- data/lib/spell/version.rb +3 -0
- data/spell.gemspec +24 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7ced55319ca8557405a6924cdb1b44d8f9b7cc1c
|
4
|
+
data.tar.gz: 2d8e7811a225f6ef5ad3eb0f302b7ecfcbec53fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b0d2f24e3351687ab6db89414c41f726e7a73a3ee8d4379ac51b06db6a25ed33a081ca3754521fd3eb482ed74aa92e74672652fec91cccd3361b061f230ec0d
|
7
|
+
data.tar.gz: ff69fb892e76be991b0452e073ac6d82829519db3a36b90d41aa09113d4050fe3fb686387ac68118d7da1f37283ef0863b9da865f00f84eb649d86a7cbaa2915
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jonathan Arnett
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Spell
|
2
|
+
|
3
|
+
Spell checker written in pure Ruby, implementing a simple bigram comparison algorithm. Spell has no external dependencies (including aspell or ispell).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'spell'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install spell
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The spell initializer can take one or two arguments.
|
24
|
+
|
25
|
+
If you do not care about using the word usage, you can simply write:
|
26
|
+
```ruby
|
27
|
+
word_list = ["alpha", "beta"]
|
28
|
+
spell = Spell::Spell.new(word_list)
|
29
|
+
spell.best_match('alphabet') #=> "alpha"
|
30
|
+
```
|
31
|
+
|
32
|
+
If you want to include word usage in the best match calculation, you must provide a hash where the keys are the words and the values are the corresponding word counts.
|
33
|
+
|
34
|
+
The default for the word usage weight (internally termed "alpha") is 0.3. This value should be in the range 0.0-1.0, where 0.0 means the word usage does not affect the output, whereas 1.0 means the most used word is always returned.
|
35
|
+
|
36
|
+
If you want to accept the default weight, you can simply write:
|
37
|
+
```ruby
|
38
|
+
word_list = { "alpha" => 2, "beta" => 20 }
|
39
|
+
spell = Spell::Spell.new(word_list)
|
40
|
+
spell.best_match('alphabet') #=> "alpha"
|
41
|
+
```
|
42
|
+
|
43
|
+
Or, if you'd rather specify a custom word weight, you can specify it like this:
|
44
|
+
```ruby
|
45
|
+
word_list = { "alpha" => 8, "beta" => 2 }
|
46
|
+
spell = Spell::Spell.new(word_list, 0.5)
|
47
|
+
spell.best_match('alphabet') #=> "beta"
|
48
|
+
```
|
49
|
+
|
50
|
+
Other than the `best_match` method, shown above, there is also a method `compare`, which returns the how similar two words are, based on shared, order-consistent bigrams compared to the maximum number of bigrams of the two words.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
word_list = ["alpha", "beta"]
|
54
|
+
spell = Spell::Spell.new(word_list)
|
55
|
+
spell.compare('alpha', 'alphabet') #=> 0.5714285714285714 (4 / 7)
|
56
|
+
```
|
57
|
+
|
58
|
+
## Development
|
59
|
+
|
60
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
61
|
+
|
62
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/J3RN/spellrb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
67
|
+
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
72
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "spell"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/spell.rb
ADDED
data/lib/spell/spell.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
module Spell
|
2
|
+
class Spell
|
3
|
+
def initialize(*args)
|
4
|
+
fail "Too many arguments given" if args.count > 3
|
5
|
+
|
6
|
+
if args[0].is_a? Hash
|
7
|
+
@word_list = args[0]
|
8
|
+
@alpha = args[1] || 0.3
|
9
|
+
elsif args[0].is_a? Array
|
10
|
+
@word_list = args[0]
|
11
|
+
else
|
12
|
+
fail "First argument must be an Array or Hash"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Return a value from 0.0-1.0 of how similar these two words are
|
17
|
+
def compare(word1, word2)
|
18
|
+
bigram_compare(bigramate(word1), bigramate(word2))
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the closest matching word in the dictionary
|
22
|
+
def best_match(given_word)
|
23
|
+
words = (@word_list.is_a? Array) ? @word_list : @word_list.keys
|
24
|
+
|
25
|
+
word_bigrams = bigramate(given_word)
|
26
|
+
word_hash = words.map do |key|
|
27
|
+
[key, bigram_compare(word_bigrams, bigramate(key))]
|
28
|
+
end.to_h
|
29
|
+
|
30
|
+
# Weight by word usage, if logical
|
31
|
+
word_hash = apply_usage_weights(word_hash) if @word_list.is_a? Hash
|
32
|
+
|
33
|
+
word_hash.max_by { |key, value| value }.first
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# Returns the number of matching bigrams between the two sets of bigrams
|
39
|
+
def num_matching(one_bigrams, two_bigrams, acc = 0)
|
40
|
+
return acc if (one_bigrams.empty? || two_bigrams.empty?)
|
41
|
+
|
42
|
+
one_two = one_bigrams.index(two_bigrams[0])
|
43
|
+
two_one = two_bigrams.index(one_bigrams[0])
|
44
|
+
|
45
|
+
if (one_two.nil? && two_one.nil?)
|
46
|
+
num_matching(one_bigrams.drop(1), two_bigrams.drop(1), acc)
|
47
|
+
else
|
48
|
+
# If one is nil, it is set to the other
|
49
|
+
two_one ||= one_two
|
50
|
+
one_two ||= two_one
|
51
|
+
|
52
|
+
if one_two < two_one
|
53
|
+
num_matching(one_bigrams.drop(one_two + 1), two_bigrams.drop(1), acc + 1)
|
54
|
+
else
|
55
|
+
num_matching(one_bigrams.drop(1), two_bigrams.drop(two_one + 1), acc + 1)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns an array of the word's bigrams (in order)
|
61
|
+
def bigramate(word)
|
62
|
+
(0..(word.length - 2)).map { |i| word.slice(i, 2) }
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns a value from 0 to 1 for how likely these two words are to be a match
|
66
|
+
def bigram_compare(word1_bigrams, word2_bigrams)
|
67
|
+
most_bigrams = [word1_bigrams.count, word2_bigrams.count].max
|
68
|
+
num_matching(word1_bigrams, word2_bigrams).to_f / most_bigrams
|
69
|
+
end
|
70
|
+
|
71
|
+
# For each word, adjust it's score by usage
|
72
|
+
#
|
73
|
+
# v = s * (1 - a) + u * a
|
74
|
+
# Where v is the new value
|
75
|
+
# a is @alpha
|
76
|
+
# s is the bigram score (0..1)
|
77
|
+
# u is the usage score (0..1)
|
78
|
+
def apply_usage_weights(word_hash)
|
79
|
+
max_usage = @word_list.values.max.to_f
|
80
|
+
max_usage = 1 if max_usage == 0
|
81
|
+
|
82
|
+
weighted_array = word_hash.map do |word, bigram_score|
|
83
|
+
usage_score = @word_list[word].to_f / max_usage
|
84
|
+
[word, (bigram_score * (1 - @alpha)) + (usage_score * @alpha)]
|
85
|
+
end
|
86
|
+
|
87
|
+
weighted_array.to_h
|
88
|
+
end
|
89
|
+
|
90
|
+
# Returns a boolean for whether or not 'word' is in the dictionary
|
91
|
+
def spelled_good?(word)
|
92
|
+
@word_list.keys.include?(word)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Increment count on each utterance
|
96
|
+
def add_count(word)
|
97
|
+
@word_list[word] += 1
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spell.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spell/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spell"
|
8
|
+
spec.version = Spell::VERSION
|
9
|
+
spec.authors = ["Jonathan Arnett"]
|
10
|
+
spec.email = ["jonarnett90@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A customizable spell checker, written in pure Ruby }
|
13
|
+
spec.description = %q{Spell checker written in pure Ruby, implementing a simple bigram comparison algorithm. Spell has no external dependencies (including aspell or ispell). }
|
14
|
+
spec.homepage = "https://github.com/J3RN/spellrb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Arnett
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: 'Spell checker written in pure Ruby, implementing a simple bigram comparison
|
42
|
+
algorithm. Spell has no external dependencies (including aspell or ispell). '
|
43
|
+
email:
|
44
|
+
- jonarnett90@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- .travis.yml
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/spell.rb
|
59
|
+
- lib/spell/spell.rb
|
60
|
+
- lib/spell/version.rb
|
61
|
+
- spell.gemspec
|
62
|
+
homepage: https://github.com/J3RN/spellrb
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: A customizable spell checker, written in pure Ruby
|
86
|
+
test_files: []
|