chap0lin commited on
Commit
8034507
1 Parent(s): 0078548

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -36
app.py CHANGED
@@ -93,34 +93,45 @@ def remove_stopwords(text, is_lower_case=False, stopwords=None):
93
  return filtered_text
94
 
95
 
96
- def pre_process():
97
- opo_texto_sem_caracteres_especiais = (remove_accented_chars(sentence))
98
- sentenceExpanded = contractions.fix(opo_texto_sem_caracteres_especiais)
99
- sentenceWithoutPunctuation = remove_special_characters(sentenceExpanded , remove_digits=True)
100
- sentenceLowered = sentenceWithoutPunctuation.lower()
101
- sentenceLemmatized = spacy_lemmatize_text(sentenceLowered)
102
- sentenceLemStopped = remove_stopwords(sentenceLemmatized, is_lower_case=False)
103
-
104
- return nltk.word_tokenize(sentenceLemStopped)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  def classify(df, new_column = True):
107
  sentencesMCTIList_xp8 = df['opo_pre_tkn']
108
 
109
- print("Dados da planilha adquiridos")
110
-
111
  formatted_sentences = []
112
  for sentence in sentencesMCTIList_xp8:
113
  formatted_sentences.append(json.loads(sentence.replace("'",'"')))
114
- # del sentencesMCTIList_xp8
115
 
116
- print(sentencesMCTIList_xp8[0])
117
- print("##########################")
118
- print(formatted_sentences[0][0])
119
-
120
- print("Transformado em W2V")
121
  words = list(reloaded_w2v_model.wv.vocab)
122
  item_shape = np.shape(reloaded_w2v_model.wv[words[0]])
123
- # print(formatted_sentences)
124
 
125
  MCTIinput_vector = []
126
  for sentence in formatted_sentences:
@@ -132,12 +143,10 @@ def classify(df, new_column = True):
132
  aux_vector.append(np.zeros(item_shape))
133
  MCTIinput_vector.append(aux_vector)
134
  del formatted_sentences
135
- print("Convertido W2V")
136
  MCTIinput_padded = pad_sequences(MCTIinput_vector, maxlen=2726, padding='pre')
137
  del MCTIinput_vector
138
- print("Sentenças com Padding")
139
- print(len(MCTIinput_padded))
140
- print(len(MCTIinput_padded[0]))
141
  predictions = reconstructed_model_CNN.predict(MCTIinput_padded)
142
  del MCTIinput_padded
143
  print(predictions)
@@ -148,6 +157,9 @@ def classify(df, new_column = True):
148
  del predictions
149
 
150
  df['classification'] = cleaned_up_predictions
 
 
 
151
  return df
152
 
153
  def gen_output(data):
@@ -166,27 +178,18 @@ def app(operacao, resultado, dados):
166
  data = pd.read_excel(dados)
167
  print("Dados Carregados!")
168
 
169
- # boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
170
- # 'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle'],
171
- # 'Price': [10,15,5,5,10,15,15,5]
172
- # }
173
- # df = pd.DataFrame(boxes, columns= ['Color','Shape','Price'])
174
- # data.to_excel("output.xlsx")
175
- # return "output.xlsx"
176
-
177
  if operacao == "Pré-processamento + Classificação" :
178
- pre_process()
179
- classify(resultado == "Nova Coluna")
180
- output = gen_output()
181
 
182
  return output
183
  elif operacao == "Apenas Pré-processamento" :
184
- pre_process()
185
- output = gen_output()
186
 
187
  return output
188
  elif operacao == "Apenas Classificação" :
189
- print("Apenas Classificação Selecionado!")
190
  df = classify(data, resultado == "Nova Coluna")
191
  output = gen_output(df)
192
 
 
93
  return filtered_text
94
 
95
 
96
+ def pre_process(df):
97
+ opo_texto_data = df['opo_texto']
98
+ opo_texto_ele_data = df['opo_texto_ele']
99
+ opo_texto_final = []
100
+
101
+ for i in range(len(opo_texto_data)):
102
+ if opo_texto_data[i] == opo_texto_ele_data[i]:
103
+ opo_texto_final.append(opo_texto_data[i])
104
+ elif pd.isna(opo_texto_ele_data[i]):
105
+ opo_texto_final.append(opo_texto_data[i])
106
+ elif len(nltk.word_tokenize(opo_texto_data[i])) < 4000:
107
+ opo_texto_final.append(opo_texto_data[i]+". "+opo_texto_ele_data[i])
108
+ else:
109
+ opo_texto_final.append(opo_texto_data[i])
110
+
111
+ pre_processed_data = []
112
+ for opo in opo_texto_final:
113
+ opo_texto_sem_caracteres_especiais = (remove_accented_chars(opo))
114
+ sentenceExpanded = contractions.fix(opo_texto_sem_caracteres_especiais)
115
+ sentenceWithoutPunctuation = remove_special_characters(sentenceExpanded , remove_digits=True)
116
+ sentenceLowered = sentenceWithoutPunctuation.lower()
117
+ sentenceLemmatized = spacy_lemmatize_text(sentenceLowered)
118
+ sentenceLemStopped = remove_stopwords(sentenceLemmatized, is_lower_case=False)
119
+ sentenceTokenized = nltk.word_tokenize(sentenceLemStopped)
120
+ pre_processed_data.append(sentenceTokenized)
121
+
122
+ df['opo_pre_tkn'] = pre_processed_data
123
+ return df
124
 
125
  def classify(df, new_column = True):
126
  sentencesMCTIList_xp8 = df['opo_pre_tkn']
127
 
 
 
128
  formatted_sentences = []
129
  for sentence in sentencesMCTIList_xp8:
130
  formatted_sentences.append(json.loads(sentence.replace("'",'"')))
131
+ del sentencesMCTIList_xp8
132
 
 
 
 
 
 
133
  words = list(reloaded_w2v_model.wv.vocab)
134
  item_shape = np.shape(reloaded_w2v_model.wv[words[0]])
 
135
 
136
  MCTIinput_vector = []
137
  for sentence in formatted_sentences:
 
143
  aux_vector.append(np.zeros(item_shape))
144
  MCTIinput_vector.append(aux_vector)
145
  del formatted_sentences
146
+
147
  MCTIinput_padded = pad_sequences(MCTIinput_vector, maxlen=2726, padding='pre')
148
  del MCTIinput_vector
149
+
 
 
150
  predictions = reconstructed_model_CNN.predict(MCTIinput_padded)
151
  del MCTIinput_padded
152
  print(predictions)
 
157
  del predictions
158
 
159
  df['classification'] = cleaned_up_predictions
160
+ if not new_column:
161
+ df = df.loc[df['classification'] == 1]
162
+
163
  return df
164
 
165
  def gen_output(data):
 
178
  data = pd.read_excel(dados)
179
  print("Dados Carregados!")
180
 
 
 
 
 
 
 
 
 
181
  if operacao == "Pré-processamento + Classificação" :
182
+ df = pre_process(data)
183
+ df = classify(df, resultado == "Nova Coluna")
184
+ output = gen_output(df)
185
 
186
  return output
187
  elif operacao == "Apenas Pré-processamento" :
188
+ df = pre_process(data)
189
+ output = gen_output(df)
190
 
191
  return output
192
  elif operacao == "Apenas Classificação" :
 
193
  df = classify(data, resultado == "Nova Coluna")
194
  output = gen_output(df)
195