我试图通过使用内置方法在文本中找到与NLTK的搭配.
现在我有以下示例文本(test和foo相互跟随,但两者之间有一个句子边框):
content_part = """test. foo 0 test. foo 1 test.
foo 2 test. foo 3 test. foo 4 test. foo 5"""
Run Code Online (Sandbox Code Playgroud)
标记化的结果collocations()
如下:
print nltk.word_tokenize(content_part)
# ['test.', 'foo', 'my', 'test.', 'foo', '1', 'test.',
# 'foo', '2', 'test.', 'foo', '3', 'test.', 'foo', '4', 'test.', 'foo', '5']
print nltk.Text(nltk.word_tokenize(content_part)).collocations()
# test. foo
Run Code Online (Sandbox Code Playgroud)
如何防止NLTK:
所以在这个例子中它根本不应该打印任何搭配,但我想你可以设想更复杂的文本,其中句子中也有搭配.
我可以猜测我需要使用Punkt句子分段器,但后来我不知道如何将它们再次组合起来找到与nltk的搭配(collocation()
似乎比仅仅计算东西更强大).
小智 8
您可以使用WordPunctTokenizer将标点符号与单词分开,然后使用apply_word_filter()过滤掉带有标点符号的双字母组合.
同样的事情可以用于三元组,因为没有找到句子边界上的搭配.
from nltk import bigrams
from nltk import collocations
from nltk import FreqDist
from nltk.collocations import *
from nltk import WordPunctTokenizer
content_part = """test. foo 0 test. foo 1 test.
foo 2 test. foo 3 test. foo 4 test, foo 4 test."""
tokens = WordPunctTokenizer().tokenize(content_part)
bigram_measures = collocations.BigramAssocMeasures()
word_fd = FreqDist(tokens)
bigram_fd = FreqDist(bigrams(tokens))
finder = BigramCollocationFinder(word_fd, bigram_fd)
finder.apply_word_filter(lambda w: w in ('.', ','))
scored = finder.score_ngrams(bigram_measures.raw_freq)
print tokens
print sorted(finder.nbest(bigram_measures.raw_freq,2),reverse=True)
Run Code Online (Sandbox Code Playgroud)
输出:
['test', '.', 'foo', '0', 'test', '.', 'foo', '1', 'test', '.', 'foo', '2', 'test', '.', 'foo', '3', 'test', '.', 'foo', '4', 'test', ',', 'foo', '4', 'test', '.']
[('4', 'test'), ('foo', '4')]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2662 次 |
最近记录: |