【问题标题】:How to find city names in string using NLP and Python如何使用 NLP 和 Python 在字符串中查找城市名称
【发布时间】:2020-06-23 10:33:55
【问题描述】:

正如您在标题中看到的,我的问题很简单。我想使用 NLP 查找城市名称。这是一个例子:

问题:

伊斯坦布尔今天天气怎么样?

答案:

["Istanbul"]

我试过这段代码,但对我没有太大帮助。

import nltk   
my_sent = "How is the weather in Izmit"

word = nltk.word_tokenize(my_sent)   
pos_tag = nltk.pos_tag(word)   
chunk = nltk.ne_chunk(pos_tag)   
NE = [ " ".join(w for w, t in ele) for ele in chunk if isinstance(ele, nltk.Tree)]   
print(NE)

我怎样才能得到这个结果?感谢您的回答。

【问题讨论】:

    标签: python python-3.x string nlp nltk


    【解决方案1】:

    这可以通过spacy 包来完成:

    首先你应该安装 spacy 并加载 en_core_web_ln:

    python3 -m pip install spacy 
    python3 -m spacy download en_core_web_lg
    

    然后使用下面的代码:

    import spacy
    
    nlp = spacy.load('en_core_web_lg')
    
    doc = nlp(u'How is the weather in Izmit')
    
    for ent in doc.ents:
        print(ent.text)
    

    【讨论】:

    • 谢谢,但我的电脑是 32 位 Windows。我在尝试安装 spacy 时遇到错误。还有其他选择吗?
    • 我以前只用 spacy 做过这个,但你可以看看 pypi.org/project/geograpy 包来做这个。
    猜你喜欢
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2019-03-12
    相关资源
    最近更新 更多