mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-27 16:21:28 +00:00
Add NLP
This commit is contained in:
37
none_demo/plugins/tuling.py
Normal file
37
none_demo/plugins/tuling.py
Normal file
@ -0,0 +1,37 @@
|
||||
import asyncio
|
||||
|
||||
from none import (
|
||||
on_command, CommandSession,
|
||||
on_natural_language, NLPSession, NLPResult
|
||||
)
|
||||
|
||||
|
||||
@on_command('tuling', aliases=('聊天', '对话'))
|
||||
async def tuling(session: CommandSession):
|
||||
message = session.get('message', prompt='我已经准备好啦,来跟我聊天吧~')
|
||||
|
||||
finish = message in ('结束', '拜拜', '再见')
|
||||
if finish:
|
||||
asyncio.ensure_future(session.send('拜拜啦,你忙吧,下次想聊天随时找我哦~'))
|
||||
return
|
||||
|
||||
# call tuling api
|
||||
reply = f'你说了:{message}'
|
||||
|
||||
one_time = session.get_optional('one_time', False)
|
||||
if one_time:
|
||||
asyncio.ensure_future(session.send(reply))
|
||||
else:
|
||||
del session.args['message']
|
||||
session.get('message', prompt=reply)
|
||||
|
||||
|
||||
@tuling.args_parser
|
||||
async def _(session: CommandSession):
|
||||
if session.current_key == 'message':
|
||||
session.args[session.current_key] = session.current_arg_text.strip()
|
||||
|
||||
|
||||
@on_natural_language
|
||||
async def _(session: NLPSession):
|
||||
return NLPResult(60.0, 'tuling', {'message': session.msg, 'one_time': True})
|
@ -1,4 +1,7 @@
|
||||
from none import CommandSession, CommandGroup
|
||||
from none import (
|
||||
CommandSession, CommandGroup,
|
||||
on_natural_language, NLPSession, NLPResult
|
||||
)
|
||||
|
||||
from . import expressions as expr
|
||||
|
||||
@ -7,34 +10,24 @@ w = CommandGroup('weather')
|
||||
|
||||
@w.command('weather', aliases=('天气', '天气预报'))
|
||||
async def weather(session: CommandSession):
|
||||
city = session.require_arg('city', prompt_expr=expr.WHICH_CITY)
|
||||
city = session.get('city', prompt_expr=expr.WHICH_CITY)
|
||||
await session.send_expr(expr.REPORT, city=city)
|
||||
|
||||
|
||||
@weather.args_parser
|
||||
async def _(session: CommandSession):
|
||||
striped_arg = session.current_arg_text.strip()
|
||||
if session.current_key:
|
||||
session.args[session.current_key] = session.current_arg_text.strip()
|
||||
session.args[session.current_key] = striped_arg
|
||||
elif striped_arg:
|
||||
session.args['city'] = striped_arg
|
||||
|
||||
|
||||
# @on_natural_language(keywords={'天气', '雨', '雪', '晴', '阴', '多云', '冰雹'},
|
||||
# only_to_me=False)
|
||||
# async def weather_nlp(session: NaturalLanguageSession):
|
||||
# return NLPResult(89.5, ('weather', 'weather'), {'city': '南京'})
|
||||
#
|
||||
#
|
||||
# @weather_nlp.condition
|
||||
# async def _(session: NaturalLanguageSession):
|
||||
# keywords = {'天气', '雨', '雪', '晴', '阴', '多云', '冰雹'}
|
||||
# for kw in keywords:
|
||||
# if kw in session.text:
|
||||
# keyword_hit = True
|
||||
# break
|
||||
# else:
|
||||
# keyword_hit = False
|
||||
# if session.ctx['to_me'] and keyword_hit:
|
||||
# return True
|
||||
# return False
|
||||
@on_natural_language({'天气', '雨', '雪', '晴', '阴'}, only_to_me=False)
|
||||
async def _(session: NLPSession):
|
||||
if not ('?' in session.msg_text or '?' in session.msg_text):
|
||||
return None
|
||||
return NLPResult(90.0, ('weather', 'weather'), {})
|
||||
|
||||
|
||||
@w.command('suggestion', aliases=('生活指数', '生活建议', '生活提示'))
|
||||
|
Reference in New Issue
Block a user