Update app.py
Browse files
app.py
CHANGED
|
@@ -47,7 +47,7 @@ def load_lexicons():
|
|
| 47 |
|
| 48 |
ES2MINI, ES2KOMI, MINI2ES, KOMI2ES, EN2MINI, EN2KOMI, MINI2EN, KOMI2EN = load_lexicons()
|
| 49 |
|
| 50 |
-
# ------------ OOV reversible
|
| 51 |
ALPHA_MINI64 = "@ptkmnslraeiouy0123456789><=:/!?.+-_*#bcdfghjvqwxzACEGHIJKLMNOPRS"[:64]
|
| 52 |
CJK_BASE = (
|
| 53 |
"天地人日月山川雨風星火水木土金石光影花草鳥犬猫魚"
|
|
@@ -91,7 +91,7 @@ def is_oov_minimax(code: str) -> bool:
|
|
| 91 |
def is_oov_komin(code: str) -> bool:
|
| 92 |
return len(code) >= 2 and code.startswith("「") and code.endswith("」")
|
| 93 |
|
| 94 |
-
# ------------ spaCy
|
| 95 |
USE_SPACY = False
|
| 96 |
try:
|
| 97 |
import spacy
|
|
@@ -127,7 +127,7 @@ def code_en(lemma: str, target: str) -> str:
|
|
| 127 |
if EN2KOMI: return EN2KOMI.get(lemma) or enc_oov_komin(lemma)
|
| 128 |
return enc_oov_komin(lemma)
|
| 129 |
|
| 130 |
-
# ------------ reglas compactas
|
| 131 |
TAM_MINI = {"Pres":"P", "Past":"T", "Fut":"F", "UNK":"P"}
|
| 132 |
TAM_KOMI = {"Pres":"Ⓟ", "Past":"Ⓣ", "Fut":"Ⓕ", "UNK":"Ⓟ"}
|
| 133 |
|
|
@@ -164,12 +164,19 @@ def extract_core(doc):
|
|
| 164 |
obls.sort(key=lambda x: x.i); advs.sort(key=lambda x: x.i)
|
| 165 |
return root, subs, objs, obls, advs
|
| 166 |
|
|
|
|
| 167 |
def realize_minimax(doc, src_lang: str, drop_articles=True, zero_copula=True):
|
| 168 |
root, subs, objs, obls, advs = extract_core(doc)
|
| 169 |
tense, is_q, is_neg = detect_tense(root), detect_polarity(doc), detect_neg(doc)
|
|
|
|
| 170 |
vlem = lemma_of(root, src_lang)
|
| 171 |
vcode = code_es(vlem, "Minimax-ASCII") if src_lang=="Español" else code_en(vlem, "Minimax-ASCII")
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
def realize_np(tokens):
|
| 175 |
outs=[]
|
|
@@ -192,15 +199,17 @@ def realize_minimax(doc, src_lang: str, drop_articles=True, zero_copula=True):
|
|
| 192 |
parts = [vcode] + S + O + ADV
|
| 193 |
return " ".join(p for p in parts if p)
|
| 194 |
|
|
|
|
| 195 |
def realize_komin(doc, src_lang: str, drop_articles=True, zero_copula=True):
|
| 196 |
root, subs, objs, obls, advs = extract_core(doc)
|
| 197 |
tense, is_q, is_neg = detect_tense(root), detect_polarity(doc), detect_neg(doc)
|
| 198 |
-
|
|
|
|
| 199 |
vcode = code_es(vlem, "Kōmín-CJK") if src_lang=="Español" else code_en(vlem, "Kōmín-CJK")
|
| 200 |
|
| 201 |
P_SUBJ, P_OBJ = "ᵖ", "ᵒ"
|
| 202 |
NEG_M, Q_FIN = "̆", "?"
|
| 203 |
-
TAM = TAM_KOMI.get(tense,"Ⓟ")
|
| 204 |
|
| 205 |
def realize_np(tokens, particle):
|
| 206 |
outs=[]
|
|
@@ -218,6 +227,7 @@ def realize_komin(doc, src_lang: str, drop_articles=True, zero_copula=True):
|
|
| 218 |
ADV.append(code_es(lem, "Kōmín-CJK") if src_lang=="Español" else code_en(lem, "Kōmín-CJK"))
|
| 219 |
|
| 220 |
v_form = vcode + TAM + (NEG_M if is_neg else "")
|
|
|
|
| 221 |
if zero_copula and vlem in ("ser","estar","be") and tense=="Pres" and not is_neg and not is_q:
|
| 222 |
parts = S + O + ADV
|
| 223 |
else:
|
|
@@ -226,7 +236,7 @@ def realize_komin(doc, src_lang: str, drop_articles=True, zero_copula=True):
|
|
| 226 |
if is_q: out += " " + Q_FIN
|
| 227 |
return out
|
| 228 |
|
| 229 |
-
# ------------ API
|
| 230 |
def build_sentence(text: str, src_lang: str, target: str, drop_articles: bool, zero_copula: bool) -> str:
|
| 231 |
if not text.strip(): return ""
|
| 232 |
if not USE_SPACY: return "[Error] spaCy no está cargado (revisa requirements.txt)."
|
|
@@ -241,57 +251,116 @@ def encode_simple(text: str, src_lang: str, target: str) -> str:
|
|
| 241 |
if not text.strip(): return ""
|
| 242 |
def repl_es(m):
|
| 243 |
key = norm_es(m.group(0))
|
| 244 |
-
|
|
|
|
| 245 |
def repl_en(m):
|
| 246 |
key = norm_en(m.group(0))
|
| 247 |
table = EN2MINI if target=="Minimax-ASCII" else EN2KOMI
|
| 248 |
-
if table
|
| 249 |
-
|
|
|
|
| 250 |
repl = repl_es if src_lang=="Español" else repl_en
|
| 251 |
return WORD_RE.sub(repl, text)
|
| 252 |
|
|
|
|
| 253 |
def decode_simple(text: str, source: str, tgt_lang: str) -> str:
|
| 254 |
-
if not text.strip():
|
|
|
|
| 255 |
code2es = MINI2ES if source=="Minimax-ASCII" else KOMI2ES
|
| 256 |
code2en = MINI2EN if source=="Minimax-ASCII" else KOMI2EN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
out=[]
|
| 258 |
-
for part in
|
| 259 |
if part is None or part=="":
|
| 260 |
continue
|
|
|
|
|
|
|
| 261 |
if part.isspace() or re.fullmatch(r"[^\w\s]+", part):
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
if source=="Minimax-ASCII"
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
return "".join(out)
|
| 275 |
|
|
|
|
| 276 |
EXPLAIN_ES = """
|
| 277 |
-
|
| 278 |
-
-
|
| 279 |
-
- **
|
| 280 |
-
-
|
| 281 |
-
- **Eficiencia**: se omiten artículos y hay **cópula cero** en presente afirmativo (*ser/estar*).
|
| 282 |
-
- Léxico: se usan **tus códigos**; lo no encontrado se marca de forma **reversible**.
|
| 283 |
"""
|
| 284 |
EXPLAIN_EN = """
|
| 285 |
-
|
| 286 |
-
-
|
| 287 |
-
- **
|
| 288 |
-
-
|
| 289 |
-
- **Efficiency**: drop articles, **zero copula** in present affirmative.
|
| 290 |
-
- Lexicon: your short codes are used; unknowns are **reversibly wrapped**.
|
| 291 |
"""
|
| 292 |
|
| 293 |
-
with gr.Blocks(title="Conlangs ·
|
| 294 |
-
gr.Markdown("# Minimax-ASCII / Kōmín-CJK ·
|
| 295 |
with gr.Row():
|
| 296 |
gr.Markdown(EXPLAIN_ES)
|
| 297 |
gr.Markdown(EXPLAIN_EN)
|
|
@@ -304,11 +373,10 @@ with gr.Blocks(title="Conlangs · Fraseador con léxico existente") as demo:
|
|
| 304 |
with gr.Row():
|
| 305 |
drop_articles = gr.Checkbox(value=True, label="Omitir artículos")
|
| 306 |
zero_copula = gr.Checkbox(value=True, label="Cópula cero (presente afirm.)")
|
| 307 |
-
btn = gr.Button("Construir", variant="primary")
|
| 308 |
out = gr.Textbox(lines=6, label="Salida")
|
| 309 |
-
|
| 310 |
|
| 311 |
-
with gr.Tab("Codificar
|
| 312 |
with gr.Row():
|
| 313 |
src_lang2 = gr.Dropdown(["Español","English"], value="Español", label="Fuente")
|
| 314 |
target2 = gr.Dropdown(["Minimax-ASCII","Kōmín-CJK"], value="Minimax-ASCII", label="Conlang")
|
|
@@ -332,3 +400,4 @@ if __name__ == "__main__":
|
|
| 332 |
|
| 333 |
|
| 334 |
|
|
|
|
|
|
| 47 |
|
| 48 |
ES2MINI, ES2KOMI, MINI2ES, KOMI2ES, EN2MINI, EN2KOMI, MINI2EN, KOMI2EN = load_lexicons()
|
| 49 |
|
| 50 |
+
# ------------ OOV reversible ------------
|
| 51 |
ALPHA_MINI64 = "@ptkmnslraeiouy0123456789><=:/!?.+-_*#bcdfghjvqwxzACEGHIJKLMNOPRS"[:64]
|
| 52 |
CJK_BASE = (
|
| 53 |
"天地人日月山川雨風星火水木土金石光影花草鳥犬猫魚"
|
|
|
|
| 91 |
def is_oov_komin(code: str) -> bool:
|
| 92 |
return len(code) >= 2 and code.startswith("「") and code.endswith("」")
|
| 93 |
|
| 94 |
+
# ------------ spaCy ------------
|
| 95 |
USE_SPACY = False
|
| 96 |
try:
|
| 97 |
import spacy
|
|
|
|
| 127 |
if EN2KOMI: return EN2KOMI.get(lemma) or enc_oov_komin(lemma)
|
| 128 |
return enc_oov_komin(lemma)
|
| 129 |
|
| 130 |
+
# ------------ reglas compactas ------------
|
| 131 |
TAM_MINI = {"Pres":"P", "Past":"T", "Fut":"F", "UNK":"P"}
|
| 132 |
TAM_KOMI = {"Pres":"Ⓟ", "Past":"Ⓣ", "Fut":"Ⓕ", "UNK":"Ⓟ"}
|
| 133 |
|
|
|
|
| 164 |
obls.sort(key=lambda x: x.i); advs.sort(key=lambda x: x.i)
|
| 165 |
return root, subs, objs, obls, advs
|
| 166 |
|
| 167 |
+
# --- Minimax (VSO) con separador reservado "·" entre código y marcadores ---
|
| 168 |
def realize_minimax(doc, src_lang: str, drop_articles=True, zero_copula=True):
|
| 169 |
root, subs, objs, obls, advs = extract_core(doc)
|
| 170 |
tense, is_q, is_neg = detect_tense(root), detect_polarity(doc), detect_neg(doc)
|
| 171 |
+
|
| 172 |
vlem = lemma_of(root, src_lang)
|
| 173 |
vcode = code_es(vlem, "Minimax-ASCII") if src_lang=="Español" else code_en(vlem, "Minimax-ASCII")
|
| 174 |
+
|
| 175 |
+
tail = TAM_MINI.get(tense, "P")
|
| 176 |
+
if is_neg: tail += "N"
|
| 177 |
+
if is_q: tail += "Q"
|
| 178 |
+
if tail:
|
| 179 |
+
vcode = f"{vcode}·{tail}"
|
| 180 |
|
| 181 |
def realize_np(tokens):
|
| 182 |
outs=[]
|
|
|
|
| 199 |
parts = [vcode] + S + O + ADV
|
| 200 |
return " ".join(p for p in parts if p)
|
| 201 |
|
| 202 |
+
# --- Kōmín (SOV) con partículas y TAM decodificables ---
|
| 203 |
def realize_komin(doc, src_lang: str, drop_articles=True, zero_copula=True):
|
| 204 |
root, subs, objs, obls, advs = extract_core(doc)
|
| 205 |
tense, is_q, is_neg = detect_tense(root), detect_polarity(doc), detect_neg(doc)
|
| 206 |
+
|
| 207 |
+
vlem = lemma_of(root, src_lang)
|
| 208 |
vcode = code_es(vlem, "Kōmín-CJK") if src_lang=="Español" else code_en(vlem, "Kōmín-CJK")
|
| 209 |
|
| 210 |
P_SUBJ, P_OBJ = "ᵖ", "ᵒ"
|
| 211 |
NEG_M, Q_FIN = "̆", "?"
|
| 212 |
+
TAM = TAM_KOMI.get(tense, "Ⓟ")
|
| 213 |
|
| 214 |
def realize_np(tokens, particle):
|
| 215 |
outs=[]
|
|
|
|
| 227 |
ADV.append(code_es(lem, "Kōmín-CJK") if src_lang=="Español" else code_en(lem, "Kōmín-CJK"))
|
| 228 |
|
| 229 |
v_form = vcode + TAM + (NEG_M if is_neg else "")
|
| 230 |
+
|
| 231 |
if zero_copula and vlem in ("ser","estar","be") and tense=="Pres" and not is_neg and not is_q:
|
| 232 |
parts = S + O + ADV
|
| 233 |
else:
|
|
|
|
| 236 |
if is_q: out += " " + Q_FIN
|
| 237 |
return out
|
| 238 |
|
| 239 |
+
# ------------ API de alto nivel ------------
|
| 240 |
def build_sentence(text: str, src_lang: str, target: str, drop_articles: bool, zero_copula: bool) -> str:
|
| 241 |
if not text.strip(): return ""
|
| 242 |
if not USE_SPACY: return "[Error] spaCy no está cargado (revisa requirements.txt)."
|
|
|
|
| 251 |
if not text.strip(): return ""
|
| 252 |
def repl_es(m):
|
| 253 |
key = norm_es(m.group(0))
|
| 254 |
+
code = ES2MINI.get(key) if target=="Minimax-ASCII" else ES2KOMI.get(key)
|
| 255 |
+
return code or (enc_oov_minimax(m.group(0)) if target=="Minimax-ASCII" else enc_oov_komin(m.group(0)))
|
| 256 |
def repl_en(m):
|
| 257 |
key = norm_en(m.group(0))
|
| 258 |
table = EN2MINI if target=="Minimax-ASCII" else EN2KOMI
|
| 259 |
+
if table and key in table:
|
| 260 |
+
return table[key]
|
| 261 |
+
return enc_oov_minimax(m.group(0)) if target=="Minimax-ASCII" else enc_oov_komin(m.group(0))
|
| 262 |
repl = repl_es if src_lang=="Español" else repl_en
|
| 263 |
return WORD_RE.sub(repl, text)
|
| 264 |
|
| 265 |
+
# --- DECODER bidireccional (pela marcadores/partículas antes de buscar) ---
|
| 266 |
def decode_simple(text: str, source: str, tgt_lang: str) -> str:
|
| 267 |
+
if not text.strip():
|
| 268 |
+
return ""
|
| 269 |
code2es = MINI2ES if source=="Minimax-ASCII" else KOMI2ES
|
| 270 |
code2en = MINI2EN if source=="Minimax-ASCII" else KOMI2EN
|
| 271 |
+
|
| 272 |
+
mini_tail_re = re.compile(r"^(?P<stem>.+?)·(?P<tail>[PTFNQ]+)$")
|
| 273 |
+
komi_np_re = re.compile(r"(?P<stem>.+?)(?:[ᵖᵒ])$") # NP partículas
|
| 274 |
+
komi_tam_re = re.compile(r"^(?P<stem>.+?)[ⓅⓉⒻ]+(?:\u0306)?$") # Verbo TAM + breve
|
| 275 |
+
|
| 276 |
+
tokens = re.split(r"(\s+|[^\w\s]+)", text)
|
| 277 |
out=[]
|
| 278 |
+
for part in tokens:
|
| 279 |
if part is None or part=="":
|
| 280 |
continue
|
| 281 |
+
|
| 282 |
+
# conserva espacios/puntuación (quita 「?」 suelta en Kōmín)
|
| 283 |
if part.isspace() or re.fullmatch(r"[^\w\s]+", part):
|
| 284 |
+
if part == "?" and source=="Kōmín-CJK":
|
| 285 |
+
continue
|
| 286 |
+
out.append(part)
|
| 287 |
+
continue
|
| 288 |
+
|
| 289 |
+
look = part
|
| 290 |
+
|
| 291 |
+
if source=="Minimax-ASCII":
|
| 292 |
+
# 1) directo
|
| 293 |
+
w_es = code2es.get(look)
|
| 294 |
+
w_en = code2en.get(look) if code2en else None
|
| 295 |
+
if tgt_lang=="Español" and w_es: out.append(w_es); continue
|
| 296 |
+
if tgt_lang=="English" and w_en: out.append(w_en); continue
|
| 297 |
+
# 2) pela sufijo ·PTFNQ...
|
| 298 |
+
m = mini_tail_re.match(look)
|
| 299 |
+
if m:
|
| 300 |
+
stem = m.group("stem")
|
| 301 |
+
w_es = code2es.get(stem)
|
| 302 |
+
w_en = code2en.get(stem) if code2en else None
|
| 303 |
+
if tgt_lang=="Español" and w_es: out.append(w_es); continue
|
| 304 |
+
if tgt_lang=="English" and w_en: out.append(w_en); continue
|
| 305 |
+
# 3) OOV reversible
|
| 306 |
+
if is_oov_minimax(look):
|
| 307 |
+
out.append(dec_oov_minimax(look)); continue
|
| 308 |
+
out.append(look); continue
|
| 309 |
+
|
| 310 |
+
else: # Kōmín-CJK
|
| 311 |
+
# 0) 「?」 pegado
|
| 312 |
+
if look.endswith("?"):
|
| 313 |
+
look = look[:-1]
|
| 314 |
+
|
| 315 |
+
# 1) directo
|
| 316 |
+
w_es = code2es.get(look)
|
| 317 |
+
w_en = code2en.get(look) if code2en else None
|
| 318 |
+
if tgt_lang=="Español" and w_es: out.append(w_es); continue
|
| 319 |
+
if tgt_lang=="English" and w_en: out.append(w_en); continue
|
| 320 |
+
|
| 321 |
+
# 2) pela NP (ᵖ/ᵒ)
|
| 322 |
+
m_np = komi_np_re.match(look)
|
| 323 |
+
if m_np:
|
| 324 |
+
stem = m_np.group("stem")
|
| 325 |
+
w_es = code2es.get(stem)
|
| 326 |
+
w_en = code2en.get(stem) if code2en else None
|
| 327 |
+
if tgt_lang=="Español" and w_es: out.append(w_es); continue
|
| 328 |
+
if tgt_lang=="English" and w_en: out.append(w_en); continue
|
| 329 |
+
look = stem
|
| 330 |
+
|
| 331 |
+
# 3) pela TAM/neg del verbo
|
| 332 |
+
m_tam = komi_tam_re.match(look)
|
| 333 |
+
if m_tam:
|
| 334 |
+
stem = m_tam.group("stem")
|
| 335 |
+
w_es = code2es.get(stem)
|
| 336 |
+
w_en = code2en.get(stem) if code2en else None
|
| 337 |
+
if tgt_lang=="Español" and w_es: out.append(w_es); continue
|
| 338 |
+
if tgt_lang=="English" and w_en: out.append(w_en); continue
|
| 339 |
+
|
| 340 |
+
# 4) OOV reversible
|
| 341 |
+
if is_oov_komin(look):
|
| 342 |
+
out.append(dec_oov_komin(look)); continue
|
| 343 |
+
|
| 344 |
+
out.append(part)
|
| 345 |
+
|
| 346 |
return "".join(out)
|
| 347 |
|
| 348 |
+
# ------------ UI y explicaciones ------------
|
| 349 |
EXPLAIN_ES = """
|
| 350 |
+
**Objetivo:** reversibilidad total (ES/EN ↔ conlang).
|
| 351 |
+
- **Minimax (VSO)**: verbo lleva sufijos TAM/neg/pregunta, separados por **`·`** del código léxico: `código·PTFNQ`.
|
| 352 |
+
- **Kōmín (SOV)**: partículas **ᵖ/ᵒ** para S/O; verbo final con **Ⓟ/Ⓣ/Ⓕ** y breve de negación; pregunta con **「?」**.
|
| 353 |
+
- El decodificador pela esas marcas antes de consultar el diccionario. OOV se mantiene **reversible**.
|
|
|
|
|
|
|
| 354 |
"""
|
| 355 |
EXPLAIN_EN = """
|
| 356 |
+
**Goal:** full reversibility (EN/ES ↔ conlang).
|
| 357 |
+
- **Minimax (VSO)**: verb carries TAM/neg/question suffixes, separated by **`·`** from the base code: `code·PTFNQ`.
|
| 358 |
+
- **Kōmín (SOV)**: particles **ᵖ/ᵒ** for S/O; final verb with **Ⓟ/Ⓣ/Ⓕ** and short neg mark; question with **「?」**.
|
| 359 |
+
- Decoder strips these markers before dictionary lookup. OOV is **reversibly wrapped**.
|
|
|
|
|
|
|
| 360 |
"""
|
| 361 |
|
| 362 |
+
with gr.Blocks(title="Conlangs · Reversible Phrase Builder") as demo:
|
| 363 |
+
gr.Markdown("# Minimax-ASCII / Kōmín-CJK · Reversible Translator")
|
| 364 |
with gr.Row():
|
| 365 |
gr.Markdown(EXPLAIN_ES)
|
| 366 |
gr.Markdown(EXPLAIN_EN)
|
|
|
|
| 373 |
with gr.Row():
|
| 374 |
drop_articles = gr.Checkbox(value=True, label="Omitir artículos")
|
| 375 |
zero_copula = gr.Checkbox(value=True, label="Cópula cero (presente afirm.)")
|
|
|
|
| 376 |
out = gr.Textbox(lines=6, label="Salida")
|
| 377 |
+
gr.Button("Construir").click(build_sentence, [text_in, src_lang, target, drop_articles, zero_copula], [out])
|
| 378 |
|
| 379 |
+
with gr.Tab("Codificar simple (solo léxico)"):
|
| 380 |
with gr.Row():
|
| 381 |
src_lang2 = gr.Dropdown(["Español","English"], value="Español", label="Fuente")
|
| 382 |
target2 = gr.Dropdown(["Minimax-ASCII","Kōmín-CJK"], value="Minimax-ASCII", label="Conlang")
|
|
|
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
+
|