habulaj commited on
Commit
2a2b881
·
verified ·
1 Parent(s): b77c8dd

Update routers/inference_createposter.py

Browse files
Files changed (1) hide show
  1. routers/inference_createposter.py +22 -43
routers/inference_createposter.py CHANGED
@@ -52,74 +52,53 @@ def fix_citation_quotes(citation_text: str) -> str:
52
  # Adicionar as aspas corretas
53
  return f"“{text.strip()}”"
54
 
55
- def clean_text_content(text: str) -> str:
56
  """
57
- Limpa o conteúdo de texto removendo tags inválidas e corrigindo formatação:
58
- - Remove todas as tags exceto <strong> e <em>
59
- - Se tiver <strong><em> juntas, prioriza <em>
60
- - Se tiver <em><strong> juntas, prioriza <strong>
61
  """
62
  if not text:
63
  return text
64
 
65
  import re
66
 
67
- # Corrigir tags aninhadas - priorizar <em> quando <strong><em>
68
- text = re.sub(r'<strong>\s*<em>(.*?)</em>\s*</strong>', r'<em>\1</em>', text)
 
69
 
70
- # Corrigir tags aninhadas - priorizar <strong> quando <em><strong>
71
- text = re.sub(r'<em>\s*<strong>(.*?)</strong>\s*</em>', r'<strong>\1</strong>', text)
72
-
73
- # Remover todas as outras tags exceto <strong> e <em>
74
- # Primeiro, proteger as tags válidas
75
- protected_tags = []
76
- strong_pattern = r'<strong>(.*?)</strong>'
77
- em_pattern = r'<em>(.*?)</em>'
78
-
79
- # Substituir temporariamente tags válidas
80
- def protect_tag(match):
81
- index = len(protected_tags)
82
- protected_tags.append(match.group(0))
83
- return f"__PROTECTED_TAG_{index}__"
84
-
85
- text = re.sub(strong_pattern, protect_tag, text)
86
- text = re.sub(em_pattern, protect_tag, text)
87
-
88
- # Remover todas as outras tags
89
- text = re.sub(r'<[^>]+>', '', text)
90
-
91
- # Restaurar tags protegidas
92
- for i, tag in enumerate(protected_tags):
93
- text = text.replace(f"__PROTECTED_TAG_{i}__", tag)
94
 
95
  return text.strip()
96
 
97
  def fix_url_citation(url: str) -> str:
98
  """
99
- Analisa uma URL e corrige o parâmetro citation se presente,
100
- além de limpar outros parâmetros de texto
101
  """
102
  try:
103
  # Parse da URL
104
  parsed_url = urlparse(url)
105
  query_params = parse_qs(parsed_url.query)
106
 
107
- # Parâmetros que precisam de limpeza geral (exceto citation)
108
- text_params = ['text', 'title', 'headline']
109
 
110
- # Processar parâmetros de texto
111
  for param in text_params:
112
  if param in query_params and query_params[param]:
113
  original_text = query_params[param][0]
114
- cleaned_text = clean_text_content(original_text)
 
 
 
 
 
 
 
115
  query_params[param] = [cleaned_text]
116
 
117
- # Processar citation especialmente
118
- if 'citation' in query_params and query_params['citation']:
119
- original_citation = query_params['citation'][0]
120
- fixed_citation = fix_citation_quotes(original_citation)
121
- query_params['citation'] = [fixed_citation]
122
-
123
  # Reconstruir a query string
124
  new_query = urlencode(
125
  {k: v[0] if isinstance(v, list) and len(v) == 1 else v for k, v in query_params.items()},
 
52
  # Adicionar as aspas corretas
53
  return f"“{text.strip()}”"
54
 
55
+ def clean_text_content_improved(text: str) -> str:
56
  """
57
+ Remove TODAS as tags HTML do texto, incluindo <em>, <strong>, <wiki>, etc.
58
+ Mantém apenas o conteúdo textual limpo.
 
 
59
  """
60
  if not text:
61
  return text
62
 
63
  import re
64
 
65
+ # Remove TODAS as tags HTML usando regex mais ampla
66
+ # Esta regex captura qualquer tag: <qualquer_coisa>
67
+ text = re.sub(r'<[^>]*>', '', text)
68
 
69
+ # Remove possíveis entidades HTML comuns
70
+ text = text.replace('&lt;', '<').replace('&gt;', '>').replace('&amp;', '&')
71
+ text = text.replace('&quot;', '"').replace('&#39;', "'")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  return text.strip()
74
 
75
  def fix_url_citation(url: str) -> str:
76
  """
77
+ Analisa uma URL e remove TODAS as tags HTML de TODOS os parâmetros de texto,
78
+ além de aplicar correções específicas para citation
79
  """
80
  try:
81
  # Parse da URL
82
  parsed_url = urlparse(url)
83
  query_params = parse_qs(parsed_url.query)
84
 
85
+ # TODOS os parâmetros que podem conter texto e tags HTML
86
+ text_params = ['text', 'title', 'headline', 'citation']
87
 
88
+ # Processar TODOS os parâmetros de texto
89
  for param in text_params:
90
  if param in query_params and query_params[param]:
91
  original_text = query_params[param][0]
92
+
93
+ # Primeiro, limpar todas as tags HTML
94
+ cleaned_text = clean_text_content_improved(original_text)
95
+
96
+ # Se for citation, aplicar correção específica das aspas
97
+ if param == 'citation':
98
+ cleaned_text = fix_citation_quotes(cleaned_text)
99
+
100
  query_params[param] = [cleaned_text]
101
 
 
 
 
 
 
 
102
  # Reconstruir a query string
103
  new_query = urlencode(
104
  {k: v[0] if isinstance(v, list) and len(v) == 1 else v for k, v in query_params.items()},