jgorset-search 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/bin/console +14 -0
- data/bin/search +27 -0
- data/bin/setup +8 -0
- data/bin/suggest +27 -0
- data/lib/search/cli.rb +37 -0
- data/lib/search/version.rb +3 -0
- data/lib/search.rb +78 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0aa929e751d2c6d415764016f589d9fb3b2986bd
|
4
|
+
data.tar.gz: df8c02c466dcf224cdbbd1cd1eea3b1f6849221d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b55fbef49bdd628750cf4015ed07386eb8aeb2c70e635258cb53c2710db5c7b6a50f78c681914498317b156acf95ae35fd09ae09b654d547670001a5df799af9
|
7
|
+
data.tar.gz: 1ddabefb8ec74b4ab0068992d9362ea2a0446ed39f8d59020d2293cc1e1413dbb5e6087db9cc3d09130a5c64dc1880629671460fffcee2b16297beb1903065f7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Johannes Gorset
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Search
|
2
|
+
|
3
|
+
This gem searches files and offers suggestions to narrow the search.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install jgorset-search
|
8
|
+
|
9
|
+
You'll need Ruby > 2.1.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Find files matching the given string
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ search 'foo' --files=spec/fixtures/*.txt
|
17
|
+
[100%] texts/just_the_word_foo.txt
|
18
|
+
[10%] texts/foo_and_other_words.txt
|
19
|
+
```
|
20
|
+
|
21
|
+
### Get suggestions for narrowing your search
|
22
|
+
|
23
|
+
```sh
|
24
|
+
$ suggest 'what is' --files=spec/fixtures/*.txt
|
25
|
+
what is the meaning of life?
|
26
|
+
what is my ip
|
27
|
+
what is pokemon go
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can
|
33
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
36
|
+
release a new version, update the version number in `version.rb`, and then run
|
37
|
+
`bundle exec rake release`, which will create a git tag for the version, push git
|
38
|
+
commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jgorset/search.
|
43
|
+
|
44
|
+
## License
|
45
|
+
|
46
|
+
The gem is available as open source under the terms of the
|
47
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "search"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/search
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'search'
|
4
|
+
require 'slop'
|
5
|
+
|
6
|
+
begin
|
7
|
+
options = Slop.parse do |o|
|
8
|
+
o.string '-f', '--files', 'A file glob to search'
|
9
|
+
o.integer '-n', '--number', 'A number of results to return', default: 10
|
10
|
+
o.on '-v', '--version' do
|
11
|
+
puts "search #{Search::VERSION}"
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
o.on '-h', '--help' do
|
15
|
+
puts o
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
search = Search::CLI.new options.arguments.first, options
|
21
|
+
|
22
|
+
search.qualities.each do |quality, filename|
|
23
|
+
puts "[#{quality}%] #{filename}"
|
24
|
+
end
|
25
|
+
rescue StandardError => error
|
26
|
+
puts error.message
|
27
|
+
end
|
data/bin/setup
ADDED
data/bin/suggest
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'search'
|
4
|
+
require 'slop'
|
5
|
+
|
6
|
+
begin
|
7
|
+
options = Slop.parse do |o|
|
8
|
+
o.string '-f', '--files', 'A file glob to search'
|
9
|
+
o.integer '-n', '--number', 'A number of results to return', default: 10
|
10
|
+
o.on '-v', '--version' do
|
11
|
+
puts "suggest #{Search::VERSION}"
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
o.on '-h', '--help' do
|
15
|
+
puts o
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
search = Search::CLI.new options.arguments.first, options
|
21
|
+
|
22
|
+
search.suggestions.each do |suggestion|
|
23
|
+
puts suggestion
|
24
|
+
end
|
25
|
+
rescue StandardError => error
|
26
|
+
puts error.message
|
27
|
+
end
|
data/lib/search/cli.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class Search
|
2
|
+
class CLI
|
3
|
+
attr_reader :search, :glob, :limit
|
4
|
+
|
5
|
+
def initialize(search, options)
|
6
|
+
@search = search
|
7
|
+
@glob = options[:files]
|
8
|
+
@limit = options[:n]
|
9
|
+
end
|
10
|
+
|
11
|
+
def files
|
12
|
+
raise Error, 'You must provide files to search' if glob.nil?
|
13
|
+
|
14
|
+
filenames.map do |filename|
|
15
|
+
File.open(filename)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def filenames
|
20
|
+
Dir.glob(glob)
|
21
|
+
end
|
22
|
+
|
23
|
+
def qualities
|
24
|
+
filenames_with_quality = files.map do |file|
|
25
|
+
[Search.new(search, haystack: file.read).quality, file.path]
|
26
|
+
end
|
27
|
+
|
28
|
+
filenames_with_quality.sort_by(&:first).reverse[0, limit]
|
29
|
+
end
|
30
|
+
|
31
|
+
def suggestions
|
32
|
+
files.map do |file|
|
33
|
+
Search.new(search, haystack: file.read).suggestions
|
34
|
+
end[0, limit]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/search.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'search/version'
|
2
|
+
require 'search/cli'
|
3
|
+
|
4
|
+
class Search
|
5
|
+
# Initializes a new search.
|
6
|
+
#
|
7
|
+
# search - A String describing the term to search for.
|
8
|
+
# haystack - A String to search.
|
9
|
+
def initialize(search, haystack:)
|
10
|
+
@search = search
|
11
|
+
@haystack = haystack
|
12
|
+
end
|
13
|
+
|
14
|
+
# Get suggestions for narrowing the search.
|
15
|
+
#
|
16
|
+
# Example
|
17
|
+
#
|
18
|
+
# search = Search.new('what is the meaning of',
|
19
|
+
# haystack: 'What is the meaning of life? And what is the meaning of lol?'
|
20
|
+
# )
|
21
|
+
#
|
22
|
+
# search.suggestions
|
23
|
+
# # => [
|
24
|
+
# # [' life', ' lol']
|
25
|
+
# # ]
|
26
|
+
#
|
27
|
+
# Returns an Array of Strings that would narrow the search.
|
28
|
+
def suggestions
|
29
|
+
suggestions = []
|
30
|
+
|
31
|
+
@haystack.scan /#{@search}(?<suggestion>\b[^.?!]+\b{1,5})/io do |match|
|
32
|
+
suggestions << match.first
|
33
|
+
end
|
34
|
+
|
35
|
+
suggestions
|
36
|
+
end
|
37
|
+
|
38
|
+
# Get the match quality.
|
39
|
+
#
|
40
|
+
# The quality is determined by how many of the search words the haystack contains.
|
41
|
+
#
|
42
|
+
# Example
|
43
|
+
#
|
44
|
+
# search = Search.new('one two'
|
45
|
+
# haystack: 'one two three four'
|
46
|
+
# )
|
47
|
+
# search.quality
|
48
|
+
# # => 50
|
49
|
+
#
|
50
|
+
# Returns an Integer describing the percentage of search words present in the haystack.
|
51
|
+
def quality
|
52
|
+
(search_words_present_in_haystack.count.to_f / search_words.count.to_f * 100).round
|
53
|
+
rescue ZeroDivisionError
|
54
|
+
0
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def search_words
|
60
|
+
@search_words ||= words_in(@search)
|
61
|
+
end
|
62
|
+
|
63
|
+
def haystack_words
|
64
|
+
@haystack_words ||= words_in(@haystack)
|
65
|
+
end
|
66
|
+
|
67
|
+
def search_words_present_in_haystack
|
68
|
+
search_words.select do |search_word|
|
69
|
+
haystack_words.include? search_word
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def words_in(string)
|
74
|
+
string.split(/[^[[:word:]]]+/)
|
75
|
+
end
|
76
|
+
|
77
|
+
class Error < StandardError; end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jgorset-search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Johannes Gorset
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: slop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.5.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.5.0
|
69
|
+
description: File search
|
70
|
+
email:
|
71
|
+
- jgorset@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- lib/search/cli.rb
|
77
|
+
- lib/search/version.rb
|
78
|
+
- lib/search.rb
|
79
|
+
- bin/console
|
80
|
+
- bin/search
|
81
|
+
- bin/setup
|
82
|
+
- bin/suggest
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
homepage: https://github.com/jgorset/search
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.0.14.1
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: File search
|
109
|
+
test_files: []
|