-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrank.py
More file actions
executable file
·49 lines (40 loc) · 1.5 KB
/
rank.py
File metadata and controls
executable file
·49 lines (40 loc) · 1.5 KB
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
#!/usr/local/bin/python3
import json
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import preprocess
def tfidf_scores(corpus, query):
cv=CountVectorizer(max_df=0.85)
word_count_vector=cv.fit_transform(corpus)
tfidf_transformer=TfidfTransformer(smooth_idf=True, use_idf=True, norm=None)
tfidf = tfidf_transformer.fit_transform(word_count_vector)
tfidf_array = tfidf.toarray()
scores = np.zeros( len(abstract))
for q in query:
if(q in cv.vocabulary_ ):
scores += tfidf_array[:,cv.vocabulary_.get(q)]
return scores
if __name__ == "__main__":
fileName = "out_file.txt"
query = input()
N = 10
alpha = 0.4
data,abstract,keywords,title = ([] for _ in range(4))
for line in open(fileName,'r', encoding='utf8'):
data.append(json.loads(line))
abstract.append(data[-1]['abstract'])
keywords.append(data[-1]['keywords'])
title.append(data[-1]['title'])
query = preprocess.dataPreProcess(query)
scores = np.zeros( len(abstract))
scores += alpha*tfidf_scores(title, query)
scores += ((1-alpha) / 2)*tfidf_scores(abstract, query)
scores += ((1-alpha) / 2)*tfidf_scores(keywords, query)
scores = scores.tolist()
scores = list(zip(scores, range(len(scores))))
scores.sort(reverse=True)
for elem in scores[:N]:
index = elem[1]
if(data[index]['titleCopy'] != ""): print("Text title:", data[index]['titleCopy'], " Text number:", index)
else: print("This text has no title, text number:", index)