From 9c794334c113c5eec9d4efe773f20bc627a56a27 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Wed, 11 Jan 2017 14:06:51 +0800 Subject: [PATCH] Make command dispatching process clearer --- commands/natural_language.py | 23 +++++++++++++---------- config.py | 3 ++- filters/command_dispatcher_0.py | 8 ++++---- little_shit.py | 8 ++++++++ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/commands/natural_language.py b/commands/natural_language.py index c3954636..1a04a8e3 100644 --- a/commands/natural_language.py +++ b/commands/natural_language.py @@ -4,7 +4,7 @@ import importlib from command import CommandRegistry from commands import core from nl_processor import parse_potential_commands -from little_shit import get_nl_processors_dir +from little_shit import get_nl_processors_dir, get_fallback_command_after_nl_processors from command import hub as cmdhub @@ -14,10 +14,12 @@ def _init(): __registry__ = cr = CommandRegistry(init_func=_init) +_fallback_command = get_fallback_command_after_nl_processors() + @cr.register('process') @cr.restrict(full_command_only=True) -def process(args_text, ctx_msg, internal=False): +def process(args_text, ctx_msg): sentence = args_text.strip() potential_commands = parse_potential_commands(sentence) potential_commands = sorted(filter(lambda x: x[0] > 60, potential_commands), key=lambda x: x[0], reverse=True) @@ -25,19 +27,20 @@ def process(args_text, ctx_msg, internal=False): most_possible_cmd = potential_commands[0] core.echo( '识别出最可能的等价命令:\n' + ' '.join((most_possible_cmd[1], most_possible_cmd[2])), - ctx_msg, - internal + ctx_msg ) ctx_msg['parsed_data'] = most_possible_cmd[3] cmdhub.call(most_possible_cmd[1], most_possible_cmd[2], ctx_msg) else: if ctx_msg.get('from_voice'): - core.echo('暂时无法理解你的意思,下面将发送图灵机器人的回复……', ctx_msg, internal) - core.tuling123(sentence, ctx_msg, internal) - else: - core.echo('暂时无法理解你的意思。\n' - '由于自然语言识别还非常不完善,建议使用命令来精确控制我。\n' - '如需帮助请发送「使用帮助」。', ctx_msg, internal) + # Special for voice message + if _fallback_command: + core.echo('暂时无法理解你的意思,下面将使用备用命令 ' + _fallback_command + '……', ctx_msg) + cmdhub.call(_fallback_command, sentence, ctx_msg) + return + core.echo('暂时无法理解你的意思。\n' + '由于自然语言识别还非常不完善,建议使用命令来精确控制我。\n' + '如需帮助请发送「使用帮助」。', ctx_msg) def _load_processors(): diff --git a/config.py b/config.py index 22af982f..df129935 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,7 @@ config = { 'fallback_command': 'natural_language.process', - 'command_start_flags': ('/', '/', '来,', '来,'), + 'fallback_command_after_nl_processors': 'core.tuling123', + 'command_start_flags': ('/', '/', '来,', '来,'), # add '' (empty string) here to allow commands without start flags 'command_name_separators': ('\.', '->', '::', '/'), # Regex 'command_args_start_flags': (',', ':', ',', ', ', ':', ': '), # Regex } diff --git a/filters/command_dispatcher_0.py b/filters/command_dispatcher_0.py index 26bddac8..e721f0c5 100644 --- a/filters/command_dispatcher_0.py +++ b/filters/command_dispatcher_0.py @@ -3,14 +3,13 @@ import sys import importlib import interactive -from config import config from filter import as_filter from command import CommandNotExistsError, CommandScopeError, CommandPermissionError from little_shit import * from commands import core from command import hub as cmdhub -_fallback_command = config.get('fallback_command') +_fallback_command = get_fallback_command() _command_start_flags = get_command_start_flags() _command_args_start_flags = get_command_args_start_flags() @@ -42,7 +41,8 @@ def _dispatch_command(ctx_msg): if text.startswith(flag): start_flag = flag break - if not start_flag or len(text) <= len(start_flag): + if start_flag is None or len(text) <= len(start_flag): + # Note: use `start_flag is None` here because empty string is allowed to be the start flag # No command, check if a session exists if interactive.has_session(source): command = [interactive.get_session(source).cmd, text] @@ -60,7 +60,7 @@ def _dispatch_command(ctx_msg): if len(command) == 1: # Add an empty argument command.append('') - # Starting a new command, so remove any previous command session + # Starting a new command, so remove previous command session, if any interactive.remove_session(source) command[0] = command[0].lower() diff --git a/little_shit.py b/little_shit.py index 1313b1c8..dfc1d8b7 100644 --- a/little_shit.py +++ b/little_shit.py @@ -92,3 +92,11 @@ def get_command_name_separators(): def get_command_args_start_flags(): return tuple(sorted(('[ \t\n]',) + config['command_args_start_flags'], reverse=True)) + + +def get_fallback_command(): + return config.get('fallback_command') + + +def get_fallback_command_after_nl_processors(): + return config.get('fallback_command_after_nl_processors')