自然语言处理学习9:NLTK中BigramCollocationFinder的使用

from nltk.collocations import BigramCollocationFinder

1. nltk.collocations.BigramCollocationFinder(word_fd,bigram_fd,window_size=2)

    用于查找和排列bigram搭配或其他关联度量的工具。

2. BigramCollocationFinder.from_words(wordswindow_size=2): 把词列表变为双词搭配。

    为给定序列中的所有bigrams构建一个BigramCollocationFinder。

3.BigramCollocationFinder.score_ngram(score_fnw1w2):

    使用给定的评分函数返回给定二元组的分数。

4. BigramCollocationFinder.apply_ngram_filter(fn):

    对任意n元词组合应用函数fn,如果fn(w1,...,wn)返回结果为True,则删除此n元词组合。

5.BigramCollocationFinder.apply_word_filter(fn):

对任意n元词组合,应用函数fn,如果(fn(w1),...,fn(wn))中有一个结果是True,

    则删除此n元词组合。

6. BigramCollocationFinder.apply_freq_filter(min_freq):

    删除频数小于min_freq的候选项。

操作实例如下:

import re
import jieba
text2 = re.sub('[..:。;;!!??%\]\[\t\n\"\')(】【)(+\-\*/<>《》]', '', text1)
word_list = jieba.lcut(text2)
word_list = [word for word in word_list if len(word)>1]

from nltk.collocations import BigramCollocationFinder
bigram_finder = BigramCollocationFinder.from_words(word_list)
bigram_finder.ngram_fd
去除频数小于4的二元词组
bigram_finder.apply_freq_filter(4)
bigram_finder.ngram_fd
FreqDist({('占崩岗', '滑坡'): 4,
          ('崩岗', '滑坡'): 24,
          ('崩岗', '面积'): 4,
          ('森林', '植被'): 6,
          ('滑坡', '面积'): 6})
去除含有'植被'的词组,去除含有 '占崩岗的词组'
 
def fn1(*words):
    return '植被' in words
def fn2(word):
    return word == '占崩岗'

bigram_finder.apply_ngram_filter(fn1)
bigram_finder.ngram_fd
FreqDist({('占崩岗', '滑坡'): 4,
          ('崩岗', '滑坡'): 24,
          ('崩岗', '面积'): 4,
          ('滑坡', '面积'): 6})

bigram_finder.apply_word_filter(fn2)
bigram_finder.ngram_fd
FreqDist({('崩岗', '滑坡'): 24, ('崩岗', '面积'): 4, ('滑坡', '面积'): 6})
 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值