【问题标题】:Python Machine Learning Classifying Words in SentencePython机器学习对句子中的单词进行分类
【发布时间】:2018-12-05 18:50:30
【问题描述】:

我想使用人工智能和机器学习技术制作个人助理。我正在使用 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!

但我不明白如何才能获取用户名。我想我应该对句子中的单词进行分类,但我不知道。你能帮忙吗?

【问题讨论】:

    标签: python python-3.x machine-learning artificial-intelligence part-of-speech


    【解决方案1】:

    您可以使用Spacy name entity recognition 工具包。它识别各种实体,包括个人、国家、组织和...

    以下代码是如何使用它的工作示例:

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

    输出:

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

    注意:在运行上述脚本之前,您可能还需要下载 Spacy 模型:

    pip install spacy
    python -m spacy download en 
    

    【讨论】:

    • 我下载了 spacy 但仍然显示 No module named spacy
    • 这里是colab版本的代码:colab.research.google.com/drive/…
    • 谢谢。当我回到家时我会尝试:)。但我认为它会起作用
    • 我也可以将它用于不同的语言吗?因为我来自土耳其;)
    • 我不确定土耳其,但你可以在他们的官方website 上查看。
    【解决方案2】:

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

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

    【讨论】:

    • inp = “是约翰” tagged_sent = pos_tag(inp.split())
    • tagged_sent的值//就像你做print(tagged_sent)时一样
    • 代码崩溃。它说 import nltk nltk.download('averaged_perceptron_tagger')
    • 是的...所以您需要下载所有这些包...和 ​​nltk 库!!只需 google..或写 nltk.download() 并下载所需的包
    • 对不起,我现在在学校。回家后我会努力的。
    猜你喜欢
    • 2018-03-05
    • 2020-07-12
    • 2012-08-06
    • 2018-08-15
    • 2019-01-27
    • 2014-07-18
    • 2014-02-03
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多