-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_concepts.py
58 lines (52 loc) · 1.97 KB
/
check_concepts.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
def get_concepts_train(training_filename):
concept_set = set()
with open(training_filename) as infile:
cur_concept = []
for line in infile:
word, tag = line.strip().split()
if cur_concept and tag != 'I':
concept_set.add(" ".join(cur_concept))
cur_concept = []
if tag == 'B' or tag == 'I':
cur_concept.append(word)
if cur_concept:
concept_set.add(" ".join(cur_concept))
if correct_tag_seq(pred_tags):
found_concept_set.add(" ".join(cur_concept))
return concept_set
def get_concepts_test(training_filename):
concept_set = set()
found_concept_set = set()
def correct_tag_seq(tag_seq):
if tag_seq[0] != 'B':
return False
for tag in tag_seq[1:]:
if tag != 'I':
return False
return True
with open(training_filename) as infile:
cur_concept = []
pred_tags = []
for line in infile:
word, true_tag, pred_tag = line.strip().split()
if cur_concept and true_tag != 'I':
concept_set.add(" ".join(cur_concept))
if correct_tag_seq(pred_tags) and pred_tag != 'I':
found_concept_set.add(" ".join(cur_concept))
cur_concept = []
pred_tags = []
if true_tag == 'B' or true_tag == 'I':
cur_concept.append(word)
pred_tags.append(pred_tag)
if cur_concept:
concept_set.add(" ".join(cur_concept))
if correct_tag_seq(pred_tags):
found_concept_set.add(" ".join(cur_concept))
return concept_set, found_concept_set
train_concept_set = get_concepts_train('concept_train_data.txt')
_, found_test_concept_set = get_concepts_test('concept_test_data.txt')
print(train_concept_set)
print()
print(found_test_concept_set)
print()
print(found_test_concept_set-train_concept_set)