使用 Gensim 的短语构建二元组时如何防止包含某些单词?
How to prevent certain words from being included when building bigrams using Gensim's Phrases?
我正在使用 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', 'service_left', 'complaint', 'to', 'the', 'manager']
注意:以下行为似乎随版本 4.0.0 发生变化!
如果您确实只使用双字母组,则可以使用函数的 common_terms={}
参数,即(根据 docs
[a] list of “stop words” that won’t affect frequency count of expressions containing them. Allow to detect expressions like “bank_of_america” or “eye_of_the_beholder”.
如果我在您的示例代码中添加一个简单的 common_terms={"service"}
,我将得到以下结果:
['Very', 'quick', 'service', 'left_a', 'big', 'tip']
['Very', 'bad', 'service', 'left_a', 'complaint', 'to', 'the', 'manager']
从 4.0.0 版本开始,gensim 似乎删除了这个参数,但用 connector_words
代替了它),参见 here。不过,结果应该大致相同!
我正在使用 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', 'service_left', 'complaint', 'to', 'the', 'manager']
注意:以下行为似乎随版本 4.0.0 发生变化!
如果您确实只使用双字母组,则可以使用函数的 common_terms={}
参数,即(根据 docs
[a] list of “stop words” that won’t affect frequency count of expressions containing them. Allow to detect expressions like “bank_of_america” or “eye_of_the_beholder”.
如果我在您的示例代码中添加一个简单的 common_terms={"service"}
,我将得到以下结果:
['Very', 'quick', 'service', 'left_a', 'big', 'tip']
['Very', 'bad', 'service', 'left_a', 'complaint', 'to', 'the', 'manager']
从 4.0.0 版本开始,gensim 似乎删除了这个参数,但用 connector_words
代替了它),参见 here。不过,结果应该大致相同!