【发布时间】:2016-06-13 11:17:05
【问题描述】:
为了预处理语料库,我打算从语料库中提取常用短语,为此我尝试在 gensim 中使用 短语 模型,我尝试了下面的代码,但它没有给我想要的输出。
我的代码
from gensim.models import Phrases
documents = ["the mayor of new york was there", "machine learning can be useful sometimes"]
sentence_stream = [doc.split(" ") for doc in documents]
bigram = Phrases(sentence_stream)
sent = [u'the', u'mayor', u'of', u'new', u'york', u'was', u'there']
print(bigram[sent])
输出
[u'the', u'mayor', u'of', u'new', u'york', u'was', u'there']
但应该是这样的
[u'the', u'mayor', u'of', u'new_york', u'was', u'there']
但是当我尝试打印训练数据的词汇时,我可以看到二元组,但它不适用于测试数据,我哪里出错了?
print bigram.vocab
defaultdict(<type 'int'>, {'useful': 1, 'was_there': 1, 'learning_can': 1, 'learning': 1, 'of_new': 1, 'can_be': 1, 'mayor': 1, 'there': 1, 'machine': 1, 'new': 1, 'was': 1, 'useful_sometimes': 1, 'be': 1, 'mayor_of': 1, 'york_was': 1, 'york': 1, 'machine_learning': 1, 'the_mayor': 1, 'new_york': 1, 'of': 1, 'sometimes': 1, 'can': 1, 'be_useful': 1, 'the': 1})
【问题讨论】: