-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
36 lines (24 loc) · 1020 Bytes
/
inference.py
File metadata and controls
36 lines (24 loc) · 1020 Bytes
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
import json
import numpy as np
from tqdm import tqdm
from embedding_projector import normalize_rows
def inference(texts, word_vecs, word2id):
id2doc = {}
domains = sorted(list(texts.keys()))
dim = word_vecs.shape[1]
doc_vecs = np.zeros(shape=(len(domains), dim))
for i, d in tqdm(enumerate(domains)):
id2doc[i] = d
w_ids = [word2id[w] for w in texts[d].strip().lower().split(' ') if w in word2id]
vec = np.sum(word_vecs[w_ids], axis=0)
if any(w_ids):
doc_vecs[i] = vec / len(w_ids)
doc_vecs = normalize_rows(doc_vecs)
doc_vecs.dump("embeddings/doc_vectors.npy")
json.dump(id2doc, open("embeddings/index2doc.json", 'w'))
if __name__ == "__main__":
id2word = json.load(open('embeddings/index2word.json', 'r'))
word2id = {w: int(i) for i, w in id2word.items()}
word_vecs = np.load("embeddings/word_vectors.npy", allow_pickle=True)
texts = json.load(open('data/all_texts.json'))
inference(texts, word_vecs, word2id)