-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargument_splitting_func.py
183 lines (149 loc) · 6.42 KB
/
argument_splitting_func.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
def is_verbal_phrase(text, nlp_model):
doc = nlp_model(text)
nsubj = 0
verb_fin = 0
for token in doc:
if 'subj' in token.dep_ or (token.dep_=='ROOT' and token.pos_ == 'NOUN'):
if token.morph.get('PronType'):
if token.morph.get('PronType')[0] != 'Rel':
nsubj += 1
else:
nsubj += 1
if token.pos_ == 'VERB' or token.pos_ == 'AUX':
if token.morph.get('VerbForm')[0] == 'Fin':
verb_fin += 1
if nsubj > 0 and verb_fin > 0:
return True
else:
return False
#####################################################################################################
#####################################################################################################
def remove_abbr_point(text):
import re
if 'Gm.b. H' in text:
text = text.replace('Gm.b. H', 'GmbH')
if 'Inc.' in text:
text = text.replace('Inc.', 'Inc ')
if 'Dr.' in text:
text = text.replace('Dr.', 'Dr ')
if 'No.' in text:
text = text.replace('No.', 'No ')
if 'Co.' in text:
text = text.replace('Co.', 'Co ')
if 'Corp.' in text:
text = text.replace('Corp.', 'Corp ')
if 'Mr.' in text:
text = text.replace('Mr.', 'Mr ')
if 'Mme.' in text:
text = text.replace('Mme.', 'Mme ')
if 'Jr.' in text:
text = text.replace('Jr.', 'Jr ')
text = re.sub(r'([A-Z])\.', r'\1 ', text)
text = re.sub(r'([0-9])\.([0-9])', r'\1,\2 ', text)
text = text.replace(' ', ' ')
return text
#####################################################################################################
#####################################################################################################
def remove_list(list_):
import re
list_clean = []
for elem in list_:
if elem:
if re.findall(r'[A-Za-z0-9]+', elem):
list_clean.append(elem)
return list_clean
#####################################################################################################
#####################################################################################################
def flatten(A):
rt = []
for i in A:
if isinstance(i,list): rt.extend(flatten(i))
else: rt.append(i)
return rt
#####################################################################################################
#####################################################################################################
def split_sent_punct(text, puncts, nlp_model):
if type(text) != list:
sents = [text]
else:
sents = text
for punct in puncts:
sents_1 = []
for sent in sents:
if punct in sent:
#Split over the punctuation
sents_2 = sent.split(punct)
#Add the punctuation to the end of the cut sentences
for i in range(len(sents_2) - 1):
sents_2[i] = sents_2[i] + punct
#Remove empty strings
sents_2 = remove_list(sents_2)
#Iteratively reconstruct verbal phrases from splits
sents_3 = []
tentative_sent_ = ''
for i, sent_3 in enumerate(sents_2):
tentative_sent_ = (tentative_sent_ + sent_3).strip()
if is_verbal_phrase(tentative_sent_, nlp_model):
sents_3.append(tentative_sent_)
tentative_sent_ = ''
elif i == (len(sents_2) - 1):
if len(sents_3) > 0:
sents_3[-1] = (sents_3[-1] + ' ' + tentative_sent_).strip()
else:
sents_3 = [tentative_sent_.strip()]
else:
continue
sents_1.append(sents_3)
else:
sents_1.append(sent)
if any(isinstance(i, list) for i in sents_1):
sents = flatten(sents_1)
else:
sents = sents_1
return sents
#####################################################################################################
#####################################################################################################
def split_sent_connective(text, connective_patterns, nlp_model):
import re
if type(text) != list:
sents = [text]
else:
sents = text
for pattern in connective_patterns:
sents_1 = []
for sent in sents:
if re.findall(pattern, sent):
#Split over the connective pattern
sents_2 = re.split(pattern, sent)
#Remove empty strings
sents_2 = remove_list(sents_2)
#Iteratively reconstruct verbal phrases from splits
sents_3 = []
tentative_sent_ = ''
for i, sent_3 in enumerate(sents_2):
tentative_sent_ = (tentative_sent_ + ' ' + sent_3).strip()
if is_verbal_phrase(tentative_sent_, nlp_model):
sents_3.append(tentative_sent_)
tentative_sent_ = ''
elif i == (len(sents_2) - 1):
if len(sents_3) > 0:
sents_3[-1] = (sents_3[-1] + ' ' + tentative_sent_).strip()
else:
sents_3 = [tentative_sent_.strip()]
else:
continue
sents_1.append(sents_3)
else:
sents_1.append(sent)
if any(isinstance(i, list) for i in sents_1):
sents = flatten(sents_1)
else:
sents = sents_1
return sents
#####################################################################################################
#####################################################################################################
def split_argument(text, punct_list, connective_patterns, nlp_model):
text_ = remove_abbr_point(text)
text_ = split_sent_punct(text_, punct_list, nlp_model)
text_ = split_sent_connective(text_, connective_patterns, nlp_model)
return text_