简体   繁体   中英

Python Machine Learning Classifying Words in Sentence

I want to make an personal assistant using artificial intelligence and machine learging techniques. I am using Python 3.7 and I have an question.

When software starts, first it will ask user's name. I want it to get user's name.

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

But I want to know name correctly if user enters an sentence. Here is an example:

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

But I want to get name even if person enters like this:

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

But I couldn't understand how can I just get the user's name. I think I should classify the words in sentence but I don't know. Can you help?

You need to get proper nouns. The below code does it:

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']

You can use Spacy name entity recognition toolkit. It recognizes various entities including Person, Country, Organization and ...

Following code is a working example of how you can use it:

import spacy
import en_core_web_sm
nlp = en_core_web_sm.load()

doc = nlp('Its John and he is working at Google')
print([(X.text, X.label_) for X in doc.ents])

Output:

[('John', 'PERSON'), ('Google', 'ORG')]

Note : You may also need to download the Spacy model before running the above script:

pip install spacy
python -m spacy download en 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM