-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNLPreprocessor.py
237 lines (168 loc) · 5.91 KB
/
NLPreprocessor.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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 30 01:20:50 2020
@author: Eduardo Vicente and Isabel Carvalho
"""
from wordspell_corrector import *
from NLPyPort.FullPipeline import *
import pandas as pd
import string
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize as tokenize
import spacy
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import floresta
import joblib
from nltk import word_tokenize
question_words = ['que','quanto','quantos','cujo','cujos','quem','onde','quando','como','por','qual','quais','para','em']
def find_greeting(words):
greetings = ['olá','ola', 'oi', 'boas', 'hey', 'viva', 'saudacoes','saudações',
'cumprimentos']
greeting = 0
idx=-1
for w in words:
if w.lower() in greetings:
greeting = 1
idx = words.index(w)
break
return greeting,idx
def find_thanks(words):
thanks = ['obrigado', 'obrigada', 'grato', 'grata', 'agradecido',
'agradecida', 'brigado', 'brigada', 'ty', 'thanks',
'thank you', 'gracias', 'agradecimentos']
thank = 0
idx = -1
for w in words:
if w.lower() in thanks:
thank = 1
idx = words.index(w)
break
return thank,idx
def remove_noise(words):
punctuation = string.punctuation
white_space = string.whitespace
endoffile = 'EOS'
filtered = []
for w in words:
if w is not endoffile and w not in punctuation and w not in white_space:
filtered.append(w)
return filtered
def remove_stopwords(words):
filtered = []
# print("STOP:\n")
# print(words)
stop_words = set(stopwords.words('portuguese'))
# print(stop_words)
for w in words:
# print(w+"\n")
if w.lower() not in question_words:
if w not in stop_words:
filtered.append(w)
else:
filtered.append(w)
return filtered
def part_of_speech(words,nlp):
pos_tag = []
# using spacy pt
for w in words:
for token in nlp(w):
#valor a nivel semantico das palavras
pos = token.pos_
pos_tag.append(pos)
return pos_tag
def stemming_lemmatization(words,nlp):
filtered = []
for w in words:
for token in nlp(w):
# encontra root de cada palavra
w = token.lemma_
filtered.append(w)
return filtered
def bag_of_words(words):
vectorizer = CountVectorizer()
# encontra o numero de vezes que cada palavra e usada
bag = vectorizer.fit_transform(words)
# valor da coluna 0 e o indice da primeira ocorrencia
df = pd.DataFrame(bag.todense(), columns=vectorizer.get_feature_names())
# print(df)
return df
def autocorrect(tokens):
arr = []
print(tokens)
for t in tokens:
if t.isalpha() and t != "EOS":
arr.append(correction(t))
print(arr)
return arr
def NLPyPort_transform(msg):
options = {
"tokenizer" : True,
"pos_tagger" : True,
"lemmatizer" : True,
"entity_recognition" : True,
"np_chunking" : True,
"pre_load" : False,
"string_or_array" : True
}
text = new_full_pipe(msg,options=options)
dic_pos = dict()
tags = text.pos_tags
#SAVE POS_TAGS POSITIONS
for i in range(len(tags)):
dic_pos[text.lemas[i]] = tags[i]
#PRINT PARAMETERS FROM TEXT:
# if(text!=0):
# print("TOKENS: "+ str(text.tokens)+" \n")
# print("POS_TAG: "+ str(text.pos_tags)+" \n")
# print("ENTITIES: "+ str(text.entities)+" \n")
# print("NP_TAGS: "+ str(text.np_tags)+" \n")
filtered = autocorrect(text.tokens)
#FIND GREETING WORDS ->
[greeting,idx_g] = find_greeting(filtered)
#FIND THANKFULL WORDS ->
[thanks,idx_t] = find_thanks(filtered)
filtered = text.lemas
#REMOVE PONCTUATION NOISE and EOS
filtered = remove_noise(filtered)
#REMOVE STOPWORDS
filtered = remove_stopwords(filtered)
arr_tag = []
for i in range(len(filtered)):
if filtered[i] in list(dic_pos.keys()):
arr_tag.append(dic_pos[filtered[i]])
return [greeting,thanks,filtered,arr_tag]
def Spacy_NLP_transform(msg):
##RETURN A TUPLE LIKE -> (greetings,thanks,[vector of tokens],[pos_tag],extra_info (target))
nlp = spacy.load('C:\\Users\\Eduardo Vicente\\Anaconda3\\Lib\\site-packages\\pt_core_news_lg\\pt_core_news_lg-2.3.0')
#TOKENIZER -TOKENIZE WORDS/ELEMENTS IN THE MESSAGE STATMENT
tokens = tokenize(msg)
# print(tokens)
#FIND GREETINGS ->
[greeting,idx_g] = find_greeting(tokens)
if greeting == 1:
tokens.pop(idx_g)
#FIND THANKFULL WORDS ->
[thanks,idx_t] = find_thanks(tokens)
if thanks == 1:
tokens.pop(idx_t)
# REMOVE WORDS ^ INDEX (GREETING OR THANKS) -> WE NEED TO PASS THIS INFORMATION FOR THE MSG CONSTRUCTOR
#REMOVE WHITESPACES AND PONCTUACTION ELEMENTS
filtered = remove_noise(tokens)
#REMOVE STOPWORDS
filtered = remove_stopwords(filtered)
#STEMMING & LEMMATIZATION
filtered = stemming_lemmatization(filtered,nlp)
#PART-OF-SPEECH TAG POS
pos_tag = part_of_speech(filtered,nlp)
print(pos_tag)
#BAG OF WORDS
# bagwords = bag_of_words(filtered)
return [greeting,thanks,filtered,pos_tag]
def preprocess_msg(msg,type_nlp):
if type_nlp:
return NLPyPort_transform(msg)
else:
return Spacy_NLP_transform(msg)
# print(arr_tag)
# preprocess_msg("Ola, estou a procura de uma camisola para a minha filha.")