File size: 16,294 Bytes
8b678a0
 
 
 
 
 
 
 
ed22bec
b027b7e
ed22bec
24446b4
07c03b6
dca43a5
3416470
dca43a5
 
 
 
 
 
55930c6
 
 
 
 
fbc5eb9
55930c6
 
 
 
 
 
 
 
 
 
 
dca43a5
 
8b678a0
350961d
183742f
 
 
 
 
 
 
 
 
350961d
183742f
 
 
 
 
350961d
 
bb7edd9
b261a6d
8b678a0
 
 
 
 
 
 
4760717
8b678a0
 
 
 
 
 
 
b027b7e
f41f5ce
8b678a0
b261a6d
 
 
 
 
2d409dd
b261a6d
 
f41f5ce
 
 
 
 
 
 
 
 
 
 
 
8949453
9a1fd99
8949453
 
 
 
9a1fd99
3ca6fdd
 
 
 
 
f41f5ce
cb07453
f41f5ce
b027b7e
 
 
 
c772e57
f41f5ce
896cd13
cb07453
896cd13
 
 
 
 
cb07453
896cd13
b261a6d
 
 
 
 
8949453
 
 
b261a6d
 
 
 
 
f41f5ce
 
 
 
 
b261a6d
f41f5ce
75f6904
728a1cc
75f6904
 
 
 
b261a6d
75f6904
 
 
728a1cc
75f6904
b261a6d
f41f5ce
 
 
e537a9b
f41f5ce
 
 
b027b7e
f41f5ce
e537a9b
 
 
 
f41f5ce
b261a6d
e537a9b
 
 
 
b027b7e
 
f41f5ce
 
b027b7e
 
f41f5ce
 
7f57ba6
b4b86ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d582260
b4b86ce
39a6d0d
b261a6d
39a6d0d
b4b86ce
b261a6d
 
7f57ba6
 
 
 
 
 
 
1bd48d7
 
 
 
b4b86ce
1bd48d7
 
 
24446b4
7f57ba6
9e20ab3
 
 
4bd9f9d
20cb6a4
 
9e20ab3
289a285
9e20ab3
289a285
 
9e20ab3
 
 
39a6d0d
b4b86ce
9e20ab3
 
289a285
9e20ab3
289a285
b4b86ce
 
9e20ab3
 
39a6d0d
b4b86ce
9e20ab3
39a6d0d
9e20ab3
39a6d0d
b4b86ce
 
 
 
 
 
 
 
39a6d0d
 
b4b86ce
 
39a6d0d
b261a6d
b4b86ce
56fe7b8
e537a9b
 
 
56fe7b8
e537a9b
 
b4b86ce
cb07453
8949453
 
b4b86ce
b261a6d
b4b86ce
 
 
b261a6d
 
 
 
 
 
 
b4b86ce
 
b261a6d
 
0e22ec4
b261a6d
dca43a5
b643cc8
350961d
b261a6d
bea2adc
56fe7b8
8949453
bea2adc
de7baaa
56fe7b8
dca43a5
bea2adc
 
7af212a
bea2adc
350961d
c74e62f
350961d
bea2adc
 
 
 
 
 
 
 
 
 
8949453
bea2adc
 
 
 
 
183742f
 
 
bea2adc
 
56fe7b8
8949453
9167704
2a508d9
ef2a11a
2a508d9
235545f
56fe7b8
8949453
bea2adc
8949453
 
bea2adc
8949453
 
0e22ec4
fa710f7
 
3dccda7
 
fa710f7
3dccda7
 
fa710f7
3dccda7
 
 
 
 
 
 
 
2446ebe
 
 
 
 
 
 
 
fa76c99
55930c6
2446ebe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import os
import subprocess
import sys
import urllib.request
import json
import pandas as pd
import spacy
import gradio as gr
import re
import unicodedata
from translit import latin_to_basic_grc
from paradigm_check import validar_substantivo_2a

# Código para o cabeçalho (CSS)
# 1. Defina as variáveis (certifique-se que o nome aqui...)
alpheios_css = """
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/alpheios-components@latest/dist/style/style-components.min.css"/>
"""

