something_like_that 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/LICENSE +18 -0
- data/README.md +56 -0
- data/lib/something_like_that/match_phrase/query.rb +20 -0
- data/lib/something_like_that/match_phrase.rb +10 -0
- data/lib/something_like_that/scorer.rb +69 -0
- data/lib/something_like_that/two_d_array.rb +44 -0
- data/lib/something_like_that/version.rb +3 -0
- data/lib/something_like_that.rb +12 -0
- data/something_like_that.gemspec +28 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3c96432f5e32fa8dfe2ca27cd0e29bd39ace63be
|
4
|
+
data.tar.gz: 46694911b3e8119b458ed794899a3fc63f5bafd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6cf181ecb47f218bfa5e933e8824b5ae8ec4a8fa4cdb6117430821d2c9cb693985b04b653c7fe0065f7364220d25599b225b6e34274338046c1f863b18de1b0
|
7
|
+
data.tar.gz: 5e7201f4ee5148c7fe4e2ac27816798e8e008849631f331233d2ec2aed2badd30ca8d56d4359bccafaedbbc17310a04e6786fe47ad23f7b8be38f57879c37d73
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright © 2017 Ryan Lue
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
8
|
+
subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
15
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Something Like That
|
2
|
+
===================
|
3
|
+
|
4
|
+
See how closely two long, multi-word phrases match each other.
|
5
|
+
|
6
|
+
Something Like That is asymmetrical, meaning “Azkaban” will match “Harry Potter and the Prisoner of Azkaban” much more strongly than vice versa. Great for ordering search results gathered from diverse sources.
|
7
|
+
|
8
|
+
Based on the modified Monge-Elkan method described in [Jimenez et al. (2009)][paper] (pdf), using the [amatch][amatch] library’s implementation of the Jaro-Winkler similarity measure.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
```bash
|
14
|
+
$ gem install something_like_that
|
15
|
+
```
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
>> require 'something_like_that'
|
22
|
+
=> true
|
23
|
+
>> query = SomethingLikeThat.new('Hannibal Lecter')
|
24
|
+
=> "Hannibal Lecter"
|
25
|
+
>> query.match('Hannibal Lecter Goes to Washington')
|
26
|
+
=> 1.0
|
27
|
+
>> query.match('Hannibal Buress')
|
28
|
+
=> 0.7071067811865476
|
29
|
+
>> query.match?('Hannibal Buress')
|
30
|
+
=> false
|
31
|
+
>> SomethingLikeThat::Scorer.threshold = 0.7
|
32
|
+
=> 0.7
|
33
|
+
>> query.match?('Hannibal Buress')
|
34
|
+
=> true
|
35
|
+
```
|
36
|
+
|
37
|
+
Config
|
38
|
+
------
|
39
|
+
|
40
|
+
This gem collects similarity scores for matching pairs of tokens (words) from two different phrases, then averages them together.
|
41
|
+
|
42
|
+
* `SomethingLikeThat::Scorer.threshold` (default = 0.8)
|
43
|
+
During the first (tokenwise) round of scoring, match scores below this value are dropped to 0. Once the resulting scores are averaged, this value determines whether `#match?` returns `true` or `false`.
|
44
|
+
* `SomethingLikeThat::Scorer.mean_exponent` (default = 2)
|
45
|
+
The method outlined by Jimenez et al. (2009) uses a [generalized mean][gmean] to favor matches over non-matches. For a two-word phrase, one exact match (1.0) and one non-match (0.0) produce an arithmetic mean (_p = 1_) of 0.5 and a quadratic mean (_p = 2_) of ~0.7. (For an in-depth analysis, [see Section 3 of their paper][paper].)
|
46
|
+
|
47
|
+
License
|
48
|
+
-------
|
49
|
+
|
50
|
+
The MIT License (MIT)
|
51
|
+
|
52
|
+
Copyright © 2017 Ryan Lue
|
53
|
+
|
54
|
+
[paper]: http://www.gelbukh.com/CV/Publications/2009/Generalized%20Mongue-Elkan%20Method%20for%20Approximate%20Text%20String.pdf
|
55
|
+
[amatch]: https://github.com/flori/amatch
|
56
|
+
[gmean]: https://en.wikipedia.org/wiki/Generalized_mean
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SomethingLikeThat
|
2
|
+
# Holds a string phrase to be compared to other (candidate) MatchPhrases.
|
3
|
+
# Queries may be abbreviated/truncated; candidates should be complete.
|
4
|
+
class Query < MatchPhrase
|
5
|
+
attr_reader :scorer
|
6
|
+
|
7
|
+
def initialize(phrase)
|
8
|
+
super phrase
|
9
|
+
@scorer = Scorer.new(self)
|
10
|
+
end
|
11
|
+
|
12
|
+
def match(candidate)
|
13
|
+
scorer.score(MatchPhrase.new(candidate))
|
14
|
+
end
|
15
|
+
|
16
|
+
def match?(candidate)
|
17
|
+
scorer.match?(MatchPhrase.new(candidate))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module SomethingLikeThat
|
2
|
+
# Holds a string phrase for a Query to be compared to.
|
3
|
+
class MatchPhrase < String
|
4
|
+
# TODO: lump determiners and prepositions in with their subjects?
|
5
|
+
# if so, then must create MatchToken class
|
6
|
+
def tokens
|
7
|
+
downcase.split(/[^\w']/).reject(&:empty?)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'amatch'
|
2
|
+
|
3
|
+
module SomethingLikeThat
|
4
|
+
# Computes similarity scores in asymmetrical comparison of string phrases.
|
5
|
+
class Scorer
|
6
|
+
@mean_exponent = 2
|
7
|
+
@threshold = 0.8
|
8
|
+
class << self
|
9
|
+
attr_reader :mean_exponent, :threshold
|
10
|
+
|
11
|
+
def mean_exponent=(p)
|
12
|
+
unless p.is_a?(Integer)
|
13
|
+
raise TypeError, 'mean_exponent must be an integer'
|
14
|
+
# TODO: add explanation in rdoc documentation?
|
15
|
+
end
|
16
|
+
@mean_exponent = p
|
17
|
+
end
|
18
|
+
|
19
|
+
def threshold=(threshold)
|
20
|
+
unless threshold.between?(0, 1)
|
21
|
+
raise RangeError, 'threshold must be between 0 and 1'
|
22
|
+
# TODO: add explanation in rdoc documentation?
|
23
|
+
end
|
24
|
+
@threshold = threshold
|
25
|
+
end
|
26
|
+
|
27
|
+
def generalized_mean(numbers, p = mean_exponent)
|
28
|
+
(numbers.map { |x| x**p }.reduce(:+).to_f / numbers.length)**(1.0 / p)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :query_matchers
|
33
|
+
|
34
|
+
def initialize(query, matcher = Amatch::JaroWinkler)
|
35
|
+
@query_matchers = query.tokens.map { |token| matcher.new(token) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def score(candidate)
|
39
|
+
average(top_scores(candidate.tokens))
|
40
|
+
end
|
41
|
+
|
42
|
+
def match?(candidate)
|
43
|
+
score(candidate) > self.class.threshold
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def top_scores(candidate_tokens, max_finder = TwoDArray)
|
49
|
+
all_scores = tokenwise_score_table(candidate_tokens)
|
50
|
+
max_finder.new(all_scores).maxima
|
51
|
+
end
|
52
|
+
|
53
|
+
def tokenwise_score_table(candidate_tokens)
|
54
|
+
query_matchers.map do |matcher|
|
55
|
+
candidate_tokens.map do |token|
|
56
|
+
apply_threshold(matcher.match(token))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def apply_threshold(score)
|
62
|
+
score > self.class.threshold ? score : 0
|
63
|
+
end
|
64
|
+
|
65
|
+
def average(scores)
|
66
|
+
self.class.generalized_mean(scores)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module SomethingLikeThat
|
4
|
+
# Holds a 2-dimensional numeric array.
|
5
|
+
# Unstable. We're holding out for a better numeric array library...
|
6
|
+
class TwoDArray < Array
|
7
|
+
def initialize(array)
|
8
|
+
super array
|
9
|
+
end
|
10
|
+
|
11
|
+
# TODO: rename (incl. corresponding method call in Scorer)
|
12
|
+
def maxima
|
13
|
+
max_vals = Array.new(length)
|
14
|
+
double = dup
|
15
|
+
max_vals.map.with_index { |_, i| i < lengths.min ? double.pop_max : 0 }
|
16
|
+
end
|
17
|
+
|
18
|
+
def pop_max
|
19
|
+
max = flatten.max
|
20
|
+
eliminate(coords(max))
|
21
|
+
max
|
22
|
+
end
|
23
|
+
|
24
|
+
def lengths
|
25
|
+
x = length
|
26
|
+
y = map(&:length).max
|
27
|
+
min, max = [x, y].minmax
|
28
|
+
OpenStruct.new(x: x, y: y, min: min, max: max)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def eliminate(axes)
|
34
|
+
delete_at(axes.x)
|
35
|
+
each { |row| row.delete_at(axes.y) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def coords(value)
|
39
|
+
x = index { |row| row.include?(value) }
|
40
|
+
y = slice(x).index(value)
|
41
|
+
OpenStruct.new(x: x, y: y)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'something_like_that/match_phrase'
|
2
|
+
require 'something_like_that/match_phrase/query'
|
3
|
+
require 'something_like_that/scorer'
|
4
|
+
require 'something_like_that/two_d_array'
|
5
|
+
|
6
|
+
# This module encapsulates the entire Something Like That gem, and contains a
|
7
|
+
# top-level wrapper method to provide a simplified public interface.
|
8
|
+
module SomethingLikeThat
|
9
|
+
def self.new(phrase)
|
10
|
+
Query.new(phrase)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'something_like_that/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'something_like_that'
|
7
|
+
spec.version = SomethingLikeThat::VERSION
|
8
|
+
spec.author = 'Ryan Lue'
|
9
|
+
spec.email = 'ryan.lue@gmail.com'
|
10
|
+
|
11
|
+
spec.summary = 'Fuzzy string matching for long, multi-word phrases.'
|
12
|
+
spec.description = 'See how closely two long, multi-word phrases ' \
|
13
|
+
'match each other. Something Like That is ' \
|
14
|
+
'asymmetrical, meaning “Azkaban” will match ' \
|
15
|
+
'“Harry Potter and the Prisoner of Azkaban” much ' \
|
16
|
+
'more strongly than vice versa. Great for ' \
|
17
|
+
'ordering search results gathered from diverse ' \
|
18
|
+
'sources.'
|
19
|
+
spec.homepage = 'https://github.com/rlue/something_like_that'
|
20
|
+
spec.license = 'MIT'
|
21
|
+
|
22
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
f.match(%r{^(spec/|\.\w)})
|
24
|
+
end
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
spec.required_ruby_version = '>= 2.3.0'
|
27
|
+
spec.add_runtime_dependency 'amatch', '~> 0.3'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: something_like_that
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Lue
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: amatch
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.3'
|
27
|
+
description: See how closely two long, multi-word phrases match each other. Something
|
28
|
+
Like That is asymmetrical, meaning “Azkaban” will match “Harry Potter and the Prisoner
|
29
|
+
of Azkaban” much more strongly than vice versa. Great for ordering search results
|
30
|
+
gathered from diverse sources.
|
31
|
+
email: ryan.lue@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- lib/something_like_that.rb
|
40
|
+
- lib/something_like_that/match_phrase.rb
|
41
|
+
- lib/something_like_that/match_phrase/query.rb
|
42
|
+
- lib/something_like_that/scorer.rb
|
43
|
+
- lib/something_like_that/two_d_array.rb
|
44
|
+
- lib/something_like_that/version.rb
|
45
|
+
- something_like_that.gemspec
|
46
|
+
homepage: https://github.com/rlue/something_like_that
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 2.3.0
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 2.6.8
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Fuzzy string matching for long, multi-word phrases.
|
70
|
+
test_files: []
|