seasy 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p290"
8
+
9
+ #
10
+ # First we attempt to load the desired environment directly from the environment
11
+ # file. This is very fast and efficicent compared to running through the entire
12
+ # CLI and selector. If you want feedback on which environment was used then
13
+ # insert the word 'use' after --create as this triggers verbose mode.
14
+ #
15
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
16
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] ; then
17
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
18
+ else
19
+ # If the environment file has not yet been created, use the RVM CLI to select.
20
+ rvm --create use "$environment_id"
21
+ fi
22
+
23
+ #
24
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
25
+ # it be automatically loaded. Uncomment the following and adjust the filename if
26
+ # necessary.
27
+ #
28
+ # filename=".gems"
29
+ # if [[ -s "$filename" ]] ; then
30
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
31
+ # fi
32
+
33
+ #
34
+ # If you use bundler and would like to run bundle each time you enter the
35
+ # directory, you can uncomment the following code.
36
+ #
37
+ # # Ensure that Bundler is installed. Install it if it is not.
38
+ # if ! command -v bundle >/dev/null; then
39
+ # printf "The rubygem 'bundler' is not installed. Installing it now.\n"
40
+ # gem install bundler
41
+ # fi
42
+ #
43
+ # # Bundle while reducing excess noise.
44
+ # printf "Bundling your gems. This may take a few minutes on a fresh clone.\n"
45
+ # bundle | grep -v '^Using ' | grep -v ' is complete' | sed '/^$/d'
46
+ #
47
+
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", ">= 2.5.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.9.2)
11
+ rcov (0.9.10)
12
+ rspec (2.6.0)
13
+ rspec-core (~> 2.6.0)
14
+ rspec-expectations (~> 2.6.0)
15
+ rspec-mocks (~> 2.6.0)
16
+ rspec-core (2.6.4)
17
+ rspec-expectations (2.6.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.6.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.6.4)
27
+ rcov
28
+ rspec (>= 2.5.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Fredrik Rubensson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = seasy
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to seasy
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Fredrik Rubensson. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "seasy"
18
+ gem.homepage = "http://github.com/froderik/seasy"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{server less search}
21
+ gem.description = %Q{An easy to use search index (requiring no external servers) with a pluggable design for index storage.}
22
+ gem.email = "fredrik@eldfluga.se"
23
+ gem.authors = ["Fredrik Rubensson"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "seasy #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
@@ -0,0 +1,29 @@
1
+ class Fragmentizer
2
+ def fragmentize str, weights = Hash.new( 0 )
3
+ split = str.split
4
+
5
+ if split.size > 1
6
+ split.each do |one|
7
+ fragmentize one, weights
8
+ end
9
+ weights
10
+ else
11
+ length = str.length
12
+
13
+ # loop over all possible intervals
14
+ (1..length).each do |interval|
15
+ fragmentize_in_interval str, interval, weights
16
+ #interval += 1
17
+ end
18
+ weights
19
+ end
20
+ end
21
+
22
+ def fragmentize_in_interval str, interval, weights
23
+ length = str.length
24
+ (0..length-interval).each do |i|
25
+ current = str[i, interval ]
26
+ weights[current] += 1
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,101 @@
1
+ require 'singleton'
2
+
3
+ module Seasy
4
+
5
+ class Configuration
6
+ include Singleton
7
+
8
+ attr_accessor :storage
9
+
10
+ def initialize
11
+ @storage = Seasy::InMemoryStorage
12
+ end
13
+ end
14
+
15
+ def configure
16
+ config = Seasy::Configuration.instance
17
+ yield config
18
+ end
19
+
20
+ class Index
21
+ attr_accessor :name
22
+
23
+ def initialize name = 'default'
24
+ @name = name
25
+ @storage = Configuration.instance.storage.new
26
+ end
27
+
28
+ def Index::default
29
+ @@defaultee = Index.new if not defined? @@defaultee
30
+ end
31
+
32
+ def Index::with_name name
33
+ stringed_name = name.to_s
34
+ @@indices = {} if not defined? @@indices
35
+ if @@indices[stringed_name].nil?
36
+ @@indices[stringed_name] = Index.new stringed_name
37
+ end
38
+ @@indices[stringed_name]
39
+ end
40
+
41
+ def add searchee, target
42
+ save target, fragmentize( searchee )
43
+ end
44
+
45
+ def fragmentize searchee
46
+ f = Fragmentizer.new
47
+ f.fragmentize searchee
48
+ end
49
+
50
+ def save target, weights
51
+ @storage.save target, weights
52
+ end
53
+
54
+ def search query
55
+ @storage.search query
56
+ end
57
+
58
+ def clear
59
+ @storage.clear
60
+ end
61
+ end
62
+
63
+ # a store got search queries as keys and an array of
64
+ # target-weight tuples as values
65
+ class InMemoryStorage
66
+ def initialize
67
+ @store = {}
68
+ end
69
+
70
+ # target is a simple value - we care not what
71
+ # weights are all fragments (indices) and their weight
72
+ # eg. { "aba" => 1, "ab" => 1, "ba" => 1, "b" => 1, "a" => 2 } for the string "aba"
73
+ def save target, weights
74
+ weights.keys.each do |key|
75
+ add weights[key], key, target
76
+ end
77
+ end
78
+
79
+ def add weight, key, target
80
+ if @store[key].nil?
81
+ @store[key] = {target => weight}
82
+ elsif @store[key][target].nil?
83
+ @store[key][target] = weight
84
+ else
85
+ @store[key][target] += weight
86
+ end
87
+ end
88
+
89
+ # return { target1 => weight, target2 => weight }
90
+ def search query
91
+ @store[query] || {}
92
+ end
93
+
94
+ def clear
95
+ @store = {}
96
+ end
97
+
98
+ end
99
+
100
+ module_function :configure
101
+ end
@@ -0,0 +1,2 @@
1
+ require 'fragmentizer'
2
+ require 'index'
@@ -0,0 +1,48 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'fragmentizer'
5
+
6
+ describe Fragmentizer do
7
+ it "should split a string in its parts" do
8
+ f = Fragmentizer.new
9
+ result = f.fragmentize( "abc" )
10
+ result.size.should == 6
11
+ result["a"].should == 1
12
+ result["b"].should == 1
13
+ result["c"].should == 1
14
+ result["ab"].should == 1
15
+ result["bc"].should == 1
16
+ result["abc"].should == 1
17
+ end
18
+
19
+ it "should count several ocurrences" do
20
+ f = Fragmentizer.new
21
+ result = f.fragmentize( "abcab" )
22
+ result.size.should == 12
23
+ result["a"].should == 2
24
+ result["b"].should == 2
25
+ result["c"].should == 1
26
+ result["ab"].should == 2
27
+ result["bc"].should == 1
28
+ result["ca"].should == 1
29
+ result["abc"].should == 1
30
+ result["abca"].should == 1
31
+ result["abcab"].should == 1
32
+ result["bca"].should == 1
33
+ result["bcab"].should == 1
34
+ result["cab"].should == 1
35
+ end
36
+
37
+ it "should count conecutive singularities" do
38
+ f = subject
39
+ result = f.fragmentize "fluffluff"
40
+ result["f"].should == 5
41
+ end
42
+
43
+ it "should split a string into parts on whitespace" do
44
+ f = Fragmentizer.new
45
+ result = f.fragmentize( "ab c" )
46
+ result.size.should == 4
47
+ end
48
+ end
@@ -0,0 +1,89 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'fragmentizer'
5
+ require 'index'
6
+
7
+ include Seasy
8
+
9
+ describe Index do
10
+ before :each do
11
+ subject.clear
12
+ end
13
+
14
+ it "should default and have some basic behaviour" do
15
+ i = subject
16
+ i.add 'fredrik den store', 1
17
+ i.search( "red" ).should == {1 => 1}
18
+ i.add 'red red wine', 2
19
+ i.search( "red").should == {2 => 2 ,1 => 1}
20
+ i.search( "e" ).should == {1 => 3, 2 => 3}
21
+ end
22
+
23
+ it "should be possible to add complex strings twice" do
24
+ i = subject
25
+ i.add 'fluff', 1
26
+ i.search( 'f' ).should == {1 => 3}
27
+ i.add 'fluffluff', 1
28
+ i.search( 'fluff' ).should == {1 => 3}
29
+ i.search( 'lu' ).should == {1 => 3}
30
+ i.search( 'f' ).should == {1 => 8}
31
+ end
32
+
33
+ it "should have named indices" do
34
+ one_index = Index.with_name 42
35
+ another_index = Index.with_name 66
36
+
37
+ one_index.name.should == '42'
38
+ another_index.name.should == '66'
39
+
40
+ one_index.add 'meaning', 'universe'
41
+ another_index.add 'evilness', 'hell'
42
+
43
+ one_index.search( 'vil' ).should == { }
44
+ another_index.search( 'vil' ).should == { 'hell' => 1 }
45
+
46
+ one_index.search( 'ean' ).should == {'universe' => 1}
47
+ another_index.search( 'ean' ).should == { }
48
+ end
49
+
50
+ it "should have a configurable storage" do
51
+ configure do |config|
52
+ config.storage = DummyStorage
53
+ end
54
+
55
+ i = Index.default
56
+ i.add 'a', 1
57
+ i.search 'a'
58
+ DummyStorage.should be_saved_once
59
+ DummyStorage.should be_searched_once
60
+ end
61
+ end
62
+
63
+ class DummyStorage
64
+ def initialize
65
+ @@saved_count = 0
66
+ @@searched_count = 0
67
+ end
68
+
69
+ def save target, weights
70
+ @@saved_count += 1
71
+ end
72
+
73
+ def search query
74
+ @@searched_count += 1
75
+ end
76
+
77
+ def DummyStorage::saved_once?
78
+ @@saved_count == 1
79
+ end
80
+
81
+ def DummyStorage::searched_once?
82
+ @@searched_count == 1
83
+ end
84
+
85
+ def clear
86
+ end
87
+ end
88
+
89
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seasy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.4
6
+ platform: ruby
7
+ authors:
8
+ - Fredrik Rubensson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-14 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.5.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.4
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description: An easy to use search index (requiring no external servers) with a pluggable design for index storage.
61
+ email: fredrik@eldfluga.se
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .rvmrc
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - lib/fragmentizer.rb
79
+ - lib/index.rb
80
+ - lib/seasy.rb
81
+ - spec/fragmentizer_spec.rb
82
+ - spec/index_spec.rb
83
+ has_rdoc: true
84
+ homepage: http://github.com/froderik/seasy
85
+ licenses:
86
+ - MIT
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: -968263035200512692
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.6.0
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: server less search
114
+ test_files: []
115
+