-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditions.py
30 lines (23 loc) · 1.04 KB
/
conditions.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
def _adjacency_conditions(condition, labels, text):
# The two components are directly connected
obj1, obj2 = labels[condition["obj1"]], labels[condition["obj2"]]
if obj1[1] == obj2[0]:
return True
if "ignore" in condition:
ignores = [line.strip() for line in open(condition["ignore"], encoding="utf-8").readlines()]
if text[obj1[1]: obj2[0]] in ignores:
return True
return False
def _special_words(condition, labels, text):
obj1, obj2 = labels[condition["obj1"]], labels[condition["obj2"]]
spwords = [line.strip() for line in open(condition["words"], encoding="utf-8").readlines()]
if text[obj1[1]: obj2[0]] in spwords:
return True
CONDITION_DICT = {
"adjacency": _adjacency_conditions,
"special_words": _special_words,
}
def meet_conditions(conditions, labels, text):
return all([_meets_conditions(condition, labels, text) for condition in conditions])
def _meets_conditions(condition, labels, text):
return CONDITION_DICT[condition["con"]](condition, labels, text)