mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-27 16:21:28 +00:00
Try to add the first natural language processor - 'translate'
This commit is contained in:
38
nl_processor.py
Normal file
38
nl_processor.py
Normal file
@ -0,0 +1,38 @@
|
||||
import re
|
||||
|
||||
import jieba.posseg
|
||||
|
||||
_processors = []
|
||||
_processors_without_keyword = []
|
||||
|
||||
|
||||
def as_processor(keywords=None):
|
||||
def decorator(func):
|
||||
if keywords:
|
||||
_processors.append((keywords, func))
|
||||
else:
|
||||
_processors_without_keyword.append(func)
|
||||
return func
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def parse_potential_commands(sentence):
|
||||
segmentation = list(jieba.posseg.cut(sentence=sentence))
|
||||
print('分词结果:', segmentation)
|
||||
potential_commands = []
|
||||
for processor in _processors:
|
||||
processed = False
|
||||
for regex in processor[0]:
|
||||
for word, flag in segmentation:
|
||||
if re.match(regex, word):
|
||||
potential_commands.append(processor[1](sentence, segmentation))
|
||||
processed = True
|
||||
# A word matched, skip the rest of words
|
||||
break
|
||||
if processed:
|
||||
# Current processor has processed, skip the rest of keywords
|
||||
break
|
||||
for func in _processors_without_keyword:
|
||||
potential_commands.append(func(sentence, segmentation))
|
||||
return potential_commands
|
Reference in New Issue
Block a user