问题在于您尝试使用的方式
apply_freq_filter。我们正在讨论单词搭配。如您所知,单词搭配是关于单词之间的依赖关系。在
BigramCollocationFinder从一个类继承类的命名
AbstractCollocationFinder和功能
apply_freq_filter属于这一类。
apply_freq_filter不应完全删除某些单词搭配,但如果某些其他功能尝试访问列表,则提供过滤后的搭配列表。
现在为什么呢?想象一下,如果过滤并置只是简单地删除它们,那么就有很多概率测度,例如似然比或PMI本身(用于计算一个词相对于语料库中其他词的概率),在从随机位置删除多个词后它们将无法正常运行在给定的语料库中。通过从给定单词列表中删除一些搭配,将禁用许多潜在的功能和计算。而且,在删除之前计算所有这些度量将带来巨大的计算开销,而用户可能根本不需要。
现在,问题是如何正确使用
apply_freq_filter function?有几种方法。在下文中,我将展示问题及其解决方案。
让我们定义一个示例语料库,并将其拆分为类似于您所做的单词的列表:
tweet_phrases = "I love iphone . I am so in love with iphone . iphone is great . samsung is great . iphone sucks. I really really love iphone cases. samsung can never beat iphone . samsung is better than apple"from nltk.collocations import *import nltk
为了进行实验,我将窗口大小设置为3:
finder = BigramCollocationFinder.from_words(tweet_phrases.split(), window_size = 3)finder1 = BigramCollocationFinder.from_words(tweet_phrases.split(), window_size = 3)
请注意,为了进行比较,我仅在
finder1以下位置使用过滤器:
finder1.apply_freq_filter(2)bigram_measures = nltk.collocations.BigramAssocMeasures()
现在,如果我写:
for k,v in finder.ngram_fd.items(): print(k,v)
输出为:
(('.', 'is'), 3)(('iphone', '.'), 3)(('love', 'iphone'), 3)(('.', 'iphone'), 2)(('.', 'samsung'), 2)(('great', '.'), 2)(('iphone', 'I'), 2)(('iphone', 'samsung'), 2)(('is', '.'), 2)(('is', 'great'), 2)(('samsung', 'is'), 2)(('.', 'I'), 1)(('.', 'am'), 1)(('.', 'sucks.'), 1)(('I', 'am'), 1)(('I', 'iphone'), 1)(('I', 'love'), 1)(('I', 'really'), 1)(('I', 'so'), 1)(('am', 'in'), 1)(('am', 'so'), 1)(('beat', '.'), 1)(('beat', 'iphone'), 1)(('better', 'apple'), 1)(('better', 'than'), 1)(('can', 'beat'), 1)(('can', 'never'), 1)(('cases.', 'can'), 1)(('cases.', 'samsung'), 1)(('great', 'iphone'), 1)(('great', 'samsung'), 1)(('in', 'love'), 1)(('in', 'with'), 1)(('iphone', 'cases.'), 1)(('iphone', 'great'), 1)(('iphone', 'is'), 1)(('iphone', 'sucks.'), 1)(('is', 'better'), 1)(('is', 'than'), 1)(('love', '.'), 1)(('love', 'cases.'), 1)(('love', 'with'), 1)(('never', 'beat'), 1)(('never', 'iphone'), 1)(('really', 'iphone'), 1)(('really', 'love'), 1)(('samsung', 'better'), 1)(('samsung', 'can'), 1)(('samsung', 'great'), 1)(('samsung', 'never'), 1)(('so', 'in'), 1)(('so', 'love'), 1)(('sucks.', 'I'), 1)(('sucks.', 'really'), 1)(('than', 'apple'), 1)(('with', '.'), 1)(('with', 'iphone'), 1)
如果为编写相同的结果,我将获得相同的结果
finder1。因此,乍一看,过滤器不起作用。但是,看看它是如何工作的:诀窍是使用
score_ngrams。
如果我
score_ngrams在上使用
finder,它将是:
finder.score_ngrams (bigram_measures.pmi)
输出为:
[(('am', 'in'), 5.285402218862249), (('am', 'so'), 5.285402218862249), (('better', 'apple'), 5.285402218862249), (('better', 'than'), 5.285402218862249), (('can', 'beat'), 5.285402218862249), (('can', 'never'), 5.285402218862249), (('cases.', 'can'), 5.285402218862249), (('in', 'with'), 5.285402218862249), (('never', 'beat'), 5.285402218862249), (('so', 'in'), 5.285402218862249), (('than', 'apple'), 5.285402218862249), (('sucks.', 'really'), 4.285402218862249), (('is', 'great'), 3.7004397181410926), (('I', 'am'), 3.7004397181410926), (('I', 'so'), 3.7004397181410926), (('cases.', 'samsung'), 3.7004397181410926), (('in', 'love'), 3.7004397181410926), (('is', 'better'), 3.7004397181410926), (('is', 'than'), 3.7004397181410926), (('love', 'cases.'), 3.7004397181410926), (('love', 'with'), 3.7004397181410926), (('samsung', 'better'), 3.7004397181410926), (('samsung', 'can'), 3.7004397181410926), (('samsung', 'never'), 3.7004397181410926), (('so', 'love'), 3.7004397181410926), (('sucks.', 'I'), 3.7004397181410926), (('samsung', 'is'), 3.115477217419936), (('.', 'am'), 2.9634741239748865), (('.', 'sucks.'), 2.9634741239748865), (('beat', '.'), 2.9634741239748865), (('with', '.'), 2.9634741239748865), (('.', 'is'), 2.963474123974886), (('great', '.'), 2.963474123974886), (('love', 'iphone'), 2.7004397181410926), (('I', 'really'), 2.7004397181410926), (('beat', 'iphone'), 2.7004397181410926), (('great', 'samsung'), 2.7004397181410926), (('iphone', 'cases.'), 2.7004397181410926), (('iphone', 'sucks.'), 2.7004397181410926), (('never', 'iphone'), 2.7004397181410926), (('really', 'love'), 2.7004397181410926), (('samsung', 'great'), 2.7004397181410926), (('with', 'iphone'), 2.7004397181410926), (('.', 'samsung'), 2.37851162325373), (('is', '.'), 2.37851162325373), (('iphone', 'I'), 2.1154772174199366), (('iphone', 'samsung'), 2.1154772174199366), (('I', 'love'), 2.115477217419936), (('iphone', '.'), 1.963474123974886), (('great', 'iphone'), 1.7004397181410922), (('iphone', 'great'), 1.7004397181410922), (('really', 'iphone'), 1.7004397181410922), (('.', 'iphone'), 1.37851162325373), (('.', 'I'), 1.37851162325373), (('love', '.'), 1.37851162325373), (('I', 'iphone'), 1.1154772174199366), (('iphone', 'is'), 1.1154772174199366)]
现在注意当我计算
finder1被过滤为2的频率的相同内容时会发生什么:
finder1.score_ngrams(bigram_measures.pmi)
和输出:
[(('is', 'great'), 3.7004397181410926), (('samsung', 'is'), 3.115477217419936), (('.', 'is'), 2.963474123974886), (('great', '.'), 2.963474123974886), (('love', 'iphone'), 2.7004397181410926), (('.', 'samsung'), 2.37851162325373), (('is', '.'), 2.37851162325373), (('iphone', 'I'), 2.1154772174199366), (('iphone', 'samsung'), 2.1154772174199366), (('iphone', '.'), 1.963474123974886), (('.', 'iphone'), 1.37851162325373)]
请注意,此列表中不存在所有频率小于2的搭配。而这正是您要寻找的结果。因此过滤器已经起作用。此外,文档还提供了有关此问题的最小提示。
我希望这能回答您的问题。否则,请告诉我。
免责声明:如果您主要处理推文,则窗口大小为13太大。如果您注意到,在我的示例语料库中,我的示例推文的大小太小,以至于将窗口大小设置为13可能会导致发现无关紧要的搭配。