// comprehend_romance_gate.el - ES / PT native telephone test (SACRED polarity).
//
// The spec is language-neutral. This gate proves the Romance front-end extracts
// SACRED polarity correctly and that negation survives parse -> realize ->
// re-parse for Spanish and Portuguese (byte-parity of the surface is NOT expected
// yet — the non-English realizer path is a generic preverbal-negator skeleton).

fn rg_line(text: String, lang: String, expected_pol: String) -> String {
    let spec: [String] = parse_spec_lang(text, lang)
    let pol_in: String = slots_get(spec, "polarity")
    let surf: String = realize(spec)
    let spec2: [String] = parse_spec_lang(surf, lang)
    let pol_out: String = slots_get(spec2, "polarity")
    let status: String = "LOST"
    if str_eq(pol_in, pol_out) { let status = "PRESERVED" }
    let okexp: String = "MISMATCH"
    if str_eq(pol_in, expected_pol) { let okexp = "ok" }
    let out: String = "IN[" + lang + "]: " + text + "\n"
    let out = out + "  spec: pol=" + pol_in + " pred=" + slots_get(spec, "predicate")
    let out = out + " agent=" + slots_get(spec, "agent")
    let out = out + " pat=" + slots_get(spec, "patient")
    let out = out + " iobj=" + slots_get(spec, "iobj")
    let out = out + " loc=" + slots_get(spec, "location")
    let out = out + " tense=" + slots_get(spec, "tense") + "\n"
    let out = out + "  realized: " + surf + "\n"
    let out = out + "  reparse:  pol=" + pol_out + "  [" + status + "]  expected=" + expected_pol + " (" + okexp + ")\n"
    return out
}

fn rg_pres(text: String, lang: String) -> Int {
    let spec: [String] = parse_spec_lang(text, lang)
    let surf: String = realize(spec)
    let spec2: [String] = parse_spec_lang(surf, lang)
    if str_eq(slots_get(spec, "polarity"), slots_get(spec2, "polarity")) { return 1 }
    return 0
}

fn rg_corr(text: String, lang: String, expected_pol: String) -> Int {
    let spec: [String] = parse_spec_lang(text, lang)
    if str_eq(slots_get(spec, "polarity"), expected_pol) { return 1 }
    return 0
}

fn run_romance_gate() -> String {
    let e1: String = "El niño no comió el pescado."
    let e2: String = "Yo nunca luché contra el océano."
    let e3: String = "El profesor lee el libro."
    let p1: String = "O professor não leu o livro."
    let p2: String = "Eu nunca lutei contra o oceano."
    let p3: String = "A menina comeu o peixe."

    let rep: String = "==== ELP Romance telephone test (ES / PT) ====\n"
    let rep = rep + rg_line(e1, "es", "neg")
    let rep = rep + rg_line(e2, "es", "neg")
    let rep = rep + rg_line(e3, "es", "aff")
    let rep = rep + rg_line(p1, "pt", "neg")
    let rep = rep + rg_line(p2, "pt", "neg")
    let rep = rep + rg_line(p3, "pt", "aff")

    let pres: Int = 0
    if rg_pres(e1, "es") == 1 { let pres = pres + 1 }
    if rg_pres(e2, "es") == 1 { let pres = pres + 1 }
    if rg_pres(e3, "es") == 1 { let pres = pres + 1 }
    if rg_pres(p1, "pt") == 1 { let pres = pres + 1 }
    if rg_pres(p2, "pt") == 1 { let pres = pres + 1 }
    if rg_pres(p3, "pt") == 1 { let pres = pres + 1 }
    let corr: Int = 0
    if rg_corr(e1, "es", "neg") == 1 { let corr = corr + 1 }
    if rg_corr(e2, "es", "neg") == 1 { let corr = corr + 1 }
    if rg_corr(e3, "es", "aff") == 1 { let corr = corr + 1 }
    if rg_corr(p1, "pt", "neg") == 1 { let corr = corr + 1 }
    if rg_corr(p2, "pt", "neg") == 1 { let corr = corr + 1 }
    if rg_corr(p3, "pt", "aff") == 1 { let corr = corr + 1 }

    let rep = rep + "-----------------------------------------------------------------\n"
    let rep = rep + "polarity PRESERVED through round-trip: " + int_to_str(pres) + "/6\n"
    let rep = rep + "polarity EXTRACTED correctly:          " + int_to_str(corr) + "/6\n"
    if pres == 6 {
        if corr == 6 { let rep = rep + "ROMANCE GATE: PASS\n" }
        else { let rep = rep + "ROMANCE GATE: FAIL (extraction)\n" }
    } else {
        let rep = rep + "ROMANCE GATE: FAIL (round-trip)\n"
    }
    return rep
}

println(run_romance_gate())
