在python中返回句子(名词POS标签)中有多少名词的函数

时间:2017-05-14 17:25:23

标签: python nltk pos-tagger

var = ("this is my problem , i need help for a function like this one ")
exampleArray = [var]
def process_language():
    for item in exampleArray:
        tokenized = nltk.word_tokenize(item)
        tagged = nltk.pos_tag(tokenized)
        print (tagged)
process_language()

此函数返回:

[('this', 'DT'), ('is', 'VBZ'), ('my', 'PRP$'), ('problem', 'NN'), (',', ','), 
('i', 'VBP'), ('need', 'VBP'), ('help', 'NN'), ('for', 'IN'), ('a', 'DT'), 
('function', 'NN'), ('like', 'IN'), ('this', 'DT'), ('one', 'NN')]

我正在寻找一个返回4的similair函数,这意味着四个名词谢谢

2 个答案:

答案 0 :(得分:1)

sentence = ("this is my problem , i need help for a function like this one ")
sentence = nltk.word_tokenize(sentence)
sent = pos_tag(sentence)
listnn = ([s for s in sent if s[1] == 'NN'])
print (listnn)
print (len(listnn))

结果是 [('problem','NN'),('help','NN'),('function','NN'),('one','NN')] 4

答案 1 :(得分:1)

Jus count

>>> from nltk import word_tokenize, pos_tag
>>> s = "this is my problem , i need help for a function like this one "
>>> sum(1 for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN'))
4

使用名词

>>> from nltk import word_tokenize, pos_tag
>>> s = "this is my problem , i need help for a function like this one "
>>> sum(1 for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN'))
4
>>> nouns = [word for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN')]
>>> len(nouns)
4
>>> nouns
['problem', 'help', 'function', 'one']

使用名词和pos

>>> from nltk import word_tokenize, pos_tag
>>> s = "this is my problem , i need help for a function like this one "
>>> nouns = [(word,pos) for word, pos in pos_tag(word_tokenize(s)) if pos.startswith('NN')]
>>> nouns
[('problem', 'NN'), ('help', 'NN'), ('function', 'NN'), ('one', 'NN')]