-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNN.py
222 lines (202 loc) · 8.4 KB
/
KNN.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
import re
from email import header
import nltk
import csv
from builtins import list
from matplotlib.dates import num2date
def Limpieza_KNN():
sno = nltk.stem.SnowballStemmer('spanish')
listaemojis =[':)', ':-)', '=)', ':]', ':D', ':-D', '=D', '>:o', '>:-o', ':o', ':-o ', ':(', ':-(', '=(', ':[ ',
';)', ';-) ', ':( ', ':*', ':-* ', ':p', ':-p ', '>:(', '>:-( ', '<3 ', ':3 ', '^_^ ', '-_- ', 'O:)',
'O:-) ', '3:)', '3:-) ', ':v ', ':|]', '8)', '8-)', 'B)', '8-) ', '8|', '8-|', 'B|', 'B-|', ':/', ':-/',
'o.O', 'O.o', ':‐)' , ':-]' ,':]' , ':-3' , ':3' , ':->' , ':>' , '8-)' , '8)' , ':-}' , ':}' ,
':o)' , ':c)' , ':^)' , '=]' , '=)' , ':D' , 'B^D' , 'xD' , '8D' , 'x‐D' , '8‐D' , 'X‐D' ,
':‐D' , '=D' , '=3' , 'XD' , ':-))' , ':‐(' , ':(' , ':‐c' , ':c' , ':‐<' , ':<' , ':‐[' ,
':[' , ':-||' , '>:[' , ':{' , ':@' , '>:(' , 'D:<' , 'D:' , 'D8' , 'D;' , 'D=' , 'DX' ,
':‐O' , ':O' , ':‐o' , ':o' , ':-0' , '8‐0' , '>:O' , ':-*' , ':*' , ':×' , ';‐)' , ';)' ,
'*-)' , '*)' , ';‐]' , ';]' , ';^)' , ':‐,' , 'D' , ':‐P' , ':P' , ':‐p' , ':p' , ':‐b' ,
':b' , '=p' , 'd:' , ':S' , '=/' , '=/' , 'O:‐)' , 'O:)' , '|;‐)' , '</3' , '<3' ]
listaemojis2 =[':)', ':-)', '=)', ':]', ':d', ':-d', '=d', '>:o', '>:-o', ':o', ':-o ', ':(', ':-(', '=(', ':[ ',
';)', ';-) ', ':( ', ':*', ':-* ', ':p', ':-p ', '>:(', '>:-( ', '<3 ', ':3 ', '^_^ ', '-_- ', 'o:)',
'o:-) ', '3:)', '3:-) ', ':v ', ':|]', '8)', '8-)', 'b)', '8-) ', '8|', '8-|', 'b|', 'b-|', ':/', ':-/',
'o.o', 'o.o', ':‐)' , ':-]' ,':]' , ':-3' , ':3' , ':->' , ':>' , '8-)' , '8)' , ':-}' , ':}' ,
':o)' , ':c)' , ':^)' , '=]' , '=)' , ':d' , 'b^d' , 'xd' , '8d' , 'x‐d' , '8‐d' , 'x‐d' ,
':‐d' , '=d' , '=3' , 'xd' , ':-))' , ':‐(' , ':(' , ':‐c' , ':c' , ':‐<' , ':<' , ':‐[' ,
':[' , ':-||' , '>:[' , ':{' , ':@' , '>:(' , 'd:<' , 'd:' , 'd8' , 'd;' , 'd=' , 'dx' ,
':‐o' , ':o' , ':‐o' , ':o' , ':-0' , '8‐0' , '>:o' , ':-*' , ':*' , ':×' , ';‐)' , ';)' ,
'*-)' , '*)' , ';‐]' , ';]' , ';^)' , ':‐,' , 'd' , ':‐p' , ':p' , ':‐p' , ':p' , ':‐b' ,
':b' , '=p' , 'd:' , ':s' , '=/' , '=/' , 'o:‐)' , 'o:)' , '|;‐)' , '</3' , '<3' ]
emoticons_str = r"""
(?:
[:=;] # Eyes
[oO\-]? # Nose (optional)
[D\)\]\(\]/\\OpP] # Mouth
)"""
regex_str = [
emoticons_str,
r'<[^>]+>', # HTML tags
r'(?:@[\w_]+)', # @-mentions
r"(?:\#+[\w_]+[\w\'_\-]*[\w_]+)", # hash-tags
r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+', # URLs
r'(?:(?:\d+,?)+(?:\.?\d+)?)', # numbers
r"(?:[a-z][a-z'\-_]+[a-z])", # words with - and '
r'(?:[\w_]+)', # other words
r'(?:\S)' # anything else
]
tokens_re = re.compile(r'(' + '|'.join(regex_str) + ')', re.VERBOSE | re.IGNORECASE)
emoticon_re = re.compile(r'^' + emoticons_str + '$', re.VERBOSE | re.IGNORECASE)
def tokenize(s):
return tokens_re.findall(s)
def preprocess(s, lowercase=False):
tokens = tokenize(s)
if lowercase:
tokens = [token if emoticon_re.search(token) else token.lower() for token in tokens]
return tokens
def es_numero(n):
try:
float(n)
except ValueError:
return False
return True
def procesartexto(tweet):
listatokenizada = preprocess(tweet)
listatokenizada = list(set(listatokenizada))
listaEliminar=[]
for i in range(len(listatokenizada)):
if es_numero(listatokenizada[i]):
listaEliminar.append(listatokenizada[i])
if len(listatokenizada[i])<=1:
listaEliminar.append(listatokenizada[i])
if listatokenizada[i].startswith('@'):
listaEliminar.append(listatokenizada[i])
if listatokenizada[i].startswith('x'):
listaEliminar.append(listatokenizada[i])
if listatokenizada[i].startswith('#'):
listaEliminar.append(listatokenizada[i])
if listatokenizada[i].startswith('http'):
listaEliminar.append(listatokenizada[i])
if listatokenizada[i] in str(stopwords):
listaEliminar.append(listatokenizada[i])
if listatokenizada[i] in listaemojis2:
listaEliminar.append(listatokenizada[i])
listaEliminar= list(set(listaEliminar))
textoprocesado = ''
for x in range(len(listaEliminar)):
listatokenizada.remove(listaEliminar[x])
for j in range(len(listatokenizada)):
textoprocesado = textoprocesado + ' '+ listatokenizada[j]
del listatokenizada, listaEliminar
return textoprocesado
def tokenizar(tweet):
listatokenizada = preprocess(tweet)
return listatokenizada
#hasta aca
C1_callcenter = []
C2_tarifa = []
C3_equipaje = []
C4_servicio = []
C5_sistema = []
C6_otros = []
stopwords = []
with open("D:/Proyecto tesis/data/lista de stopwords.csv", 'r') as my_file:
reader = csv.reader(my_file, delimiter=';')
stopwords = list(reader)
with open("D:/Proyecto tesis/data/Clusters.csv", 'r') as my_file:
reader = csv.reader(my_file, delimiter=';')
lista = list(reader)
NUM =1
for i in lista:
texto = str(i)
texto = str.replace(texto,"'","")
texto = str.replace(texto, "[", "")
texto = str.replace(texto, "]", "")
clusters = list(texto.split(','))
C1_callcenter.append(clusters[0])
C2_tarifa.append(clusters[1])
C3_equipaje.append(clusters[2])
C4_servicio.append(clusters[3])
C5_sistema.append(clusters[4])
C1_callcenter = list(set(C1_callcenter))
C2_tarifa = list(set(C2_tarifa))
C3_equipaje = list(set(C3_equipaje))
C4_servicio = list(set(C4_servicio))
C5_sistema = list(set(C5_sistema))
C6_otros = list(set(C6_otros))
with open("D:/Proyecto tesis/data/Data_svm_id.csv", 'r') as my_file:
reader = csv.reader(my_file, delimiter=';' )
tweets = list(reader)
print(tweets)
tweets.pop(0)
Data_lista =[]
for i in tweets:
tweet = procesartexto(str(i)).upper()
tweet_split = list(tweet.split(' '))
c1 = 0
c2 = 0
c3 = 0
c4 = 0
c5 = 0
c6 = 0
for j in range(len(tweet_split)):
if len(tweet_split[j]) > 3:
if tweet_split[j] in str(C1_callcenter):
c1 = c1 +1
if tweet_split[j] in str(C2_tarifa):
c2 = c2 +1
if tweet_split[j] in str(C3_equipaje):
c3 = c3 +1
if tweet_split[j] in str(C4_servicio):
c4 = c4 +1
if tweet_split[j] in str(C5_sistema):
c5 = c5 +1
mayor = []
mayor.append(c1)
mayor.append(c2)
mayor.append(c3)
mayor.append(c4)
mayor.append(c5)
aux = 0
i=1
for i in range(5):
if aux <= mayor[i]:
pos = i +1
aux = mayor[i]
if aux == 0 :
pos=5
Data_lista.append([tweet , c1, c2, c3 ,c4, c5, 'C' + str(pos)])
NUM = NUM +1
#Data_lista.append([tweet, aux ,str(pos)])
del mayor
for x in C1_callcenter:
if x == ' ':
C1_callcenter.remove(x)
if x == '':
C1_callcenter.remove(x)
for x in C2_tarifa:
if x == ' ':
C2_tarifa.remove(x)
if x == '':
C2_tarifa.remove(x)
for x in C3_equipaje:
if x == ' ':
C3_equipaje.remove(x)
if x == '':
C3_equipaje.remove(x)
for x in C4_servicio:
if x == ' ':
C4_servicio.remove(x)
if x == '':
C4_servicio.remove(x)
for x in C5_sistema:
if x == ' ':
C5_sistema.remove(x)
if x == '':
C5_sistema.remove(x)
for x in Data_lista:
print (x)
with open('D:/Proyecto tesis/data/data_knn.csv', 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL , delimiter =';')
wr.writerow(['Tweet', 'C1', 'C2','C3','C4','C5','CLUSTER'])
#wr.writerow(['Tweet', 'mayor', 'grupo'])
wr.writerows(Data_lista)
print("OKK")