This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopularity.py
73 lines (52 loc) · 1.47 KB
/
popularity.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pickle
from natasha import (
Doc,
Segmenter,
NewsEmbedding,
NewsMorphTagger,
MorphVocab
)
from pandas import read_csv
from navec import Navec
from tqdm import tqdm
PATH = 'navec_hudlit_v1_12B_500K_300d_100q.tar' # Name of file for Navec
NAME = 'popularity'
# Natasha Setup.
segm = Segmenter()
_emb = NewsEmbedding()
morph_tagger = NewsMorphTagger(_emb)
morph_vocab = MorphVocab()
def query_to_noun(query: str) -> list[str]:
doc = Doc(query.lower())
doc.segment(segmenter=segm)
doc.tag_morph(morph_tagger)
res_arr = []
for token in doc.tokens:
if token.pos == 'NOUN':
token.lemmatize(morph_vocab)
res_arr.append(token.lemma)
return res_arr
# Navec setup.
navec = Navec.load(PATH)
# Data load.
data = read_csv('query_popularity.csv')
data.dropna(inplace=True)
data.reset_index(inplace=True)
pop_dict: dict[str, float] = {}
number_dict: dict[str, int] = {}
for i in tqdm(range(data.shape[0])):
text = data.loc[i, 'query']
text_popular = data.loc[i, 'query_popularity']
noun_list = query_to_noun(text)
for noun in noun_list:
if noun in pop_dict:
pop_dict[noun] += text_popular
number_dict[noun] += 1
else:
pop_dict[noun] = text_popular
number_dict[noun] = 1
for key in tqdm(pop_dict.keys()):
pop_dict[key] /= number_dict[key]
# Dump.
with open(NAME + '.pkl', 'wb') as f:
pickle.dump(pop_dict, f, pickle.HIGHEST_PROTOCOL)