本文整理汇总了Python中nltk.collocations.BigramCollocationFinder.score_ngrams方法的典型用法代码示例。如果您正苦于以下问题:Python BigramCollocationFinder.score_ngrams方法的具体用法?Python BigramCollocationFinder.score_ngrams怎么用?Python BigramCollocationFinder.score_ngrams使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.collocations.BigramCollocationFinder
的用法示例。
在下文中一共展示了BigramCollocationFinder.score_ngrams方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getTopFeaturesForClass
# 需要导入模块: from nltk.collocations import BigramCollocationFinder [as 别名]
# 或者: from nltk.collocations.BigramCollocationFinder import score_ngrams [as 别名]
def getTopFeaturesForClass(documents, noOfFeaturesPerClass=10):
''' Feature values are in integer.
[{document vector}, classId]
'''
classToFeaturesMap = defaultdict(list)
word_fd = nltk.FreqDist(feature for doc in documents for feature, count in doc[0].iteritems() for i in range(count))
for document, clusterId in documents:
if clusterId not in word_fd: word_fd[clusterId]=0
word_fd[clusterId]+=1
bigram_fd = nltk.FreqDist((feature, doc[1]) for doc in documents for feature, count in doc[0].iteritems() for i in range(count))
bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder(word_fd, bigram_fd)
scored = finder.score_ngrams(bigram_measures.pmi)
for (feature, classId), score in scored: classToFeaturesMap[classId].append((feature, score))
returnData = []
for classId, features in classToFeaturesMap.iteritems(): returnData.append((classId, features[:noOfFeaturesPerClass]))
return returnData
示例2: FreqDist
# 需要导入模块: from nltk.collocations import BigramCollocationFinder [as 别名]
# 或者: from nltk.collocations.BigramCollocationFinder import score_ngrams [as 别名]
print "---------- 100 collocations -----------"
overall_text.collocations(num=100)
print "---------- ---------------- -----------"
print overall_text.concordance('Imperium')
index = nltk.text.ConcordanceIndex(master_tokens, key=lambda s:s.lower())
sys.exit(0)
from nltk import bigrams
from nltk import collocations
from nltk import FreqDist
from nltk.collocations import BigramCollocationFinder
# http://nltk.googlecode.com/svn/trunk/doc/howto/collocations.html
# http://stackoverflow.com/questions/9151326/python-nltk-find-collocations-without-dot-separated-words
bigram_measures = collocations.BigramAssocMeasures()
word_fd = FreqDist(master_tokens)
bigram_fd = FreqDist(bigrams(master_tokens))
finder = BigramCollocationFinder(word_fd, bigram_fd)
#finder.apply_word_filter(lambda w: w in ('.', ','))
# only when collocation occurs 3+ times
finder.apply_freq_filter(3)
scored = finder.score_ngrams(bigram_measures.raw_freq)
#print sorted(bigram for bigram, score in scored)
print "========================================="
print sorted(finder.nbest(bigram_measures.raw_freq,200),reverse=True)