python - 如何只保留单词表中的名词词? python NLTK

标签 python nltk text-processing wordnet pos-tagger

我有一个单词表,其中包含许多主题。主题是从句子中自动提取的。我只想保留主题中的名词。如您所见,有些主题有 adj,我想将其删除。

wordlist=['country','all','middle','various drinks','few people','its reputation','German Embassy','many elections']
returnlist=[]
for word in wordlist:
    x=wn.synsets(word)
    for syn in x:
        if syn.pos() == 'n':
            returnlist.append(word)
            break
print returnlist

上面的结果是:

['country','it',  'middle']

但是,我想要得到的结果应该是这样的

   wordlist=['country','it', 'middle','drinks','people','reputation','German Embassy','elections']

怎么做?

最佳答案

首先,您的列表是未很好标记化文本的结果,因此我再次对它们进行了标记化 然后搜索所有单词的 pos 以找到 pos 包含 NN 的名词:

>>> text=' '.join(wordlist).lower()
>>> tokens = nltk.word_tokenize(text)
>>> tags = nltk.pos_tag(tokens)
>>> nouns = [word for word,pos in tags if (pos == 'NN' or pos == 'NNP' or pos == 'NNS' or pos == 'NNPS')
]
>>> nouns
['country', 'drinks', 'people', 'Embassy', 'elections']

关于python - 如何只保留单词表中的名词词? python NLTK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40167612/

相关文章:

python - 如何从 XML 文件中获取数据?

python - 具有 Lambda 函数的 HSTACK CNN 输出

python - PERL 脚本的输出未使用 Python 加载到 MySQL

Java replaceAll 无法替换字符串中的一个字符

python - 清理 UTF-8 编码的字符串

python - 如何将 NLTK 图发送到文件?

python - 分组相似的字符串

python - 编辑两个 Pandas 列之间的距离

regex - 如何在Swift中提取 "---"之间的多行字符串

text-processing - Java 文本预处理和清理