-
Notifications
You must be signed in to change notification settings - Fork 0
/
faltu.py
216 lines (181 loc) · 6.43 KB
/
faltu.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
import os.path
variations = dict()
'''
variations['a'] = "a!@#$%^&*"
variations['b']= "b!@#$%^&*36"
variations['c'] ="c!@#$%^&*"
variations['d'] ="d!!@#$%^&*"
variations['e']= "e!@#$%^&*"
variations['f']= "f!@#$%^&*"
variations['g']= "g!@#$%^&*9"
variations['h']= "h!@#$%^&*"
variations['i']= "i!@#$%^&*"
variations['j']= "j!@#$%^&*"
variations['k']= "k!@#$%^&*"
variations['l']= "l!@#$%^&*"
variations['m']= "m!@#$%^&*"
variations['n']= "n!@#$%^&*"
variations['o']= "o!@#$%^&*0"
variations['p']= "p!@#$%^&*"
variations['q']= "q!@#$%^&*"
variations['r']= "r!@#$%^&*"
variations['s']= "s!@#$%^&*5"
variations['t']= "t!@#$%^&*+"
variations['u']= "u!@#$%^&*"
variations['v']= "v!@#$%^&*"
variations['w']= "w!@#$%^&*"
variations['x']= "x!@#$%^&*"
variations['y']= "y!@#$%^&*"
variations['z']= "z!@#$%^&*"
variations['0']="0o"
variations['1']="1li"
variations['2']="2"
variations['3']="3"
variations['4']="4"
variations['5']="5s"
variations['6']="6"
variations['7']="7"
variations['8']="8"
variations['9']="9g"'''
vowel = {'a','e','i','o','u'}
variations = {}
variations['a'] = {'a','','ae','i','e','o','u','ea'}
variations['b']= {'b','6','bh' ,'13'}
variations['c']={'c','','s'}
variations['d'] = {'d','dh','dh','da','da','di','de','th'}
variations['e'] = {'e','ae','a','i','o','u','','ea'}
variations['f'] = {'f','ph'}
variations['g'] ={'g','j','ga','','ji','je' }
variations['h'] = {'h','ha','hai','he','han','hi',''}
variations['i'] = {'i','ii','e','ae','a','o','u','','ea'}
variations['j'] = {'j','ja','g'}
variations['k']= {'k','ka','ke','ki','ko','ku','ch'}
variations['l']= {'l','la','1'}
variations['m'] ={'m','ma','me','am',''}
variations['n'] = {'n','na','ne'}
variations['o'] = {'o','oh','u','a','i','e','','oe','oi','oa','oy'}
variations['p'] = {'p','pi','pe','pea','pae'}
variations['q'] = {'q','qu','k','kyu'}
variations['r'] = {'r','re','ri','ra','ro','ru','d','rh','ar','rae','rea'}
variations['s']= {'s','c','sh','se','si','sa','so','su','','5'}
variations['t'] = {'t','th','the','ta','ti','te','tha'}
variations['u'] = {'u','o','e','a','i','yu','you','yo',''}
variations['v']={'v','we','vi','vhi','bhi','b','bi','be','vh','w','va','vaa',''}
variations['w'] ={'w','v','vh','','va'}
variations['x']={'x','aks','ex','ax','oks',''}
variations['y'] ={'y','vai','ya','ye','yi','yh','yeh','yo','yu','','j','jhe','je'}
variations['z'] = {'z','j','je','ji','az','s','y'}
variations[' ']={' ',' ',' ',' ',' ',''}
variations['0']={'0','o'}
variations['1']={'1','i','l'}
variations['2']={'2'}
variations['3']={'3'}
variations['4']={'4'}
variations['5']={'5','s'}
variations['6']={'6'}
variations['7']={'7'}
variations['8']={'8'}
variations['9']={'9'}
def check(s1,s2):
if (variations[s1].issuperset({s2}) ):
return True
return False
def edit_distance(s1, s2):
m=len(s1)+1
n=len(s2)+1
tbl = {}
for i in range(m): tbl[i,0]=i
for j in range(n): tbl[0,j]=j
for i in range(1, m):
for j in range(1, n):
if(check(s1[i-1],s2[j-1])):
cost = 0
else :
cost =1
tbl[i,j] = min(tbl[i, j-1]+1, tbl[i-1, j]+1, tbl[i-1, j-1]+cost)
return tbl[i,j]
def reformed_edit_dist(s1,s2):
m=len(s1)+1
n=len(s2)+1
tbl = {}
for i in range(m): tbl[i,0]=i
for j in range(n): tbl[0,j]=j
for i in range(1, m):
for j in range(1, n):
if(check(s1[i-1],s2[j-1])):
cost = 0
else :
cost =1
tbl[i,j] = min(tbl[i, j-1]+1, tbl[i-1, j]+1, tbl[i-1, j-1]+cost)
if(i>=1 and j>=2):
if(check(s1[i-1],s2[j-2:j])):
cost = 0
else :
cost =2
tbl[i,j] = min(tbl[i, j-2]+2, tbl[i-1, j]+1,tbl[i-1,j-2]+cost,tbl[i,j])
if(i>=2 and j>=1):
if (check(s2[j-1],s1[i-2:i])):
cost = 0
else :
cost =2
tbl[i,j] = min(tbl[i, j-1]+1, tbl[i-2, j]+2,tbl[i-2,j-1]+cost,tbl[i,j])
if(vowel.issuperset({s1[i-1]})):
tbl[i,j] = min (tbl[i-1,j],tbl[i,j])
if(vowel.issuperset({s2[j-1]})):
tbl[i,j] = min(tbl[i,j-1],tbl[i,j])
return tbl[i,j]
def getFiles(folder,List,path):
ppath = path+"/"+folder
if os.path.isdir(ppath):
for files in os.listdir(ppath):
getFiles(files,List,ppath)
else:
List.append(ppath)
print reformed_edit_dist('gada','gadh')
'''
def do(filename,abuseFile):
frequencies = open("EnglishFrequencies.txt","a")
dataFile = open(filename)
abuses = open(abuseFile)
for abuse in abuses:
abuse = abuse.split('\n')[0].lower()
tweets = open(filename)
for row in tweets:
tweet = row.split('\t')[2].split('\n')[0].lower()
#print tweet
if (len(abuse.split(' '))>1 and tweet.find(abuse)!= -1):
frequencies.write(abuse+'\t'+abuse+'\t'+filename+'\t'+row)
print abuse
else :
for word in tweet.split(' '):
if(len(abuse.split(' '))==1):
count =0
i=0
for i in range(0,len(word)):
if word[i]=='!' or word[i]=='@' or word[i]=='#' or word[i]=='$' or word[i]=='%' or word[i]=='^' or word[i]=='&' or word[i]=='*' :
count = count +1
if(count == 0):
if(word == abuse):
frequencies.write(abuse+'\t'+word+'\t'+filename+'\t'+row)
print word
if((len(abuse)-count)>0 and count!=0):
val = edit_distance(abuse,word)
if(val ==0):
frequencies.write(abuse+'\t'+word+'\t'+filename+'\t'+row)
print abuse +" "+word #+ " count = "+str(count) +" "+str(len(abuse))
tweets.close()
frequencies.close()
#abuseFolder = open("abuseFolder")
#tweetFolder = open("tweetFolder")
abuseFiles = []
tweetFiles =[]
path = "/home/jg/Documents/socialComputing"
getFiles("abuseFolder/EnglishSwears.txt",abuseFiles,path)
getFiles("tweetFolder/EN",tweetFiles,path)
print abuseFiles
print tweetFiles
#do("aapsweep_TOTAL_CME.txt" ,"HindiSwears.txt")
for i in abuseFiles:
for j in tweetFiles:
do(j,i)
'''