How to find place name inside a sentence using NLP and Python?

試著忘記壹切 提交于 2019-12-23 04:46:02

问题


I have a sentence:

firstSentence = "Gnaeus Cornelius Scipio Hispanus expels all Jews from the city of Rome."

And I want to find the name of the place ("Rome").

I tried using geography:

from geograpy import extraction

e = extraction.Extractor(text=firstSentence)

e.find_entities()
print (e.places())

But I'm receiving error of:

    raise NotImplementedError("Use label() to access a node label.")
NotImplementedError: Use label() to access a node label.

I've also tried this:

import nltk
tagged = nltk.pos_tag(nltk.word_tokenize(firstSentence))
entities = nltk.chunk.ne_chunk(tagged)
print entities

But receiving:

(S
139/CD
  (ORGANIZATION
    BCE/NNP
    Gnaeus/NNP
    Cornelius/NNP
    Scipio/NNP
    Hispanus/NNP)
  expels/VBZ
  all/DT
  Jews/NNS
  from/IN
  the/DT
  city/NN
  of/IN
  (GPE Rome/NNP)
  ./.)

All of my places are going to be Cities or Countries.

I just need simple NLP functionality of finding place within a sentence. I don't mind using any other solution as long it is succeed. How can I do it?


回答1:


from geotext import GeoText
sentence = " Tokyo is city in Japan "
places = GeoText(sentence)
print places.countries
print places.cities


来源:https://stackoverflow.com/questions/49518172/how-to-find-place-name-inside-a-sentence-using-nlp-and-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!