-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract bm25 compontent to its own module
- Loading branch information
1 parent
0dcc7dc
commit a612678
Showing
2 changed files
with
67 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import List | ||
|
||
|
||
class Search: | ||
|
||
NUMBER_ENTRIES_TO_RETURN= 15 | ||
def __init__(self): | ||
import nltk | ||
self.tokenizer = nltk.tokenize.RegexpTokenizer(r"\w+") | ||
self.lemmatizer = nltk.stem.WordNetLemmatizer() | ||
from python_search.configuration.loader import ConfigurationLoader | ||
self.commands = ConfigurationLoader().load_config().commands | ||
self.entries: List[str] = list(self.commands.keys()) | ||
self.bm25 = self.setup_bm25() | ||
|
||
def setup_bm25(self): | ||
tokenized_corpus = [ | ||
self.tokenize((key + str(value))) for key, value in self.commands.items() | ||
] | ||
from rank_bm25 import BM25Okapi as BM25 | ||
|
||
bm25 = BM25(tokenized_corpus) | ||
return bm25 | ||
|
||
def search(self, query): | ||
if not query: | ||
return self.entries[0: self.NUMBER_ENTRIES_TO_RETURN], [] | ||
|
||
|
||
tokenized_query = self.tokenize(query) | ||
|
||
matches = self.bm25.get_top_n( | ||
tokenized_query, self.entries, n=self.NUMBER_ENTRIES_TO_RETURN | ||
) | ||
|
||
return matches, tokenized_query | ||
|
||
|
||
def tokenize(self, string): | ||
tokens = self.tokenizer.tokenize(string) | ||
lemmas = [self.lemmatizer.lemmatize(t) for t in tokens] | ||
return lemmas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters