Add class IntentCommand to replace class NLPResult

This commit is contained in:
Richard Chien 2019-01-21 21:31:26 +08:00
parent 9d138a0595
commit 35ba57c059
2 changed files with 46 additions and 15 deletions

View File

@ -106,7 +106,8 @@ from .plugin import (load_plugin, load_plugins, load_builtin_plugins,
get_loaded_plugins) get_loaded_plugins)
from .message import message_preprocessor, Message, MessageSegment from .message import message_preprocessor, Message, MessageSegment
from .command import on_command, CommandSession, CommandGroup from .command import on_command, CommandSession, CommandGroup
from .natural_language import on_natural_language, NLPSession, NLPResult from .natural_language import (on_natural_language, NLPSession, NLPResult,
IntentCommand)
from .notice_request import (on_notice, NoticeSession, from .notice_request import (on_notice, NoticeSession,
on_request, RequestSession) on_request, RequestSession)
@ -117,6 +118,6 @@ __all__ = [
'get_loaded_plugins', 'get_loaded_plugins',
'message_preprocessor', 'Message', 'MessageSegment', 'message_preprocessor', 'Message', 'MessageSegment',
'on_command', 'CommandSession', 'CommandGroup', 'on_command', 'CommandSession', 'CommandGroup',
'on_natural_language', 'NLPSession', 'NLPResult', 'on_natural_language', 'NLPSession', 'NLPResult', 'IntentCommand',
'on_notice', 'NoticeSession', 'on_request', 'RequestSession', 'on_notice', 'NoticeSession', 'on_request', 'RequestSession',
] ]

View File

@ -72,10 +72,30 @@ class NLPSession(BaseSession):
class NLPResult(NamedTuple): class NLPResult(NamedTuple):
"""
Deprecated.
Use class IntentCommand instead.
"""
confidence: float confidence: float
cmd_name: Union[str, CommandName_T] cmd_name: Union[str, CommandName_T]
cmd_args: Optional[CommandArgs_T] = None cmd_args: Optional[CommandArgs_T] = None
def to_intent_command(self):
return IntentCommand(confidence=self.confidence,
name=self.cmd_name,
args=self.cmd_args,
current_arg=None)
class IntentCommand(NamedTuple):
"""
To represent a command that we think the user may be intended to call.
"""
confidence: float
name: Union[str, CommandName_T]
args: Optional[CommandArgs_T] = None
current_arg: Optional[str] = None
async def handle_natural_language(bot: NoneBot, ctx: Context_T) -> bool: async def handle_natural_language(bot: NoneBot, ctx: Context_T) -> bool:
""" """
@ -135,24 +155,34 @@ async def handle_natural_language(bot: NoneBot, ctx: Context_T) -> bool:
futures.append(asyncio.ensure_future(p.func(session))) futures.append(asyncio.ensure_future(p.func(session)))
if futures: if futures:
# wait for possible results, and sort them by confidence # wait for intent commands, and sort them by confidence
results = [] intent_commands = []
for fut in futures: for fut in futures:
try: try:
results.append(await fut) res = await fut
if isinstance(res, NLPResult):
intent_commands.append(res.to_intent_command())
elif isinstance(res, IntentCommand):
intent_commands.append(res)
except Exception as e: except Exception as e:
logger.error('An exception occurred while running ' logger.error('An exception occurred while running '
'some natural language processor:') 'some natural language processor:')
logger.exception(e) logger.exception(e)
results = sorted(filter(lambda r: r, results),
key=lambda r: r.confidence, reverse=True) intent_commands.sort(key=lambda ic: ic.confidence, reverse=True)
logger.debug(f'NLP results: {results}') logger.debug(f'Intent commands: {intent_commands}')
if results and results[0].confidence >= 60.0:
# choose the result with highest confidence if intent_commands and intent_commands[0].confidence >= 60.0:
logger.debug(f'NLP result with highest confidence: {results[0]}') # choose the intent command with highest confidence
return await call_command(bot, ctx, results[0].cmd_name, chosen_cmd = intent_commands[0]
args=results[0].cmd_args, logger.debug(
check_perm=False) f'Intent command with highest confidence: {chosen_cmd}')
return await call_command(
bot, ctx, chosen_cmd.name,
args=chosen_cmd.args,
current_arg=chosen_cmd.current_arg,
check_perm=False
)
else: else:
logger.debug('No NLP result having enough confidence') logger.debug('No intent command has enough confidence')
return False return False