无法使Counter()在python中工作

2024-10-02 12:37:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一个计数器,它使用POS trigrams列表来检查一个大的trigrams列表并找到它们的频率。 目前我的代码如下:

from nltk import trigrams
from nltk.tokenize import wordpunct_tokenize
from nltk import bigrams
from collections import Counter
import nltk
text= ["This is an example sentence."]
trigram_top= ['PRP', 'MD', 'VB']

   for words in text:
      tokens = wordpunct_tokenize (words)
      tags = nltk.pos_tag (tokens)
      trigram_list=trigrams(tags)
      list_tri=Counter (t for t in trigram_list if t in trigram_top)
      print list_tri

我的柜台是空的。我怎么修补这个? 在早期的版本中,我确实得到了数据,但是每次迭代都会不断增加(在实际程序中,文本是不同文件的集合)。 有人有主意吗?在


Tags: textinfromimport列表fortopcounter
1条回答
网友
1楼 · 发布于 2024-10-02 12:37:40

让我们放一些print来调试:

from nltk import trigrams
from nltk.tokenize import wordpunct_tokenize
from nltk import bigrams
from collections import Counter
import nltk
text= ["This is an example sentence."]
trigram_top= ['PRP', 'MD', 'VB']

for words in text:
    tokens = wordpunct_tokenize (words)
    print tokens
    tags = nltk.pos_tag (tokens)
    print tags
    list_tri=Counter (t[0] for t in tags if t[1] in trigram_top)
    print list_tri

#['This', 'is', 'an', 'example', 'sentence', '.']
#[('This', 'DT'), ('is', 'VBZ'), ('an', 'DT'), ('example', 'NN'), ('sentence', 'NN'), ('.', '.')]
#Counter()

注意,list=部分是多余的,我已经将生成器更改为只接受单词而不是pos标记

我们可以看到,没有一个pos标记直接匹配您的trigram_top-您可能需要修改您的比较检查来迎合VB/VBZ。。。在

一种可能是改变路线:

^{pr2}$

相关问题 更多 >

    热门问题