Biifruu commited on
Commit
52b9ecf
·
verified ·
1 Parent(s): 3be3327

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -33,45 +33,47 @@ def fix_common_ocr_errors(text):
33
  return text
34
 
35
  def format_text_to_markdown(text):
 
 
 
 
36
  lines = text.splitlines()
37
- final_lines = []
38
- buffer = []
39
-
40
- def flush_buffer():
41
- if buffer:
42
- final_lines.append(" ".join(buffer).strip())
43
- final_lines.append("") # salto de línea para nuevo párrafo
44
- buffer.clear()
45
-
46
  for line in lines:
47
  line = line.strip()
48
  if not line:
49
- flush_buffer()
50
  continue
51
- if re.match(r"^(posible causa|causa):", line, re.IGNORECASE):
52
- flush_buffer()
53
- final_lines.append("### 🛑 Posible causa")
54
- final_lines.append("")
55
- final_lines.append(re.sub(r"^(posible causa|causa):", "", line, flags=re.IGNORECASE).strip())
56
- elif re.match(r"^(posible solución|solución):", line, re.IGNORECASE):
57
- flush_buffer()
58
- final_lines.append("### ✅ Posible solución")
59
- final_lines.append("")
60
- final_lines.append(re.sub(r"^(posible solución|solución):", "", line, flags=re.IGNORECASE).strip())
61
- elif re.match(r"^descripción del problema", line, re.IGNORECASE):
62
- flush_buffer()
63
- final_lines.append("### 📝 Descripción del problema")
64
  elif re.match(r"^\d+\.", line):
65
- flush_buffer()
66
- final_lines.append("- " + line)
67
- elif re.match(r"^•\s*", line):
68
- flush_buffer()
69
- final_lines.append("- " + line)
70
  else:
71
- buffer.append(line)
72
-
73
- flush_buffer()
74
- return "\n".join(final_lines)
 
 
 
 
 
 
 
 
75
 
76
  def translate_text(text):
77
  if len(text.strip()) < 5:
 
33
  return text
34
 
35
  def format_text_to_markdown(text):
36
+ # Corrige errores comunes
37
+ text = fix_common_ocr_errors(text)
38
+
39
+ # Línea por línea
40
  lines = text.splitlines()
41
+ markdown_lines = []
42
+
 
 
 
 
 
 
 
43
  for line in lines:
44
  line = line.strip()
45
  if not line:
 
46
  continue
47
+
48
+ # Encabezados conocidos
49
+ if re.search(r"\b(posible causa)\b", line, re.IGNORECASE):
50
+ markdown_lines.append("### 🛑 Posible causa\n")
51
+ continue
52
+ elif re.search(r"\b(posible solución)\b", line, re.IGNORECASE):
53
+ markdown_lines.append("### ✅ Posible solución\n")
54
+ continue
55
+ elif re.search(r"\b(descripción del problema)\b", line, re.IGNORECASE):
56
+ markdown_lines.append("### 📝 Descripción del problema\n")
57
+ continue
 
 
58
  elif re.match(r"^\d+\.", line):
59
+ markdown_lines.append(f"- {line}")
60
+ continue
61
+ elif re.match(r"^[•*-]", line):
62
+ markdown_lines.append(f"- {line[1:].strip()}")
63
+ continue
64
  else:
65
+ # Detecta si la línea es basura (letras sin sentido, símbolos)
66
+ if len(line) < 5:
67
+ continue
68
+ if re.search(r"[^\w\s.,:;¡!¿?\-()]", line):
69
+ symbols = re.findall(r"[^\w\s.,:;¡!¿?\-()]", line)
70
+ if len(symbols) > 3:
71
+ continue # basura
72
+ markdown_lines.append(line)
73
+
74
+ # Une líneas y separa párrafos correctamente
75
+ formatted_text = "\n\n".join(markdown_lines)
76
+ return formatted_text
77
 
78
  def translate_text(text):
79
  if len(text.strip()) < 5: