-
Notifications
You must be signed in to change notification settings - Fork 676
/
NLP DOC2VEC
87 lines (64 loc) · 2.92 KB
/
NLP DOC2VEC
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gensim, logging
from gensim.models import Word2Vec
from nltk import sent_tokenize, word_tokenize, pos_tag
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
from gensim import utils
from gensim.models.doc2vec import LabeledSentence
from gensim.models import Doc2Vec
import numpy
import numpy as np
from random import shuffle
class LabeledLineSentence(object):
def __init__(self, sources):
self.sources = sources
flipped = {}
# make sure that keys are unique
for key, value in sources.items():
if value not in flipped:
flipped[value] = [key]
else:
raise Exception('Non-unique prefix encountered')
def __iter__(self):
for source, prefix in self.sources.items():
with utils.smart_open(source) as fin:
for item_no, line in enumerate(fin):
yield LabeledSentence(utils.to_unicode(line).split(), [prefix + '_%s' % item_no])
def to_array(self):
self.sentences = []
for source, prefix in self.sources.items():
with utils.smart_open(source) as fin:
for item_no, line in enumerate(fin):
self.sentences.append(LabeledSentence(utils.to_unicode(line).split(), [prefix + '_%s' % item_no]))
return self.sentences
def sentences_perm(self):
shuffle(self.sentences)
return self.sentences
docs=np.savetxt('textosDoc2Vec.txt',documents,fmt='%s')
sources = {'textosDoc2Vec.txt':'TRAIN'}
sentences = LabeledLineSentence(sources)
model = Doc2Vec(min_count=1, window=5, size=10, sample=1e-4, negative=5, workers=8)
model.build_vocab(sentences.to_array())
for epoch in range(10):
model.train(sentences.sentences_perm(),total_examples=model.corpus_count,epochs=model.iter)
model.save('./Doc2Vec.d2v')
new_model = Doc2Vec.load('./Doc2Vec.d2v')
new_model
new_model.most_similar('computer')
sentences=[]
for i in range(0,len(documents)):
sentences.append(word_tokenize(str(documents[i])))
import numpy as np
len(np.unique(np.concatenate(sentences)))
array=[np.ndarray.flatten(new_model[sentences[i]]) for i in range(0,len(documents))]
array
new_model.most_similar(positive=['Human', 'error'], negative=['trees'], topn=4)
new_model.similarity('computer', 'system')