Update modules/text_analysis/morpho_analysis.py
Browse files
modules/text_analysis/morpho_analysis.py
CHANGED
@@ -156,87 +156,6 @@ def generate_arc_diagram(doc):
|
|
156 |
logger.error(f"Error general en generate_arc_diagram: {str(e)}")
|
157 |
return None
|
158 |
|
159 |
-
"""
|
160 |
-
def generate_arc_diagram(doc):
|
161 |
-
sentences = list(doc.sents)
|
162 |
-
arc_diagrams = []
|
163 |
-
for sent in sentences:
|
164 |
-
html = displacy.render(sent, style="dep", options={"distance": 100})
|
165 |
-
html = html.replace('height="375"', 'height="200"')
|
166 |
-
html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
|
167 |
-
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
|
168 |
-
arc_diagrams.append(html)
|
169 |
-
return arc_diagrams
|
170 |
-
|
171 |
-
|
172 |
-
def generate_arc_diagram(doc):
|
173 |
-
|
174 |
-
Genera diagramas de arco para cada oración en el documento.
|
175 |
-
|
176 |
-
Args:
|
177 |
-
doc: Documento procesado por spaCy
|
178 |
-
Returns:
|
179 |
-
list: Lista de diagramas en formato HTML
|
180 |
-
|
181 |
-
arc_diagrams = []
|
182 |
-
for sent in doc.sents:
|
183 |
-
words = [token.text for token in sent]
|
184 |
-
# Calculamos el ancho del SVG basado en la longitud de la oración
|
185 |
-
svg_width = max(600, len(words) * 120)
|
186 |
-
# Altura fija para cada oración
|
187 |
-
svg_height = 350 # Controla la altura del SVG
|
188 |
-
|
189 |
-
# Renderizamos el diagrama de dependencias
|
190 |
-
html = displacy.render(sent, style="dep", options={
|
191 |
-
"add_lemma":False, # Introduced in version 2.2.4, this argument prints the lemma’s in a separate row below the token texts.
|
192 |
-
"arrow_spacing": 12, #This argument is used for adjusting the spacing between arrows in px to avoid overlaps.
|
193 |
-
"arrow_width": 2, #This argument is used for adjusting the width of arrow head in px.
|
194 |
-
"arrow_stroke": 2, #This argument is used for adjusting the width of arrow path in px.
|
195 |
-
"collapse_punct": True, #It attaches punctuation to the tokens.
|
196 |
-
"collapse_phrases": False, # This argument merges the noun phrases into one token.
|
197 |
-
"compact":False, # If you will take this argument as true, you will get the “Compact mode” with square arrows that takes up less space.
|
198 |
-
"color": "#ffffff",
|
199 |
-
"bg": "#0d6efd",
|
200 |
-
"compact": False, #Put the value of this argument True, if you want to use fine-grained part-of-speech tags (Token.tag_), instead of coarse-grained tags (Token.pos_).
|
201 |
-
"distance": 100, # Aumentamos la distancia entre palabras
|
202 |
-
"fine_grained": False, #Put the value of this argument True, if you want to use fine-grained part-of-speech tags (Token.tag_), instead of coarse-grained tags (Token.pos_).
|
203 |
-
"offset_x": 55, # This argument is used for spacing on left side of the SVG in px.
|
204 |
-
"word_spacing": 25, #This argument is used for adjusting the vertical spacing between words and arcs in px.
|
205 |
-
})
|
206 |
-
|
207 |
-
# Ajustamos el tamaño del SVG y el viewBox
|
208 |
-
html = re.sub(r'width="(\d+)"', f'width="{svg_width}"', html)
|
209 |
-
html = re.sub(r'height="(\d+)"', f'height="{svg_height}"', html)
|
210 |
-
html = re.sub(r'<svg', f'<svg viewBox="0 0 {svg_width} {svg_height}"', html)
|
211 |
-
|
212 |
-
#html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
|
213 |
-
#html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
|
214 |
-
|
215 |
-
# Movemos todo el contenido hacia abajo
|
216 |
-
#html = html.replace('<g', f'<g transform="translate(50, {svg_height - 200})"')
|
217 |
-
|
218 |
-
# Movemos todo el contenido hacia arriba para eliminar el espacio vacío en la parte superior
|
219 |
-
html = re.sub(r'<g transform="translate\((\d+),(\d+)\)"',
|
220 |
-
lambda m: f'<g transform="translate({m.group(1)},10)"', html)
|
221 |
-
|
222 |
-
|
223 |
-
# Ajustamos la posición de las etiquetas de las palabras
|
224 |
-
html = html.replace('dy="1em"', 'dy="-1em"')
|
225 |
-
|
226 |
-
# Ajustamos la posición de las etiquetas POS
|
227 |
-
html = html.replace('dy="0.25em"', 'dy="-3em"')
|
228 |
-
|
229 |
-
# Aumentamos el tamaño de la fuente para las etiquetas POS
|
230 |
-
html = html.replace('.displacy-tag {', '.displacy-tag { font-size: 14px;')
|
231 |
-
|
232 |
-
# Rotamos las etiquetas de las palabras para mejorar la legibilidad
|
233 |
-
#html = html.replace('class="displacy-label"', 'class="displacy-label" transform="rotate(30)"')
|
234 |
-
|
235 |
-
arc_diagrams.append(html)
|
236 |
-
return arc_diagrams
|
237 |
-
"""
|
238 |
-
|
239 |
-
|
240 |
|
241 |
#################################################################################################
|
242 |
def get_detailed_pos_analysis(doc):
|
|
|
156 |
logger.error(f"Error general en generate_arc_diagram: {str(e)}")
|
157 |
return None
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
#################################################################################################
|
161 |
def get_detailed_pos_analysis(doc):
|