使用 nltk 过滤 trigram 标签
Filter trigram tags with nltk
我想找出一个语料库的八卦,但限制至少有两个八卦不是专有名词。到目前为止,这是我的代码。
def collocation_finder(text,window_size):
ign = stopwords.words('english')
#Clean the text
finder = TrigramCollocationFinder.from_words(text, window_size)
finder.apply_freq_filter(2)
finder.apply_word_filter(lambda w: len(w) < 2 or w.lower() in ign)
finder.apply_word_filter(lambda w: next(iter(w)) in propernouns)
trig_mes = TrigramAssocMeasures()
#Get trigrams based on raw frequency
collocs = finder.nbest(trig_mes.raw_freq,10)
scores = finder.score_ngrams( trig_mes.raw_freq)
return(collocs)
其中 propernouns 是语料库中所有专有名词的列表。
问题是我的最后一句话过滤了本应确保我没有超出我的限制的那个。有什么想法吗?
这应该是你想要的
finder.apply_ngram_filter(lambda w1, w2, w3: sum([w1 n propernouns, w2 in propernouns, w3 in propernouns]) >= 2)
我想找出一个语料库的八卦,但限制至少有两个八卦不是专有名词。到目前为止,这是我的代码。
def collocation_finder(text,window_size):
ign = stopwords.words('english')
#Clean the text
finder = TrigramCollocationFinder.from_words(text, window_size)
finder.apply_freq_filter(2)
finder.apply_word_filter(lambda w: len(w) < 2 or w.lower() in ign)
finder.apply_word_filter(lambda w: next(iter(w)) in propernouns)
trig_mes = TrigramAssocMeasures()
#Get trigrams based on raw frequency
collocs = finder.nbest(trig_mes.raw_freq,10)
scores = finder.score_ngrams( trig_mes.raw_freq)
return(collocs)
其中 propernouns 是语料库中所有专有名词的列表。
问题是我的最后一句话过滤了本应确保我没有超出我的限制的那个。有什么想法吗?
这应该是你想要的
finder.apply_ngram_filter(lambda w1, w2, w3: sum([w1 n propernouns, w2 in propernouns, w3 in propernouns]) >= 2)