Spaces:
Sleeping
Sleeping
Update modules/text_analysis/discourse_analysis.py
Browse files
modules/text_analysis/discourse_analysis.py
CHANGED
|
@@ -18,7 +18,157 @@ from .semantic_analysis import (
|
|
| 18 |
POS_TRANSLATIONS,
|
| 19 |
ENTITY_LABELS
|
| 20 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def compare_semantic_analysis(text1, text2, nlp, lang):
|
| 23 |
"""
|
| 24 |
Realiza el análisis semántico comparativo entre dos textos
|
|
|
|
| 18 |
POS_TRANSLATIONS,
|
| 19 |
ENTITY_LABELS
|
| 20 |
)
|
| 21 |
+
#####################
|
| 22 |
+
# Define colors for grammatical categories
|
| 23 |
+
POS_COLORS = {
|
| 24 |
+
'ADJ': '#FFA07A', 'ADP': '#98FB98', 'ADV': '#87CEFA', 'AUX': '#DDA0DD',
|
| 25 |
+
'CCONJ': '#F0E68C', 'DET': '#FFB6C1', 'INTJ': '#FF6347', 'NOUN': '#90EE90',
|
| 26 |
+
'NUM': '#FAFAD2', 'PART': '#D3D3D3', 'PRON': '#FFA500', 'PROPN': '#20B2AA',
|
| 27 |
+
'SCONJ': '#DEB887', 'SYM': '#7B68EE', 'VERB': '#FF69B4', 'X': '#A9A9A9',
|
| 28 |
+
}
|
| 29 |
|
| 30 |
+
POS_TRANSLATIONS = {
|
| 31 |
+
'es': {
|
| 32 |
+
'ADJ': 'Adjetivo', 'ADP': 'Preposición', 'ADV': 'Adverbio', 'AUX': 'Auxiliar',
|
| 33 |
+
'CCONJ': 'Conjunción Coordinante', 'DET': 'Determinante', 'INTJ': 'Interjección',
|
| 34 |
+
'NOUN': 'Sustantivo', 'NUM': 'Número', 'PART': 'Partícula', 'PRON': 'Pronombre',
|
| 35 |
+
'PROPN': 'Nombre Propio', 'SCONJ': 'Conjunción Subordinante', 'SYM': 'Símbolo',
|
| 36 |
+
'VERB': 'Verbo', 'X': 'Otro',
|
| 37 |
+
},
|
| 38 |
+
'en': {
|
| 39 |
+
'ADJ': 'Adjective', 'ADP': 'Preposition', 'ADV': 'Adverb', 'AUX': 'Auxiliary',
|
| 40 |
+
'CCONJ': 'Coordinating Conjunction', 'DET': 'Determiner', 'INTJ': 'Interjection',
|
| 41 |
+
'NOUN': 'Noun', 'NUM': 'Number', 'PART': 'Particle', 'PRON': 'Pronoun',
|
| 42 |
+
'PROPN': 'Proper Noun', 'SCONJ': 'Subordinating Conjunction', 'SYM': 'Symbol',
|
| 43 |
+
'VERB': 'Verb', 'X': 'Other',
|
| 44 |
+
},
|
| 45 |
+
'fr': {
|
| 46 |
+
'ADJ': 'Adjectif', 'ADP': 'Préposition', 'ADV': 'Adverbe', 'AUX': 'Auxiliaire',
|
| 47 |
+
'CCONJ': 'Conjonction de Coordination', 'DET': 'Déterminant', 'INTJ': 'Interjection',
|
| 48 |
+
'NOUN': 'Nom', 'NUM': 'Nombre', 'PART': 'Particule', 'PRON': 'Pronom',
|
| 49 |
+
'PROPN': 'Nom Propre', 'SCONJ': 'Conjonction de Subordination', 'SYM': 'Symbole',
|
| 50 |
+
'VERB': 'Verbe', 'X': 'Autre',
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
ENTITY_LABELS = {
|
| 55 |
+
'es': {
|
| 56 |
+
"Personas": "lightblue",
|
| 57 |
+
"Lugares": "lightcoral",
|
| 58 |
+
"Inventos": "lightgreen",
|
| 59 |
+
"Fechas": "lightyellow",
|
| 60 |
+
"Conceptos": "lightpink"
|
| 61 |
+
},
|
| 62 |
+
'en': {
|
| 63 |
+
"People": "lightblue",
|
| 64 |
+
"Places": "lightcoral",
|
| 65 |
+
"Inventions": "lightgreen",
|
| 66 |
+
"Dates": "lightyellow",
|
| 67 |
+
"Concepts": "lightpink"
|
| 68 |
+
},
|
| 69 |
+
'fr': {
|
| 70 |
+
"Personnes": "lightblue",
|
| 71 |
+
"Lieux": "lightcoral",
|
| 72 |
+
"Inventions": "lightgreen",
|
| 73 |
+
"Dates": "lightyellow",
|
| 74 |
+
"Concepts": "lightpink"
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
CUSTOM_STOPWORDS = {
|
| 79 |
+
'es': {
|
| 80 |
+
# Artículos
|
| 81 |
+
'el', 'la', 'los', 'las', 'un', 'una', 'unos', 'unas',
|
| 82 |
+
# Preposiciones comunes
|
| 83 |
+
'a', 'ante', 'bajo', 'con', 'contra', 'de', 'desde', 'en',
|
| 84 |
+
'entre', 'hacia', 'hasta', 'para', 'por', 'según', 'sin',
|
| 85 |
+
'sobre', 'tras', 'durante', 'mediante',
|
| 86 |
+
# Conjunciones
|
| 87 |
+
'y', 'e', 'ni', 'o', 'u', 'pero', 'sino', 'porque',
|
| 88 |
+
# Pronombres
|
| 89 |
+
'yo', 'tú', 'él', 'ella', 'nosotros', 'vosotros', 'ellos',
|
| 90 |
+
'ellas', 'este', 'esta', 'ese', 'esa', 'aquel', 'aquella',
|
| 91 |
+
# Verbos auxiliares comunes
|
| 92 |
+
'ser', 'estar', 'haber', 'tener',
|
| 93 |
+
# Palabras comunes en textos académicos
|
| 94 |
+
'además', 'también', 'asimismo', 'sin embargo', 'no obstante',
|
| 95 |
+
'por lo tanto', 'entonces', 'así', 'luego', 'pues',
|
| 96 |
+
# Números escritos
|
| 97 |
+
'uno', 'dos', 'tres', 'primer', 'primera', 'segundo', 'segunda',
|
| 98 |
+
# Otras palabras comunes
|
| 99 |
+
'cada', 'todo', 'toda', 'todos', 'todas', 'otro', 'otra',
|
| 100 |
+
'donde', 'cuando', 'como', 'que', 'cual', 'quien',
|
| 101 |
+
'cuyo', 'cuya', 'hay', 'solo', 'ver', 'si', 'no',
|
| 102 |
+
# Símbolos y caracteres especiales
|
| 103 |
+
'#', '@', '/', '*', '+', '-', '=', '$', '%'
|
| 104 |
+
},
|
| 105 |
+
'en': {
|
| 106 |
+
# Articles
|
| 107 |
+
'the', 'a', 'an',
|
| 108 |
+
# Common prepositions
|
| 109 |
+
'in', 'on', 'at', 'by', 'for', 'with', 'about', 'against',
|
| 110 |
+
'between', 'into', 'through', 'during', 'before', 'after',
|
| 111 |
+
'above', 'below', 'to', 'from', 'up', 'down', 'of',
|
| 112 |
+
# Conjunctions
|
| 113 |
+
'and', 'or', 'but', 'nor', 'so', 'for', 'yet',
|
| 114 |
+
# Pronouns
|
| 115 |
+
'i', 'you', 'he', 'she', 'it', 'we', 'they', 'this',
|
| 116 |
+
'that', 'these', 'those', 'my', 'your', 'his', 'her',
|
| 117 |
+
# Auxiliary verbs
|
| 118 |
+
'be', 'am', 'is', 'are', 'was', 'were', 'been', 'have',
|
| 119 |
+
'has', 'had', 'do', 'does', 'did',
|
| 120 |
+
# Common academic words
|
| 121 |
+
'therefore', 'however', 'thus', 'hence', 'moreover',
|
| 122 |
+
'furthermore', 'nevertheless',
|
| 123 |
+
# Numbers written
|
| 124 |
+
'one', 'two', 'three', 'first', 'second', 'third',
|
| 125 |
+
# Other common words
|
| 126 |
+
'where', 'when', 'how', 'what', 'which', 'who',
|
| 127 |
+
'whom', 'whose', 'there', 'here', 'just', 'only',
|
| 128 |
+
# Symbols and special characters
|
| 129 |
+
'#', '@', '/', '*', '+', '-', '=', '$', '%'
|
| 130 |
+
},
|
| 131 |
+
'fr': {
|
| 132 |
+
# Articles
|
| 133 |
+
'le', 'la', 'les', 'un', 'une', 'des',
|
| 134 |
+
# Prepositions
|
| 135 |
+
'à', 'de', 'dans', 'sur', 'en', 'par', 'pour', 'avec',
|
| 136 |
+
'sans', 'sous', 'entre', 'derrière', 'chez', 'avant',
|
| 137 |
+
# Conjunctions
|
| 138 |
+
'et', 'ou', 'mais', 'donc', 'car', 'ni', 'or',
|
| 139 |
+
# Pronouns
|
| 140 |
+
'je', 'tu', 'il', 'elle', 'nous', 'vous', 'ils',
|
| 141 |
+
'elles', 'ce', 'cette', 'ces', 'celui', 'celle',
|
| 142 |
+
# Auxiliary verbs
|
| 143 |
+
'être', 'avoir', 'faire',
|
| 144 |
+
# Academic words
|
| 145 |
+
'donc', 'cependant', 'néanmoins', 'ainsi', 'toutefois',
|
| 146 |
+
'pourtant', 'alors',
|
| 147 |
+
# Numbers
|
| 148 |
+
'un', 'deux', 'trois', 'premier', 'première', 'second',
|
| 149 |
+
# Other common words
|
| 150 |
+
'où', 'quand', 'comment', 'que', 'qui', 'quoi',
|
| 151 |
+
'quel', 'quelle', 'plus', 'moins',
|
| 152 |
+
# Symbols
|
| 153 |
+
'#', '@', '/', '*', '+', '-', '=', '$', '%'
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
##############################################################################################################
|
| 158 |
+
def get_stopwords(lang_code):
|
| 159 |
+
"""
|
| 160 |
+
Obtiene el conjunto de stopwords para un idioma específico.
|
| 161 |
+
Combina las stopwords de spaCy con las personalizadas.
|
| 162 |
+
"""
|
| 163 |
+
try:
|
| 164 |
+
nlp = spacy.load(f'{lang_code}_core_news_sm')
|
| 165 |
+
spacy_stopwords = nlp.Defaults.stop_words
|
| 166 |
+
custom_stopwords = CUSTOM_STOPWORDS.get(lang_code, set())
|
| 167 |
+
return spacy_stopwords.union(custom_stopwords)
|
| 168 |
+
except:
|
| 169 |
+
return CUSTOM_STOPWORDS.get(lang_code, set())
|
| 170 |
+
|
| 171 |
+
#################
|
| 172 |
def compare_semantic_analysis(text1, text2, nlp, lang):
|
| 173 |
"""
|
| 174 |
Realiza el análisis semántico comparativo entre dos textos
|