Conjunto de cambios a1c3fea en modelado_topicos para utils/freeling.py
- Fecha y hora:
- 20/08/2015 16:17:31 (hace 9 años)
- Branches:
- master, preprocesamiento, v1.0
- Children:
- 75c9087
- Parents:
- 5b95488
- Fichero:
-
- 1 editado
Leyenda
- No modificado
- Añadido
- Eliminado
-
utils/freeling.py
r5b95488 ra1c3fea 25 25 return output 26 26 27 28 def clean_words(words_fl): 29 words_fl = [w.encode('unicode-escape') for w in words_fl] 30 return [w.replace(u'\xe0'.encode('unicode-escape'),u'a').replace(u'\xe8'.encode('unicode-escape'),u'e').replace(u'\xec'.encode('unicode-escape'),u'i').replace(u'\xf2'.encode('unicode-escape'),u'o').replace(u'\xfa'.encode('unicode-escape'),u'u').replace(u'\xe1'.encode('unicode-escape'),u'a').replace(u'\xe9'.encode('unicode-escape'),u'e').replace(u'\xed'.encode('unicode-escape'),u'i').replace(u'\xf3'.encode('unicode-escape'),u'o').replace(u'\xfa'.encode('unicode-escape'),u'u').replace(u'á',u'a').replace(u'é',u'e').replace(u'Ã',u'i').replace(u'ó',u'o').replace(u'ú',u'u').replace(u'à ',u'a').replace(u'Ú',u'e').replace(u'ì',u'i').replace(u'ò',u'o').replace(u'ù',u'u') for w in words_fl if w not in stopwords.words('spanish') and w not in '*+.,?¿!¡":;-=/$@#ââ()[]{}' and not w.isdigit() and len(w) > 3] 27 def clean_words(w): 28 w = w.encode('unicode-escape') 29 return w.replace(u'\xe0'.encode('unicode-escape'),u'a').replace(u'\xe8'.encode('unicode-escape'),u'e').replace(u'\xec'.encode('unicode-escape'),u'i').replace(u'\xf2'.encode('unicode-escape'),u'o').replace(u'\xfa'.encode('unicode-escape'),u'u').replace(u'\xe1'.encode('unicode-escape'),u'a').replace(u'\xe9'.encode('unicode-escape'),u'e').replace(u'\xed'.encode('unicode-escape'),u'i').replace(u'\xf3'.encode('unicode-escape'),u'o').replace(u'\xfa'.encode('unicode-escape'),u'u').replace(u'á',u'a').replace(u'é',u'e').replace(u'Ã',u'i').replace(u'ó',u'o').replace(u'ú',u'u').replace(u'à ',u'a').replace(u'Ú',u'e').replace(u'ì',u'i').replace(u'ò',u'o').replace(u'ù',u'u') 31 30 32 31 def is_pos(word,pos_list): … … 40 39 if len(indexes) == 0: return word 41 40 42 #if len(indexes) == 0: raise Exception("LA PALABRA NO SE ENCUENTRA EN EL DOCUMENTO: cosa rara!")43 41 index = 1 44 42 complete_word = word … … 71 69 72 70 73 def select_pos( words_fl,pos_list=['V','A','N','R','D','P','C','I','S']):71 def select_pos(path,words_fl,pos_list=['V','A','N','R','D','P','C','I','S']): 74 72 output_list = [] 73 all_words_list = [] 75 74 for item in words_fl.split('\n'): 76 75 try: 77 if item.split(' ')[0].decode('utf8') not in stopwords.words('spanish') and is_pos(item.split(' ')[2],pos_list): 76 w = item.split(' ')[0] 77 cleaned_word = clean_words(w.decode('utf8')) 78 all_words_list += [cleaned_word] 79 if w.decode('utf8') not in stopwords.words('spanish') and is_pos(item.split(' ')[2],pos_list) and w not in '*+.,?¿!¡":;-=/$@#ââ()[]{}' and not w.isdigit() and len(w) > 3: 78 80 # Selecciona el lema 79 81 #output_list += [item.split(' ')[1]] 80 82 # Selecciona la palabra original 81 output_list += [item.split(' ')[0]] 83 #output_list += [item.split(' ')[0]] 84 output_list += [cleaned_word] 82 85 except IndexError: 83 86 pass 87 na_file = open(path,'w','utf8') 88 na_file.write(' '.join(all_words_list)) 89 na_file.close() 84 90 return output_list 85 91 86 87 92 def preprocess(corpus_path,do_fl=True): 88 freeling_cmd = shlex.split('/home/jredondo/Proyectos/Analisis_del_Discurso/src/freeling/bin/analyzer -f /home/jredondo/Proyectos/Analisis_del_Discurso/src/freeling/share/freeling/config/es.cfg',' ')89 93 freeling_corpus_path = corpus_path + '../freeling/' 94 no_accent_path = corpus_path + '../noaccent/' 90 95 ret_val = dict() 91 96 corpus_words = [] … … 94 99 file_string = open(corpus_path+file,'r','utf8').read() 95 100 if do_fl: 101 freeling_cmd = shlex.split('/home/jredondo/Proyectos/Analisis_del_Discurso/src/freeling/bin/analyzer -f /home/jredondo/Proyectos/Analisis_del_Discurso/src/freeling/share/freeling/config/es.cfg',' ') 96 102 # Lematización con FREELING 97 103 words_fl = call_freeling(freeling_cmd,file_string) … … 112 118 #'I', interjecciones 113 119 #'S', preposiciones 114 words_fl = select_pos(words_fl=words_fl,pos_list=['A','R','V','N'])115 120 #################################### 116 121 #################################### 122 123 words_fl = select_pos(no_accent_path+file,words_fl=words_fl,pos_list=['A','R','V','N']) 124 117 125 # Quitar STOPWORDS y caracteres no deseados 118 126 words_pp = all_complete_words(words_fl) 119 words_pp = clean_words(words_pp)120 127 ret_val[file] = words_pp 121 128 corpus_words += words_pp 122 129 i += 1 123 #print "Pre-procesado el archivo: " + file124 #print "####################################"125 #print words_pp , '(' + str(i) + ')'126 #print "####################################"127 #print "####################################"128 130 129 131 return ret_val,set(corpus_words) … … 146 148 files_to_lower(path_orig,path_dest) 147 149 """ 148 corpus_path = '/home/jredondo/Proyectos/Analisis_del_Discurso/src/topic-explorer/demo-data/corpus_propuestas/' 150 #corpus_path = '/home/jredondo/Proyectos/Analisis_del_Discurso/src/topic-explorer/demo-data/corpus_propuestas/' 151 corpus_path = '/home/cenditel/Interpretacion/demo-data/corpus_propuestas/' 149 152 lower_corpus_path = corpus_path + 'lower/' 150 153 pp_corpus_path = corpus_path + 'pp/' … … 153 156 exclude_words = ['descripcion','justificacion','construccion','desarrollo','comunidad','comunal','proyecto','prueblo','desarrollar','mismo','nacional','pueblo','sistema'] 154 157 exclude_words = [w.encode('utf8') for w in exclude_words] 155 #vocab_idf = idf(file_words_pp,corpus_words)156 #print sorted(vocab_idf.items(),key=operator.itemgetter(1), reverse=True)157 158 excluded = open(corpus_path+'excluded.txt','w','utf8') 158 159 added_files = [] … … 171 172 continue 172 173 173 #coef = float(len(set(words_pp)))/float(len(words_pp))174 #print coef, len(words_pp), file175 #if (coef <= 0.5) or len(words_pp) <= 150: continue176 174 if len(words_pp) <= 50: continue 177 175 # Guardar archivo 178 176 file_pp = open(pp_corpus_path+file,'w','utf8') 179 177 added_files.append(words_pp) 180 for w in words_pp: 181 #condition = vocab_idf[w] 182 #if condition >= 2.0 and condition <= 6.1 and not '_' in w: 183 #if condition >= 2.0 and not '_' in w: 184 178 for w in words_pp: 185 179 if w not in exclude_words and not '_' in w: 186 187 #try:188 # file_pp.write(w.encode('utf8') + ' ')189 #except UnicodeDecodeError:190 180 file_pp.write(w + ' ') 191 181 else: 192 182 try: 193 #excluded.write(w.encode('utf8') + ' ' + str(condition) + ' (' + file + ')\n')194 183 excluded.write(w.encode('utf8') + ' (' + file + ')\n') 195 184 except UnicodeDecodeError: 196 #excluded.write(w + ' ' + str(condition) + ' (' + file + ')\n')197 185 excluded.write(w + ' (' + file + ')\n') 198 186 file_pp.close() 199 187 excluded.close() 200 188 201 print "Documentos repetidos: ", repeated_count 202 print "Palabras en el vocabulario: ", len(corpus_words) 189
Nota: Vea TracChangeset
para ayuda en el uso del visor de conjuntos de cambios.