Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

jurisdicciones.py 13 KB

You have to be logged in to leave a comment. Sign In
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  1. import re
  2. import os
  3. import pandas as pd
  4. import numpy as np
  5. import click
  6. from tqdm import tqdm
  7. from unicodedata import normalize
  8. from pjud import data
  9. def load_article_cot(article: str, src_path = './data/raw/cot'):
  10. tqdm.pandas()
  11. with open(f'{src_path}/{article}.txt', 'r') as file:
  12. contenido = ''
  13. for line in file.readlines():
  14. contenido += line
  15. return contenido
  16. def garantia():
  17. regex_jg=r"(?:(?P<Region>^[\w \']+)\:\n\n)|(?P<JG>^[\w. \-]+)\,\scon\s(?P<Jueces>[\w.\-]+)[a-z\-\s\,]+(?P<Competencia>\.|\s[\w. \-\,]+)"
  18. matches = re.findall(regex_jg, load_article_cot('Juzgados_Garantia'), re.MULTILINE)
  19. data_jg = []
  20. for item in range(0, len(matches)):
  21. if matches[item][0] != '':
  22. region = matches[item][0].upper()
  23. else:
  24. if matches[item][1] != '':
  25. ciudad = matches[item][1].upper()
  26. if ciudad.find("JUZGADO") != -1:
  27. juzgado = ciudad
  28. else:
  29. juzgado = f"JUZGADO DE GARANTIA {ciudad}"
  30. if matches[item][2] != '':
  31. cantidad_jueces = data.cleandata.transforma_numero(matches[item][2])
  32. if matches[item][3] == '.':
  33. competencia = ciudad
  34. else:
  35. if matches[item][3] != '':
  36. competencia = matches[item][3].upper()
  37. competencia = competencia.replace(" Y ", ",")
  38. competencia = competencia.replace(" E ", ",")
  39. competencia = competencia.replace(".", "")
  40. comunas = competencia.split(",")
  41. for comuna in comunas:
  42. data_jg.append([region, juzgado, ciudad, cantidad_jueces, comuna.strip(), 'GARANTIA'])
  43. df_juzgados_garantia = pd.DataFrame(data_jg,
  44. columns=['REGION', 'TRIBUNAL', 'ASIENTO', 'JUECES', 'COMUNA', 'TIPO JUZGADO'])
  45. # Elimino tildes de las columnas object
  46. cols = df_juzgados_garantia.select_dtypes(include = ["object"]).columns
  47. df_juzgados_garantia[cols] = df_juzgados_garantia[cols].progress_apply(data.cleandata.elimina_tilde)
  48. data.save_feather(df_juzgados_garantia, 'generates_JuzgadosGarantia')
  49. click.echo('Generado archivo Feather. Proceso Terminado')
  50. def top():
  51. # Se construye una expresion regular para captar una lista con la información que se desea procesar, y así generar un dataframe.
  52. regex_top=r"(?:(?P<Region>^[\w \']+)\:\n)|(?P<JG>^[\w. \-]+)\,\scon\s(?P<Jueces>[\w.\-]+)[a-z\-\s\,]+(?P<Competencia>\.|\s[\w. \-\,\']+)"
  53. matches = re.findall(regex_top, load_article_cot('Tribunales_orales'), re.MULTILINE)
  54. data_top=[]
  55. for item in range(0,len(matches)):
  56. if matches[item][0] != '':
  57. region = matches[item][0].upper()
  58. else:
  59. if matches[item][1] != '':
  60. ciudad = matches[item][1].upper()
  61. if ciudad.find("TRIBUNAL") != -1:
  62. juzgado = ciudad
  63. else:
  64. juzgado = f"TRIBUNAL DE JUICIO ORAL EN LO PENAL {ciudad}"
  65. if matches[item][2] != '':
  66. cantidad_jueces = data.cleandata.transforma_numero(matches[item][2])
  67. if matches[item][3] == '.':
  68. competencia = ciudad
  69. else:
  70. if matches[item][3] != '':
  71. competencia = matches[item][3].upper()
  72. competencia = competencia.replace(" Y ",",")
  73. competencia = competencia.replace(" E ",",")
  74. competencia = competencia.replace(".","")
  75. comunas = competencia.split(",")
  76. for comuna in comunas:
  77. data_top.append([region,juzgado,ciudad,cantidad_jueces,comuna.strip(),'ORAL'])
  78. df_tribunal_oral = pd.DataFrame(data_top, columns = ['REGION','TRIBUNAL','ASIENTO','JUECES','COMUNA','TIPO JUZGADO'])
  79. df_tribunal_oral['JUECES'] = df_tribunal_oral['JUECES'].fillna(0).astype(np.int8)
  80. click.echo('Eliminando tildes')
  81. cols = df_tribunal_oral.select_dtypes(include = ["object"]).columns
  82. df_tribunal_oral[cols] = df_tribunal_oral[cols].progress_apply(data.cleandata.elimina_tilde)
  83. data.save_feather(df_tribunal_oral,'generates_TribunalOral')
  84. click.echo('Generado archivo Feather. Proceso Terminado')
  85. def juzgados_letras():
  86. regex_jl = r"(?:Art.\s[0-9\s(bis|ter|quáter)]+\.\s[\w\s]+\,(?P<Region>[\w\s\']+)\,[\w\s]+:\s+)|(?:(?:^[A]\.\-\s[\w\s]+:\s+)(?:(?:[\w\s\,]+[.|\;])+))|(?:(?:[\-\s][\w\s]+\:\s*)|(?:\s*(?:(?P<cant_juzg>[\w]+)(?:\s[J|j][a-z\,\s]+)(?P<JG>[\w\s]+)[\,|y]\s[\w]+\s(?P<Jueces>[\w]+)\s[a-z\s\,]+(?P<Competencia>[\w|\s|\,]+)[\;|\.]$)\s*))"
  87. matches = re.findall(regex_jl, load_article_cot('juzgadoletras'), re.MULTILINE)
  88. data_jl = []
  89. for item in range(0,len(matches)):
  90. if matches[item][0] != '':
  91. region = f"REGION{matches[item][0].upper()}"
  92. else:
  93. if matches[item][2] != '':
  94. ciudad = matches[item][2].upper()
  95. juzgado = f"JUZGADO DE LETRAS Y GARANTIA {ciudad}"
  96. if matches[item][3] != '':
  97. if matches[item][3] == 'competencia':
  98. cantidad_jueces = 1
  99. else:
  100. cantidad_jueces = data.cleandata.transforma_numero(matches[item][3])
  101. if matches[item][4] != '':
  102. if matches[item][4] == 'a':
  103. competencia = ciudad
  104. else:
  105. if matches[item][4] != '':
  106. competencia = matches[item][4].upper()
  107. competencia = competencia.replace(" Y ",",")
  108. competencia = competencia.replace(" E ",",")
  109. competencia = competencia.replace(".","")
  110. comunas = competencia.split(",")
  111. for comuna in comunas:
  112. data_jl.append([region,juzgado,ciudad,cantidad_jueces,comuna.strip(),'LETRAS Y GARANTIA'])
  113. df_juzgados_letras = pd.DataFrame(data_jl,columns = ['REGION','TRIBUNAL','ASIENTO','JUECES','COMUNA','TIPO JUZGADO'])
  114. click.echo('Elimando tildes')
  115. cols = df_juzgados_letras.select_dtypes(include = ["object"]).columns
  116. df_juzgados_letras[cols] = df_juzgados_letras[cols].progress_apply(data.cleandata.elimina_tilde)
  117. data.save_feather(df_juzgados_letras, 'generates_JuzgadosLetras')
  118. click.echo('Generado archivo Feather. Proceso Terminado')
  119. def extraccion_comunas(filtro, df):
  120. add_comunas = []
  121. for indice in filtro.index:
  122. region = filtro["REGION"][indice]
  123. tribunal = filtro["TRIBUNAL"][indice]
  124. asiento = filtro["ASIENTO"][indice]
  125. jueces = filtro["JUECES"][indice]
  126. tipo_juzgado = filtro["TIPO JUZGADO"][indice]
  127. provincia = filtro["COMUNA"][indice].split("PROVINCIA DE ")
  128. comunas_de_provincias = df.loc[df["Nombre Provincia"] == provincia[1],"Nombre Comuna"]
  129. for var_comuna in range(len(comunas_de_provincias)):
  130. comuna = comunas_de_provincias.values[var_comuna]
  131. add_comunas.append([region,tribunal,asiento,jueces,comuna,tipo_juzgado])
  132. return(add_comunas)
  133. def juzgados_penales(path_pjud = "data/interim/pjud", path_subdere = "data/interim/subdere"):
  134. tqdm.pandas()
  135. df_juzgados_garantia = pd.read_feather(f"{path_pjud}/generates_JuzgadosGarantia.feather")
  136. df_tribunal_oral = pd.read_feather(f"{path_pjud}/generates_TribunalOral.feather")
  137. df_juzgados_letras = pd.read_feather(f"{path_pjud}/generates_JuzgadosLetras.feather")
  138. df_provincias = pd.read_feather(f"{path_subdere}/generates_Provincias.feather")
  139. # Existen dos provincias en el listado.
  140. filtro_provincia_top = df_tribunal_oral[df_tribunal_oral['COMUNA'].str.contains("PROVINCIA", case = False)]
  141. df_tribunal_oral.drop(filtro_provincia_top.index, axis = 0, inplace = True)
  142. # Analizo Dataframe , ya que acá estan los Juzgados de Letras con Competencia Común, y en algunos casos estos pueden
  143. # No corresponder a Juzgados Penales. Para saber eso, debo verificar que no existan JG en esas comunas, en el caso de
  144. # existir debo eliminar registro.
  145. filtro_provincia_jl = df_juzgados_letras[df_juzgados_letras['COMUNA'].str.contains("PROVINCIA", case = False)]
  146. df_juzgados_letras.drop(filtro_provincia_jl.index, axis = 0, inplace = True)
  147. filtro_provincia_jl.loc[229,"COMUNA"] = 'PROVINCIA DE MELIPILLA'
  148. filtro_provincia_jl.drop(176, inplace = True)
  149. df_nuevas_comunas_jl = pd.DataFrame(extraccion_comunas(filtro_provincia_jl, df_provincias),
  150. columns = ['REGION','TRIBUNAL','ASIENTO','JUECES','COMUNA','TIPO JUZGADO'])
  151. df_juzgados_letras = df_juzgados_letras.append(df_nuevas_comunas_jl, ignore_index = True)
  152. # Exploro las provincias y extraigo las comunas -> Caso Tribunales orales
  153. df_nuevas_comunas_top = pd.DataFrame(extraccion_comunas(filtro_provincia_top, df_provincias),
  154. columns = ['REGION','TRIBUNAL','ASIENTO','JUECES','COMUNA','TIPO JUZGADO'])
  155. df_tribunal_oral = df_tribunal_oral.append(df_nuevas_comunas_top, ignore_index = True)
  156. # Acá debo eliminar los juzgados de letras que estan cubiertos por la competencia de un juzgado de Garantía
  157. juzgados_garantias = df_juzgados_garantia['COMUNA'].unique().tolist()
  158. drop_jl = []
  159. for jl in df_juzgados_letras.index:
  160. if df_juzgados_letras['COMUNA'][jl] in juzgados_garantias:
  161. drop_jl.append(jl)
  162. df_juzgados_letras.drop(drop_jl, axis = 0, inplace = True)
  163. df_juzgados_penales = pd.concat([df_juzgados_garantia,df_juzgados_letras], join = "inner")
  164. df_juzgados_penales.reset_index(inplace = True)
  165. # El caso de Alto Hospicio es particular, no aparece en el COT, ya que fue parte de una actualizacion
  166. # de la Ley y fue sumado como Juzgado especial. Debemos agregarlo a Juzgados Penales
  167. alto_hospicio = [('REGION DE TARAPACA','JUZGADO DE LETRAS Y GARANTIA ALTO HOSPICIO','ALTO HOSPICIO','4',
  168. 'ALTO HOSPICIO','LETRAS Y GARANTIA')]
  169. df_alto_hospicio = pd.DataFrame(alto_hospicio, columns = ['REGION','TRIBUNAL','ASIENTO','JUECES','COMUNA','TIPO JUZGADO'])
  170. df_juzgados_penales = pd.concat([df_juzgados_penales, df_alto_hospicio], join = "inner")
  171. df_juzgados_penales['JUECES'] = df_juzgados_penales['JUECES'].fillna(0).astype(np.int8)
  172. df_juzgados_penales.reset_index(drop = True)
  173. # Transformo las variables para que queden igual en los 3 datasets
  174. click.echo('Tranformación ... Separando regiones')
  175. df_juzgados_penales['REGION'] = df_juzgados_penales['REGION'].progress_apply(data.cleandata.separa_regiones)
  176. df_tribunal_oral['REGION'] = df_tribunal_oral['REGION'].progress_apply(data.cleandata.separa_regiones)
  177. click.echo('Tranformación ... Creando asiento de tribunales')
  178. df_juzgados_penales['ASIENTO'] = df_juzgados_penales['ASIENTO'].progress_apply(data.cleandata.transforma_asiento)
  179. df_tribunal_oral['ASIENTO'] = df_tribunal_oral['ASIENTO'].progress_apply(data.cleandata.transforma_asiento)
  180. # Transformo valores de Regiones a similar a BBDD Pjud
  181. regiones_subdere = df_provincias["Nombre Región"].unique()
  182. regiones_top = df_tribunal_oral["REGION"].unique()
  183. for region in regiones_subdere:
  184. for region_top in regiones_top:
  185. if region_top.find(region) != -1:
  186. df_provincias['Nombre Región'] = df_provincias['Nombre Región'].replace(region, region_top)
  187. click.echo('Tranformación ... Separando regiones')
  188. df_provincias['Nombre Región'] = df_provincias['Nombre Región'].progress_apply(data.cleandata.separa_regiones)
  189. # Creare dataframe con tribunales ORales- Garantia y Letras
  190. top = df_tribunal_oral[['REGION','TRIBUNAL','ASIENTO','COMUNA','JUECES','TIPO JUZGADO']]
  191. jg = df_juzgados_penales[['REGION','TRIBUNAL','ASIENTO','COMUNA','JUECES','TIPO JUZGADO']]
  192. df_listado_tribunales = pd.concat([top, jg], join = "inner")
  193. click.echo('Tranformación ... Cambiando Nombres')
  194. df_tribunal_oral['TRIBUNAL'] = df_tribunal_oral['TRIBUNAL'].progress_apply(data.cleandata.cambio_nombre_juzgados)
  195. df_juzgados_penales['TRIBUNAL'] = df_juzgados_penales['TRIBUNAL'].progress_apply(data.cleandata.cambio_nombre_juzgados)
  196. df_listado_tribunales['TRIBUNAL'] = df_listado_tribunales['TRIBUNAL'].progress_apply(data.cleandata.cambio_nombre_juzgados)
  197. data.save_feather(df_provincias, 'generates_DataRegiones', path='data/processed/subdere')
  198. data.save_feather(df_listado_tribunales, 'generates_ListadoTribunales', path='data/processed/pjud')
  199. data.save_feather(df_tribunal_oral, 'generates_TribunalesOrales', path='data/processed/pjud')
  200. data.save_feather(df_juzgados_penales, 'generates_JuzgadosPenales', path='data/processed/pjud')
  201. click.echo('Generado archivo Feather. Proceso Terminado')
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...