[英]how to get only the nouns from a sentence
我試圖找出句子中存在哪些名詞,我使用的是 nltk 中的 pos_tag 但效果不是很好這是我的代碼/函數
def Noun(sentence):
lista=[]
words=(word_tokenize(sentence))
pos=pos_tag(words)
for i in range(len(pos)):
if((pos[i][1].startswith('N'))):
lista.append(pos[i][0])
else:
pass
return pos,lista
例如:tweet="讓我們和 Thomas 談談,看看他是否會來參加聚會" Noun(tweet) expected:
output: ['Thomas','party']
我得到了什么:
['let', 'talk', 'Thomas', 'party'])
你的代碼沒有問題。 使用的算法“pos_tag”是錯誤輸出的原因。 它將這四個詞顯示為名詞:
[('let', 'NN'), ("'s", 'POS'), ('talk', 'NN'), ('to', 'TO'), ('Thomas', 'NNP'), ('and', 'CC'), ('check', 'VB'), ('if', 'IN'), ('he', 'PRP'), ('will', 'MD'), ('come', 'VB'), ('to', 'TO'), ('the', 'DT'), ('party', 'NN'), ('.', '.')]
您可以嘗試 unigram 標記、n-gram 標記等。點擊此鏈接了解詳細信息: https ://www.nltk.org/book/ch05.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.