Update modules/text_analysis/morpho_analysis.py
Browse filesEsta versi贸n actualizada del archivo morph_analysis.py incluye las siguientes mejoras:
get_detailed_pos_analysis: Proporciona un an谩lisis detallado de las categor铆as gramaticales, incluyendo conteos, porcentajes y ejemplos.
get_morphological_analysis: Ofrece un an谩lisis morfol贸gico detallado de las palabras principales del texto.
get_sentence_structure_analysis: Analiza la estructura de las oraciones, identificando elementos clave como sujetos, objetos y verbos.
perform_advanced_morphosyntactic_analysis: Funci贸n principal que combina todos los an谩lisis anteriores y el diagrama de arco existente.
modules/text_analysis/morpho_analysis.py
CHANGED
@@ -114,4 +114,75 @@ def generate_arc_diagram(doc, lang_code):
|
|
114 |
html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
|
115 |
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
|
116 |
arc_diagrams.append(html)
|
117 |
-
return arc_diagrams
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
|
115 |
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', lambda m: f'<g transform="translate({m.group(1)},50)"', html)
|
116 |
arc_diagrams.append(html)
|
117 |
+
return arc_diagrams
|
118 |
+
|
119 |
+
#################################################################################################
|
120 |
+
def get_detailed_pos_analysis(doc):
|
121 |
+
"""
|
122 |
+
Realiza un an谩lisis detallado de las categor铆as gramaticales (POS) en el texto.
|
123 |
+
"""
|
124 |
+
pos_counts = Counter(token.pos_ for token in doc)
|
125 |
+
total_tokens = len(doc)
|
126 |
+
pos_analysis = []
|
127 |
+
for pos, count in pos_counts.items():
|
128 |
+
percentage = (count / total_tokens) * 100
|
129 |
+
pos_analysis.append({
|
130 |
+
'pos': pos,
|
131 |
+
'count': count,
|
132 |
+
'percentage': round(percentage, 2),
|
133 |
+
'examples': [token.text for token in doc if token.pos_ == pos][:5] # Primeros 5 ejemplos
|
134 |
+
})
|
135 |
+
return sorted(pos_analysis, key=lambda x: x['count'], reverse=True)
|
136 |
+
|
137 |
+
#################################################################################################
|
138 |
+
def get_morphological_analysis(doc):
|
139 |
+
"""
|
140 |
+
Realiza un an谩lisis morfol贸gico detallado de las palabras en el texto.
|
141 |
+
"""
|
142 |
+
morphology_analysis = []
|
143 |
+
for token in doc:
|
144 |
+
if token.pos_ in ['NOUN', 'VERB', 'ADJ', 'ADV']: # Enfocarse en categor铆as principales
|
145 |
+
morphology_analysis.append({
|
146 |
+
'text': token.text,
|
147 |
+
'lemma': token.lemma_,
|
148 |
+
'pos': token.pos_,
|
149 |
+
'tag': token.tag_,
|
150 |
+
'dep': token.dep_,
|
151 |
+
'shape': token.shape_,
|
152 |
+
'is_alpha': token.is_alpha,
|
153 |
+
'is_stop': token.is_stop,
|
154 |
+
'morph': str(token.morph)
|
155 |
+
})
|
156 |
+
return morphology_analysis
|
157 |
+
|
158 |
+
#################################################################################################
|
159 |
+
def get_sentence_structure_analysis(doc):
|
160 |
+
"""
|
161 |
+
Analiza la estructura de las oraciones en el texto.
|
162 |
+
"""
|
163 |
+
sentence_analysis = []
|
164 |
+
for sent in doc.sents:
|
165 |
+
sentence_analysis.append({
|
166 |
+
'text': sent.text,
|
167 |
+
'root': sent.root.text,
|
168 |
+
'root_pos': sent.root.pos_,
|
169 |
+
'num_tokens': len(sent),
|
170 |
+
'num_words': len([token for token in sent if token.is_alpha]),
|
171 |
+
'subjects': [token.text for token in sent if "subj" in token.dep_],
|
172 |
+
'objects': [token.text for token in sent if "obj" in token.dep_],
|
173 |
+
'verbs': [token.text for token in sent if token.pos_ == "VERB"]
|
174 |
+
})
|
175 |
+
return sentence_analysis
|
176 |
+
|
177 |
+
#################################################################################################
|
178 |
+
def perform_advanced_morphosyntactic_analysis(text, nlp):
|
179 |
+
"""
|
180 |
+
Realiza un an谩lisis morfosint谩ctico avanzado del texto.
|
181 |
+
"""
|
182 |
+
doc = nlp(text)
|
183 |
+
return {
|
184 |
+
'pos_analysis': get_detailed_pos_analysis(doc),
|
185 |
+
'morphological_analysis': get_morphological_analysis(doc),
|
186 |
+
'sentence_structure': get_sentence_structure_analysis(doc),
|
187 |
+
'arc_diagram': generate_arc_diagram(doc, nlp.lang)
|
188 |
+
}
|