【发布时间】:2018-02-19 06:02:16
【问题描述】:
我想从我提到的例句中得到二元组和三元组。
我的代码适用于二元组。但是,它并没有捕获数据中的三元组(例如,人机交互,这在我的句子中的 5 处提到)
方法 1 下面提到的是我在 Gensim 中使用 Phrases 的代码。
from gensim.models import Phrases
documents = ["the mayor of new york was there", "human computer interaction and machine learning has now become a trending research area","human computer interaction is interesting","human computer interaction is a pretty interesting subject", "human computer interaction is a great and new subject", "machine learning can be useful sometimes","new york mayor was present", "I love machine learning because it is a new subject area", "human computer interaction helps people to get user friendly applications"]
sentence_stream = [doc.split(" ") for doc in documents]
bigram = Phrases(sentence_stream, min_count=1, threshold=1, delimiter=b' ')
trigram = Phrases(bigram_phraser[sentence_stream])
for sent in sentence_stream:
bigrams_ = bigram_phraser[sent]
trigrams_ = trigram[bigrams_]
print(bigrams_)
print(trigrams_)
方法 2 我什至尝试同时使用 Phraser 和 Phraser,但没有成功。
from gensim.models import Phrases
from gensim.models.phrases import Phraser
documents = ["the mayor of new york was there", "human computer interaction and machine learning has now become a trending research area","human computer interaction is interesting","human computer interaction is a pretty interesting subject", "human computer interaction is a great and new subject", "machine learning can be useful sometimes","new york mayor was present", "I love machine learning because it is a new subject area", "human computer interaction helps people to get user friendly applications"]
sentence_stream = [doc.split(" ") for doc in documents]
bigram = Phrases(sentence_stream, min_count=1, threshold=2, delimiter=b' ')
bigram_phraser = Phraser(bigram)
trigram = Phrases(bigram_phraser[sentence_stream])
for sent in sentence_stream:
bigrams_ = bigram_phraser[sent]
trigrams_ = trigram[bigrams_]
print(bigrams_)
print(trigrams_)
请帮我解决这个获取三元组的问题。
我正在关注 Gensim 的example documentation。
【问题讨论】:
标签: python data-mining text-mining word2vec gensim