# /modules/morpho_analysis.py | |
import spacy | |
from collections import Counter | |
# Define colors for grammatical categories | |
POS_COLORS = { | |
# ... (mantén tus definiciones de colores aquí) | |
} | |
POS_TRANSLATIONS = { | |
# ... (mantén tus traducciones aquí) | |
} | |
def get_repeated_words_colors(doc): | |
word_counts = Counter(token.text.lower() for token in doc if token.pos_ != 'PUNCT') | |
repeated_words = {word: count for word, count in word_counts.items() if count > 1} | |
word_colors = {} | |
for token in doc: | |
if token.text.lower() in repeated_words: | |
word_colors[token.text.lower()] = POS_COLORS.get(token.pos_, '#FFFFFF') | |
return word_colors | |
def highlight_repeated_words(doc, word_colors): | |
highlighted_text = [] | |
for token in doc: | |
if token.text.lower() in word_colors: | |
color = word_colors[token.text.lower()] | |
highlighted_text.append(f'<span style="background-color: {color};">{token.text}</span>') | |
else: | |
highlighted_text.append(token.text) | |
return ' '.join(highlighted_text) |