spanish-quechua-detector / classifier.py
marcossalinas
First commit
f7cb5b1
import re
# import pickle
class Classifier:
def __init__(self, dict_reemplazo, ngram_vectorizer, transformer, svm_model) -> None:
self.dict_reemplazo = dict_reemplazo
self.ngram_vectorizer = ngram_vectorizer
self.transformer = transformer
self.svm_model = svm_model
def reemplazar_caracteres_diferentes(self, texto, dictionary):
return texto.translate(dictionary)
def eliminar_ruido(self, texto, caracteres):
nuevo_texto = texto
for c in caracteres:
nuevo_texto = re.sub(c, '', nuevo_texto)
return nuevo_texto
def eliminar_espacios(self, string):
nuevo_string = string.strip()
nuevo_string = ' '.join(nuevo_string.split())
return nuevo_string
def predict(self, npt_txt):
txt = self.eliminar_espacios(
self.eliminar_ruido(
self.reemplazar_caracteres_diferentes(
self.eliminar_espacios(
self.eliminar_ruido(npt_txt, [r'[^\w\s^\´\’]'])), self.dict_reemplazo), [r'\d+', '_']))
vctr = self.transformer.transform(self.ngram_vectorizer.transform([txt]))
return 'Español' if self.svm_model.predict(vctr)[0] == 0 else 'Quechua'
# if __name__ == '__main__':
# with open('dict_reemplazo', 'rb') as f:
# dict_reemplazo = pickle.load(f)
# with open('ngram_vectorizer', 'rb') as f:
# ngram_vectorizer = pickle.load(f)
# with open('transformer', 'rb') as f:
# transformer = pickle.load(f)
# with open('svm_model', 'rb') as f:
# svm_model = pickle.load(f)
# classifier = Classifier(dict_reemplazo, ngram_vectorizer, transformer, svm_model)
# with open('classifier.pickle', 'wb') as f:
# pickle.dump(classifier, f)
# with open('classifier.pickle', 'rb') as f:
# my_classifier = pickle.load(f)
# for txt in ['¿Maytaq ashkallanchikega', 'Entonces el Inka dijo ¡Mach\'a!', '¡Aragan kanki wamraqa', 'Señora, ¿yanapariwayta atiwaqchu?', '¿A dónde vas?', '324#@$%']:
# print (my_classifier.predict(txt))