如何解决使用 Gensim 的短语构建二元组时如何防止包含某些单词?
我正在使用 Gensim 的 Phraser 模型在一些评论中查找二元组,以便稍后在 LDA 主题建模场景中使用。我的问题是评论经常提到“服务”这个词,因此 Phraser 发现了许多不同的双字母组合,其中“服务”是一对(例如“helpful_service”、“good_service”、“service_price”)。
然后这些内容会出现在最终结果中的多个主题中*。我在想,如果我能够告诉 Phraser not 在制作二元组时包含“服务”,我可以防止这种情况发生。这可能吗?
(*) 我知道在多个主题中出现与“服务”相关的二元组确实可能是最佳结果,但我只是想尝试将它们排除在外。
示例代码:
# import gensim models
from gensim.models import Phrases
from gensim.models.phrases import Phraser
# sample data
data = [
"Very quick service left a big tip","Very bad service left a complaint to the manager"
]
data_words = [doc.split(" ") for doc in data]
# build the bigram model
bigram_phrases = Phrases(data_words,min_count=2,threshold=0,scoring='npmi')
# note I used the arguments above to force "service" based bigrams to be created for this example
bigram_phraser = Phraser(bigram_phrases)
# print the result
for word in data_words:
tokens_ = bigram_phraser[word]
print(tokens_)
以上打印:
['Very','quick','service_left','a','big','tip']
['Very','bad','complaint','to','the','manager']
解决方法
注意:以下行为似乎在 4.0.0 版中有所改变!
如果您确实只使用二元组,则可以使用该函数的 common_terms={}
参数,即(根据 docs
[a] 不会影响包含它们的表达式的频率计数的“停用词”列表。允许检测诸如“bank_of_america”或“eye_of_the_beholder”之类的表达式。
如果我在您的示例代码中添加一个简单的 common_terms={"service"}
,我会得到以下结果:
['Very','quick','service','left_a','big','tip']
['Very','bad','complaint','to','the','manager']
从 4.0.0 版开始,gensim 似乎删除了此参数,但替换为 connector_words
),参见 here。不过,结果应该基本相同!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。