-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc2vec.py
More file actions
executable file
·40 lines (35 loc) · 1.34 KB
/
Copy pathdoc2vec.py
File metadata and controls
executable file
·40 lines (35 loc) · 1.34 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
#!/usr/bin/env python3.7
import gensim
import json
import os
import sys
def load_dataset():
examples = []
for idx, doc in enumerate(os.listdir('./bpe')):
with open('./bpe/%s' % doc, 'r') as f:
words = f.read().split()
example = gensim.models.doc2vec.TaggedDocument(words, [doc])
examples.append(example)
sys.stdout.write('\rLoading text document %d...' % idx)
print()
return examples
def make_predictions(model):
for idx, doc in enumerate(os.listdir('./bpe')):
with open('./bpe/%s' % doc, 'r') as f:
words = f.read().split()
result = model.infer_vector(words)
with open('./docvec/%s' % doc.split('/')[-1].replace('.bpe.txt', '.json'), 'w') as f:
f.write(json.dumps(list(map(str, list(result)))))
sys.stdout.write('\rEmbedding text document %d...' % idx)
def main():
train = load_dataset()
model = gensim.models.doc2vec.Doc2Vec(vector_size=100, min_count=2, epochs=20)
print('Discovering corpus vocabulary...')
model.build_vocab(train)
print('Building doc2vec model...')
model.train(train, total_examples=model.corpus_count, epochs=model.epochs)
model.save('./doc2vec.model')
#model = gensim.models.doc2vec.Doc2Vec.load('./doc2vec.model')
make_predictions(model)
if __name__ == '__main__':
main()