fuzzy_ruby 0.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.
- checksums.yaml +7 -0
- data/lib/fuzzy_ruby.rb +64 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e0fe2ae6cd524a0bfa7a0c3e4653d3f79dfa351a
|
4
|
+
data.tar.gz: 9d71a16cd1c4ac1051b7dcbc2f585c26bc364bec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f0664c599f047b153874de54f2665ecff13dd69982ca768ebd113575e3292ff1d949734b43dd7f974ae17db4079b906f55c354bb2eea5f6f884c83f7a38869a9
|
7
|
+
data.tar.gz: c793499508e103b9697292af23355248fc2656439ba8502a1779c2f44a380c9adb3e88065a7bb8726f13f57f1756f46d740ce5aba0d3b07817464c199373ffc9
|
data/lib/fuzzy_ruby.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
class Fuzzy
|
2
|
+
|
3
|
+
def self.find(strings, input)
|
4
|
+
# array to store weights of each string
|
5
|
+
@weights = Array.new(strings.length, -1)
|
6
|
+
|
7
|
+
# array to store each string
|
8
|
+
@strings = strings.dup
|
9
|
+
|
10
|
+
# array to return
|
11
|
+
ret = Array.new
|
12
|
+
|
13
|
+
# build regular expression
|
14
|
+
regexp = /#{build_regexp(input)}/
|
15
|
+
|
16
|
+
# for each string, calculate weight
|
17
|
+
@strings.each_index do |i|
|
18
|
+
assign_weights(i, regexp)
|
19
|
+
end
|
20
|
+
|
21
|
+
# delete string if there is no match
|
22
|
+
@weights.each_index do |i|
|
23
|
+
if @weights[i] == -1
|
24
|
+
@weights.delete_at(i)
|
25
|
+
strings.delete_at(i)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# return array of strings with best match (least weight)
|
30
|
+
@weights.map.with_index.sort_by(&:first).map(&:last).each do |index|
|
31
|
+
ret.push strings[index]
|
32
|
+
end
|
33
|
+
|
34
|
+
return ret
|
35
|
+
end
|
36
|
+
|
37
|
+
# build the regular expression
|
38
|
+
def self.build_regexp(string)
|
39
|
+
regexp = ""
|
40
|
+
string.each_char do |c|
|
41
|
+
regexp << "(\w*)#{c}"
|
42
|
+
end
|
43
|
+
regexp << "(\w*)"
|
44
|
+
end
|
45
|
+
|
46
|
+
# recursive function to assign weight to string
|
47
|
+
# once there is match, removes one character from the reg exp and repeat
|
48
|
+
def self.assign_weights(index, regexp)
|
49
|
+
weight = @strings[index] =~ regexp
|
50
|
+
if weight == nil
|
51
|
+
return
|
52
|
+
end
|
53
|
+
@weights[index] += weight
|
54
|
+
if !done(index)
|
55
|
+
@strings[index] = @strings[index][weight+1..@strings[index].length]
|
56
|
+
assign_weights(index, /#{regexp.source[5..regexp.source.length]}/)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.done(index)
|
61
|
+
@strings[index] == nil || @weights[index] == -1 ? true : false
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fuzzy_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danny
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Fuzzy finder for Ruby
|
14
|
+
email: dannyqho@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/fuzzy_ruby.rb
|
20
|
+
homepage: http://www.github.com/Danny--
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Fuzzy finder for Ruby
|
44
|
+
test_files: []
|