-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.py
executable file
·93 lines (77 loc) · 2.14 KB
/
reduce.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
import json
from difflib import SequenceMatcher
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
def search(k,x):
count=0
for y in data[k]["possibilities"]:
if y["full_form"]==x and count>0:
return True
elif y["full_form"]==x:
count+=1
return False
def similar(a, b):
a=a.lower()
b=b.lower()
return SequenceMatcher(None, a, b).ratio()
filename = 'data/acronyms_best.json'
f = open(filename, 'r+')
data = json.load(f)
################################
# kept = 0
# removed =0
# for k,v in data.items():
# for poss in v["possibilities"]:
# if search(k,poss["full_form"]):
# v["possibilities"].remove(poss)
# print "Removed : "+poss["full_form"]
# removed+=1
# else:
# print "Kept : " +poss["full_form"]
# kept+=1
# print kept
# print removed
###############################
# ns=0
# for k,v in data.items():
#
# flag=0
# for poss in v["possibilities"]:
# rat = similar(v["full_form"], poss["full_form"])
# if rat>=0.8:
# flag=1
# break
# if flag==0:
# ns+=1
# del data[k]
#
# print "not similar :"+str(ns)
###############################
stop_words = set(stopwords.words('english'))
tokeniser = RegexpTokenizer(r'\w+')
count1=0
count2=0
count3=0
for k,v in data.items():
if len(tokeniser.tokenize(v["full_form"].lower()))<2:
print "Removing "+k
del data[k]
try:
for a in tokeniser.tokenize(v["full_form"].lower()):
if a in stop_words:
print "RRemoving "+v["full_form"]
del data[k]
except KeyError:
pass
for poss in v["possibilities"]:
try:
for b in tokeniser.tokenize(poss["full_form"]):
if b in stop_words:
print "removing " + poss["full_form"]
v["possibilities"].remove(poss)
except ValueError:
print "Error"
pass
#
with open('data/acronyms_best.json', 'w') as outfile:
json.dump(data, outfile)