-
Notifications
You must be signed in to change notification settings - Fork 4
/
RuleTest.py
138 lines (123 loc) · 5.32 KB
/
RuleTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import random
from RuleParser import RuleParser
# 用词典可以快速匹配到
poet_names = {'李白':1,'李白冰':2,'杜甫':3}
poetry_names = {'将进酒':1,'沁园春':2}
poetry_sentences = {'黄河之水天上来':1,'海上升明月':2}
music_names = {'海阔天空':1,'匆匆':2}
# 外部库查找实现
def hook_lib_method_impl(match_string,lib_name,params):
#print("hook_lib_method_impl 库中查找,库名:"+lib_name+" 查找句子:"+match_string)
# 传递参数测试
assert(params=='HELLO')
# 返回的关键词数组
matched_strings = []
current_database = None
if lib_name == '诗人':
current_database = poet_names
elif lib_name == '诗名':
current_database = poetry_names
elif lib_name == '诗句':
current_database = poetry_sentences
elif lib_name == '歌曲':
current_database = music_names
else:
# print("hook_lib_method_impl 未找到库,库名:"+lib_name+" 查找句子:"+match_string)
return matched_strings
for i in range(len(match_string)):
search_string = match_string[:i+1]
if search_string in current_database:
# print("hook_lib_method_impl 匹配到库:"+lib_name+" 关键词:"+search_string+" match_string:"+match_string)
matched_strings.append(search_string)
# print("hook_lib_method_impl 库:"+lib_name+" 全部匹配到的关键词:"+str(matched_strings))
return matched_strings
# 外部库生成实现
def hook_generate_lib_method_impl(lib_name,params):
#print("hook_generate_lib_method_impl 库名:"+lib_name)
generate_string = ''
current_database = None
if lib_name == '诗人':
current_database = poet_names
elif lib_name == '诗名':
current_database = poetry_names
elif lib_name == '诗句':
current_database = poetry_sentences
elif lib_name == '歌曲':
current_database = music_names
else:
# print("hook_generate_lib_method_impl 未找到库,库名:"+lib_name)
return generate_string
values_len = len(current_database.keys())
if values_len == 0:
return generate_string
random_index = random.randint(0,values_len - 1)
for i,item in enumerate(current_database):
if i == random_index:
generate_string = item
break
return generate_string
def test_sentence(rule_parser,sentence):
success,keywords,keywords_pos,lib_names,nodes_path = rule_parser.match(sentence)
if success:
print("\n=========================匹配句子=================================")
print("句子:"+sentence)
print("关键词:"+str(keywords)+" \n关联库:"+str(lib_names)+" \n位置:"+str(keywords_pos))
path_trace = ''
for node in nodes_path:
if path_trace == '':
path_trace = str(node)
else:
path_trace += "-->"+str(node)
print("节点路径:"+path_trace)
print("=================================================================\n")
else:
print("\n=================================================================")
print("匹配失败:"+sentence)
print("=================================================================\n")
if __name__ == '__main__':
# 创建一个实例
rule_parser = RuleParser()
# 自定义词库匹配查询
rule_parser.set_match_lib_hook(hook_lib_method_impl,"HELLO")
# 解析规则
rule = "#sys.任意文本##诗人#[的]#诗名#的(介绍|说明|#歌曲#)[啊|哦|#呵呵#|额]"
print("解析规则:"+rule)
rule_parser.parse(rule)
# 默认关闭,设为True打开
rule_parser.set_debug(False)
# 测试句子
test_sentence(rule_parser,'李白的将进酒的介绍哦')
test_sentence(rule_parser,'李白将进酒的介绍')
test_sentence(rule_parser,'我请问李白冰将进酒的介绍')
# ”李白是谁“不能命中
test_sentence(rule_parser,'李白是谁')
# 重置新规则
rule = "#诗人#是谁"
print("解析规则:"+rule)
rule_parser.parse(rule)
# ”李白是谁“命中
test_sentence(rule_parser,'李白是谁')
# 加入内置数字测试
rule = "[请问]#sys.数字#个人是什么字"
print("解析规则:"+rule)
rule_parser.parse(rule)
test_sentence(rule_parser,'三个人是什么字')
test_sentence(rule_parser,'请问三个人是什么字')
test_sentence(rule_parser,'请问十二个人是什么字')
# 根据规则随机生成句子
rule = "#sys.任意文本##诗人#[的]#诗名#的(介绍|说明|#歌曲#)[啊|哦|额]"
rule_parser.parse(rule)
rule_parser.set_generate_lib_hook(hook_generate_lib_method_impl,'HI')
sentence,keywords,keywords_pos,lib_names,nodes_path = rule_parser.generate()
print("\n======================生成句子=====================================")
print("句子:"+sentence)
print("关键词:"+str(keywords)+" \n关联库:"+str(lib_names)+" \n位置:"+str(keywords_pos))
path_trace = ''
for node in nodes_path:
if path_trace == '':
path_trace = str(node)
else:
path_trace += "-->"+str(node)
print("节点路径:"+path_trace)
print("=================================================================\n")
test_sentence(rule_parser,sentence)