File size: 2,137 Bytes
f7cb5b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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))