Create morpho_analysis.py
Browse files- modules/morpho_analysis.py +33 -0
modules/morpho_analysis.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# /modules/morpho_analysis.py
|
2 |
+
import spacy
|
3 |
+
from collections import Counter
|
4 |
+
|
5 |
+
# Define colors for grammatical categories
|
6 |
+
POS_COLORS = {
|
7 |
+
# ... (mantén tus definiciones de colores aquí)
|
8 |
+
}
|
9 |
+
|
10 |
+
POS_TRANSLATIONS = {
|
11 |
+
# ... (mantén tus traducciones aquí)
|
12 |
+
}
|
13 |
+
|
14 |
+
def get_repeated_words_colors(doc):
|
15 |
+
word_counts = Counter(token.text.lower() for token in doc if token.pos_ != 'PUNCT')
|
16 |
+
repeated_words = {word: count for word, count in word_counts.items() if count > 1}
|
17 |
+
|
18 |
+
word_colors = {}
|
19 |
+
for token in doc:
|
20 |
+
if token.text.lower() in repeated_words:
|
21 |
+
word_colors[token.text.lower()] = POS_COLORS.get(token.pos_, '#FFFFFF')
|
22 |
+
|
23 |
+
return word_colors
|
24 |
+
|
25 |
+
def highlight_repeated_words(doc, word_colors):
|
26 |
+
highlighted_text = []
|
27 |
+
for token in doc:
|
28 |
+
if token.text.lower() in word_colors:
|
29 |
+
color = word_colors[token.text.lower()]
|
30 |
+
highlighted_text.append(f'<span style="background-color: {color};">{token.text}</span>')
|
31 |
+
else:
|
32 |
+
highlighted_text.append(token.text)
|
33 |
+
return ' '.join(highlighted_text)
|