gpt4 book ai didi

python - 如何找到包含预定义单词的二元组?

转载 作者:行者123 更新时间:2023-12-01 08:33:18 25 4
gpt4 key购买 nike

我知道可以从下面链接的示例中找到具有特定单词的二元组:

finder = BigramCollocationFinder.from_words(text.split())
word_filter = lambda w1, w2: "man" not in (w1, w2)
finder.apply_ngram_filter(word_filter)

bigram_measures = nltk.collocations.BigramAssocMeasures()
raw_freq_ranking = finder.nbest(bigram_measures.raw_freq, 10) #top-10
>>>

nltk: how to get bigrams containing a specific word

但如果我需要包含两个预定义单词的二元组,我不确定如何应用它。

示例:

我的句子:“你好,昨天我看到一个人在走路。另一边有另一个人大喊:“你是谁,伙计?”

给定一个列表:["yesterday", "other", "I", "side"]我怎样才能获得包含给定单词的二元语法列表。 IE:[("昨天", "我"), ("其他", "一边")]

最佳答案

您想要的可能是一个 word_filter 函数,仅当特定二元组中的所有单词都属于列表时才返回 False

def word_filter(x, y):
if x in lst and y in lst:
return False
return True

其中lst = ["昨天", "我", "其他", "一边"]

请注意,此函数正在从外部作用域访问 lst - 这是一件危险的事情,因此请确保不要在该函数内对 lst 进行任何更改word_filter函数

关于python - 如何找到包含预定义单词的二元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53839740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com