mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-28 08:41:29 +00:00
Try to add the first natural language processor - 'translate'
This commit is contained in:
@ -1,12 +1,39 @@
|
||||
import jieba
|
||||
import os
|
||||
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 command import hub as cmdhub
|
||||
|
||||
__registry__ = cr = CommandRegistry()
|
||||
|
||||
def _init():
|
||||
_load_processors()
|
||||
|
||||
|
||||
__registry__ = cr = CommandRegistry(init_func=_init)
|
||||
|
||||
|
||||
@cr.register('process')
|
||||
@cr.restrict(full_command_only=True)
|
||||
def process(args_text, ctx_msg, internal=False):
|
||||
print('自然语言消息处理', args_text)
|
||||
print(list(jieba.cut_for_search(args_text)))
|
||||
def process(sentence, ctx_msg, internal=False):
|
||||
sentence = sentence.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)
|
||||
if len(potential_commands) > 0:
|
||||
most_possible_cmd = potential_commands[0]
|
||||
ctx_msg['parsed_data'] = most_possible_cmd[3]
|
||||
cmdhub.call(most_possible_cmd[1], most_possible_cmd[2], ctx_msg)
|
||||
else:
|
||||
core.echo('我暂时不理解你在说什么哦~', ctx_msg, internal)
|
||||
|
||||
|
||||
def _load_processors():
|
||||
processor_mod_files = filter(
|
||||
lambda filename: filename.endswith('.py') and not filename.startswith('_'),
|
||||
os.listdir(get_nl_processors_dir())
|
||||
)
|
||||
command_mods = [os.path.splitext(file)[0] for file in processor_mod_files]
|
||||
for mod_name in command_mods:
|
||||
importlib.import_module('nl_processors.' + mod_name)
|
||||
|
@ -47,7 +47,27 @@ _lang_alias_map = {
|
||||
'汉语': 'zh',
|
||||
'英文': 'en',
|
||||
'日文': 'jp',
|
||||
'韩文': 'kor'
|
||||
'韩文': 'kor',
|
||||
'法文': 'fra',
|
||||
'西班牙文': 'spa',
|
||||
'阿拉伯文': 'ara',
|
||||
'俄文': 'ru',
|
||||
'葡萄牙文': 'pt',
|
||||
'德文': 'de',
|
||||
'意大利文': 'it',
|
||||
'希腊文': 'el',
|
||||
'荷兰文': 'nl',
|
||||
'波兰文': 'pl',
|
||||
'保加利亚文': 'bul',
|
||||
'爱沙尼亚文': 'est',
|
||||
'丹麦文': 'dan',
|
||||
'芬兰文': 'fin',
|
||||
'捷克文': 'cs',
|
||||
'罗马尼亚文': 'rom',
|
||||
'斯洛文尼亚文': 'slo',
|
||||
'瑞典文': 'swe',
|
||||
'匈牙利文': 'hu',
|
||||
'越南文': 'vie'
|
||||
}
|
||||
|
||||
|
||||
@ -67,7 +87,7 @@ def translate(args_text, ctx_msg):
|
||||
return translate_to('简体中文 ' + args_text, ctx_msg)
|
||||
|
||||
|
||||
@cr.register('translate_to', 'translate-to', '翻译到', '翻译成')
|
||||
@cr.register('translate_to', 'translate-to', '翻译到', '翻译成', '翻译为')
|
||||
def translate_to(args_text, ctx_msg):
|
||||
args = args_text.strip().split(' ', 1)
|
||||
if len(args) < 2 or (args[0] not in _lang_map and args[0] not in _lang_alias_map):
|
||||
|
Reference in New Issue
Block a user