2017-01-02 23:51:19 +08:00
|
|
|
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):
|
2017-01-03 01:06:11 +08:00
|
|
|
result = processor[1](sentence, segmentation)
|
|
|
|
if result:
|
|
|
|
potential_commands.append(result)
|
2017-01-02 23:51:19 +08:00
|
|
|
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:
|
2017-01-03 01:06:11 +08:00
|
|
|
result = func(sentence, segmentation)
|
|
|
|
if result:
|
|
|
|
potential_commands.append(result)
|
2017-01-13 12:00:39 +08:00
|
|
|
print('可能的命令:', potential_commands)
|
2017-01-02 23:51:19 +08:00
|
|
|
return potential_commands
|