-
Notifications
You must be signed in to change notification settings - Fork 0
/
extraction_compile_edit.py
361 lines (288 loc) · 10.8 KB
/
extraction_compile_edit.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 11:30:00 2015
@author: 1152
"""
import csv
import re
from ngram import NGram
import geocoder
def abbreviation(sentences):
corpus = csv.reader(open("D:/tasya/python/code/Geo-Tag/corpus/abbreviation.csv"))
dictionary={}
for row in corpus:
dictionary[row[0]]=row[1]
for word in sentences.split():
if word in dictionary.keys():
sentences = re.sub(word,dictionary[word],sentences)
return sentences
def alay_normalizer(kalimat):
#kamusalay
kamusnya = csv.reader(open("d:/kamusalay.csv"))
kamus={}
for row in kamusnya:
kamus[row[0]]=row[1]
# preprocessing
# huruf mejadi lower case semua
ubah=kalimat.lower()
# menghilangkan link
ubah=re.sub("http://\w.\w+.\w+|https://\w.\w+.\w+"," ", ubah)
# perulangan a>2 =a
ubah=re.sub(r'([a-z])\1\1+', r'\1', ubah)
# menghilangkan puntuation tertentu
ubah=re.sub("\?|!|\""," ", ubah)
# rules alay
# replace ea to ya
ubah=re.sub("\sea|^ea"," ya",ubah)
# replace th to c
tuples=re.findall("\w+th\w+|\w+th",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("th","c",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace 2 to z
tuples=re.findall("^2[^\d]\w+|\s2[^\d]\w+|[^\d]2[^\d]|\s2[^\d]\w+|\w+[^\d]2\s|\w+[^\d]2\w+[^\d]\w\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("2","z",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace 3 to e
tuples=re.findall("^3[^\d]\w+|\s3[^\d]\w+|[^\d]3[^\d]|\s3[^\d]\w+|\w+[^\d]3\s|\w+[^\d]3\w+[^\d]\w\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("3","e",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
#replace 5 to s
tuples=re.findall("\w+[^\d]5[^\d]",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("5","s",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace 6 to g
tuples=re.findall("^6[^\d]\w+|\s6[^\d]\w+|[^\d]6[^\d]|\s6[^\d]\w+|\w+[^\d]6\s|\w+[^\d]6\w+[^\d]\w\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("6","g",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace 7 to j
tuples=re.findall("^7[^\d]\w+|\s7[^\d]\w+|[^\d]7[^\d]|\s7[^\d]\w+|\w+[^\d]7\s|\w+[^\d]7\w+[^\d]\w\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("7","j",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace 8 to b
tuples=re.findall("^8[^\d]\w+|\s8[^\d]\w+|[^\d]8[^\d]|\s8[^\d]\w+|\w+[^\d]8\s|\w+[^\d]8\w+[^\d]\w\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("8","b",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace 9 to g
tuples=re.findall("^9[^\d]\w+|\s9[^\d]\w+|[^\d]9[^\d]|\s9[^\d]\w+|\w+[^\d]9\s|\w+[^\d]9\w+[^\d]\w\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("9","g",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace 0 to o
tuples=re.findall("^0[^\d]\w+|\s0[^\d]\w+|[^\d]0[^\d]|\s0[^\d]\w+|\w+[^\d]0\s|\w+[^\d]0\w+[^\d]\w\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("0","o", tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
#token2f=re.findall("[^\d]\w+4[^\d]+",token1) #nonumber #noawal #2step
#token2=re.sub("4","empat",token2f[0]) #nonumber #noawal #2step #masukkamus aja
# replace zt to s
tuples=re.findall("\w+zt\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("zt","s",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace z to s
tuples=re.findall("\w+z\s",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("z","s",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
# replace ua to w
tuples=re.findall("\wua\w+",ubah)
ganti={}
for tuple in tuples:
ganti[tuple]=re.sub("ua","w",tuple)
for i in ganti.keys():
ubah=re.sub(i,ganti[i],ubah)
ubah=re.sub("ie","i",ubah)
ubah=re.sub("q|x","k",ubah)
ubah=re.sub("b\s|b$","p ",ubah)
#Kalo kitab
ubah=re.sub("dh\s|dh$","t ",ubah)
ubah=re.sub("oe","u",ubah)
ubah=re.sub("nk\s|nk$","ng ",ubah)
ubah=re.sub("aw\s|aw$","au ",ubah)
ubah=re.sub("\su\s"," kamu ",ubah)
ubah=re.sub("\saj\s|\sja\s"," saja ",ubah)
# cek kamus
ubah1=ubah
for word in ubah1.split():
if word in kamus.keys():
ubah = re.sub(word,kamus[word],ubah)
#ubah = re.sub(kamus.keys(),kamus.values(), ubah)
return ubah
def extraction_location(sentences):
with open("D:/tasya/python/code/Geo-Tag/corpus/triger-word.csv") as file:
reader = csv.reader(file)
reader.next()
corpus = []
for column in reader:
corpus.append(column[0])
file_stopwords = csv.reader(open("D:/tasya/python/code/Geo-Tag/corpus/stopword_twitter.csv"))
stopwords = []
for column in file_stopwords:
stopwords.append(column[0])
file_stopwords_formal = csv.reader(open("D:/tasya/python/code/Geo-Tag/corpus/stopwords.csv"))
for column in file_stopwords_formal:
stopwords.append(column[0])
list_of_words = sentences.split()
location = []
for word in list_of_words:
if word in corpus:
location.append(sentences.split(word)[0])
location.append(sentences.split(word)[1])
sentences = sentences.split(word)[1]
for i, word in enumerate(location):
if len(word.split()) <=2:
word = word.replace(' ','')
location.pop(i)
location.insert(i,word)
for i in range(len(location)):
if (2*i)+1 < len(location)-1:
location.pop((2*i)+1)
location.insert((2*i)+1,"")
#stopwords
for i, word0 in enumerate(location):
for word1 in word0.split():
if word1 in stopwords:
word0 = re.sub(word1,"",word0)
location.pop(i)
location.insert(i,word0)
location_out = []
for word in location:
if len(word.split()) > 0:
location_out.append(' '.join(word.split()))
if len(location_out) == 0:
location = "Location not found"
else:
location = location_out
return location
def extraction_location_corpus(sentences):
with open("D:/tasya/python/code/Geo-Tag/corpus/lokasilibrary.csv") as file:
reader = csv.reader(file)
reader.next()
corpus_location = []
for column in reader:
corpus_location.append(column[0].lower())
for word in sentences.split():
if word in corpus_location:
location = word
#print location
else:
location = "Location not found"
return location
def local_langdetect(sentence):
with open("D:/tasya/Project/GeoTag-Pulse Lab/kamus bahasa daerah/kamus fix/kamus jawa tanpa terjemahan part1.txt") as file:
jawa = file.read().split("\n")
with open("D:/tasya/Project/GeoTag-Pulse Lab/kamus bahasa daerah/kamus fix/kamus minang tanpa terjemahan part1.txt") as file:
minang = file.read().split("\n")
with open("D:/tasya/Project/GeoTag-Pulse Lab/kamus bahasa daerah/kamus fix/kamus sunda tanpa terjemahan part1.txt") as file:
sunda = file.read().split("\n")
list_word = sentence.split()
score_jawa = 0
score_minang = 0
score_sunda = 0
for word in list_word:
if word in jawa:
score_jawa +=1
if word in minang:
score_minang += 1
if word in sunda:
score_sunda +=1
if score_jawa > score_minang and score_jawa>score_sunda:
lang = "jawa"
elif score_minang > score_jawa and score_minang>score_sunda:
lang = "minang"
elif score_sunda > score_minang and score_sunda>score_jawa:
lang = "sunda"
elif score_jawa == score_sunda:
lang = "sunda atau jawa"
elif score_jawa ==score_minang:
lang = "jawa atau minang"
elif score_sunda == score_minang:
lang = "sunda atau minang"
else:
lang = "Bahasa tidak terdeteksi"
return lang
def ngram_sentences(sentences,nc):
words=re.sub("\'","",sentences)
words=re.sub("[^a-zA-Z]"," ",words)
words=words.split()
nw=len(words)
ngr=nw-nc+1
ng=[]
if(nc>1):
for i in range(ngr):
ng.append(" ".join(words[i:(i+nc)]))
return ng
elif(nc==1):
return words
if __name__ =='__main__':
"""
Ekstraksi Lokasi based on tweet
"""
sentences = "mau pergi skrg ke seminar di jkt coz udah mepEt bangedh"
sentences = alay_normalizer(sentences)
sentences = abbreviation(sentences)
if extraction_location_corpus(sentences) != "Location not found":
location = extraction_location_corpus(sentences)
elif extraction_location(sentences) !="Location not found":
location = extraction_location(sentences)
else:
location = local_langdetect(sentences)
print location
"""
get long lat from geocoder
"""
address_longlat = []
for address in location:
g = geocoder.google(address)
list_longlat = g.latlnga
list_longlat.insert(0,address)
address_longlat.append(list_longlat)
print address_longlat
"""
get long lat from data POI using Ngram
"""
with open("D:/tasya/python/code/Geo-Tag/corpus/sample-poi1.csv") as file:
reader = csv.reader(file)
#reader.next()
corpus = []
for row in reader:
corpus.append(row[0])
corpus_name = []
for word in corpus:
corpus_name.append(word.split(';')[0])
address = []
G = NGram(corpus_name)
G_latlng = NGram(corpus)
for word in location:
out = G.search(word)
out1 = G_latlng.append(out[0][0])
address.append(out1[0][0])