fccoelho aider (anthropic/claude-sonnet-4-20250514) commited on
Commit
fc7ec2a
·
1 Parent(s): eef235b

refactor: corrigir função de extração de referências com regex

Browse files

Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>

Files changed (1) hide show
  1. app.py +58 -65
app.py CHANGED
@@ -145,79 +145,74 @@ def extract_references_with_regex(text):
145
  r'^([A-Z][A-Za-z\s,&.-]+?&[A-Za-z\s,&.-]+?)\.\s*\((\d{4}[a-z]?)\)\.\s*([^.]+?)\.\s*([^.]+?)\.?\s*$'
146
  ]
147
 
148
- # Dividir texto em linhas
149
- lines = text.split('\n')
150
-
151
- # Processar cada linha
152
-
153
- # Tentar cada padrão
154
- for pattern in patterns:
155
- reflist = re.findall(pattern, text, re.MULTILINE | re.UNICODE|re.DOTALL)
156
 
157
  if reflist:
158
- # change the code below to process the list of references in reflist AI!
159
- if len(groups) >= 4:
160
- authors = groups[0].strip()
 
 
161
 
162
- # Para o padrão numerado especial (5 grupos)
163
- if len(groups) == 5:
164
- title = groups[1].strip()
165
- journal = groups[2].strip()
166
- volume = groups[3].strip()
167
- year = groups[4].strip()
168
- pages = "" # Será extraído depois do journal
169
- else:
170
- # Para outros padrões (4 grupos)
171
- year = groups[1].strip()
172
- title = groups[2].strip()
173
- journal = groups[3].strip()
174
- volume = ""
175
 
176
- # Validações adicionais
177
- # Verificar se tem pelo menos um autor válido
178
- if not re.search(r'[A-Z][a-z]+', authors):
179
- continue
180
 
181
- # Verificar se o título não é muito curto
182
- if len(title) < 10:
183
- continue
184
 
185
- # Verificar se não é uma linha de cabeçalho ou rodapé
186
- if re.search(r'(page|vol|volume|number|issue)\s*\d+', line, re.IGNORECASE):
187
- continue
188
 
189
- # Extrair DOI se presente
190
- doi_match = re.search(r'doi[:\s]*([^\s,]+)', journal, re.IGNORECASE)
191
- doi = doi_match.group(1) if doi_match else ""
192
 
193
- # Extrair volume e páginas (se não foram extraídos pelo padrão especial)
194
- if len(groups) != 5:
195
- vol_pages_match = re.search(r'(\d+)\s*\(?\d*\)?\s*[,:]\s*(\d+[-–]\d+)', journal)
196
- volume = vol_pages_match.group(1) if vol_pages_match else ""
197
- pages = vol_pages_match.group(2) if vol_pages_match else ""
198
- else:
199
- # Para o padrão numerado, extrair páginas do journal
200
- pages_match = re.search(r'(\d+[-–]\d+)', journal)
201
- pages = pages_match.group(1) if pages_match else ""
202
 
203
- # Limpar campos
204
- authors = re.sub(r'\s+', ' ', authors)
205
- title = re.sub(r'\s+', ' ', title)
206
- journal = re.sub(r'\s+', ' ', journal)
207
 
208
- reference = {
209
- "authors": authors,
210
- "title": title,
211
- "journal": journal,
212
- "year": year,
213
- "volume": volume,
214
- "pages": pages,
215
- "doi": doi,
216
- "line_number": line_num + 1 # Para debug
217
- }
218
 
219
- references.append(reference)
220
- break # Parar na primeira correspondência para esta linha
221
 
222
  # Remover duplicatas baseadas no título e ano
223
  seen_refs = set()
@@ -229,9 +224,7 @@ def extract_references_with_regex(text):
229
 
230
  if key not in seen_refs:
231
  seen_refs.add(key)
232
- # Remover campo de debug antes de retornar
233
- ref_clean = {k: v for k, v in ref.items() if k != "line_number"}
234
- unique_references.append(ref_clean)
235
 
236
  # Ordenar por ano (mais recente primeiro)
237
  unique_references.sort(key=lambda x: x.get("year", "0"), reverse=True)
 
145
  r'^([A-Z][A-Za-z\s,&.-]+?&[A-Za-z\s,&.-]+?)\.\s*\((\d{4}[a-z]?)\)\.\s*([^.]+?)\.\s*([^.]+?)\.?\s*$'
146
  ]
147
 
148
+ # Processar cada padrão
149
+ for pattern_index, pattern in enumerate(patterns):
150
+ reflist = re.findall(pattern, text, re.MULTILINE | re.UNICODE | re.DOTALL)
 
 
 
 
 
151
 
152
  if reflist:
153
+ for ref_match in reflist:
154
+ groups = ref_match
155
+
156
+ if len(groups) >= 4:
157
+ authors = groups[0].strip()
158
 
159
+ # Para o padrão numerado especial (6 grupos)
160
+ if len(groups) == 6:
161
+ title = groups[2].strip()
162
+ journal = groups[3].strip()
163
+ volume = groups[4].strip()
164
+ year = groups[5].strip()
165
+ pages = ""
166
+ else:
167
+ # Para outros padrões (4 grupos)
168
+ year = groups[1].strip()
169
+ title = groups[2].strip()
170
+ journal = groups[3].strip()
171
+ volume = ""
172
 
173
+ # Validações adicionais
174
+ # Verificar se tem pelo menos um autor válido
175
+ if not re.search(r'[A-Z][a-z]+', authors):
176
+ continue
177
 
178
+ # Verificar se o título não é muito curto
179
+ if len(title) < 10:
180
+ continue
181
 
182
+ # Verificar se não é uma linha de cabeçalho ou rodapé
183
+ if re.search(r'(page|vol|volume|number|issue)\s*\d+', journal, re.IGNORECASE):
184
+ continue
185
 
186
+ # Extrair DOI se presente
187
+ doi_match = re.search(r'doi[:\s]*([^\s,]+)', journal, re.IGNORECASE)
188
+ doi = doi_match.group(1) if doi_match else ""
189
 
190
+ # Extrair volume e páginas (se não foram extraídos pelo padrão especial)
191
+ if len(groups) != 6:
192
+ vol_pages_match = re.search(r'(\d+)\s*\(?\d*\)?\s*[,:]\s*(\d+[-–]\d+)', journal)
193
+ volume = vol_pages_match.group(1) if vol_pages_match else ""
194
+ pages = vol_pages_match.group(2) if vol_pages_match else ""
195
+ else:
196
+ # Para o padrão numerado, extrair páginas do journal
197
+ pages_match = re.search(r'(\d+[-–]\d+)', journal)
198
+ pages = pages_match.group(1) if pages_match else ""
199
 
200
+ # Limpar campos
201
+ authors = re.sub(r'\s+', ' ', authors)
202
+ title = re.sub(r'\s+', ' ', title)
203
+ journal = re.sub(r'\s+', ' ', journal)
204
 
205
+ reference = {
206
+ "authors": authors,
207
+ "title": title,
208
+ "journal": journal,
209
+ "year": year,
210
+ "volume": volume,
211
+ "pages": pages,
212
+ "doi": doi
213
+ }
 
214
 
215
+ references.append(reference)
 
216
 
217
  # Remover duplicatas baseadas no título e ano
218
  seen_refs = set()
 
224
 
225
  if key not in seen_refs:
226
  seen_refs.add(key)
227
+ unique_references.append(ref)
 
 
228
 
229
  # Ordenar por ano (mais recente primeiro)
230
  unique_references.sort(key=lambda x: x.get("year", "0"), reverse=True)