gpt4 book ai didi

python - 如何使用 NLTK ne_chunk 提取 GPE(位置)?

转载 作者:行者123 更新时间:2023-12-01 02:11:31 27 4
gpt4 key购买 nike

我正在尝试实现一个代码来使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com