-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtagger.py
142 lines (109 loc) · 4.17 KB
/
tagger.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
139
140
141
142
import nltk
from nltk import word_tokenize
from nltk import sent_tokenize
from nltk import pos_tag
import json
import sys
import os
import helper as h
from multiprocessing import Pool
aux = ['did', 'do', 'does', 'am', 'is', 'are', 'was', 'were', 'have', 'has', 'had', "'d", "'s", "'re", 'can', 'could', 'shall', 'should', 'may', 'might', 'must', 'will', 'would']
rel_clause_heur = ['NN', 'NNS', 'NNP', 'NNPS', 'DT', 'JJ', 'PDT', 'POS', 'PRP', 'PRP$', 'CD']
def tagObj(obj):
sent = obj["sentence"]
obj['clauseType'] = None
obj['phrase'] = ''
obj['wh'] = None
tagged = pos_tag(word_tokenize(sent))
lowered_pos = [(x[0].lower(), x[1]) for x in tagged]
obj['wh'] = h.getWh(tagged)
if not obj['wh']:
return None
start_wh = h.start_to_wh(lowered_pos, obj['wh'])
obj['questType'] = ""
obj['heuristic'] = ""
# Tag Question Type:
if not h.x_in_set("V", tagged, is_pos=True):
obj['questType'] = "Fragment"
obj['heuristic'] = 'frag'
# Strict Embedded Heuristic
elif h.emb_seq(start_wh, rel_clause_heur):
obj['questType'] = "Embedded Question"
obj['heuristic'] = 'strict_emb'
# Strict Relative Clause Heuristic
elif h.rel_clause_seq(start_wh, rel_clause_heur):
obj['questType'] = "Relative Clause"
obj['heuristic'] = 'strict_rel'
# Loose Relative Clause Heuristic
elif h.x_in_set(rel_clause_heur, start_wh, is_pos=True):
obj['questType'] = "Relative Clause"
obj['heuristic'] = 'rel_clause'
# Strict Root Question Heuristic
elif h.is_sub_aux_inv(lowered_pos, obj['wh'], aux, rel_clause_heur):
obj['questType'] = "Root Question"
obj['heuristic'] = 'sub_aux_inv'
# Loose Root Question Heuristic
elif "?" in lowered_pos[-1][0]:
obj['questType'] = "Root Question"
obj['heuristic'] = '?'
# Loose Embedded Question Heuristic
elif h.x_in_set("V", start_wh, is_pos=True):
obj['questType'] = "Embedded Question"
obj['heuristic'] = 'v_before_wh'
else:
obj['questType'] = "Ambiguous"
obj['heuristic'] = 'amb'
# Second pass:
if obj['heuristic'] == 'rel_clause':
if h.x_in_set("V", start_wh, is_pos=True) and not h.is_sub_aux_inv(lowered_pos, obj['wh'], aux, rel_clause_heur):
obj['questType'] = "Embedded Question"
obj['heuristic'] = 'second_pass'
v_before_wh = h.get_v_before_wh(tagged, obj['wh'])
v_1_after, v_2_after, v_3_after = h.get_three_v_after_wh(tagged, obj['wh'])
wh_v1 = h.get_set_wh_v1(tagged, obj['wh'])
obj['v_before'] = v_before_wh
obj['v1_after'] = v_1_after
obj['v2_after'] = v_2_after
obj['v3_after'] = v_3_after
obj['wh_v1'] = wh_v1
obj['mat_verb'] = h.modded_lemma(v_before_wh)
obj['emb_verb'] = h.modded_lemma(v_1_after)
modals = ['can', 'could', 'may', 'might', 'shall', 'should', 'will', 'would', 'must']
# Tag Clause Type:
if v_1_after.lower() in modals:
obj['clauseType'] = "Modal"
elif h.x_in_set('to', wh_v1, is_pos=False) and obj['questType'] != "Root Question":
obj['clauseType'] = "Non-Finite"
else:
obj['clauseType'] = "Finite"
return obj
def tagList(json:list):
ret = []
for obj in json:
ret.append(tagObj(obj))
return json
def pool_tag(jlist):
p = Pool()
return p.map(tagObj, jlist)
def multi_run():
jList = []
with open('unread/corpus.json', 'r') as j:
jList = json.load(j)
tagged = pool_tag(jList)
# Remove None objects in list
tagged = [x for x in tagged if x is not None]
with open('edited/edited_corpus.json', 'w') as j:
json.dump(tagged, j, indent=4)
if __name__ == '__main__':
filepath = sys.argv[1]
filename = os.path.basename(filepath)
jList = []
with open(filepath, 'r') as j:
jList = json.load(j)
tagged = pool_tag(jList)
# Remove None objects in list
tagged = [x for x in tagged if x is not None]
end_filename = f'tagged_{filename}'
with open(end_filename, 'w') as j:
json.dump(tagged, j, indent=4)
print(f"Successfully tagged {filename} and created {end_filename}")