mini_search 1.0.3
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/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +267 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/formula1.svg +144 -0
- data/formula2.svg +79 -0
- data/lib/mini_search.rb +75 -0
- data/lib/mini_search/bm_25.rb +14 -0
- data/lib/mini_search/downcase_filter.rb +9 -0
- data/lib/mini_search/idf.rb +12 -0
- data/lib/mini_search/inverted_index.rb +163 -0
- data/lib/mini_search/inverted_index_spec.rb +34 -0
- data/lib/mini_search/language_support/portuguese.rb +19 -0
- data/lib/mini_search/pipeline.rb +20 -0
- data/lib/mini_search/remove_punctuation_filter.rb +11 -0
- data/lib/mini_search/standard_whitespace_tokenizer.rb +9 -0
- data/lib/mini_search/stemmer/portuguese.rb +365 -0
- data/lib/mini_search/stemmer_filter.rb +19 -0
- data/lib/mini_search/stop_words_filter.rb +13 -0
- data/lib/mini_search/strip_filter.rb +9 -0
- data/lib/mini_search/synonyms_filter.rb +19 -0
- data/lib/mini_search/tf.rb +9 -0
- data/lib/mini_search/version.rb +3 -0
- data/mini_search.gemspec +29 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: acd453c483524bd3ab16323edf2d237a6c037ac6aac48d365f395c80a343a606
|
4
|
+
data.tar.gz: '0681c134db08c7b354ef53ef6670aa7d984de43fbb959175cf94f0efc7d0d86c'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 726b0e4d969f9040d1b18ec9967dbf0cf28fc473b36377279f03b5ee782f8e619c9ee0e89087a540ac61a7a27fc8757113f15c7acd3153bc6b6e5fd427174fc7
|
7
|
+
data.tar.gz: 816d97d2bb91349bbe9b134d5d1b8425910fa1507c76db0743e5d9921bf74c1446e5939a50b34b1116bf0712a279af5766587ec771557ba66f29527f2f719b92
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at andrewaguiar6@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mini_search (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
mini_search!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Andrew S Aguiar
|
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,267 @@
|
|
1
|
+
# MiniSearch
|
2
|
+
|
3
|
+
A simple and naive mini search engine in memory using BM25 algorithm.
|
4
|
+
|
5
|
+
MiniSearch implements a inverted index (basically a hashmap where terms are keys and values are documents that contains that key.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mini_search'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mini_search
|
22
|
+
|
23
|
+
## Inverted Index
|
24
|
+
|
25
|
+
MiniSearch implements a inverted index (basically a hashmap where terms are keys and values are documents that contains that key.
|
26
|
+
|
27
|
+
Lets take two small documents as examples:
|
28
|
+
|
29
|
+
```
|
30
|
+
doc1 = 'The domestic dog is a member of the genus Canis, which forms part of the wolf-like canids'
|
31
|
+
doc2 = 'The cat is a small carnivorous mammal. It is the only domesticated species in the family Felidae and often referred to as the domestic cat.
|
32
|
+
```
|
33
|
+
|
34
|
+
To create an inverted index we start with an empty hashmap:
|
35
|
+
|
36
|
+
```
|
37
|
+
ii = {}
|
38
|
+
```
|
39
|
+
|
40
|
+
Now for a given document we transform its text in tokens (words):
|
41
|
+
|
42
|
+
```
|
43
|
+
doc1 = ["The", "domestic", "dog", "is", "a", "member", "of", "the", "genus", "Canis,", "which", "forms", "part", "of", "the", "wolf-like", "canids"]
|
44
|
+
doc2 = ["The", "cat", "is", "a", "small", "carnivorous", "mammal.", "It", "is", "the", "only", "domesticated", "species", "in", "the", "family", "Felidae", "and", "often", "referred", "to", "as", "the", "domestic", "cat."]
|
45
|
+
```
|
46
|
+
|
47
|
+
We take each term and create use it as a key in are hashmap `ii` and the value will be a list with all documents containing that term.
|
48
|
+
|
49
|
+
```
|
50
|
+
def index(doc_id, doc, ii)
|
51
|
+
# 1 - tokenizer
|
52
|
+
tokens = doc.split(' ')
|
53
|
+
|
54
|
+
tokens.each { |token| ii[token] ||= []; ii[token] << doc_id }
|
55
|
+
end
|
56
|
+
|
57
|
+
ii = {}
|
58
|
+
|
59
|
+
index(:doc1, doc1, ii)
|
60
|
+
index(:doc2, doc2, ii)
|
61
|
+
|
62
|
+
puts ii
|
63
|
+
|
64
|
+
# {
|
65
|
+
# 'The' => [:doc1, :doc2],
|
66
|
+
# 'domestic' => [:doc1, :doc2],
|
67
|
+
# 'dog' => [:doc1],
|
68
|
+
# 'is' => [:doc1, :doc2],
|
69
|
+
# 'a' => [:doc1, :doc2],
|
70
|
+
# 'member' => [:doc1],
|
71
|
+
# 'of' => [:doc1, :doc1],
|
72
|
+
# 'the' => [:doc1, :doc2],
|
73
|
+
# 'genus' => [:doc1],
|
74
|
+
# 'Canis,' => [:doc1],
|
75
|
+
# 'which' => [:doc1],
|
76
|
+
# 'forms' => [:doc1],
|
77
|
+
# 'part' => [:doc1],
|
78
|
+
# 'wolf-like' => [:doc1],
|
79
|
+
# 'canids' => [:doc1],
|
80
|
+
# 'cat' => [:doc2],
|
81
|
+
# 'small' => [:doc2],
|
82
|
+
# 'carnivorous' => [:doc2],
|
83
|
+
# 'mammal.' => [:doc2],
|
84
|
+
# 'It' => [:doc2],
|
85
|
+
# 'only' => [:doc2],
|
86
|
+
# 'domesticated' => [:doc2],
|
87
|
+
# 'species' => [:doc2],
|
88
|
+
# 'in' => [:doc2],
|
89
|
+
# 'family' => [:doc2],
|
90
|
+
# 'Felidae' => [:doc2],
|
91
|
+
# 'and' => [:doc2],
|
92
|
+
# 'often' => [:doc2],
|
93
|
+
# 'referred' => [:doc2],
|
94
|
+
# 'to' => [:doc2],
|
95
|
+
# 'as' => [:doc2],
|
96
|
+
# 'cat.' => [:doc2]
|
97
|
+
# }
|
98
|
+
```
|
99
|
+
|
100
|
+
Now it is ease to perform any search, if we want to get all documents about `cat` we could simply take the term cat and see the list o documents
|
101
|
+
in it `'cat' => [:doc2]`, if we want to search for 2 or more terms we can do the same `small cat` = `'cat' => [:doc2] and 'small' => [:doc2]`.
|
102
|
+
|
103
|
+
Clearly we can improve our index performing some transformations in the tokens before indexing them. For instance we can see we have `cat` and `cat.`
|
104
|
+
tokens, we have `The` and `the`. lets clean the data before indexing.
|
105
|
+
|
106
|
+
Lets change our define an index pipeline that will be called everytime a document is indexed
|
107
|
+
|
108
|
+
```
|
109
|
+
def index(doc_id, doc, ii)
|
110
|
+
# 1 - tokenizer
|
111
|
+
tokens = doc.split(' ')
|
112
|
+
|
113
|
+
# 2 - trim
|
114
|
+
tokens = tokens.map(&:strip)
|
115
|
+
|
116
|
+
# 3 - downcase all tokens
|
117
|
+
tokens = tokens.map(&:downcase)
|
118
|
+
|
119
|
+
# 4 - remove punctuation
|
120
|
+
tokens = tokens.map { |token| token.tr(',.!;:', '') }
|
121
|
+
|
122
|
+
tokens.each { |token| ii[token] ||= []; ii[token] << doc_id }
|
123
|
+
|
124
|
+
# ... index
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
With this changes our index would be:
|
129
|
+
|
130
|
+
```
|
131
|
+
{
|
132
|
+
'the' => [:doc1, :doc2],
|
133
|
+
'domestic' => [:doc1, :doc2],
|
134
|
+
'dog' => [:doc1],
|
135
|
+
'is' => [:doc1, :doc2],
|
136
|
+
'a' => [:doc1, :doc2],
|
137
|
+
'member' => [:doc1],
|
138
|
+
'of' => [:doc1],
|
139
|
+
'genus' => [:doc1],
|
140
|
+
'canis' => [:doc1],
|
141
|
+
'which' => [:doc1],
|
142
|
+
'forms' => [:doc1],
|
143
|
+
'part' => [:doc1],
|
144
|
+
'wolf-like' => [:doc1],
|
145
|
+
'canids' => [:doc1],
|
146
|
+
'cat' => [:doc2],
|
147
|
+
'small' => [:doc2],
|
148
|
+
'carnivorous' => [:doc2],
|
149
|
+
'mammal' => [:doc2],
|
150
|
+
'it' => [:doc2],
|
151
|
+
'only' => [:doc2],
|
152
|
+
'domesticated' => [:doc2],
|
153
|
+
'species' => [:doc2],
|
154
|
+
'in' => [:doc2],
|
155
|
+
'family' => [:doc2],
|
156
|
+
'felidae' => [:doc2],
|
157
|
+
'and' => [:doc2],
|
158
|
+
'often' => [:doc2],
|
159
|
+
'referred' => [:doc2],
|
160
|
+
'to' => [:doc2],
|
161
|
+
'as' => [:doc2]
|
162
|
+
}
|
163
|
+
```
|
164
|
+
|
165
|
+
Pretty better now, we could apply other steps like removing some words that are irrelevant for us (stop words),
|
166
|
+
add synonyms for some words but this other changes are specifics from languages.
|
167
|
+
|
168
|
+
TODO
|
169
|
+
|
170
|
+
## Language support (stop words, stemmers)
|
171
|
+
|
172
|
+
TODO
|
173
|
+
|
174
|
+
## BM25 (from wikipedia)
|
175
|
+
|
176
|
+
BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document, regardless
|
177
|
+
of their proximity within the document. It is a family of scoring functions with slightly different components and parameters.
|
178
|
+
One of the most prominent instantiations of the function is as follows.
|
179
|
+
|
180
|
+
Given a query Q, containing keywords `q1....qn` the BM25 score of a document `D` is:
|
181
|
+
|
182
|
+

|
183
|
+
|
184
|
+
where `f(qi, D)` is qi's term frequency (tf) in the document `D`, `|D|` is the length of the document `D` in words, and avgdl is the
|
185
|
+
average document length in the text collection from which documents are drawn. `k1` and `b` are free parameters, usually chosen, in absence of
|
186
|
+
an advanced optimization, as `k1 in |1.2,2.0|` and `b = 0.75`. `IDF(qi)` is the IDF (inverse document frequency) weight of the query term
|
187
|
+
`qi`. It is usually computed as:
|
188
|
+
|
189
|
+

|
190
|
+
|
191
|
+
where `N` is the total number of documents in the collection, and `n(q)` is the number of documents containing `qi`.
|
192
|
+
|
193
|
+
There are several interpretations for IDF and slight variations on its formula. In the original BM25 derivation,
|
194
|
+
the IDF component is derived from the Binary Independence Model.
|
195
|
+
|
196
|
+
The above formula for IDF has drawbacks for terms appearing in more than half of the corpus documents. These terms' IDF is negative,
|
197
|
+
so for any two almost-identical documents, one which contains the term may be ranked lower than one which does not. This is often an
|
198
|
+
undesirable behavior, so many applications adjust the IDF formula in various ways:
|
199
|
+
|
200
|
+
Each summand can be given a floor of 0, to trim out common terms;
|
201
|
+
The IDF function can be given a floor of a constant `e`, to avoid common terms being ignored at all;
|
202
|
+
The IDF function can be replaced with a similarly shaped one which is non-negative, or strictly positive to avoid terms being ignored at all.
|
203
|
+
|
204
|
+
## Usage
|
205
|
+
|
206
|
+
First we create an inverted Index
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
idx = MiniSearch.new_index
|
210
|
+
|
211
|
+
# Then we index some documents (a document is a simple Hash with :id and :indexed_field in it)
|
212
|
+
|
213
|
+
idx.index(id: 1, indexed_field: 'red duck')
|
214
|
+
idx.index(id: 2, indexed_field: 'yellow big dog')
|
215
|
+
idx.index(id: 3, indexed_field: 'small cat')
|
216
|
+
idx.index(id: 4, indexed_field: 'red monkey noisy')
|
217
|
+
idx.index(id: 5, indexed_field: 'small horse')
|
218
|
+
idx.index(id: 6, indexed_field: 'purple turtle')
|
219
|
+
idx.index(id: 7, indexed_field: 'tiny red spider')
|
220
|
+
idx.index(id: 8, indexed_field: 'big blue whale')
|
221
|
+
idx.index(id: 9, indexed_field: 'huge elephant')
|
222
|
+
idx.index(id: 10, indexed_field: 'red big cat')
|
223
|
+
|
224
|
+
# Then we can search for our documents
|
225
|
+
|
226
|
+
result = idx.search('RED cat ')
|
227
|
+
|
228
|
+
# The result will be something like:
|
229
|
+
|
230
|
+
puts result
|
231
|
+
|
232
|
+
# {
|
233
|
+
# documents: [
|
234
|
+
# { document: { id: 10, indexed_field: 'red big cat' }, score: 2.726770362793935 },
|
235
|
+
# { document: { id: 3, indexed_field: 'small cat' }, score: 1.860138656065616 },
|
236
|
+
# { document: { id: 4, indexed_field: 'red monkey noisy' }, score: 0.630035123281377 },
|
237
|
+
# { document: { id: 7, indexed_field: 'tiny red spider' }, score: 0.630035123281377 },
|
238
|
+
# { document: { id: 1, indexed_field: 'red duck' }, score: 0.5589416657904823 }
|
239
|
+
# ],
|
240
|
+
# idfs: {
|
241
|
+
# 'cat' => 1.2237754316221157,
|
242
|
+
# 'red' => 0.36772478012531734
|
243
|
+
# },
|
244
|
+
# processed_terms: ['red', 'cat']
|
245
|
+
# }
|
246
|
+
```
|
247
|
+
|
248
|
+
We can see results are sorted by score, notice that the document we index can have any other
|
249
|
+
fields like name, price and etc. But only `:id` and `:indexed_field` are required
|
250
|
+
|
251
|
+
## Development
|
252
|
+
|
253
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
254
|
+
|
255
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
256
|
+
|
257
|
+
## Contributing
|
258
|
+
|
259
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mini_search. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
260
|
+
|
261
|
+
## License
|
262
|
+
|
263
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
264
|
+
|
265
|
+
## Code of Conduct
|
266
|
+
|
267
|
+
Everyone interacting in the MiniSearch project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mini_search/blob/master/CODE_OF_CONDUCT.md).
|