fuzzy-string-match 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +202 -0
- data/README.md +68 -0
- data/benchmark/vs_amatch.rb +54 -0
- data/lib/fuzzystringmatch.rb +200 -0
- data/test/fuzzystringmatch_spec.rb +139 -0
- metadata +111 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# What is fuzzy-string-match
|
2
|
+
|
3
|
+
* fuzzy-string-match is a fuzzy string matching library for ruby.
|
4
|
+
* It is fast. ( written in C with RubyInline )
|
5
|
+
* It suports only Jaro-Winkler distance algorithm.
|
6
|
+
* This program was ported by hand from lucene-3.0.2. (lucene is Java product)
|
7
|
+
* If you want to add another string distance algorithm, please port by yourself and contact me <kiyoka@sumibi.org>.
|
8
|
+
|
9
|
+
## Installing
|
10
|
+
1. gem install fuzzy-string-match
|
11
|
+
|
12
|
+
## Features
|
13
|
+
* Caluclate Jaro-Winkler distance of two strings.
|
14
|
+
* Pure ruby version can handle both ascii and UTF8 strings. (and slow)
|
15
|
+
* Native version can only ascii strings. (and fast)
|
16
|
+
|
17
|
+
## Sample code
|
18
|
+
* Native version
|
19
|
+
|
20
|
+
<code>
|
21
|
+
require 'fuzzystringmatch'
|
22
|
+
jarow = FuzzyStringMatch::JaroWinkler.new.create( :native )
|
23
|
+
p jarow.getDistance( "jones", "johnson" )
|
24
|
+
</code>
|
25
|
+
|
26
|
+
* Pure ruby version
|
27
|
+
|
28
|
+
<code>
|
29
|
+
require 'fuzzystringmatch'
|
30
|
+
jarow = FuzzyStringMatch::JaroWinkler.new.create( :pure )
|
31
|
+
p jarow.getDistance( "ああ", "あい" )
|
32
|
+
</code>
|
33
|
+
|
34
|
+
## Sample on irb
|
35
|
+
|
36
|
+
<code>
|
37
|
+
irb(main):001:0> require 'fuzzystringmatch'
|
38
|
+
require 'fuzzystringmatch'
|
39
|
+
=> true
|
40
|
+
|
41
|
+
irb(main):002:0> jarow = FuzzyStringMatch::JaroWinkler.new.create( :native )
|
42
|
+
jarow = FuzzyStringMatch::JaroWinkler.new.create( :native )
|
43
|
+
=> #<FuzzyStringMatch::JaroWinklerNative:0x000001011b0010>
|
44
|
+
|
45
|
+
irb(main):003:0> jarow.getDistance( "al", "al" )
|
46
|
+
jarow.getDistance( "al", "al" )
|
47
|
+
=> 1.0
|
48
|
+
|
49
|
+
irb(main):004:0> jarow.getDistance( "dixon", "dicksonx" )
|
50
|
+
jarow.getDistance( "dixon", "dicksonx" )
|
51
|
+
=> 0.8133333333333332
|
52
|
+
</code>
|
53
|
+
|
54
|
+
## Requires
|
55
|
+
- RubyInline
|
56
|
+
- Ruby 1.9.1 or higher
|
57
|
+
|
58
|
+
## Author
|
59
|
+
- Copyright (C) Kiyoka Nishiyama <kiyoka@sumibi.org>
|
60
|
+
- I ported from java source code of lucene-3.0.2.
|
61
|
+
|
62
|
+
## See also
|
63
|
+
- http://en.wikipedia.org/wiki/Jaro–Winkler_distance
|
64
|
+
- http://lucene.apache.org/
|
65
|
+
- http://github.com/naoya/perl-text-jarowinkler
|
66
|
+
|
67
|
+
## License
|
68
|
+
- Apache 2.0 LICENSE
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
#
|
3
|
+
# http://www.ruby-lang.org/ja/man/html/benchmark.html
|
4
|
+
#
|
5
|
+
require 'benchmark'
|
6
|
+
require 'amatch'
|
7
|
+
require './lib/fuzzystringmatch'
|
8
|
+
|
9
|
+
looptimes = 10000
|
10
|
+
|
11
|
+
names50 = [ "Aichi", "Akita", "Aomori", "Chiba", "Ehime", "Fukui", "Fukuoka", "Fukushima", "Gifu", "Gunma",
|
12
|
+
"Hiroshima", "Hokkaido", "Hyogo", "Ibaraki", "Ishikawa", "Iwate", "Kagawa", "Kagoshima", "Kanagawa", "Kochi",
|
13
|
+
"Kumamoto", "Kyoto", "Mie", "Miyagi", "Miyazaki", "Nagano", "Nagasaki", "Nara", "Niigata", "Oita",
|
14
|
+
"Okayama", "Okinawa", "Osaka", "Saga", "Saitama", "Shiga", "Shimane", "Shizuoka", "Tochigi", "Tokushima",
|
15
|
+
"Tokyo", "Tottori", "Toyama", "Wakayama", "Yamagata", "Yamaguchi", "Yamanashi", "Dummy1", "Dummy2", "Dummy3" ]
|
16
|
+
names = names50 + names50
|
17
|
+
|
18
|
+
keyword = "Tokyo"
|
19
|
+
|
20
|
+
printf( " --- \n" )
|
21
|
+
printf( " --- Each match functions will be called %dMega times. --- \n", (names.size * looptimes) / (1000.0 * 1000.0) )
|
22
|
+
printf( " --- \n" )
|
23
|
+
|
24
|
+
puts "[Amatch]"
|
25
|
+
puts Benchmark::CAPTION
|
26
|
+
puts Benchmark.measure {
|
27
|
+
jarow = Amatch::JaroWinkler.new keyword
|
28
|
+
looptimes.times { |n|
|
29
|
+
names.map { |x|
|
30
|
+
jarow.match( x )
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
puts "[this Module (pure)]"
|
36
|
+
puts Benchmark::CAPTION
|
37
|
+
puts Benchmark.measure {
|
38
|
+
jarow = FuzzyStringMatch::JaroWinkler.new.create
|
39
|
+
looptimes.times { |n|
|
40
|
+
names.map { |x|
|
41
|
+
jarow.getDistance( keyword, x )
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
45
|
+
puts "[this Module (native)]"
|
46
|
+
puts Benchmark::CAPTION
|
47
|
+
puts Benchmark.measure {
|
48
|
+
jarow = FuzzyStringMatch::JaroWinkler.new.create( :native )
|
49
|
+
looptimes.times { |n|
|
50
|
+
names.map { |x|
|
51
|
+
jarow.getDistance( keyword, x )
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
@@ -0,0 +1,200 @@
|
|
1
|
+
#
|
2
|
+
# Fuzzy String Match
|
3
|
+
#
|
4
|
+
# Copyright 2010 Kiyoka Nishiyama
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module FuzzyStringMatch
|
19
|
+
|
20
|
+
class JaroWinkler
|
21
|
+
def create( type = :pure ) # factory method
|
22
|
+
case type
|
23
|
+
when :pure
|
24
|
+
JaroWinklerPure.new
|
25
|
+
when :native
|
26
|
+
JaroWinklerNative.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class JaroWinklerPure
|
32
|
+
THRESHOLD = 0.7
|
33
|
+
|
34
|
+
def getDistance( s1, s2 )
|
35
|
+
a1 = s1.split( // )
|
36
|
+
a2 = s2.split( // )
|
37
|
+
|
38
|
+
if s1.size > s2.size
|
39
|
+
(max,min) = a1,a2
|
40
|
+
else
|
41
|
+
(max,min) = a2,a1
|
42
|
+
end
|
43
|
+
|
44
|
+
range = [ (max.size / 2 - 1), 0 ].max
|
45
|
+
indexes = Array.new( min.size, -1 )
|
46
|
+
flags = Array.new( max.size, false )
|
47
|
+
|
48
|
+
matches = 0;
|
49
|
+
(0 ... min.size).each { |mi|
|
50
|
+
c1 = min[mi]
|
51
|
+
xi = [mi - range, 0].max
|
52
|
+
xn = [mi + range + 1, max.size].min
|
53
|
+
|
54
|
+
(xi ... xn).each { |i|
|
55
|
+
if (not flags[i]) && ( c1 == max[i] )
|
56
|
+
indexes[mi] = i
|
57
|
+
flags[i] = true
|
58
|
+
matches += 1
|
59
|
+
break
|
60
|
+
end
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
ms1 = Array.new( matches, nil )
|
65
|
+
ms2 = Array.new( matches, nil )
|
66
|
+
|
67
|
+
si = 0
|
68
|
+
(0 ... min.size).each { |i|
|
69
|
+
if (indexes[i] != -1)
|
70
|
+
ms1[si] = min[i]
|
71
|
+
si += 1
|
72
|
+
end
|
73
|
+
}
|
74
|
+
|
75
|
+
si = 0
|
76
|
+
(0 ... max.size).each { |i|
|
77
|
+
if flags[i]
|
78
|
+
ms2[si] = max[i]
|
79
|
+
si += 1
|
80
|
+
end
|
81
|
+
}
|
82
|
+
|
83
|
+
transpositions = 0
|
84
|
+
(0 ... ms1.size).each { |mi|
|
85
|
+
if ms1[mi] != ms2[mi]
|
86
|
+
transpositions += 1
|
87
|
+
end
|
88
|
+
}
|
89
|
+
|
90
|
+
prefix = 0
|
91
|
+
(0 ... min.size).each { |mi|
|
92
|
+
if s1[mi] == s2[mi]
|
93
|
+
prefix += 1
|
94
|
+
else
|
95
|
+
break
|
96
|
+
end
|
97
|
+
}
|
98
|
+
|
99
|
+
if 0 == matches
|
100
|
+
0.0
|
101
|
+
else
|
102
|
+
m = matches.to_f
|
103
|
+
t = (transpositions/ 2)
|
104
|
+
j = ((m / s1.size) + (m / s2.size) + ((m - t) / m)) / 3.0;
|
105
|
+
return j < THRESHOLD ? j : j + [0.1, 1.0 / max.size].min * prefix * (1 - j)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
require 'inline'
|
111
|
+
class JaroWinklerNative
|
112
|
+
inline do |builder|
|
113
|
+
builder.add_compile_flags '-std=c99'
|
114
|
+
builder.c_raw 'int max( int a, int b ) { return ((a)>(b)?(a):(b)); }'
|
115
|
+
builder.c_raw 'int min( int a, int b ) { return ((a)<(b)?(a):(b)); }'
|
116
|
+
builder.c_raw 'double double_min( double a, double b ) { return ((a)<(b)?(a):(b)); }'
|
117
|
+
builder.c '
|
118
|
+
double getDistance( char *s1, char *s2 )
|
119
|
+
{
|
120
|
+
char *_max;
|
121
|
+
char *_min;
|
122
|
+
int _max_length = 0;
|
123
|
+
int _min_length = 0;
|
124
|
+
if ( strlen(s1) > strlen(s2) ) {
|
125
|
+
_max = s1; _max_length = strlen(s1);
|
126
|
+
_min = s2; _min_length = strlen(s2);
|
127
|
+
}
|
128
|
+
else {
|
129
|
+
_max = s2; _max_length = strlen(s2);
|
130
|
+
_min = s1; _min_length = strlen(s1);
|
131
|
+
}
|
132
|
+
int range = max( _max_length / 2 - 1, 0 );
|
133
|
+
|
134
|
+
int indexes[_min_length];
|
135
|
+
for( int i = 0 ; i < _min_length ; i++ ) {
|
136
|
+
indexes[i] = -1;
|
137
|
+
}
|
138
|
+
|
139
|
+
int flags[_max_length];
|
140
|
+
for( int i = 0 ; i < _max_length ; i++ ) {
|
141
|
+
flags[i] = 0;
|
142
|
+
}
|
143
|
+
int matches = 0;
|
144
|
+
for (int mi = 0; mi < _min_length; mi++) {
|
145
|
+
char c1 = _min[mi];
|
146
|
+
for (int xi = max(mi - range, 0), xn = min(mi + range + 1, _max_length); xi < xn; xi++ ) {
|
147
|
+
if (!flags[xi] && (c1 == _max[xi])) {
|
148
|
+
indexes[mi] = xi;
|
149
|
+
flags[xi] = 1;
|
150
|
+
matches++;
|
151
|
+
break;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
char ms1[matches];
|
157
|
+
char ms2[matches];
|
158
|
+
int ms1_length = matches;
|
159
|
+
|
160
|
+
for (int i = 0, si = 0; i < _min_length; i++) {
|
161
|
+
if (indexes[i] != -1) {
|
162
|
+
ms1[si] = _min[i];
|
163
|
+
si++;
|
164
|
+
}
|
165
|
+
}
|
166
|
+
for (int i = 0, si = 0; i < _max_length; i++) {
|
167
|
+
if (flags[i]) {
|
168
|
+
ms2[si] = _max[i];
|
169
|
+
si++;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
int transpositions = 0;
|
173
|
+
for (int mi = 0; mi < ms1_length; mi++) {
|
174
|
+
if (ms1[mi] != ms2[mi]) {
|
175
|
+
transpositions++;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
int prefix = 0;
|
179
|
+
for (int mi = 0; mi < _min_length; mi++) {
|
180
|
+
if (s1[mi] == s2[mi]) {
|
181
|
+
prefix++;
|
182
|
+
} else {
|
183
|
+
break;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
double m = (double) matches;
|
188
|
+
if (matches == 0) {
|
189
|
+
return 0.0;
|
190
|
+
}
|
191
|
+
int t = transpositions / 2;
|
192
|
+
double j = ((m / strlen(s1) + m / strlen(s2) + (m - t) / m)) / 3;
|
193
|
+
double jw = j < 0.7 ? j : j + double_min(0.1, 1.0 / _max_length) * prefix
|
194
|
+
* (1 - j);
|
195
|
+
return jw;
|
196
|
+
}'
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- encoding: utf-8 -*-
|
3
|
+
#
|
4
|
+
# fuzzystringmatch_spec.rb - "RSpec file for FuzzyStringMatch module "
|
5
|
+
#
|
6
|
+
# Copyright (c) 2010 Kiyoka Nishiyama <kiyoka@sumibi.org>
|
7
|
+
#
|
8
|
+
# Licensed to the Apache Software Foundation (ASF) under one or more
|
9
|
+
# contributor license agreements. See the NOTICE file distributed with
|
10
|
+
# this work for additional information regarding copyright ownership.
|
11
|
+
# The ASF licenses this file to You under the Apache License, Version 2.0
|
12
|
+
# (the "License"); you may not use this file except in compliance with
|
13
|
+
# the License. You may obtain a copy of the License at
|
14
|
+
#
|
15
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
16
|
+
#
|
17
|
+
# Unless required by applicable law or agreed to in writing, software
|
18
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
19
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
20
|
+
# See the License for the specific language governing permissions and
|
21
|
+
# limitations under the License.
|
22
|
+
#
|
23
|
+
#
|
24
|
+
require 'fuzzystringmatch'
|
25
|
+
require 'amatch'
|
26
|
+
|
27
|
+
describe FuzzyStringMatch, "when some string distances (Pure) are" do
|
28
|
+
before do
|
29
|
+
@jarow = FuzzyStringMatch::JaroWinkler.new.create
|
30
|
+
end
|
31
|
+
it "should" do
|
32
|
+
@jarow.getDistance( "henka", "henkan" ).should be_close( 0.9722, 0.0001 )
|
33
|
+
@jarow.getDistance( "al", "al" ).should == 1.0
|
34
|
+
@jarow.getDistance( "martha", "marhta" ).should be_close( 0.9611, 0.0001 )
|
35
|
+
@jarow.getDistance( "jones", "johnson" ).should be_close( 0.8323, 0.0001 )
|
36
|
+
@jarow.getDistance( "abcvwxyz", "cabvwxyz" ).should be_close( 0.9583, 0.0001 )
|
37
|
+
@jarow.getDistance( "dwayne", "duane" ).should be_close( 0.8400, 0.0001 )
|
38
|
+
@jarow.getDistance( "dixon", "dicksonx" ).should be_close( 0.8133, 0.0001 )
|
39
|
+
@jarow.getDistance( "fvie", "ten" ).should == 0.0
|
40
|
+
lambda {
|
41
|
+
d1 = @jarow.getDistance("zac ephron", "zac efron")
|
42
|
+
d2 = @jarow.getDistance("zac ephron", "kai ephron")
|
43
|
+
d1 > d2
|
44
|
+
}.should be_true
|
45
|
+
lambda {
|
46
|
+
d1 = @jarow.getDistance("brittney spears", "britney spears")
|
47
|
+
d2 = @jarow.getDistance("brittney spears", "brittney startzman")
|
48
|
+
d1 > d2
|
49
|
+
}.should be_true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe FuzzyStringMatch, "when some string distances (Native) are" do
|
54
|
+
before do
|
55
|
+
@jarow = FuzzyStringMatch::JaroWinkler.new.create( :native )
|
56
|
+
end
|
57
|
+
it "should" do
|
58
|
+
@jarow.getDistance( "henka", "henkan" ).should be_close( 0.9722, 0.0001 )
|
59
|
+
@jarow.getDistance( "al", "al" ).should == 1.0
|
60
|
+
@jarow.getDistance( "martha", "marhta" ).should be_close( 0.9611, 0.0001 )
|
61
|
+
@jarow.getDistance( "jones", "johnson" ).should be_close( 0.8323, 0.0001 )
|
62
|
+
@jarow.getDistance( "abcvwxyz", "cabvwxyz" ).should be_close( 0.9583, 0.0001 )
|
63
|
+
@jarow.getDistance( "dwayne", "duane" ).should be_close( 0.8400, 0.0001 )
|
64
|
+
@jarow.getDistance( "dixon", "dicksonx" ).should be_close( 0.8133, 0.0001 )
|
65
|
+
@jarow.getDistance( "fvie", "ten" ).should == 0.0
|
66
|
+
lambda {
|
67
|
+
d1 = @jarow.getDistance("zac ephron", "zac efron")
|
68
|
+
d2 = @jarow.getDistance("zac ephron", "kai ephron")
|
69
|
+
d1 > d2
|
70
|
+
}.should be_true
|
71
|
+
lambda {
|
72
|
+
d1 = @jarow.getDistance("brittney spears", "britney spears")
|
73
|
+
d2 = @jarow.getDistance("brittney spears", "brittney startzman")
|
74
|
+
d1 > d2
|
75
|
+
}.should be_true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def amatch_getDistance( s1, s2 )
|
80
|
+
@jarow = Amatch::JaroWinkler.new( s1 )
|
81
|
+
@jarow.match( s2 )
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
describe FuzzyStringMatch, "when some UTF8 string distances (Pure) are" do
|
86
|
+
before do
|
87
|
+
@jarow = FuzzyStringMatch::JaroWinkler.new.create
|
88
|
+
end
|
89
|
+
it "should" do
|
90
|
+
@jarow.getDistance( "al", "al" ).should == 1.0
|
91
|
+
@jarow.getDistance( "martha", "marhta" ).should be_close( 0.9611, 0.0001 )
|
92
|
+
@jarow.getDistance( "jones", "johnson" ).should be_close( 0.8323, 0.0001 )
|
93
|
+
@jarow.getDistance( "abcvwxyz", "cabvwxyz" ).should be_close( 0.9583, 0.0001 )
|
94
|
+
@jarow.getDistance( "dwayne", "duane" ).should be_close( 0.8400, 0.0001 )
|
95
|
+
@jarow.getDistance( "dixon", "dicksonx" ).should be_close( 0.8133, 0.0001 )
|
96
|
+
@jarow.getDistance( "fvie", "ten" ).should == 0.0
|
97
|
+
lambda {
|
98
|
+
d1 = @jarow.getDistance("zac ephron", "zac efron")
|
99
|
+
d2 = @jarow.getDistance("zac ephron", "kai ephron")
|
100
|
+
d1 > d2
|
101
|
+
}.should be_true
|
102
|
+
lambda {
|
103
|
+
d1 = @jarow.getDistance("brittney spears", "britney spears")
|
104
|
+
d2 = @jarow.getDistance("brittney spears", "brittney startzman")
|
105
|
+
d1 > d2
|
106
|
+
}.should be_true
|
107
|
+
@jarow.getDistance( "スパゲティー", "スパゲッティー" ).should be_close( 0.9666, 0.0001 )
|
108
|
+
@jarow.getDistance( "スパゲティー", "スパゲティ" ).should be_close( 0.9722, 0.0001 )
|
109
|
+
@jarow.getDistance( "スティービー・ワンダー", "スピーディー・ワンダー" ).should be_close( 0.8561, 0.0001 )
|
110
|
+
@jarow.getDistance( "マイケル・ジャクソン", "ジャイケル・マクソン" ).should be_close( 0.8000, 0.0001 )
|
111
|
+
@jarow.getDistance( "まつもとゆきひろ", "まつもとひろゆき" ).should be_close( 0.9500, 0.0001 )
|
112
|
+
@jarow.getDistance( "クライエント", "クライアント" ).should be_close( 0.9222, 0.0001 )
|
113
|
+
@jarow.getDistance( "サーバー", "サーバ" ).should be_close( 0.9416, 0.0001 )
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
describe Amatch, "when use Amatch gem, results are" do
|
119
|
+
it "should" do
|
120
|
+
amatch_getDistance( "henka", "henkan" ).should be_close( 0.9666, 0.0001 ) ## amatch's result value is different from lucene version.
|
121
|
+
amatch_getDistance( "al", "al" ).should == 1.0
|
122
|
+
amatch_getDistance( "martha", "marhta" ).should be_close( 0.9611, 0.0001 )
|
123
|
+
amatch_getDistance( "jones", "johnson" ).should be_close( 0.8323, 0.0001 )
|
124
|
+
amatch_getDistance( "abcvwxyz", "cabvwxyz" ).should be_close( 0.9583, 0.0001 )
|
125
|
+
amatch_getDistance( "dwayne", "duane" ).should be_close( 0.8400, 0.0001 )
|
126
|
+
amatch_getDistance( "dixon", "dicksonx" ).should be_close( 0.8133, 0.0001 )
|
127
|
+
amatch_getDistance( "fvie", "ten" ).should == 0.0
|
128
|
+
lambda {
|
129
|
+
d1 = amatch_getDistance("zac ephron", "zac efron")
|
130
|
+
d2 = amatch_getDistance("zac ephron", "kai ephron")
|
131
|
+
d1 > d2
|
132
|
+
}.should be_true
|
133
|
+
lambda {
|
134
|
+
d1 = amatch_getDistance("brittney spears", "britney spears")
|
135
|
+
d2 = amatch_getDistance("brittney spears", "brittney startzman")
|
136
|
+
d1 > d2
|
137
|
+
}.should be_true
|
138
|
+
end
|
139
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fuzzy-string-match
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 0
|
9
|
+
version: 0.9.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kiyoka Nishiyama
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-13 00:00:00 +09: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
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: amatch
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: RubyInline
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 3
|
56
|
+
- 8
|
57
|
+
- 6
|
58
|
+
version: 3.8.6
|
59
|
+
type: :runtime
|
60
|
+
version_requirements: *id003
|
61
|
+
description: calculate Jaro Winkler distance.
|
62
|
+
email: kiyoka@sumibi.org
|
63
|
+
executables: []
|
64
|
+
|
65
|
+
extensions: []
|
66
|
+
|
67
|
+
extra_rdoc_files:
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
files:
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- benchmark/vs_amatch.rb
|
74
|
+
- lib/fuzzystringmatch.rb
|
75
|
+
- test/fuzzystringmatch_spec.rb
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: http://github.com/kiyoka/fuzzy-string-match
|
78
|
+
licenses: []
|
79
|
+
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options:
|
82
|
+
- --charset=UTF-8
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 1
|
92
|
+
- 9
|
93
|
+
- 1
|
94
|
+
version: 1.9.1
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.3.7
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: fuzzy string matching library
|
110
|
+
test_files:
|
111
|
+
- test/fuzzystringmatch_spec.rb
|