1 python nlp geolocation named-entity-recognition nltk
我正在尝试实现一个代码来检查特定区域的天气状况,使用 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))
Run Code Online (Sandbox Code Playgroud)
是来自预训练模型的对象GPE标签。Treene_chunk
>>> 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'), ('?', '.')])
Run Code Online (Sandbox Code Playgroud)
要遍历树,请参阅如何遍历 NLTK 树对象?
也许,您正在寻找对Python 列表的 NLTK 命名实体识别进行轻微修改的东西
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
Run Code Online (Sandbox Code Playgroud)
[出去]:
>>> 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']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8253 次 |
| 最近记录: |