-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamobjsubj.py
146 lines (91 loc) · 3.59 KB
/
streamobjsubj.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
from read_info import load_file
import streamlit as st
import pandas as pd
from collections import Counter
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import spacy
from sklearn.naive_bayes import GaussianNB
from sklearn.feature_extraction.text import CountVectorizer
from sklearn import svm
from sklearn import preprocessing
from models import train_svm, plot_learning_curve, learning_curves_preprocess, test_cases
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from collections import Counter
from joblib import dump, load
import os
# from models import train_svm
from bert_serving.client import BertClient
def to_obj_sub(value):
if value == 'Objetivo':
return value
return 'Subjetivo'
def generate_dataframes(type, data):
yudy_rule = st.checkbox("Correct match", key='correctMatch')
all = pd.DataFrame([{'text': values.text, 'ans': values.answers[0].answer} for values in data])
# This is incorrect asumtion
if yudy_rule:
data = [value for value in data if max(Counter((v.answer for v in value.answers)).values())>=2 ]
if 'Todo' == type:
return all, pd.DataFrame([{'text': values.text, 'ans': values.answers[0].answer} for values in data])
if 'Objetivo - Subjetivo' == type:
return all, pd.DataFrame([{'text': values.text, 'ans': to_obj_sub(values.answers[0].answer)} for values in data])
if "Positivo - Neutro - Negativo":
return all, pd.DataFrame([{'text': values.text, 'ans': values.answers[0].answer} for values in data if values.answers[0].answer != 'Objetivo'])
raise Exception(f"type is {type}")
file_path = st.text_input('Path', 'saved.json')
data = load_file(file_path)
types = ['Todo', "Positivo - Neutro - Negativo", 'Objetivo - Subjetivo']
t = st.selectbox('Tipo de dato', types, key='dataselector')
all_df, df = generate_dataframes(t,data)
df
d = df.describe()
d
gb = df.groupby('ans').count()
gb
labels = gb.index
values = gb.text
pieAns = go.Figure(data=[go.Pie(labels=labels, values=values)])
st.plotly_chart(pieAns)
le = preprocessing.LabelEncoder()
Y = le.fit_transform(df.ans)
st.markdown('****')
if st.checkbox('Count Vectorizer'):
st.title('Models with count vectorizer')
@st.cache(allow_output_mutation=True)
def vectorizeTexts(texts):
cv = CountVectorizer()
X = cv.fit_transform(texts)
X = X.toarray()
return cv, X
cv, X = vectorizeTexts(all_df.text)
X = cv.transform(df.text).toarray() # NOTE: Está transformada a densa
cv_model_name = st.text_input('Model Name', 'countvectorizer.joblib')
dump(cv, os.path.join('models', cv_model_name))
title = 'LinearSVC'
test_cases(f'CountVectorizer {t}', X, Y, svm.LinearSVC(), le)
if st.checkbox('SpacyVect'):
@st.cache(allow_output_mutation=True)
def vectorizeTextsSpacy(texts):
@st.cache(allow_output_mutation=True)
def load_spacy():
return spacy.load('es')
nlp = load_spacy()
X = []
for t in texts:
X.append(nlp(t).vector)
return X
X= vectorizeTextsSpacy(df.text)
st.title('Models with Spacy tensor')
test_cases(f'SpacyVectorizer {t}', X, Y, svm.LinearSVC(), le)
if st.checkbox('BertVect'):
st.title('Models with count BERT')
@st.cache(allow_output_mutation=True)
def vectorizeBERT(texts):
bc=BertClient(ip='10.6.122.217', timeout=30000)
print(bc)
resp = bc.encode(list(texts))
return resp
X= vectorizeBERT(df.text)
test_cases(f'Bert {t}', X, Y, svm.LinearSVC(), le)