/
acronym_extraction_with_delta.py
297 lines (213 loc) · 5.49 KB
/
acronym_extraction_with_delta.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import csv
import enchant
import re
from collections import Counter
from utils import cleanse
from nltk import ngrams
from abbreviation_extraction_develop.abbreviations import schwartz_hearst
def most_frequent_abbreviations():
with open("most_frequent_abbreviations.csv", "r") as csvfile:
abbreviations = []
readCSV = csv.reader(csvfile, delimiter = ',')
for row in readCSV:
abbreviations.append(row[0])
abbreviations[2] = 'MCQ'
abbreviations[12] = 'PYQ'
return abbreviations
def match(a, b):
if len(a) == 1:
if b[0].lower() == a[0].lower():
return True
elif len(a) == 2:
if len(b) >= 2 and b[0].lower() == a[0].lower and b[1].lower() == a[1].lower():
return True
return False
def preprocess_abbreviations(reduced_abbreviations):
all_initials = []
# Tokenise into initials
for word in reduced_abbreviations:
initials = []
i = 0
while i < len(word):
if word[i] == '.':
i += 1
if i + 1 < len(word) and word[i+1].islower():
initials.append(str(word[i] + word[i+1]))
i+=2
else:
initials.append(word[i])
i += 1
initials_1 = []
for initial in initials:
initials_1.append(initial.lower())
all_initials.append(initials_1)
return all_initials
def preprocess_descriptions():
with open('abbreviation_data_set.csv') as csvfile:
descriptions = []
names = []
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
if len(row) > 1:
descriptions.append(cleanse(row[1]))
names.append(cleanse(row[0]))
return descriptions, names
def find_proximal_expansion(all_initials, descriptions, names, threshold):
index = 0
# print(all_initials[2])
abbrevation_expansions = {}
for initials in all_initials:
index += 1
m = len(initials)
abbreviation = ''.join(initials)
abbrevation_expansions[abbreviation] = []
matchings = []
print(index, abbreviation)
regex = '^' + '.* '.join(initials) + '.*$'
r = re.compile(regex)
matchings = []
for delta in range(0, threshold + 1):
if len(matchings) > 0:
break
for description in descriptions:
n = m + delta
grams = ngrams(description.split(), n)
for gram in grams:
flag = 0
if abbreviation in gram:
continue
for word in gram:
if word.startswith(abbreviation):
flag = 1
break
if flag == 1:
continue
pattern = ' '.join(gram)
if r.match(pattern):
matchings.append(pattern)
for name in names:
n = m + delta
grams = ngrams(name.split(), n)
for gram in grams:
flag = 0
if abbreviation in gram:
continue
for word in gram:
if word.startswith(abbreviation):
flag = 1
break
if flag == 1:
continue
pattern = ' '.join(gram)
if r.match(pattern):
matchings.append(pattern)
frequent_matchings = Counter(matchings)
i = 0
for matching in frequent_matchings.most_common():
abbrevation_expansions[abbreviation].append(matching[0])
i += 1
if i == 5:
break
return abbrevation_expansions
def merge_results(threshold):
frequent_abbreviations = most_frequent_abbreviations()
all_initials = preprocess_abbreviations(frequent_abbreviations)
descriptions, names = preprocess_descriptions()
abbreviation_expansions = find_proximal_expansion(all_initials, descriptions, names, threshold)
` schwartz_hearst_abbreviation_expansions = schwartz_hearst.extract_abbreviation_definition_pairs('./abbreviation_data_set.csv')
fields = ['Acronym', 'Expansions']
with open("final_merged_abbreviations.csv", "w") as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
merged_abbreviations = {}
for pairs in schwartz_hearst_abbreviation_expansions.keys():
merged_abbreviations[pairs] = []
merged_abbreviations[pairs].append(schwartz_hearst_abbreviation_expansions[pairs])
for abbreviation in frequent_abbreviations:
if abbreviation in merged_abbreviations:
left = 3
if abbreviation.lower() in abbreviation_expansions:
for word in abbreviation_expansions[abbreviation.lower()]:
if word.lower() == merged_abbreviations[abbreviation][0].lower():
continue
left -= 1
merged_abbreviations[abbreviation].append(word)
if left == 0:
break
else:
left = 4
merged_abbreviations[abbreviation] = []
if abbreviation.lower() in abbreviation_expansions:
for word in abbreviation_expansions[abbreviation.lower()]:
left -= 1
merged_abbreviations[abbreviation].append(word)
if left == 0:
break
for key in merged_abbreviations.keys():
row = []
row.append(key)
for word in merged_abbreviations[key]:
row.append(word)
csvwriter.writerow(row)
merge_results(1)
# Interesting Results:
# Correct:
# HRM: Human Resource Management
# NTSE:
# OOP:
# TOM:
# IELTS:
# HTML:
# RRB:
# PYQ:
# UPPSC:
# TNPSC:
# RPSC:
# JEE:
# MCQ:
# MCQs
# CGL:
# AFCAT: Only once
# KVS:
# DBMS:
# JPSC:
# HR: Highly Recommended, Human resource, Human Rights, Hindin Reasoning
# EE: Entrance exams
# MPTET:
# RC: Reading Comprehension, Revision Course
# GK: general Knowledge
# PHP: Progression harmonic Progression
# WBPSC:
# CLAT:
# KPSC:
# GPSC:
# DNA:
# SQL:
# PPSC:
# GRE:
# LIC: Learn Important Concepts
# MCQS:
# SNAP:
# STL:
# SOT:
# P&C:
# sin
# tan
# cos
# EMI:
# JIPMER:
# A.P
# GP
# JAM
# G.D.
# ZP: Zila Parishad
# I/O: Input-Output
# op-amp: operational amplifier
# Failed at :
# CTET
# AFCAT
# 60 minutes
# IIT
# TET
# B-School
# stem: statement