Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用NLTK ne_块提取GPE(位置)?_Python_Geolocation_Nlp_Nltk_Named Entity Recognition - Fatal编程技术网

Python 如何使用NLTK ne_块提取GPE(位置)?

Python 如何使用NLTK ne_块提取GPE(位置)?,python,geolocation,nlp,nltk,named-entity-recognition,Python,Geolocation,Nlp,Nltk,Named Entity Recognition,我试图实现一个代码,使用OpenWeatherMapAPI和NLTK检查特定区域的天气状况,以找到实体名称识别。但我无法找到将GPE中存在的实体(给出位置的实体)传递给API请求的方法,在本例中为Chicago。请帮助我的语法。下面给出的代码 谢谢你的帮助 import nltk from nltk import load_parser import requests import nltk from nltk import word_tokenize from nltk.corpus impo

我试图实现一个代码,使用OpenWeatherMapAPI和NLTK检查特定区域的天气状况,以找到实体名称识别。但我无法找到将GPE中存在的实体(给出位置的实体)传递给API请求的方法,在本例中为Chicago。请帮助我的语法。下面给出的代码

谢谢你的帮助

import nltk
from nltk import load_parser
import requests
import nltk
from nltk import word_tokenize
from nltk.corpus import stopwords

sentence = "What is the weather in Chicago today? "
tokens = word_tokenize(sentence)

stop_words = set(stopwords.words('english'))

clean_tokens = [w for w in tokens if not w in stop_words]

tagged = nltk.pos_tag(clean_tokens)

print(nltk.ne_chunk(tagged))

GPE
是预先训练的
ne_区块
模型中的
对象标签

>>> from nltk import word_tokenize, pos_tag, ne_chunk
>>> sent = "What is the weather in Chicago today?"
>>> ne_chunk(pos_tag(word_tokenize(sent)))
Tree('S', [('What', 'WP'), ('is', 'VBZ'), ('the', 'DT'), ('weather', 'NN'), ('in', 'IN'), Tree('GPE', [('Chicago', 'NNP')]), ('today', 'NN'), ('?', '.')])
要遍历树,请参见

也许,你正在寻找一种对

[out]:

>>> sent = "What is the weather in New York today?"
>>> get_continuous_chunks(sent, 'GPE')
['New York']

>>> sent = "What is the weather in New York and Chicago today?"
>>> get_continuous_chunks(sent, 'GPE')
['New York', 'Chicago']

>>> sent = "What is the weather in New York"
>>> get_continuous_chunks(sent, 'GPE')
['New York']

>>> sent = "What is the weather in New York and Chicago"
>>> get_continuous_chunks(sent, 'GPE')
['New York', 'Chicago']

GPE
是预先训练的
ne_区块
模型中的
对象标签

>>> from nltk import word_tokenize, pos_tag, ne_chunk
>>> sent = "What is the weather in Chicago today?"
>>> ne_chunk(pos_tag(word_tokenize(sent)))
Tree('S', [('What', 'WP'), ('is', 'VBZ'), ('the', 'DT'), ('weather', 'NN'), ('in', 'IN'), Tree('GPE', [('Chicago', 'NNP')]), ('today', 'NN'), ('?', '.')])
要遍历树,请参见

也许,你正在寻找一种对

[out]:

>>> sent = "What is the weather in New York today?"
>>> get_continuous_chunks(sent, 'GPE')
['New York']

>>> sent = "What is the weather in New York and Chicago today?"
>>> get_continuous_chunks(sent, 'GPE')
['New York', 'Chicago']

>>> sent = "What is the weather in New York"
>>> get_continuous_chunks(sent, 'GPE')
['New York']

>>> sent = "What is the weather in New York and Chicago"
>>> get_continuous_chunks(sent, 'GPE')
['New York', 'Chicago']

以下是我针对您的情况提出的解决方案:

第一步。单词标记、词性标记、名称实体识别:代码如下:

    Xstring = "What is the weather in New York and Chicago today?"

    tokenized_doc  = word_tokenize(Xstring)
    tagged_sentences = nltk.pos_tag(tokenized_doc )
    NE= nltk.ne_chunk(tagged_sentences )
    NE.draw()
第二步。在名称实体识别后提取所有命名实体(如上所述)

步骤3.现在只提取GPE标签

   for tag in named_entities:
      #print(tag[1])
      if tag[1]=='GPE':   #Specify any tag which is required
        print(tag)
以下是我的输出:

  ('New York', 'GPE')
  ('Chicago', 'GPE')

以下是我针对您的情况提出的解决方案:

第一步。单词标记、词性标记、名称实体识别:代码如下:

    Xstring = "What is the weather in New York and Chicago today?"

    tokenized_doc  = word_tokenize(Xstring)
    tagged_sentences = nltk.pos_tag(tokenized_doc )
    NE= nltk.ne_chunk(tagged_sentences )
    NE.draw()
第二步。在名称实体识别后提取所有命名实体(如上所述)

步骤3.现在只提取GPE标签

   for tag in named_entities:
      #print(tag[1])
      if tag[1]=='GPE':   #Specify any tag which is required
        print(tag)
以下是我的输出:

  ('New York', 'GPE')
  ('Chicago', 'GPE')