做一个问答系统,提出的问题里面提取参数,去调用api怎么做
https://gitee.com/starplatinum111/AnyQ
例:
import requests
import json
import nltk
# 问题
question = "北京天气怎么样?"
# 分词和词性标注
tokens = nltk.word_tokenize(question)
tags = nltk.pos_tag(tokens)
# 命名实体识别
ne_chunked = nltk.ne_chunk(tags)
entities = []
for chunk in ne_chunked:
if hasattr(chunk, 'label') and chunk.label() == 'GPE':
entities.append(' '.join(c[0] for c in chunk))
# 调用API
if len(entities) > 0:
url = "http://api.openweathermap.org/data/2.5/weather?q={}&appid=your_app_id".format(entities[0])
response = requests.get(url)
data = json.loads(response.text)
print("天气:{}".format(data['weather'][0]['description']))
else:
print("无法识别问题中的地点参数")