-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_rules.py
47 lines (42 loc) · 1.71 KB
/
generate_rules.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
import json
file = json.load(open('rules_train_dev.json'))
good_rules = json.load(open('rules_w_prec_greater_60.json'))
total = 0
for relation in file:
if good_rules and relation.replace('_slash_', '/') not in good_rules:
print (relation)
continue
if good_rules:
current_rules = sorted(good_rules[relation.replace('_slash_', '/')], key=lambda k: len(good_rules[relation.replace('_slash_', '/')][k]), reverse=True)
if len(current_rules)>1:
print (relation, current_rules)
with open('%s_unit.yml'%relation, 'w') as f:
count = 0
for trigger in file[relation]:
rules = [r for r in file[relation][trigger]]
relation = relation.replace('/', '_slash_')
for rule in rules:
if good_rules==None or count in good_rules[relation.replace('_slash_', '/')][current_rules[0]]:
f.write('''
- name: ${label}_${count}_%d
label: ${label}
priority: ${rulepriority}
pattern: |
trigger = %s
subject: ${subject_type} = %s
object: ${object_type} = %s\n'''%(count, trigger, ' '.join(rule['subj']), ' '.join(rule['obj'])))
total += 1
if good_rules and len(current_rules)>1:
for c in current_rules[1:]:
if count in good_rules[relation.replace('_slash_', '/')][c]:
f.write('''
- name: ${label}_%s_%d
label: ${label}
priority: ${rulepriority}
pattern: |
trigger = %s
subject: ${subject_type} = %s
object: ${object_type} = %s\n'''%(c, count, trigger, ' '.join(rule['subj']), ' '.join(rule['obj'])))
total += 1
count += 1
print (total)