alpheios_js = """
function() {
    const loadAlpheios = () => {
        import("https://cdn.jsdelivr.net/npm/alpheios-embedded@latest/dist/alpheios-embedded.min.js")
        .then(embedLib => {
            const alpheios = embedLib.AlpheiosEmbedded.ImportLib();
            alpheios.activate({
                clientId: 'httpshuggingfacecospacesAniseFpln-ddgp-plus-teste',
                authStatus: 'notLoggedIn'
            });
            console.log("Alpheios Ativado com Sucesso!");
        }).catch(e => console.error("Erro Alpheios:", e));
    };

    if (document.readyState === 'complete') {
        loadAlpheios();
    } else {
        window.addEventListener('load', loadAlpheios);
    }
}
"""

meu_css_customizado = """
/* Força o aumento dos títulos (Labels) acima das tabelas e caixas */
.gradio-container .gr-form label span, 
.gradio-container span[data-testid="block-info"],
.gradio-container .label-wrap span {
    font-size: 24px !important; 
    font-weight: bold !important;
    color: #1a472a !important; /* Um verde escuro para destacar */
    display: block !important;
    margin-bottom: 8px !important;
}

/* Aumenta o cabeçalho das colunas das tabelas */
.thead th span {
    font-size: 18px !important;
    font-weight: bold !important;
}


# --- 1. INSTALAÇÃO E CARREGAMENTO ---
def install_odycy():
    try:
        import grc_odycy_joint_sm
    except ImportError:
        url = "https://huggingface.co/chcaa/grc_odycy_joint_sm/resolve/main/grc_odycy_joint_sm-any-py3-none-any.whl"
        valid_wheel_name = "grc_odycy_joint_sm-1.0.0-py3-none-any.whl"
        urllib.request.urlretrieve(url, valid_wheel_name)
        subprocess.check_call([sys.executable, "-m", "pip", "install", valid_wheel_name, "--no-deps"])
        os.remove(valid_wheel_name)

install_odycy()

try:
    import grc_odycy_joint_sm
    nlp = grc_odycy_joint_sm.load()
except:
    nlp = spacy.blank("grc")

TAG_MAP = {
    'NOUN': 'Substantivo', 'VERB': 'Verbo', 'ADJ': 'Adjetivo',
    'DET': 'Artigo/Det.', 'PRON': 'Pronome', 'ADV': 'Advérbio',
    'ADP': 'Preposição', 'CCONJ': 'Conjunção', 'SCONJ': 'Conjunção Sub.',
    'PART': 'Partícula', 'PROPN': 'Nome Próprio', 'PUNCT': 'Pontuação',
    'AUX': 'Auxiliar/Cópula', 'NUM': 'Numeral'
}

def load_json(path):
    if os.path.exists(path):
        with open(path, 'r', encoding='utf-8') as f:
            return json.load(f)
    return {}

INDEX_LEMAS = load_json('ddgp_index_lemas.json')
INDEX_FORMAS = load_json('ddgp_index_formas_final.json')
FORMA_TO_LEMA = load_json('ddgp_forma_to_lema.json')
ENTRIES = load_json('ddgp3x_entry.json')
ABREV = load_json('abrev.json')

css_content = ""
# Parte 1: Lê os arquivos existentes (O que você já tinha)
for f_css in ['style.css', 'style_map.css']:
    if os.path.exists(f_css):
        with open(f_css, 'r', encoding='utf-8') as f:
            css_content += f.read() + "\n"

css_content += "textarea { font-size: 20px !important; line-height: 1.6 !important;font-family: 'Gentium Plus', 'Alegreya', serif !important; } "
css_content += "table, th, td { font-size: 20px !important; } "
css_content += ".gradio-container label span { font-size: 20px !important; } "
css_content += ".gr-table td { font-size: 20px !important; padding: 10px !important; } "
css_content += ".prose { font-size: 20px !important; } "

# --- 2. FUNÇÕES DE APOIO E ORDENAÇÃO ---

def normalizar_grego(texto):
    if not texto: return ""
    texto = unicodedata.normalize('NFD', texto.lower())
    texto = "".join(c for c in texto if not unicodedata.combining(c))
    return unicodedata.normalize('NFC', texto).strip()

def ordem_grega(lema):
    alfabeto_map = {
        'α': 1, 'β': 2, 'γ': 3, 'δ': 4, 'ε': 5, 'ζ': 6, 'η': 7, 'θ': 8,
        'ι': 9, 'κ': 10, 'λ': 11, 'μ': 12, 'ν': 13, 'ξ': 14, 'ο': 15, 'π': 16,
        'ρ': 17, 'σ': 18, 'ς': 18, 'τ': 19, 'υ': 20, 'φ': 21, 'χ': 22, 'ψ': 23, 'ω': 24
    }
    lema_limpo = normalizar_grego(lema)
    return [alfabeto_map.get(char, 99) for char in lema_limpo]

def aplicar_abreviaturas_seguro(texto):
    if not texto: return ""
    sorted_abrevs = sorted(ABREV.keys(), key=len, reverse=True)
    for ab in sorted_abrevs:
        pattern = r'\b' + re.escape(ab) + r'(?=\s|[.,;:]|$)'
        info = ABREV[ab]
        desc = info.get('descricao', '')
        categoria = info.get('categoria', '')
        classe_css = "autor-sc" if categoria == 'autor' else "abrev"
        subst = f'<span class="{classe_css}" title="{desc}">{ab}</span>'
        texto = re.sub(pattern, subst, texto)
    return texto

def format_entry_html(entry_id):
    entry = ENTRIES.get(str(entry_id))
    if not entry: return None
    gword = entry.get('gword', '')
    pdesc = entry.get('pdesc', '')
    pdesc = aplicar_abreviaturas_seguro(pdesc)
    pdesc = re.sub(r'〈(.*?)〉', r'<span class="etimo">〈\1〉</span>', pdesc)
    
    return f'''
    <div class="result-box" style="text-transform: none !important; font-variant: normal !important;">
        <div style="color: #1a4d8f; font-size: 1.3em; font-weight: bold; margin-bottom: 6px; text-transform: none !important; font-variant: normal !important;">
            {gword}
        </div>
        <div style="line-height: 1.6; text-transform: none !important; font-variant: normal !important;">
            {pdesc}
        </div>
    </div>
    '''

# --- 3. CONSULTA E ANÁLISE ---

def consultar_ddgp(termo):
    if not termo: return ""
    # Translitera se for latim
    if any(ord(c) < 128 for c in termo if c.isalpha()):
        termo = latin_to_basic_grc(termo)
    
    termo_norm = normalizar_grego(termo)
    ids = []
    
    # BUSCA ESTRITA: Tentamos apenas o termo exato ou variações numéricas (ex: logos, logos1)
    # Isso evita que a busca traga palavras que apenas "começam" com o termo.
    tentativas = [termo_norm] + [f"{termo_norm}{i}" for i in range(1, 4)]
    
    for b in tentativas:
        if b in INDEX_LEMAS:
            ids.append(INDEX_LEMAS[b])
    
    if not ids: return ""
    
    html = ""
    for eid in sorted(set(ids)):
        res = format_entry_html(eid)
        if res: html += res
    return html

def analisar_texto(texto):

    # --- SUB-FUNÇÕES DE APOIO (INTERNAS E IDENTADAS) ---
    def limpar_crases(t):
        if not t: return ""
        crases = {
            "ὦνθρωπε": "ὦ ἄνθρωπε", "ὥνθρωπος": "ὁ ἄνθρωπος", "κἀγώ": "καὶ ἐγώ",
            "κἀμέ": "καὶ ἐmé", "τοὔνομα": "τὸ ὄνομα", "θαἰμάτια": "τὰ ἱμάτια",
            "τἀνδρεῖα": "τὰ ἀνδρεῖα", "κἄν": "καὶ ἄν", "θἄτερον": "τὸ ἕτερον",
            "προὔργου": "πρὸ ἔργου", "κἀκεῖνος": "καὶ ἐκεῖνος"
        }
        for crase, original in crases.items():
            t = t.replace(crase, original)
        return t

    def lematizar_sais(palavra):
        palavra = palavra.lower()
        excecoes_nominais = {
            "συρακούσαις": "Συράκουσαι", "ὀδρύσαις": "Ὀδρύσαι", "ἀργινούσαις": "Ἀργινοῦσαι",
            "κρήσσαις": "Κρῆσσα", "θρᾴσσαις": "Θρᾷσσα", "κιλίσσαις": "Κίλισσα",
            "φοινίσσαις": "Φοινίσσαι", "μελίσσαις": "μέλισσα", "μούσαις": "Μοῦσα",
            "μώσαις": "Μοῦσα", "βύρσαις": "βύρσα", "πάσαις": "πᾶς", "ἁπάσαις": "ἅπας",
            "ξυμπάσαις": "σύμπas", "ὅσαις": "ὅσος", "ὁπόσαις": "ὅποσος", "ἴσαις": "ἴσος", "σαῖς": "σός"
        }
        if palavra in excecoes_nominais:
            return f"Lema Nominal: {excecoes_nominais[palavra]}"
        return None

    # --- INÍCIO DO PROCESSAMENTO ---
    if not texto: 
        return None, None, "0", "0", "0", "0", "0", ""

    # 1. Vacina de Crases aplicada antes de tudo
    texto_limpo = limpar_crases(texto)
    
    # 2. Inicialização do spaCy e variáveis
    doc = nlp(texto_limpo)
    dados = []
    verbetes_dict = {}
    lemas_unicos_processados = set()

    for token in doc:
        # --- PROTEÇÃO CONTRA PONTUAÇÃO (Aqui resolvemos os dois pontos :) ---
        if token.pos_ == "PUNCT" or token.text in [':', '.', ',', ';', '·']:
            l_orig = token.text
            l_norm = token.text
            pos_pt = "Pontuação"
            morph_info = "-"
        else:
            # A. Variáveis Básicas do Token
            l_orig = token.lemma_
            l_norm = normalizar_grego(l_orig)
            pos_pt = TAG_MAP.get(token.pos_, token.pos_)
        
            # Morfologia Original da IA (Limpeza das tags)
            morph_info = str(token.morph).replace("Case=", "").replace("Gender=", "").replace("Number=", "").replace("VerbForm=", "").replace("Person=", "").replace("Tense=", "").replace("Mood=", "").replace("Voice=", "")
            if not morph_info: morph_info = "-"


       # --- E. HIERARQUIA DE DECISÃO (SEGURA) ---
        analise_sais = lematizar_sais(token.text)
        correcao_paradigma = validar_substantivo_2a(token.text, l_orig)
        tem_no_ddgp = l_norm in INDEX_LEMAS
        lemas_nominais = ('ος', 'ις', 'ας', 'ης', 'ον')

        # 1. Definimos o padrão (IA)
        l_final = l_orig
        pos_final = pos_pt
        morph_final = morph_info

        # 2. Aplicamos as correções por ordem de importância
        
        # Se for uma exceção de -sais, corrigimos o LEMA e a CLASSE
        if analise_sais and "Lema Nominal" in analise_sais:
            l_final = analise_sais.split(": ")[1]
            pos_final = "Substantivo/Adj"
            # Mantemos o morph_final da IA ou Alpheios para ver se é Dativo ou Particípio

        # Se o Alpheios (Paradigma) encontrou algo, ele define a Morfologia
        if correcao_paradigma:
            pos_final = "Substantivo"
            morph_final = correcao_paradigma
        
        # Regra do DDGP (Se a IA confundir substantivo com verbo)
        elif pos_pt == "Verbo" and tem_no_ddgp and l_norm.endswith(lemas_nominais):
            pos_final = "Substantivo"
            morph_final = f"Morfologia nominal (IA: {morph_info})"

        # --- F. ALIMENTAR A TABELA (Este bloco PRECISA estar aqui) ---
        if token.pos_ not in ['PUNCT', 'SYM', 'SPACE']:
            dados.append({
                'Palavra': token.text, 
                'Lema': l_final, 
                'Classe': pos_final, 
                'Morfologia': morph_final
            })

            # Busca no Dicionário apenas uma vez por lema
            if l_norm not in lemas_unicos_processados:
                res_html = consultar_ddgp(l_norm)
                if res_html:
                    verbetes_dict[l_final] = res_html 
                lemas_unicos_processados.add(l_norm)

    # --- FINALIZAÇÃO E RETORNO PARA GRADIO ---
    aviso_html = '''
    <div style="background-color: #f8f9fa; padding: 10px; border-left: 4px solid #1a4d8f; margin-bottom: 15px; font-size: 0.9em; color: #555;">
        💡 <b>Dica:</b> Caso um lema não apareça automaticamente abaixo, utilize a aba "Busca direta no DDGP" para consultá-lo manualmente.
    </div>
    '''
    
    lexico_html = aviso_html
    # Ordena os verbetes alfabeticamente para o dicionário
    for lema_ord in sorted(verbetes_dict.keys(), key=ordem_grega):
        lexico_html += verbetes_dict[lema_ord]

    # Prepara o DataFrame e Estatísticas
    df = pd.DataFrame(dados)
    n_tokens = len(df)
    n_types = df['Palavra'].str.lower().nunique()
    n_lemas = df['Lema'].nunique()
    
    ttr = (n_types / n_tokens) if n_tokens > 0 else 0
    ltr = (n_lemas / n_tokens) if n_tokens > 0 else 0
    
    csv_path = "analise_filologica.csv"
    df.to_csv(csv_path, index=False)
    
    # Retorna os 8 elementos que a sua interface Gradio espera
    return (df, csv_path, 
            str(n_tokens), str(n_types), str(n_lemas), 
            f"{ltr:.2f}", f"{ttr:.2f}", lexico_html)

# --- 4. INTERFACE ---
# Modifique esta linha para incluir head e js:

with gr.Blocks(title="DDGP + OdyCy", theme=gr.themes.Default(text_size="lg"), css=meu_css_customizado) as demo:
    with gr.Row():
        with gr.Column(scale=1, min_width=100):
            gr.HTML('''
                <div style="display: flex; align-items: center; justify-content: flex-start; height: 80px;">
                    <img src="https://raw.githubusercontent.com/aniseferreira/DDGP_Plus/main/ddgp/logo.png" style="height: 80px;">
                </div>
            ''')
    # ... restante do seu código ...
        with gr.Column(scale=4):
            gr.Markdown("# Estação Filológica DDGP & OdyCy")
            gr.Markdown("## DDGP Plus: Análise lexical e consulta ao Dicionário Digital Grego-Português.")

    with gr.Tab("📝 Análise lexical"):
        txt = gr.Textbox(label="Texto em Grego Antigo", lines=6, placeholder="Insira o texto aqui sem aspas...Δειναὶ γὰρ αἱ γυναῖκες εὑρίσκειν τέχνας.", elem_classes="text-grande")
        btn = gr.Button("🚀 Executar Análise", variant="primary")
        
        with gr.Row():
            t1 = gr.Label(label="Tokens (Total)")
            t2 = gr.Label(label="Types (Formas Únicas)")
            t3 = gr.Label(label="Lemas (Entradas)")
            t4 = gr.Label(label="LTR (Lema-Token)")
            t5 = gr.Label(label="TTR (Type-Token)")
            
        with gr.Row():
            with gr.Column(scale=2):
                out_t = gr.Dataframe(label="Formas")
                out_f = gr.File(label="Exportar CSV")
            with gr.Column(scale=1):
                gr.Markdown("### 📖 Léxico Contextual")
                out_l = gr.HTML()
            
    with gr.Tab("🔍 Busca direta no DDGP"):
        in_b = gr.Textbox(label="Busca direta no DDGP", placeholder="(ex. λόγος, logos)")
        btn_b = gr.Button("Consultar Base")
        out_b = gr.HTML()

    gr.Markdown(f'''
    ---
    **DDGP Plus** — Analisador Morfológico e Dicionário Digital de Grego–Português 2026 v.1  
    Baseado originalmente no Dicionário Grego-Português e diretamente no Dicionário Digital Grego–Português (DDGP e DGP - ver créditos em [hipatia.fclar.unesp.br](http://hipatia.fclar.unesp.br))<br>
    Projetos Abertos em Letras Clássicas Digitais. **Responsável**: Anise D'Orange Ferreira.
     _Desenvolvimento técnico e programação_ assistida por Gemini (Google AI/Workspace).<br>
    Sob licença CC BY-NC-SA 4.0.  
    ''')

    btn.click(analisar_texto, inputs=txt, 
              outputs=[out_t, out_f, t1, t2, t3, t4, t5, out_l],
              api_name="analisar")
    
    btn_b.click(consultar_ddgp, inputs=in_b, outputs=out_b,
                api_name="consultar")

# No final do seu app.py, substitua demo.launch() por:

   # No final do bloco 'with gr.Blocks...'
    def carregar_e_analisar(request: gr.Request):
        params = request.query_params
        texto_da_url = params.get("text", "")
        return texto_da_url

    # Quando o Space carrega: 
    # 1. Preenche o texto
    # 2. Se houver texto, dispara a função de análise imediatamente
    demo.load(carregar_e_analisar, None, txt).then(
        fn=analisar_texto,
        inputs=txt,
        outputs=[out_t, out_f, t1, t2, t3, t4, t5, out_l]
    )

    # Combine os estilos (opcional, mas recomendado para manter seu layout)
    full_css = css_content + "\n" + alpheios_css

    # Chame o launch assim:
    demo.launch(
        css=full_css,
        head=alpheios_css, # O Alpheios precisa do link no head
        js=alpheios_js,
        ssr_mode=False  # <--- Adicione esta linha aqui
    )