From 986e232e798c7806c43147d89f89e89c1286a013 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Tue, 3 Jan 2017 14:48:45 +0800 Subject: [PATCH] Fix bug in translate --- commands/natural_language.py | 5 +++++ nl_processors/translate.py | 23 ++++++++++------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/commands/natural_language.py b/commands/natural_language.py index 24ff7cfc..c3954636 100644 --- a/commands/natural_language.py +++ b/commands/natural_language.py @@ -23,6 +23,11 @@ def process(args_text, ctx_msg, internal=False): potential_commands = sorted(filter(lambda x: x[0] > 60, potential_commands), key=lambda x: x[0], reverse=True) if len(potential_commands) > 0: most_possible_cmd = potential_commands[0] + core.echo( + '识别出最可能的等价命令:\n' + ' '.join((most_possible_cmd[1], most_possible_cmd[2])), + ctx_msg, + internal + ) ctx_msg['parsed_data'] = most_possible_cmd[3] cmdhub.call(most_possible_cmd[1], most_possible_cmd[2], ctx_msg) else: diff --git a/nl_processors/translate.py b/nl_processors/translate.py index d6203729..d3258a0b 100644 --- a/nl_processors/translate.py +++ b/nl_processors/translate.py @@ -3,14 +3,14 @@ import re from nl_processor import as_processor _query_lang_matcher = [ - re.compile('[把将]?[\s,.,。]?(.*?)[\s,.,。]?(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))?翻译[成为到](\w+?[文语])(?![\s::,,.。])'), - re.compile('(\w+?)[\s,.,。]?(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))?[的用](\w+?[文语])') -] - -_lang_query_matcher = [ - re.compile('.*[把将]?(?:(?:这[个]?|[下后][面]?)(?:词[组]?|句(?:子|话)?|短语))?翻译[成为到]\s*(\w+?[文语])[\s::,,](.*)'), - re.compile('[用]?(\w+[文语])\w+?(?:说|讲|表达|表示)(.*)(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))'), - re.compile('[用]?(\w+[文语])\w+?(?:说|讲|表达|表示)(.*)'), + re.compile('(?:(?:要|[应]?该)?怎么|怎样|如何)?[把将]?[\s,.,。]?(?P.*?)[\s,.,。]?' + '(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))?翻译[成为到](?P\w+?[文语])(?![\s::,,.。])'), + re.compile('(?P.+?)[\s,.,。]?(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))?的(?P\w+?[文语])'), + re.compile('.*?[把将]?(?:(?:[下后][面])?(?:这[个]?|[下后][面]?)(?:词[组]?|句(?:子|话)?|短语))?' + '翻译[成为到]\s*(?P\w+?[文语])[\s::,,](?P.*)'), + re.compile('.*[用]?(?P\w+?[文语])\w*?(?:说|讲|表达|表示)' + '(?P.*)(?:这[个]?(?:词[组]?|句(?:子|话)?|短语))'), + re.compile('.*[用]?(?P\w+?[文语])\w*?(?:说|讲|表达|表示)[\s::,,]?(?P.*)'), ] @@ -18,13 +18,10 @@ _lang_query_matcher = [ def _processor(sentence, segmentation): lang = None query = None - for matcher in _query_lang_matcher + _lang_query_matcher: + for matcher in _query_lang_matcher: m = matcher.match(sentence) if m: - if matcher in _lang_query_matcher: - lang, query = m.group(1), m.group(2) - else: - lang, query = m.group(2), m.group(1) + lang, query = m.group('lang'), m.group('query') break if lang and query: print('翻译: 目标语言:', lang, ', 待翻译文本:', query)