goto_string 0.1.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.
@@ -0,0 +1,5 @@
1
+ == 0.1.0 / 2007-07-20
2
+
3
+ * 1 major enhancement
4
+ * Gemification of goto_string library
5
+
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/goto_string.rb
6
+ test/goto_string_test.rb
@@ -0,0 +1,49 @@
1
+ goto_string
2
+ by Max Muermann
3
+
4
+
5
+ == DESCRIPTION:
6
+
7
+ goto_string is a small library that implements a substring matching and ranking algorithm. The matching and ranking is similar to that found in Quicksilver or TextMate
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Partial string matching
12
+ * The algorithm is not particularly performant
13
+
14
+ == SYNOPSIS:
15
+
16
+ [""]
17
+
18
+ == REQUIREMENTS:
19
+
20
+ * None
21
+
22
+ == INSTALL:
23
+
24
+ * sudo gem install goto_string
25
+
26
+ == LICENSE:
27
+
28
+ (The MIT License)
29
+
30
+ Copyright (c) 2007 FIX
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining
33
+ a copy of this software and associated documentation files (the
34
+ 'Software'), to deal in the Software without restriction, including
35
+ without limitation the rights to use, copy, modify, merge, publish,
36
+ distribute, sublicense, and/or sell copies of the Software, and to
37
+ permit persons to whom the Software is furnished to do so, subject to
38
+ the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be
41
+ included in all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
44
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ require './lib/goto_string.rb'
4
+
5
+ Hoe.new('goto_string', GotoString::VERSION) do |p|
6
+ p.rubyforge_name = 'goto_string'
7
+ p.author = 'Max Muermann'
8
+ p.email = 'max@muermann.org'
9
+ p.summary = 'Quicksilver-like partial string matching'
10
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
11
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
12
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
13
+ end
@@ -0,0 +1,82 @@
1
+ require 'enumerator'
2
+ require 'jcode'
3
+
4
+ module GotoString
5
+ VERSION = "0.1.0"
6
+
7
+ class Matcher
8
+
9
+ @@weight_consecutive = 14
10
+ @@weight_positional = 1
11
+
12
+ def Matcher.weight_consecutive= val
13
+ @@weight_consecutive=val
14
+ end
15
+
16
+ def Matcher.weight_positional= val
17
+ @@weight_positional=val
18
+ end
19
+
20
+ def Matcher.match( pattern, candidates, &block )
21
+
22
+ pattern.gsub! "'",""
23
+ pattern.downcase!
24
+
25
+ matches = []
26
+ candidates.each do |candidate_obj|
27
+ if block_given?
28
+ candidate = yield candidate_obj
29
+ else
30
+ candidate = candidate_obj
31
+ end
32
+ candidate = candidate.downcase
33
+
34
+ pat = pattern.dup.unpack('c*')
35
+ can = candidate.dup
36
+
37
+ pattern_len = pattern.size
38
+ matched_strings = []
39
+ current_pos = 0
40
+ consecutive = 0
41
+ sum_positions = 0
42
+
43
+ while (s=pat.shift) && (index=can.index(s))
44
+ current_pos += index+1
45
+ can = can[index+1..-1]
46
+
47
+ if (0 != index || matched_strings.empty?)
48
+ matched_strings << [s.chr,current_pos-1]
49
+ else
50
+ matched_strings.last[0] << s.chr
51
+ end
52
+
53
+ end
54
+
55
+ next if !pat.empty?
56
+
57
+ sum_positions = 0
58
+ consecutive = 0
59
+
60
+ matched_strings.each do |m|
61
+ ms = m[0].size
62
+ consecutive += (ms-1)*(ms-1)
63
+ sum_positions += (m[1]+m[1]+ms-1)*ms/2
64
+ end
65
+
66
+ score_position = ((pattern_len.to_f)*(pattern_len.to_f+1)/2.0) / sum_positions
67
+ score_consecutive = consecutive.to_f / (pattern_len*pattern_len)
68
+ matches <<[ candidate_obj, candidate, ( score_consecutive * @@weight_consecutive + score_position *@@weight_positional)/(@@weight_consecutive+@@weight_positional), matched_strings]
69
+ end
70
+ matches.sort do |a,b|
71
+ if a[2]<b[2]
72
+ 1
73
+ elsif a[2]>b[2]
74
+ -1
75
+ else
76
+ a[1].size <=> b[1].size
77
+ end
78
+ end
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class GotoStringTest < Test::Unit::TestCase
4
+ fixtures :notes
5
+
6
+ def test_simple
7
+ s = %w( iamapala implementation impl simple nomatch )
8
+ a = GotoString::Matcher.match 'impl', s
9
+ assert a
10
+ assert_equal 4, a.size
11
+ assert_equal 'impl', a[0][0]
12
+ assert_equal 'implementation', a[1][0]
13
+ assert_equal 'simple', a[2][0]
14
+ assert_equal 'iamapala', a[3][0]
15
+ end
16
+
17
+ def test_with_block
18
+ s = %w( iamapala implementation impl simple nomatch )
19
+ s = [1,2,3,4,5].zip(s)
20
+ a = GotoString::Matcher.match 'impl', s do |e| e[1] end
21
+ assert a
22
+ assert_equal 4, a.size
23
+ assert_equal [3, 'impl'], a[0][0]
24
+ assert_equal [2, 'implementation'], a[1][0]
25
+ end
26
+
27
+ def test_weights
28
+ GotoString::Matcher.weight_consecutive = 0
29
+ GotoString::Matcher.weight_positional = 1
30
+ s = %w( iamapala implementation impl simple nomatch )
31
+ a = GotoString::Matcher.match 'impl', s
32
+ assert a
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: goto_string
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-07-20 00:00:00 +10:00
8
+ summary: Quicksilver-like partial string matching
9
+ require_paths:
10
+ - lib
11
+ email: max@muermann.org
12
+ homepage: " by Max Muermann"
13
+ rubyforge_project: goto_string
14
+ description: "== FEATURES/PROBLEMS: * Partial string matching * The algorithm is not particularly performant == SYNOPSIS: [\"\"] == REQUIREMENTS:"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Max Muermann
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - lib/goto_string.rb
37
+ - test/goto_string_test.rb
38
+ test_files: []
39
+
40
+ rdoc_options:
41
+ - --main
42
+ - README.txt
43
+ extra_rdoc_files:
44
+ - History.txt
45
+ - Manifest.txt
46
+ - README.txt
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ requirements: []
52
+
53
+ dependencies:
54
+ - !ruby/object:Gem::Dependency
55
+ name: hoe
56
+ version_requirement:
57
+ version_requirements: !ruby/object:Gem::Version::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.1
62
+ version: