如何使用NLP和Python在句子中找到地名?

时间:2018-03-27 16:34:51

标签: python nlp

我有一句话:

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

我想找到这个地方的名字(“罗马”)。

我尝试使用地理位置:

from geograpy import extraction

e = extraction.Extractor(text=firstSentence)

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

但我收到的错误是:

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

我也试过这个:

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

但接受:

(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)
  ./.)

我所有的地方都将成为城市或国家。

我只需要在句子中找到位置的简单NLP功能。只要成功,我不介意使用任何其他解决方案。我该怎么做?

1 个答案:

答案 0 :(得分:2)

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