dynarex-levenshtein 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dynarexlevenshtein.rb +55 -0
- metadata +66 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# file: dynarexlevenshtein.rb
|
4
|
+
|
5
|
+
require 'dynarex'
|
6
|
+
|
7
|
+
class DynarexLevenshtein
|
8
|
+
|
9
|
+
attr_reader :to_dynarex, :distance
|
10
|
+
|
11
|
+
def initialize(a,b)
|
12
|
+
@distance = levenshtein(a,b)
|
13
|
+
end
|
14
|
+
|
15
|
+
def levenshtein(a,b)
|
16
|
+
|
17
|
+
a, b = [a, b].sort_by(&:length)
|
18
|
+
n, m = a.length, b.length
|
19
|
+
|
20
|
+
schema = 'table[title,string1,string2]/rows(' + (['line','label'] + (1..n)\
|
21
|
+
.map{|x| "col%s" % x}).join(',') + ')'
|
22
|
+
@to_dynarex = Dynarex.new(schema)
|
23
|
+
|
24
|
+
@to_dynarex.summary[:title] = 'Calculates the Levenshtein distance between a and b.'
|
25
|
+
@to_dynarex.summary[:string1] = a
|
26
|
+
@to_dynarex.summary[:string2] = b
|
27
|
+
|
28
|
+
current = (n+1).times.to_a
|
29
|
+
|
30
|
+
top_label = ['0','',''] + a.split('')
|
31
|
+
label = [''] + b.split('')
|
32
|
+
h = Hash[([:line, :label] + (0..n).map{|x| ("col%s" % x).to_sym})\
|
33
|
+
.zip(top_label)]
|
34
|
+
@to_dynarex.create h
|
35
|
+
|
36
|
+
(1..m).each do |i|
|
37
|
+
previous, current = current, [i]+[0]*n
|
38
|
+
|
39
|
+
h = Hash[([:line, :label] + (0..n).map{|x| ("col%s" % x).to_sym}).zip([i.to_s, \
|
40
|
+
label[i-1]] + previous)]
|
41
|
+
@to_dynarex.create h
|
42
|
+
|
43
|
+
(1..n).each do |j|
|
44
|
+
delete, change, add = current[j-1], previous[j-1], previous[j]
|
45
|
+
change += 1 if a[j-1] != b[i-1]
|
46
|
+
current[j] = [add+1, delete+1, change].min
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
h = Hash[([:line, :label] + (0..n).map{|x| ("col%s" % x).to_sym}).zip([m.to_s, label[-1]] + current)]
|
51
|
+
@to_dynarex.create h
|
52
|
+
|
53
|
+
current[n]
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynarex-levenshtein
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James Robertson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-04-10 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: dynrex
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files: []
|
34
|
+
|
35
|
+
files:
|
36
|
+
- lib/dynarexlevenshtein.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage:
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.5.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: dynarex-levenshtein
|
65
|
+
test_files: []
|
66
|
+
|