作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一个代码来使用 OpenWeatherMap API 和 NLTK 来检查特定区域的天气状况,以查找实体名称识别。但我无法找到将 GPE 中存在的实体(给出位置)(在本例中为芝加哥)传递给我的 API 请求的方法。请帮我解决语法问题。下面给出的代码。
感谢您的帮助
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_chunk
模型的 Tree
对象的标签。
>>> 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'), ('?', '.')])
要遍历树,请参阅 How to Traverse an NLTK Tree object?
也许,您正在寻找对NLTK Named Entity recognition to a Python list稍加修改的东西
from nltk import word_tokenize, pos_tag, ne_chunk
from nltk import Tree
def get_continuous_chunks(text, label):
chunked = ne_chunk(pos_tag(word_tokenize(text)))
prev = None
continuous_chunk = []
current_chunk = []
for subtree in chunked:
if type(subtree) == Tree and subtree.label() == label:
current_chunk.append(" ".join([token for token, pos in subtree.leaves()]))
if current_chunk:
named_entity = " ".join(current_chunk)
if named_entity not in continuous_chunk:
continuous_chunk.append(named_entity)
current_chunk = []
else:
continue
return continuous_chunk
[输出]:
>>> 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']
关于python - 如何使用 NLTK ne_chunk 提取 GPE(位置)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48660547/
我正在尝试使用 nltk 中的 ne_chunk 和 pos_tag 对句子进行分块。 from nltk import tag from nltk.tag import pos_tag from n
我正在尝试实现一个代码来使用 OpenWeatherMap API 和 NLTK 来检查特定区域的天气状况,以查找实体名称识别。但我无法找到将 GPE 中存在的实体(给出位置)(在本例中为芝加哥)传递
我是一名优秀的程序员,十分优秀!