File size: 2,057 Bytes
efe9b39
 
 
 
 
 
 
 
 
 
92405d9
efe9b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92405d9
 
 
efe9b39
92405d9
efe9b39
 
 
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
"""Shared helpers for latincy-lexicon demo pages (11, 12, 13)."""

from __future__ import annotations

import tempfile
from pathlib import Path

import streamlit as st


DEFAULT_SENTENCE = "Poeta bonus carmina pulchra scribit."


@st.cache_resource(show_spinner="Building Whitaker's Words data (first load only)…")
def build_lexicon_artifacts() -> tuple[Path, Path]:
    """Build lexicon.json + analyzer.json once per session.

    Artifacts are cached under a temp directory. First call takes
    ~5–10s; subsequent calls short-circuit via Streamlit's
    cache_resource and are instant.
    """
    from latincy_lexicon.build import build

    output_dir = Path(tempfile.gettempdir()) / "latincy-dashboard-lexicon"
    output_dir.mkdir(parents=True, exist_ok=True)

    lexicon_path = output_dir / "lexicon.json"
    analyzer_path = output_dir / "analyzer.json"

    if not (lexicon_path.exists() and analyzer_path.exists()):
        build(output_dir=output_dir)

    return lexicon_path, analyzer_path


@st.cache_resource(show_spinner="Loading LatinCy pipeline…")
def load_lexicon_pipeline(model_name: str):
    """Load a LatinCy model with whitakers_words + paradigm_generator attached.

    Cached per model_name — all three lexicon demo pages share the same
    pipeline instance, so switching pages is instant after the first
    load.
    """
    import spacy

    nlp = spacy.load(model_name)
    lexicon_path, analyzer_path = build_lexicon_artifacts()

    nlp.add_pipe(
        "whitakers_words",
        config={
            "lexicon_path": str(lexicon_path),
            "analyzer_path": str(analyzer_path),
        },
        last=True,
    )
    nlp.add_pipe(
        "paradigm_generator",
        config={"analyzer_path": str(analyzer_path)},
        last=True,
    )
    return nlp


def sentence_picker(key: str) -> str:
    """Render a free-text area prefilled with a simple example sentence."""
    return st.text_area(
        "Latin text:",
        value=DEFAULT_SENTENCE,
        height=100,
        key=f"text_{key}",
    )