1
votes

I'm trying to use Doc2Vec to read in a file that is a list of sentences like this:

The elephant flaps its large ears to cool the blood in them and its body.

A house is a permanent building or structure for people or families to live in.

...

What I want to do is generate two files, one with unique words from these sentences and another that has one corresponding vector per line (if there's no vector output I want to output a vector of 0's)

I'm getting the vocab fine with my code but I can't seem to figure out how to print out the individual sentence vectors. I have looked through the documentation and haven't found much help. Here is what my code looks like so far.

sentences = []
for uid, line in enumerate(open(filename)):
    sentences.append(LabeledSentence(words=line.split(), labels=['SENT_%s' %       uid]))

model = Doc2Vec(alpha=0.025, min_alpha=0.025)
model.build_vocab(sentences)
for epoch in range(10):
    model.train(sentences)
    model.alpha -= 0.002
    model.min_alpha = model.alpha
sent_reg = r'[SENT].*'
for item in model.vocab.keys():
    sent = re.search(sent_reg, item)
    if sent:
        continue
    else:
        print item

###I'm not sure how to produce the vectors from here and this doesn't work##   
sent_id = 0
for item in model:
    print model["SENT_"+str(sent_id)]
    sent_id += 1
1
Have you tried setting min_count = 1? Doc2Vec(min_count = 1) - slizb

1 Answers

3
votes

With the latest gensim (0.12.1) you could try:

print model.docvecs["SENT_"+str(sent_id)]