dam_lev 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.rvmrc +1 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.md +33 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/dam_lev.rb +37 -0
- data/spec/DamLev_spec.rb +29 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +104 -0
data/.document
ADDED
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use 1.9.2@DamLev
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Allen Madsen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# DamLev
|
2
|
+
|
3
|
+
DamLev implements the [Damerau–Levenshtein distance algorithm][1]. It is an algorithm that measures the distance between two strings taking into account deletions, insertions, substitutions, and transpositions. It's written in pure ruby.
|
4
|
+
|
5
|
+
[1]: http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
|
6
|
+
|
7
|
+
## Get it!
|
8
|
+
|
9
|
+
gem install dam_lev
|
10
|
+
|
11
|
+
## Use it!
|
12
|
+
|
13
|
+
require 'dam_lev'
|
14
|
+
|
15
|
+
DamLev.distance("DamLev", "DamLev") # => 0
|
16
|
+
DamLev.distance("DamLev", "Damev") # => 1 # deletion
|
17
|
+
DamLev.distance("DamLev", "DamLiev") # => 1 # insertion
|
18
|
+
DamLev.distance("DamLev", "Dam7ev") # => 1 # substitution
|
19
|
+
DamLev.distance("DamLev", "DameLv") # => 2 # transposition
|
20
|
+
|
21
|
+
## Note on Patches/Pull Requests
|
22
|
+
|
23
|
+
* Fork the project.
|
24
|
+
* Make your feature addition or bug fix.
|
25
|
+
* Add tests for it. This is important so I don't break it in a
|
26
|
+
future version unintentionally.
|
27
|
+
* Commit, do not mess with rakefile, version, or history.
|
28
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
29
|
+
* Send me a pull request. Bonus points for topic branches.
|
30
|
+
|
31
|
+
## Copyright
|
32
|
+
|
33
|
+
Copyright (c) 2010 Allen Madsen. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "dam_lev"
|
10
|
+
gem.summary = %Q{Measure the distance between two strings.}
|
11
|
+
gem.description = %Q{Measures the distance between two strings using the Damerau–Levenshtein distance algorithm. For more, see http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance }
|
12
|
+
gem.email = "blatyo@gmail.com"
|
13
|
+
gem.homepage = "http://github.com/blatyo/DamLev"
|
14
|
+
gem.authors = ["Allen Madsen"]
|
15
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
16
|
+
gem.add_development_dependency "yard", ">= 0"
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
31
|
+
spec.libs << 'lib' << 'spec'
|
32
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
33
|
+
spec.rcov = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :spec => :check_dependencies
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
begin
|
40
|
+
require 'yard'
|
41
|
+
YARD::Rake::YardocTask.new
|
42
|
+
rescue LoadError
|
43
|
+
task :yardoc do
|
44
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
45
|
+
end
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/dam_lev.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class DamLev
|
2
|
+
def self.distance(str1, str2)
|
3
|
+
return str1.length if str2.empty?
|
4
|
+
return str2.length if str1.empty?
|
5
|
+
|
6
|
+
distance_matrix = build_matrix(str1, str2)
|
7
|
+
|
8
|
+
(1..str1.length).each do |i|
|
9
|
+
(1..str2.length).each do |j|
|
10
|
+
sub_cost = str1[i-1] == str2[j-1] ? 0 : 1
|
11
|
+
|
12
|
+
distance_matrix[i][j] = [distance_matrix[i-1][j] + 1, # deletion
|
13
|
+
distance_matrix[i][j-1] + 1, # insertion
|
14
|
+
distance_matrix[i-1][j-1] + sub_cost].min # substitution
|
15
|
+
|
16
|
+
if i > 1 && j > 1 && str1[i-1] == str2[j-2] && str1[i-2] == str2[j-1]
|
17
|
+
distance_matrix[i+1][j+1] = [distance_matrix[i][j],
|
18
|
+
distance_matrix[i-2][j-2] + sub_cost].min # transposition
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
distance_matrix.last.last
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def self.build_matrix(str1, str2)
|
29
|
+
distance_matrix = [(0..str2.length).to_a]
|
30
|
+
|
31
|
+
(1..str1.length).each do |i|
|
32
|
+
distance_matrix[i] = [i]
|
33
|
+
end
|
34
|
+
|
35
|
+
distance_matrix
|
36
|
+
end
|
37
|
+
end
|
data/spec/DamLev_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe DamLev do
|
4
|
+
describe ".distance" do
|
5
|
+
it "should calculate a distance of zero between the same string" do
|
6
|
+
DamLev.distance("DamLev", "DamLev").should == 0
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should calculate a distance of one when a character has been deleted between two strings" do
|
10
|
+
DamLev.distance("DamLev", "Damev").should == 1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should calculate a distance of one when a character has been inserted between two strings" do
|
14
|
+
DamLev.distance("DamLev", "DamLiev").should == 1
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should calculate a distance of one when a character has been substituted between two strings" do
|
18
|
+
DamLev.distance("DamLev", "Dam7ev").should == 1
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should calculate a distance of two when two characters have been swapped between two strings" do
|
22
|
+
DamLev.distance("DamLev", "DameLv").should == 2
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should calculate a distance equal to the longest string if the other is empty" do
|
26
|
+
DamLev.distance("DamLev", "").should == "DamLev".length
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dam_lev
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Allen Madsen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-04 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 2
|
31
|
+
- 9
|
32
|
+
version: 1.2.9
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: yard
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: "Measures the distance between two strings using the Damerau\xE2\x80\x93Levenshtein distance algorithm. For more, see http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance "
|
49
|
+
email: blatyo@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- .rvmrc
|
61
|
+
- Gemfile
|
62
|
+
- LICENSE
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- VERSION
|
66
|
+
- lib/dam_lev.rb
|
67
|
+
- spec/DamLev_spec.rb
|
68
|
+
- spec/spec.opts
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/blatyo/DamLev
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.3.7
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Measure the distance between two strings.
|
102
|
+
test_files:
|
103
|
+
- spec/DamLev_spec.rb
|
104
|
+
- spec/spec_helper.rb
|