【问题标题】:Checking against Nltk POS tags检查 Nltk POS 标签
【发布时间】:2013-03-22 23:24:33
【问题描述】:

我只是在使用 Python 学习 nltk。我正在使用 POS 标记。我想知道的是如何使用标签。例如,这是伪代码:

words = []
teststr = "George did well in the test."
tokens = nltk.word_tokenize(teststr)
words = nltk.pos_tag(tokens)

我想做这样的事情:

if words[i] == "proper noun":
    #do something

如何检查一个词是名词还是动词或任何其他词性。 有人可以帮我吗? 谢谢。

【问题讨论】:

    标签: nltk


    【解决方案1】:

    如果您查看 pos_tag 函数调用的结果,则会返回以下列表:

    [('George', 'NNP'), ('did', 'VBD'), ('well', 'RB'), ('in', 'IN'), ('the', 'DT'), ('test', 'NN'), ('.', '.')]
    

    如果您遍历列表以根据值是专有名词来做某事,您将需要以下代码:

    if words[i][1] == 'NNP':
        # do something
    

    NNP 是单数专有名词。该列表中的每个条目都是一个元组,其中第一个值是单词,第二个值是 pos。

    【讨论】:

    • 非常感谢瑞恩!我现在明白了。抱歉,没有声誉,因此无法投票。
    猜你喜欢
    • 2012-11-26
    • 2016-07-02
    • 2017-06-21
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多