-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_04_search_query.py
53 lines (40 loc) · 1.7 KB
/
run_04_search_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pickle
import nltk
import numpy as np
from nltk.stem.snowball import SnowballStemmer
from snowballstemmer import TurkishStemmer
from utils import remove_duplicated_articles
class ModelQuery:
indexed_articles = {}
def __init__(self, modelfile):
self.stammer = SnowballStemmer('english') # turkish is not supported.
# self.stemmer = TurkishStemmer()
self.indexed_articles = pickle.load(open(modelfile, 'rb'))
def tokenize_query(self, query):
tokens = nltk.word_tokenize(query)
tokens = [self.stammer.stem(x) for x in tokens]
return tokens
def search_for_query(self, query):
tokens = self.tokenize_query(query)
scipy_sparse_matrix = self.indexed_articles['matrix']
matrix = scipy_sparse_matrix.toarray()
print("Matix shape: ", matrix.shape)
articles = self.indexed_articles['articles']
terms = self.indexed_articles['terms']
number_of_terms = len(terms)
bag = [0] * number_of_terms
for term in tokens:
if term in terms:
bag[terms.index(term)] += 1
bag = np.array(bag).astype(float)
results = []
article_count, words_count = matrix.shape
for i in range(article_count):
correlation_result = np.correlate(bag, matrix[i, :])
results.append((correlation_result[0], articles[i]))
results = sorted(results, key=lambda x: x[0], reverse=True)
filtered_results = [(score, article) for (score, article) in results if score > 0]
return remove_duplicated_articles(filtered_results)
#
# modelQuery = ModelQuery(modelfile="model_files/output.data")
# print(modelQuery.search_for_query("gıda"))