Spaces:
Running
Running
Update modules/text_analysis/semantic_analysis.py
Browse files
modules/text_analysis/semantic_analysis.py
CHANGED
|
@@ -71,7 +71,100 @@ ENTITY_LABELS = {
|
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
##############################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def perform_semantic_analysis(text, nlp, lang_code):
|
| 76 |
"""
|
| 77 |
Realiza el análisis semántico completo del texto.
|
|
@@ -125,25 +218,53 @@ def fig_to_html(fig):
|
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
-
def identify_key_concepts(doc):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
def create_concept_graph(doc, key_concepts):
|
|
|
|
|
|
|
|
|
|
| 136 |
G = nx.Graph()
|
| 137 |
-
for concept
|
| 138 |
-
|
| 139 |
for sent in doc.sents:
|
| 140 |
-
|
| 141 |
-
for
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
if G.has_edge(concept1, concept2):
|
| 144 |
G[concept1][concept2]['weight'] += 1
|
| 145 |
else:
|
| 146 |
G.add_edge(concept1, concept2, weight=1)
|
|
|
|
| 147 |
return G
|
| 148 |
|
| 149 |
def visualize_concept_graph(G, lang_code):
|
|
|
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
+
CUSTOM_STOPWORDS = {
|
| 75 |
+
'es': {
|
| 76 |
+
# Artículos
|
| 77 |
+
'el', 'la', 'los', 'las', 'un', 'una', 'unos', 'unas',
|
| 78 |
+
# Preposiciones comunes
|
| 79 |
+
'a', 'ante', 'bajo', 'con', 'contra', 'de', 'desde', 'en',
|
| 80 |
+
'entre', 'hacia', 'hasta', 'para', 'por', 'según', 'sin',
|
| 81 |
+
'sobre', 'tras', 'durante', 'mediante',
|
| 82 |
+
# Conjunciones
|
| 83 |
+
'y', 'e', 'ni', 'o', 'u', 'pero', 'sino', 'porque',
|
| 84 |
+
# Pronombres
|
| 85 |
+
'yo', 'tú', 'él', 'ella', 'nosotros', 'vosotros', 'ellos',
|
| 86 |
+
'ellas', 'este', 'esta', 'ese', 'esa', 'aquel', 'aquella',
|
| 87 |
+
# Verbos auxiliares comunes
|
| 88 |
+
'ser', 'estar', 'haber', 'tener',
|
| 89 |
+
# Palabras comunes en textos académicos
|
| 90 |
+
'además', 'también', 'asimismo', 'sin embargo', 'no obstante',
|
| 91 |
+
'por lo tanto', 'entonces', 'así', 'luego', 'pues',
|
| 92 |
+
# Números escritos
|
| 93 |
+
'uno', 'dos', 'tres', 'primer', 'primera', 'segundo', 'segunda',
|
| 94 |
+
# Otras palabras comunes
|
| 95 |
+
'cada', 'todo', 'toda', 'todos', 'todas', 'otro', 'otra',
|
| 96 |
+
'donde', 'cuando', 'como', 'que', 'cual', 'quien',
|
| 97 |
+
'cuyo', 'cuya', 'hay', 'solo', 'ver', 'si', 'no',
|
| 98 |
+
# Símbolos y caracteres especiales
|
| 99 |
+
'#', '@', '/', '*', '+', '-', '=', '$', '%'
|
| 100 |
+
},
|
| 101 |
+
'en': {
|
| 102 |
+
# Articles
|
| 103 |
+
'the', 'a', 'an',
|
| 104 |
+
# Common prepositions
|
| 105 |
+
'in', 'on', 'at', 'by', 'for', 'with', 'about', 'against',
|
| 106 |
+
'between', 'into', 'through', 'during', 'before', 'after',
|
| 107 |
+
'above', 'below', 'to', 'from', 'up', 'down', 'of',
|
| 108 |
+
# Conjunctions
|
| 109 |
+
'and', 'or', 'but', 'nor', 'so', 'for', 'yet',
|
| 110 |
+
# Pronouns
|
| 111 |
+
'i', 'you', 'he', 'she', 'it', 'we', 'they', 'this',
|
| 112 |
+
'that', 'these', 'those', 'my', 'your', 'his', 'her',
|
| 113 |
+
# Auxiliary verbs
|
| 114 |
+
'be', 'am', 'is', 'are', 'was', 'were', 'been', 'have',
|
| 115 |
+
'has', 'had', 'do', 'does', 'did',
|
| 116 |
+
# Common academic words
|
| 117 |
+
'therefore', 'however', 'thus', 'hence', 'moreover',
|
| 118 |
+
'furthermore', 'nevertheless',
|
| 119 |
+
# Numbers written
|
| 120 |
+
'one', 'two', 'three', 'first', 'second', 'third',
|
| 121 |
+
# Other common words
|
| 122 |
+
'where', 'when', 'how', 'what', 'which', 'who',
|
| 123 |
+
'whom', 'whose', 'there', 'here', 'just', 'only',
|
| 124 |
+
# Symbols and special characters
|
| 125 |
+
'#', '@', '/', '*', '+', '-', '=', '$', '%'
|
| 126 |
+
],
|
| 127 |
+
'fr': {
|
| 128 |
+
# Articles
|
| 129 |
+
'le', 'la', 'les', 'un', 'une', 'des',
|
| 130 |
+
# Prepositions
|
| 131 |
+
'à', 'de', 'dans', 'sur', 'en', 'par', 'pour', 'avec',
|
| 132 |
+
'sans', 'sous', 'entre', 'derrière', 'chez', 'avant',
|
| 133 |
+
# Conjunctions
|
| 134 |
+
'et', 'ou', 'mais', 'donc', 'car', 'ni', 'or',
|
| 135 |
+
# Pronouns
|
| 136 |
+
'je', 'tu', 'il', 'elle', 'nous', 'vous', 'ils',
|
| 137 |
+
'elles', 'ce', 'cette', 'ces', 'celui', 'celle',
|
| 138 |
+
# Auxiliary verbs
|
| 139 |
+
'être', 'avoir', 'faire',
|
| 140 |
+
# Academic words
|
| 141 |
+
'donc', 'cependant', 'néanmoins', 'ainsi', 'toutefois',
|
| 142 |
+
'pourtant', 'alors',
|
| 143 |
+
# Numbers
|
| 144 |
+
'un', 'deux', 'trois', 'premier', 'première', 'second',
|
| 145 |
+
# Other common words
|
| 146 |
+
'où', 'quand', 'comment', 'que', 'qui', 'quoi',
|
| 147 |
+
'quel', 'quelle', 'plus', 'moins',
|
| 148 |
+
# Symbols
|
| 149 |
+
'#', '@', '/', '*', '+', '-', '=', '$', '%'
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
##############################################################################################################
|
| 154 |
+
def get_stopwords(lang_code):
|
| 155 |
+
"""
|
| 156 |
+
Obtiene el conjunto de stopwords para un idioma específico.
|
| 157 |
+
Combina las stopwords de spaCy con las personalizadas.
|
| 158 |
+
"""
|
| 159 |
+
try:
|
| 160 |
+
nlp = spacy.load(f'{lang_code}_core_news_sm')
|
| 161 |
+
spacy_stopwords = nlp.Defaults.stop_words
|
| 162 |
+
custom_stopwords = CUSTOM_STOPWORDS.get(lang_code, set())
|
| 163 |
+
return spacy_stopwords.union(custom_stopwords)
|
| 164 |
+
except:
|
| 165 |
+
return CUSTOM_STOPWORDS.get(lang_code, set())
|
| 166 |
+
|
| 167 |
+
|
| 168 |
def perform_semantic_analysis(text, nlp, lang_code):
|
| 169 |
"""
|
| 170 |
Realiza el análisis semántico completo del texto.
|
|
|
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
+
def identify_key_concepts(doc, min_freq=2, min_length=3):
|
| 222 |
+
"""
|
| 223 |
+
Identifica conceptos clave en el texto, excluyendo stopwords
|
| 224 |
+
y aplicando criterios de frecuencia y longitud.
|
| 225 |
+
"""
|
| 226 |
+
stopwords = get_stopwords(doc.lang_)
|
| 227 |
+
word_freq = Counter()
|
| 228 |
+
|
| 229 |
+
for token in doc:
|
| 230 |
+
if (token.text.lower() not in stopwords and # No es stopword
|
| 231 |
+
token.is_alpha and # Es alfabético
|
| 232 |
+
len(token.text) >= min_length and # Longitud mínima
|
| 233 |
+
not token.is_punct and # No es puntuación
|
| 234 |
+
not token.like_num): # No es número
|
| 235 |
+
|
| 236 |
+
# Usar el lema en lugar del token para unificar variantes
|
| 237 |
+
word_freq[token.lemma_] += 1
|
| 238 |
+
|
| 239 |
+
# Filtrar por frecuencia mínima y ordenar por frecuencia
|
| 240 |
+
key_concepts = [(word, freq) for word, freq in word_freq.items()
|
| 241 |
+
if freq >= min_freq]
|
| 242 |
+
key_concepts.sort(key=lambda x: x[1], reverse=True)
|
| 243 |
+
|
| 244 |
+
return key_concepts[:10] # Retornar los 10 conceptos más frecuentes
|
| 245 |
|
| 246 |
|
| 247 |
def create_concept_graph(doc, key_concepts):
|
| 248 |
+
"""
|
| 249 |
+
Crea un grafo de relaciones entre conceptos.
|
| 250 |
+
"""
|
| 251 |
G = nx.Graph()
|
| 252 |
+
concept_words = {concept[0] for concept in key_concepts}
|
| 253 |
+
|
| 254 |
for sent in doc.sents:
|
| 255 |
+
sentence_concepts = []
|
| 256 |
+
for token in sent:
|
| 257 |
+
if token.lemma_ in concept_words:
|
| 258 |
+
sentence_concepts.append(token.lemma_)
|
| 259 |
+
|
| 260 |
+
# Crear conexiones entre conceptos en la misma oración
|
| 261 |
+
for i, concept1 in enumerate(sentence_concepts):
|
| 262 |
+
for concept2 in sentence_concepts[i+1:]:
|
| 263 |
if G.has_edge(concept1, concept2):
|
| 264 |
G[concept1][concept2]['weight'] += 1
|
| 265 |
else:
|
| 266 |
G.add_edge(concept1, concept2, weight=1)
|
| 267 |
+
|
| 268 |
return G
|
| 269 |
|
| 270 |
def visualize_concept_graph(G, lang_code):
|