AIdeaText commited on
Commit
51a9744
·
verified ·
1 Parent(s): 988c451

Update modules/text_analysis/morpho_analysis.py

Browse files
modules/text_analysis/morpho_analysis.py CHANGED
@@ -114,14 +114,61 @@ def highlight_repeated_words(doc, word_colors):
114
  return ' '.join(highlighted_text)
115
 
116
  #################################################################################################
117
- def generate_arc_diagram(doc, lang_code):
118
- sentences = list(doc.sents)
119
  arc_diagrams = []
120
- for sent in sentences:
121
- html = displacy.render(sent, style="dep", options={"distance": 100})
122
- html = html.replace('height="375"', 'height="200"')
123
- html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
124
- html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  arc_diagrams.append(html)
126
  return arc_diagrams
127
 
 
114
  return ' '.join(highlighted_text)
115
 
116
  #################################################################################################
117
+ def generate_arc_diagram(doc):
 
118
  arc_diagrams = []
119
+ for sent in doc.sents:
120
+ words = [token.text for token in sent]
121
+ # Calculamos el ancho del SVG basado en la longitud de la oración
122
+ svg_width = max(600, len(words) * 120)
123
+ # Altura fija para cada oración
124
+ svg_height = 350 # Controla la altura del SVG
125
+
126
+ # Renderizamos el diagrama de dependencias
127
+ html = displacy.render(sent, style="dep", options={
128
+ "add_lemma":False, # Introduced in version 2.2.4, this argument prints the lemma’s in a separate row below the token texts.
129
+ "arrow_spacing": 12, #This argument is used for adjusting the spacing between arrows in px to avoid overlaps.
130
+ "arrow_width": 2, #This argument is used for adjusting the width of arrow head in px.
131
+ "arrow_stroke": 2, #This argument is used for adjusting the width of arrow path in px.
132
+ "collapse_punct": True, #It attaches punctuation to the tokens.
133
+ "collapse_phrases": False, # This argument merges the noun phrases into one token.
134
+ "compact":False, # If you will take this argument as true, you will get the “Compact mode” with square arrows that takes up less space.
135
+ "color": "#ffffff",
136
+ "bg": "#0d6efd",
137
+ "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_).
138
+ "distance": 100, # Aumentamos la distancia entre palabras
139
+ "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_).
140
+ "offset_x": 55, # This argument is used for spacing on left side of the SVG in px.
141
+ "word_spacing": 25, #This argument is used for adjusting the vertical spacing between words and arcs in px.
142
+ })
143
+
144
+ # Ajustamos el tamaño del SVG y el viewBox
145
+ html = re.sub(r'width="(\d+)"', f'width="{svg_width}"', html)
146
+ html = re.sub(r'height="(\d+)"', f'height="{svg_height}"', html)
147
+ html = re.sub(r'<svg', f'<svg viewBox="0 0 {svg_width} {svg_height}"', html)
148
+
149
+ #html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
150
+ #html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
151
+
152
+ # Movemos todo el contenido hacia abajo
153
+ #html = html.replace('<g', f'<g transform="translate(50, {svg_height - 200})"')
154
+
155
+ # Movemos todo el contenido hacia arriba para eliminar el espacio vacío en la parte superior
156
+ html = re.sub(r'<g transform="translate\((\d+),(\d+)\)"',
157
+ lambda m: f'<g transform="translate({m.group(1)},10)"', html)
158
+
159
+
160
+ # Ajustamos la posición de las etiquetas de las palabras
161
+ html = html.replace('dy="1em"', 'dy="-1em"')
162
+
163
+ # Ajustamos la posición de las etiquetas POS
164
+ html = html.replace('dy="0.25em"', 'dy="-3em"')
165
+
166
+ # Aumentamos el tamaño de la fuente para las etiquetas POS
167
+ html = html.replace('.displacy-tag {', '.displacy-tag { font-size: 14px;')
168
+
169
+ # Rotamos las etiquetas de las palabras para mejorar la legibilidad
170
+ #html = html.replace('class="displacy-label"', 'class="displacy-label" transform="rotate(30)"')
171
+
172
  arc_diagrams.append(html)
173
  return arc_diagrams
174