2
我使用Gensim的doc2vec方法來讀取我的文本文件,每行包含1個句子。它將我的文件讀入一個字典,其中的鍵是一個標記化的術語列表,值是句子編號。python gensim從doc2vec檢索原文句子taggedlinedocument
這裏是我的代碼:新文件的
from gensim import utils
from gensim.models.doc2vec import LabeledSentence,TaggedLineDocument
from gensim.models import Doc2Vec
new_file = open('new_file.txt','w')
with open('myfile.txt','r') as inp:
for line in inp:
utils.simple_preprocess(line)
file1.write(str(utils.simple_preprocess(line)) + "\n")
file1.close()
輸出示例:
[u'hi', u'how', u'are', u'you']
[u'its', u'such', u'great', u'day']
[u'its', u'such', u'great', u'day']
[u'its', u'such', u'great', u'day']
然後我把這個列表插入gensim的taggedlinedocument功能:
s = TaggedLineDocument('myfile.txt')
for k,v in s:
print k, v
輸出示例:
[u'hi', u'how', u'are', u'you'] [0]
[u'hi', u'how', u'are', u'you'] [1]
[u'hi', u'how', u'are', u'you'] [2]
[u'its', u'such', u'a', u'great', u'day'] [3]
[u'its', u'such', u'a', u'great', u'day'] [4]
問題是,鑑於標籤id(示例0),我如何找回原始語句?