Repository navigation
Expand file tree
/
Copy pathcompareTFIDF.py
More file actions
244 lines (195 loc) · 7.73 KB
/
Copy pathcompareTFIDF.py
File metadata and controls
244 lines (195 loc) · 7.73 KB
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
import re
import nltk
import optuna
import pandas as pd
import simplemma
import torch
import spacy
import stanza
from nltk import RegexpTokenizer
from nltk.corpus import stopwords, wordnet
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
REGEX_NUMBERS = r"[^\W_]+"
REGEX_LETTERS = r"[a-záéíóúñü]+" # para no incluir numeros
RANDOM_STATE = 777
SEEDS = [101, 202, 303]
TEST_SIZE = 0.2
N_PAIRS = 15
N_TRIALS = 5
TRAIN_ITERATIONS = 500
EPSILON = 1e-5
FIXED_ALPHA = 0.01
optuna.logging.set_verbosity(optuna.logging.WARNING)
# Descargar recursos de NLTK.
nltk.download("punkt", quiet=True) # Tokenizador
nltk.download("punkt_tab", quiet=True) # Tokenizador
nltk.download("stopwords", quiet=True) # Stopwords
nltk.download("wordnet", quiet=True) # Synsets
nltk.download("omw-1.4", quiet=True) # Lematizador
# Descargar recursos de Spacy.
spacy_nlp = spacy.load("es_core_news_lg")
# Descargar recursos de Stanza.
stanza_nlp = stanza.Pipeline(
"es",
processors="tokenize,lemma",
use_gpu=True
)
dataset = Dataset("FEINA_1.xlsx").read()
dataset.corpus = dataset.corpus[:N_PAIRS * 2]
dataset.labels = dataset.labels[:N_PAIRS * 2]
# Stopwords
STOP_WORDS_ES = set(stopwords.words("spanish"))
# Tokenizadores
def tokenize_nltk(text, regex=None):
return word_tokenize(text.lower(), language="spanish") # permite $ %
def tokenize_regex(text, regex):
return RegexpTokenizer(regex).tokenize(text.lower())
# Lematizadores
def nltk_lemmatizer(token):
synsets = wordnet.synsets(token, lang="spa")
if synsets:
lemmas = synsets[0].lemma_names("spa")
if lemmas:
return lemmas[0].lower().replace("_", " ")
return token
def simplemma_lemmatizer(token):
return simplemma.lemmatize(token, lang="es")
# Preprocesadores
def preprocess(text, tokenizer, lemmatizer, regex=None):
tokens = tokenizer(text, regex)
return [
lemmatizer(token)
for token in tokens
if token not in STOP_WORDS_ES
]
def preprocess_spacy(text, mode):
doc = spacy_nlp(text)
lemmas = []
for token in doc:
lemma = token.lemma_.lower()
if mode == 0:
if (token.is_alpha or token.like_num) and not token.is_stop:
lemmas.append(lemma)
elif mode == 1: # permite $ %
if not token.is_space and not token.is_stop:
lemmas.append(lemma)
return lemmas
def preprocess_stanza(text, mode, regex):
doc = stanza_nlp(text)
lemmas = []
for sentence in doc.sentences:
for token in sentence.words:
lemma = token.lemma.lower()
if mode == 0:
if re.fullmatch(regex, lemma) and lemma not in STOP_WORDS_ES:
lemmas.append(lemma)
elif mode ==1: # permite $ %
if lemma not in STOP_WORDS_ES:
lemmas.append(lemma)
return lemmas
def split_data(matrix, labels, seed):
labels_tensor = torch.tensor(labels, dtype=torch.float32).view(-1, 1)
data_tensor = torch.tensor(matrix.toarray(), dtype=torch.float32)
x_train, x_test, y_train, y_test = train_test_split(
data_tensor,
labels_tensor,
test_size=TEST_SIZE,
random_state=seed,
stratify=labels,
)
x_train = x_train.to(DEVICE)
x_test = x_test.to(DEVICE)
y_train = y_train.to(DEVICE)
y_test = y_test.to(DEVICE)
return x_train, x_test, y_train, y_test
def train_regression(split, alpha, seed):
x_train, x_test, y_train, y_test = split
torch.manual_seed(seed)
model = TrainRegression()
_, history_error = model.train(
[x_train, y_train],
iterations=TRAIN_ITERATIONS,
epsilon=EPSILON,
alpha=alpha,
)
mae = model.medium_absolute_error([x_test, y_test]).item()
accuracy = model.accuracy([x_test, y_test]).item()
return mae, accuracy, len(history_error)
def alpha_range(split):
x_train, _, _, _ = split
data = TrainRegression.transformData(x_train)
gram = (data.T @ data) / data.shape[0]
lipschitz = torch.linalg.eigvalsh(gram).max().item() / 4
alpha_max = 1 / lipschitz
alpha_min = alpha_max / 1000
return alpha_min, alpha_max, lipschitz
def calibrate_alpha(split, seed):
alpha_min, alpha_max, _ = alpha_range(split)
def objective(trial):
alpha = trial.suggest_float("alpha", alpha_min, alpha_max, log=True)
mae, _, _ = train_regression(split, alpha, seed)
return mae
sampler = optuna.samplers.TPESampler(seed=seed)
study = optuna.create_study(direction="minimize", sampler=sampler)
study.optimize(objective, n_trials=N_TRIALS)
return study.best_params["alpha"], study.best_value
def test_preprocessor(name, preprocessor):
print(f"\n{'=' * 100}")
print(f"Preprocesador: {name}")
print("\nEjemplos de tokens:")
corpus_tokens = [preprocessor(text) for text in dataset.corpus]
for i, tokens in enumerate(corpus_tokens[:3], start=1):
print(f"{i}. {tokens[:35]}")
vectorizer = TfidfVectorizer(analyzer=lambda tokens: tokens)
matrix = vectorizer.fit_transform(corpus_tokens)
tokens = vectorizer.get_feature_names_out()
token_counts = [len(tokens) for tokens in corpus_tokens]
print("\nResumen TF-IDF:")
print("Documentos:", len(dataset.corpus))
print("Vocabulario:", len(tokens))
print("Promedio de tokens:", round(sum(token_counts) / len(token_counts), 3))
print("Maximo de tokens:", max(token_counts))
rows = []
for seed in SEEDS:
split = split_data(matrix, dataset.labels, seed)
alpha_min, alpha_max, lipschitz = alpha_range(split)
fixed_mae, fixed_accuracy, _ = train_regression(split, FIXED_ALPHA, seed)
tuned_alpha, _ = calibrate_alpha(split, seed)
tuned_mae, tuned_accuracy, tuned_iterations = train_regression(split, tuned_alpha, seed)
rows.append({
"seed": seed,
"lipschitz": round(lipschitz, 6),
"alpha_min": round(alpha_min, 6),
"alpha_max": round(alpha_max, 6),
"alpha_fijo": FIXED_ALPHA,
"mae_fijo": round(fixed_mae, 6),
"accuracy_fijo": round(fixed_accuracy, 6),
"alpha_calibrado": round(tuned_alpha, 6),
"iteraciones": tuned_iterations,
"mae_calibrado": round(tuned_mae, 6),
"accuracy_calibrado": round(tuned_accuracy, 6),
})
print("\nResultados por seed:")
print(pd.DataFrame(rows).to_string(index=False))
return rows
preprocessors = [
("nltk modo nltk", lambda text: preprocess(text, tokenize_nltk, nltk_lemmatizer)),
("nltk modo regex con numeros", lambda text: preprocess(text, tokenize_regex, nltk_lemmatizer, REGEX_NUMBERS)),
("nltk modo regex sin numeros", lambda text: preprocess(text, tokenize_regex, nltk_lemmatizer, REGEX_LETTERS)),
("regex con numeros + simplemma", lambda text: preprocess(text, tokenize_regex, simplemma_lemmatizer, REGEX_NUMBERS)),
("regex sin numeros + simplemma", lambda text: preprocess(text, tokenize_regex, simplemma_lemmatizer, REGEX_LETTERS)),
("spacy lg modo estandar", lambda text: preprocess_spacy(text, 0)),
("spacy lg modo permisivo", lambda text: preprocess_spacy(text, 1)),
("stanza modo estandar con numeros", lambda text: preprocess_stanza(text, 0, REGEX_NUMBERS)),
("stanza modo estandar sin numeros", lambda text: preprocess_stanza(text, 0, REGEX_LETTERS)),
("stanza modo permisivo con numeros", lambda text: preprocess_stanza(text, 1, REGEX_NUMBERS)),
("stanza modo permisivo sin numeros", lambda text: preprocess_stanza(text, 1, REGEX_LETTERS))
]
def main():
print("N_TRIALS:", N_TRIALS)
rows = []
for name, preprocessor in preprocessors:
rows.extend(test_preprocessor(name, preprocessor))
if __name__ == "__main__":
main()