首页 > 解决方案 > Python机器学习对句子中的单词进行分类

问题描述

我想使用人工智能和机器学习技术制作个人助理。我正在使用 Python 3.7,我有一个问题。

软件启动时,首先会询问用户名。我希望它获取用户名。

in = input('Hey, what is your name?')
#some classifier things
#...
print = input('Nice to meet you ' + in + '!')

但是如果用户输入一个句子,我想正确地知道名字。这是一个例子:

Hey, what is your name?
John
Nice to meet you John!

但即使有人这样输入,我也想得到名字:

Hey, what is your name?
It's John.
Nice to meet you John!

但我不明白我怎么才能得到用户的名字。我想我应该对句子中的单词进行分类,但我不知道。你能帮我吗?

标签: pythonpython-3.xmachine-learningartificial-intelligencepart-of-speech

解决方案


你需要得到专有名词。下面的代码做到了:

from nltk.tag import pos_tag

sentence = " It's John"
tagged_sent = pos_tag(sentence.split())

propernouns = [word for word,pos in tagged_sent if pos == 'NNP']

推荐阅读