mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-28 08:41:29 +00:00
Update docs
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
from nonebot import on_command, CommandSession
|
||||
from nonebot import on_natural_language, NLPSession, NLPResult
|
||||
from nonebot import on_natural_language, NLPSession, IntentCommand
|
||||
from jieba import posseg
|
||||
|
||||
from .data_source import get_weather_of_city
|
||||
@ -15,21 +15,27 @@ async def weather(session: CommandSession):
|
||||
@weather.args_parser
|
||||
async def _(session: CommandSession):
|
||||
stripped_arg = session.current_arg_text.strip()
|
||||
if session.current_key:
|
||||
session.args[session.current_key] = stripped_arg
|
||||
elif stripped_arg:
|
||||
session.args['city'] = stripped_arg
|
||||
|
||||
if session.is_first_run:
|
||||
if stripped_arg:
|
||||
session.state['city'] = stripped_arg
|
||||
return
|
||||
|
||||
if not stripped_arg:
|
||||
session.pause('要查询的城市名称不能为空呢,请重新输入')
|
||||
|
||||
session.state[session.current_key] = stripped_arg
|
||||
|
||||
|
||||
# on_natural_language 装饰器将函数声明为一个自然语言处理器
|
||||
# keywords 表示需要响应的关键词,类型为任意可迭代对象,元素类型为 str
|
||||
# 如果不传入 keywords,则响应所有没有被当作命令处理的消息
|
||||
@on_natural_language(keywords=('天气',))
|
||||
@on_natural_language(keywords={'天气'})
|
||||
async def _(session: NLPSession):
|
||||
# 去掉消息首尾的空白符
|
||||
stripped_msg_text = session.msg_text.strip()
|
||||
stripped_msg = session.msg_text.strip()
|
||||
# 对消息进行分词和词性标注
|
||||
words = posseg.lcut(stripped_msg_text)
|
||||
words = posseg.lcut(stripped_msg)
|
||||
|
||||
city = None
|
||||
# 遍历 posseg.lcut 返回的列表
|
||||
@ -39,5 +45,5 @@ async def _(session: NLPSession):
|
||||
# ns 词性表示地名
|
||||
city = word.word
|
||||
|
||||
# 返回处理结果,三个参数分别为置信度、命令名、命令会话的参数
|
||||
return NLPResult(90.0, 'weather', {'city': city})
|
||||
# 返回意图命令,前两个参数必填,分别表示置信度和意图命令名
|
||||
return IntentCommand(90.0, 'weather', current_arg=city or '')
|
||||
|
Reference in New Issue
Block a user