Kevin Meza Landeros

Script para generar archivos para CoreNLP (regex NER).

1 +# Importacion de librerias
2 +import pandas as pd
3 +import re
4 +import argparse
5 +import os
6 +
7 +__author__ = 'kevinml'
8 +
9 +# Objective
10 +# Take two column files and make 3 different files:
11 +# 1.- <FileName>_1Word_NoGreek.txt - Archivo donde la primer columna es unipalabra y SOLO contiene numeros alfanumericos.
12 +# 2.- <FileName>_Words_NoGreek.tx - Archivo donde la primer columna es multipalabra y SOLO contiene numeros alfanumericos.
13 +# 3.- <FileName>_1Word_Greek.txt - Archivo donde la primer columna es unipalabra y contiene caracteres NO alfanumericos.
14 +#
15 +# Input parameters
16 +# --inputPath=PATH Path of inputfiles.
17 +# --outputPath=PATH Path of outputfiles.
18 +# --iFile Archivo a partir del cual se obtendran los 3 archivos.
19 +#
20 +# Output
21 +#
22 +#
23 +# Examples
24 +# python filtering.py --inputPath /home/kevinml/Dropbox/LCG/Labs/PGC/automatic-extraction-GEO --outputPath /home/kevinml/Dropbox/LCG/Labs/PGC/automatic-extraction-GEO --iFile NER_words.txt
25 +
26 +####################################################################################
27 +# FUNCTIONS #
28 +####################################################################################
29 +
30 +def alphanum_and_NOGreek(word):
31 + ''' Esta funcion regresa True si en la palabra que recibe como parametro NO SE ENCUENTRAN CARACTERES ALFANUMERICOS
32 + y regresa False si en la palabra se encuentra algún caracter NO ALFANUMERICO
33 + '''
34 + if re.search("\W", word):
35 + return False
36 + else:
37 + return True
38 +
39 +####################################################################################
40 +# MAIN PROGRAM #
41 +####################################################################################
42 +
43 +if __name__ == '__main__':
44 +
45 + # Definicion de Parametros
46 + parser = argparse.ArgumentParser()
47 + parser.add_argument('--inputPath', help="Ruta donde se encuentra el archivo a procesar. Ej: --inputPath /home/kevinml/transporter-substrate-interactions/", required=True)
48 + parser.add_argument('--outputPath', help="Ruta donde se depositaran los archivos resultantes. Ej: --outputPath /home/kevinml/transporter-substrate-interactions/", required=True)
49 + parser.add_argument('--iFile', help="Archivo a procesar. Ej: --iFile NER_words.txt", required=True)
50 + args = parser.parse_args()
51 +
52 + # Se imprimen los parametros ingresados
53 + print('\n-------------------------------- PARAMETERS --------------------------------\n')
54 + print('Input Path: ' + str(args.inputPath))
55 + print('File: ' + str(args.iFile))
56 + print('Output Path: ' + str(args.outputPath))
57 + print('\n-------------------------------- PROCESSING --------------------------------\n')
58 +
59 + # Se abre el archivo a procesar
60 + file = pd.read_csv(os.path.join(args.inputPath, args.iFile), sep = "\t")
61 +
62 + print("######################\n# PRIMER ARCHIVO #\n######################")
63 + conditions = []
64 + lines = []
65 + # Se abre el primer archivo
66 + with open (os.path.join(args.outputPath,str(args.iFile[:-4]) + "_1Word_NoGreek.txt"), "w+") as oFile:
67 + for index, row in file.iterrows():
68 + if len(row[0].split(" ")) == 1 and alphanum_and_NOGreek(str(row[0].split(" ")[0])) == True: # Se verifica que en la primer columna solo haya un palabra y que esta solo tenga caracteres alfanumericos.
69 + conditions.append(row[0])
70 + lines.append(row[1])
71 + # Se escriben en el primer archivo aquellos valores que cumplen las condiciones.
72 + for i in range(len(lines)):
73 + oFile.write(conditions[i] + "\t" + lines[i] + '\n')
74 +
75 + print("\nArchivo de contenidos de una sola palabra ha sido generado. NOTA: Se han excluido letras griegas.\nNombre del archivo:" + str(args.iFile[:-4]) + "_1Word_NoGreek.txt\n")
76 +
77 + print("#######################\n# SEGUNDO ARCHIVO #\n#######################")
78 + conditions_2 = []
79 + lines_2 = []
80 + # Se abre el segundo archivo
81 + with open (os.path.join(args.outputPath,str(args.iFile[:-4]) + "_Words_NoGreek.txt"), "w+") as oFile:
82 + for index, row in file.iterrows():
83 + # La bandera en 1 indica que ninguna palabra de la primer columna tiene caracteres NO alfanumericos
84 + # La bandera en 0 indica que al menos una palabra tienes caracteres NO alfanumericos.
85 + bandera = 1
86 + # Con el for se va a verificando la presencia de caracteres alfanumericos en cada palabra de la primera columna
87 + for i in range(0, len(row[0].split(" "))):
88 + if alphanum_and_NOGreek(str(row[0].split(" ")[i])) == False:
89 + bandera = 0
90 + if bandera == 1:
91 + conditions_2.append(row[0])
92 + lines_2.append(row[1])
93 + # Se escriben en el primer archivo aquellos valores que cumplen las condiciones.
94 + for i in range(len(lines_2)):
95 + oFile.write(conditions_2[i] + "\t" + lines_2[i] + '\n')
96 +
97 + print("\nArchivo de contenidos de varias palabras ha sido generado. NOTA: Se han excluido letras griegas.\nNombre del archivo:" + str(args.iFile[:-4]) + "SeveralWords_NoGreek_Filter\n")
98 +
99 + print("######################\n# TERCER ARCHIVO #\n######################")
100 + conditions_3 = []
101 + lines_3 = []
102 + # Se abre el tercer archivo
103 + with open (os.path.join(args.outputPath,str(args.iFile[:-4]) + "_1Word_Greek.txt"), "w+") as oFile:
104 + for index, row in file.iterrows():
105 + # Se verifica que la primer columna sea unipalabra.
106 + if len(row[0].split(" ")) == 1:
107 + conditions_3.append(row[0])
108 + lines_3.append(row[1])
109 + # Se escriben en el primer archivo aquellos valores que cumplen las condiciones.
110 + for i in range(len(lines_3)):
111 + oFile.write(conditions_3[i] + "\t" + lines_3[i] + '\n')
112 +
113 + print("\nArchivo de contenidos de una palabra ha sido generado:.nNombre del archivo:" + str(args.iFile[:-4]) + "SeveralWords_Greek_Filter\n")
...\ No newline at end of file ...\ No newline at end of file