| | |
| | import streamlit as st |
| | import spacy |
| | import networkx as nx |
| | import matplotlib.pyplot as plt |
| | from collections import Counter, defaultdict |
| | from sklearn.feature_extraction.text import TfidfVectorizer |
| | from sklearn.metrics.pairwise import cosine_similarity |
| |
|
| | |
| | POS_COLORS = { |
| | 'ADJ': '#FFA07A', 'ADP': '#98FB98', 'ADV': '#87CEFA', 'AUX': '#DDA0DD', |
| | 'CCONJ': '#F0E68C', 'DET': '#FFB6C1', 'INTJ': '#FF6347', 'NOUN': '#90EE90', |
| | 'NUM': '#FAFAD2', 'PART': '#D3D3D3', 'PRON': '#FFA500', 'PROPN': '#20B2AA', |
| | 'SCONJ': '#DEB887', 'SYM': '#7B68EE', 'VERB': '#FF69B4', 'X': '#A9A9A9', |
| | } |
| |
|
| | POS_TRANSLATIONS = { |
| | 'es': { |
| | 'ADJ': 'Adjetivo', 'ADP': 'Preposici贸n', 'ADV': 'Adverbio', 'AUX': 'Auxiliar', |
| | 'CCONJ': 'Conjunci贸n Coordinante', 'DET': 'Determinante', 'INTJ': 'Interjecci贸n', |
| | 'NOUN': 'Sustantivo', 'NUM': 'N煤mero', 'PART': 'Part铆cula', 'PRON': 'Pronombre', |
| | 'PROPN': 'Nombre Propio', 'SCONJ': 'Conjunci贸n Subordinante', 'SYM': 'S铆mbolo', |
| | 'VERB': 'Verbo', 'X': 'Otro', |
| | }, |
| | 'en': { |
| | 'ADJ': 'Adjective', 'ADP': 'Preposition', 'ADV': 'Adverb', 'AUX': 'Auxiliary', |
| | 'CCONJ': 'Coordinating Conjunction', 'DET': 'Determiner', 'INTJ': 'Interjection', |
| | 'NOUN': 'Noun', 'NUM': 'Number', 'PART': 'Particle', 'PRON': 'Pronoun', |
| | 'PROPN': 'Proper Noun', 'SCONJ': 'Subordinating Conjunction', 'SYM': 'Symbol', |
| | 'VERB': 'Verb', 'X': 'Other', |
| | }, |
| | 'fr': { |
| | 'ADJ': 'Adjectif', 'ADP': 'Pr茅position', 'ADV': 'Adverbe', 'AUX': 'Auxiliaire', |
| | 'CCONJ': 'Conjonction de Coordination', 'DET': 'D茅terminant', 'INTJ': 'Interjection', |
| | 'NOUN': 'Nom', 'NUM': 'Nombre', 'PART': 'Particule', 'PRON': 'Pronom', |
| | 'PROPN': 'Nom Propre', 'SCONJ': 'Conjonction de Subordination', 'SYM': 'Symbole', |
| | 'VERB': 'Verbe', 'X': 'Autre', |
| | } |
| | } |
| |
|
| | ENTITY_LABELS = { |
| | 'es': { |
| | "Personas": "lightblue", |
| | "Lugares": "lightcoral", |
| | "Inventos": "lightgreen", |
| | "Fechas": "lightyellow", |
| | "Conceptos": "lightpink" |
| | }, |
| | 'en': { |
| | "People": "lightblue", |
| | "Places": "lightcoral", |
| | "Inventions": "lightgreen", |
| | "Dates": "lightyellow", |
| | "Concepts": "lightpink" |
| | }, |
| | 'fr': { |
| | "Personnes": "lightblue", |
| | "Lieux": "lightcoral", |
| | "Inventions": "lightgreen", |
| | "Dates": "lightyellow", |
| | "Concepts": "lightpink" |
| | } |
| | } |
| |
|
| | def identify_key_concepts(doc): |
| | word_freq = Counter([token.lemma_.lower() for token in doc if token.pos_ in ['NOUN', 'VERB'] and not token.is_stop]) |
| | key_concepts = word_freq.most_common(10) |
| | return [(concept, float(freq)) for concept, freq in key_concepts] |
| |
|
| | def create_concept_graph(doc, key_concepts): |
| | G = nx.Graph() |
| | |
| | |
| | for concept, freq in key_concepts: |
| | G.add_node(concept, weight=freq) |
| | |
| | |
| | for sent in doc.sents: |
| | sent_concepts = [token.lemma_.lower() for token in sent if token.lemma_.lower() in dict(key_concepts)] |
| | for i, concept1 in enumerate(sent_concepts): |
| | for concept2 in sent_concepts[i+1:]: |
| | if G.has_edge(concept1, concept2): |
| | G[concept1][concept2]['weight'] += 1 |
| | else: |
| | G.add_edge(concept1, concept2, weight=1) |
| | |
| | return G |
| |
|
| | def visualize_concept_graph(G, lang): |
| | fig, ax = plt.subplots(figsize=(12, 8)) |
| | pos = nx.spring_layout(G, k=0.5, iterations=50) |
| | |
| | node_sizes = [G.nodes[node]['weight'] * 100 for node in G.nodes()] |
| | nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color='lightblue', alpha=0.8, ax=ax) |
| | nx.draw_networkx_labels(G, pos, font_size=10, font_weight="bold", ax=ax) |
| | |
| | edge_weights = [G[u][v]['weight'] for u, v in G.edges()] |
| | nx.draw_networkx_edges(G, pos, width=edge_weights, alpha=0.5, ax=ax) |
| | |
| | title = { |
| | 'es': "Relaciones entre Conceptos Clave", |
| | 'en': "Key Concept Relations", |
| | 'fr': "Relations entre Concepts Cl茅s" |
| | } |
| | ax.set_title(title[lang], fontsize=16) |
| | ax.axis('off') |
| | |
| | plt.tight_layout() |
| | return fig |
| |
|
| | def perform_semantic_analysis(text, nlp, lang): |
| | doc = nlp(text) |
| | |
| | |
| | key_concepts = identify_key_concepts(doc) |
| | |
| | |
| | concept_graph = create_concept_graph(doc, key_concepts) |
| | relations_graph = visualize_concept_graph(concept_graph, lang) |
| | |
| | return { |
| | 'key_concepts': key_concepts, |
| | 'relations_graph': relations_graph |
| | } |
| |
|
| | __all__ = ['perform_semantic_analysis', 'ENTITY_LABELS', 'POS_TRANSLATIONS'] |