sakuru 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +7 -0
- data/bin/sakuru +5 -0
- data/examples/basic.rb +13 -0
- data/lib/sakuru.rb +2 -0
- data/lib/sakuru/command.rb +40 -0
- data/lib/sakuru/database.rb +65 -0
- data/lib/sakuru/version.rb +3 -0
- data/sakuru.gemspec +24 -0
- data/test/run-test.rb +11 -0
- data/test/test-database.rb +47 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d7175d2d96ac65b8bebf91d65952993b73b9e734
|
4
|
+
data.tar.gz: f41f4ecad99bd2e00a2505c918440a17b52e65a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 59e6d10262aed1c6e677d1881d1ddc52fd86572dc6eaed3f211d49405e19a2c1545ae9a446ab4eacd20783ceb60968fa853dede7204c0c1a6d803968c2c635a1
|
7
|
+
data.tar.gz: a2c44811e47630b4b1860231aba219ea213da05a338f6581ade73edd58395521946c7b31b652f7e3cbc1fad6a866184e715cd37434cb1dd2dabb08b26c3af890
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Masafumi Yokoyama <myokoym@gmail.com>
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Sakuru [![Gem Version](https://badge.fury.io/rb/sakuru.svg)](http://badge.fury.io/rb/sakuru)
|
2
|
+
|
3
|
+
A tiny full-text search engine by pure Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install sakuru
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require "sakuru"
|
13
|
+
|
14
|
+
db = Sakuru::Database.new
|
15
|
+
db.add(file_path)
|
16
|
+
db.add(uri)
|
17
|
+
db.search(query)
|
18
|
+
db.save(output_path)
|
19
|
+
db.load(saved_file_path)
|
20
|
+
```
|
21
|
+
|
22
|
+
## License
|
23
|
+
|
24
|
+
MIT License. See LICENSE.txt for details.
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/sakuru
ADDED
data/examples/basic.rb
ADDED
data/lib/sakuru.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "sakuru/version"
|
3
|
+
|
4
|
+
module Sakuru
|
5
|
+
class Command
|
6
|
+
def self.run(arguments)
|
7
|
+
new(arguments).run
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(arguments)
|
11
|
+
@options = parse_options(arguments)
|
12
|
+
end
|
13
|
+
|
14
|
+
def run
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def parse_options(arguments)
|
19
|
+
options = {}
|
20
|
+
|
21
|
+
parser = OptionParser.new(<<-USAGE)
|
22
|
+
Usage:
|
23
|
+
require "sakuru"
|
24
|
+
|
25
|
+
db = Sakuru::Database.new
|
26
|
+
db.add(file_path)
|
27
|
+
db.add(uri)
|
28
|
+
db.search(query)
|
29
|
+
db.save(output_path)
|
30
|
+
db.load(saved_file_path)
|
31
|
+
USAGE
|
32
|
+
|
33
|
+
parser.version = VERSION
|
34
|
+
|
35
|
+
parser.parse!(arguments)
|
36
|
+
|
37
|
+
options
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module Sakuru
|
5
|
+
class Database
|
6
|
+
attr_reader :files
|
7
|
+
def initialize
|
8
|
+
@inverted_index = {}
|
9
|
+
@files = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def add(path)
|
13
|
+
if @files.include?(path)
|
14
|
+
id = @files.index(path)
|
15
|
+
@inverted_index.each do |key, posting_list|
|
16
|
+
@inverted_index[key].delete(id)
|
17
|
+
end
|
18
|
+
else
|
19
|
+
@files << path
|
20
|
+
id = @files.index(path)
|
21
|
+
end
|
22
|
+
|
23
|
+
open(path) do |file|
|
24
|
+
file.each_line do |line|
|
25
|
+
# TODO: normalize and tokenize.
|
26
|
+
line.split(/\s+/).each do |word|
|
27
|
+
next if word.empty?
|
28
|
+
@inverted_index[word] ||= []
|
29
|
+
# TODO: add position
|
30
|
+
@inverted_index[word] << id
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def search(query)
|
37
|
+
results = {}
|
38
|
+
# TODO: normalize and tokenize.
|
39
|
+
ids = @inverted_index[query]
|
40
|
+
return results unless ids
|
41
|
+
ids.each do |id|
|
42
|
+
file = @files[id]
|
43
|
+
results[file] ||= 0
|
44
|
+
results[file] += 1
|
45
|
+
end
|
46
|
+
results
|
47
|
+
end
|
48
|
+
|
49
|
+
def save(output_path)
|
50
|
+
data = {
|
51
|
+
"files" => @files,
|
52
|
+
"inverted_index" => @inverted_index,
|
53
|
+
}
|
54
|
+
File.open(output_path, "w") do |file|
|
55
|
+
JSON.dump(data, file)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def load(saved_file_path)
|
60
|
+
data = JSON.load(File.read(saved_file_path))
|
61
|
+
@files = data["files"]
|
62
|
+
@inverted_index = data["inverted_index"]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/sakuru.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sakuru/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sakuru"
|
8
|
+
spec.version = Sakuru::VERSION
|
9
|
+
spec.authors = ["Masafumi Yokoyama"]
|
10
|
+
spec.email = ["myokoym@gmail.com"]
|
11
|
+
spec.description = %q{A tiny full-text search engine by pure Ruby.}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://github.com/myokoym/sakuru"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency("test-unit", ">= 3.0.0")
|
22
|
+
spec.add_development_dependency("bundler")
|
23
|
+
spec.add_development_dependency("rake")
|
24
|
+
end
|
data/test/run-test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
base_dir = File.expand_path("..", File.dirname(__FILE__))
|
4
|
+
lib_dir = File.join(base_dir, "lib")
|
5
|
+
test_dir = File.join(base_dir, "test")
|
6
|
+
$LOAD_PATH.unshift(lib_dir)
|
7
|
+
$LOAD_PATH.unshift(test_dir)
|
8
|
+
|
9
|
+
require "test-unit"
|
10
|
+
|
11
|
+
exit Test::Unit::AutoRunner.run(true, test_dir)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "tempfile"
|
2
|
+
require "sakuru/database"
|
3
|
+
|
4
|
+
class DatabaseTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@database = Sakuru::Database.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_add
|
10
|
+
@database.add(__FILE__)
|
11
|
+
assert_equal([__FILE__], @database.files)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_search
|
15
|
+
@database.add(__FILE__)
|
16
|
+
assert_equal({__FILE__ => 1},
|
17
|
+
@database.search("DatabaseTest"))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_search_not_match
|
21
|
+
@database.add(__FILE__)
|
22
|
+
assert_equal({},
|
23
|
+
@database.search("\0"))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_save
|
27
|
+
@database.add(__FILE__)
|
28
|
+
file = Tempfile.new("sakuru")
|
29
|
+
@database.save(file)
|
30
|
+
file.flush
|
31
|
+
assert_equal([__FILE__],
|
32
|
+
JSON.load(file)["files"])
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_load
|
36
|
+
data = {
|
37
|
+
"files" => ["piyo.txt"],
|
38
|
+
"inverted_index" => {},
|
39
|
+
}
|
40
|
+
file = Tempfile.new("sakuru")
|
41
|
+
JSON.dump(data, file)
|
42
|
+
file.flush
|
43
|
+
@database.load(file)
|
44
|
+
assert_equal(["piyo.txt"],
|
45
|
+
@database.files)
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sakuru
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masafumi Yokoyama
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A tiny full-text search engine by pure Ruby.
|
56
|
+
email:
|
57
|
+
- myokoym@gmail.com
|
58
|
+
executables:
|
59
|
+
- sakuru
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/sakuru
|
69
|
+
- examples/basic.rb
|
70
|
+
- lib/sakuru.rb
|
71
|
+
- lib/sakuru/command.rb
|
72
|
+
- lib/sakuru/database.rb
|
73
|
+
- lib/sakuru/version.rb
|
74
|
+
- sakuru.gemspec
|
75
|
+
- test/run-test.rb
|
76
|
+
- test/test-database.rb
|
77
|
+
homepage: https://github.com/myokoym/sakuru
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: A tiny full-text search engine by pure Ruby.
|
101
|
+
test_files:
|
102
|
+
- test/run-test.rb
|
103
|
+
- test/test-database.rb
|
104
|
+
has_rdoc:
|