Files
inbraille/public/modules/translator.js
T
Lucas RayanandClaude Opus 5.5 07aaf8e1a7
CI / Trivy (dependências, segredos e configuração) (push) Successful in 1m2s
CI / Testes e imagem Docker (linux/arm64) (push) Successful in 4m24s
Release / Plano (reaproveitar a imagem?) (push) Successful in 5s
Release / ci (push) Skipped
Release / Publica a imagem (linux/arm64) (push) Successful in 18s
Release / Cria a release no Gitea (push) Successful in 4s
feat: novo site e interface; tradução Braille no navegador (1.4.0)
Interface reescrita (design próprio, tema claro/escuro, a11y WCAG 2.2 AA),
seções do site e artigo IDEIA @ CSBC 2025. Tradutor portado para JS com
paridade 100% contra o Python; sem Tailwind nem axios.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:51:53 -03:00

551 lines
19 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Tradutor texto <-> Braille 100% no navegador.
// Porte fiel de src/translation/{encoder,decoder,translator}.py (e dos limites de
// src/web/schemas.py). encode(payload)/decode(payload) devolvem o mesmo objeto que
// response.data devolvia em POST /api/encode e /api/decode.
//
// Tudo que o Python faz com str (code points, isalpha, isupper, rstrip...) e reproduzido aqui
// de forma explicita; try/except do Python viram try/catch com a mesma granularidade.
import {
BRAZILIAN, NORTH_AMERICAN, BLANK, INITIAL_CELLS, FINAL_CELLS, TONE_CELLS, PUNCTUATION_CELLS,
BUZZING_INITIALS, YU_INITIALS, ACCENTS, INITIAL_BY_CELL, FINAL_BY_CELL, FINAL_STANDALONE_BY_CELL,
TONE_BY_CELL, PUNCTUATION_BY_CELL,
} from "./braille_data.js";
export const MAX_TEXT_LENGTH = 5000;
const ALPHABETS = ["Brazilian", "North American", "Chinese"];
// ---------- helpers que imitam o Python ----------
const PY_WHITESPACE = /[\t\n\v\f\r\x1c-\x1f \x85\xa0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+$/;
const rstrip = (s) => s.replace(PY_WHITESPACE, "");
const chars = (s) => Array.from(s);
const RE_ALPHA = /^\p{L}$/u; // Python isalpha: categorias L*
// Python isdigit/isnumeric usam Numeric_Type, que \p{Nd}/\p{N} nao cobrem sozinhos (ex.: ², ①, 一, 〇).
// Faixas extras geradas a partir do unicodedata do Python 3.12 (Unicode 15).
const DIGIT_EXTRA = [[0xb2,0xb3],[0xb9,0xb9],[0x1369,0x1371],[0x19da,0x19da],[0x2070,0x2070],[0x2074,0x2079],[0x2080,0x2089],[0x2460,0x2468],[0x2474,0x247c],[0x2488,0x2490],[0x24ea,0x24ea],[0x24f5,0x24fd],[0x24ff,0x24ff],[0x2776,0x277e],[0x2780,0x2788],[0x278a,0x2792],[0x10a40,0x10a43],[0x10e60,0x10e68],[0x11052,0x1105a],[0x1f100,0x1f10a]];
const NUMERIC_EXTRA = [[0x3405,0x3405],[0x3483,0x3483],[0x382a,0x382a],[0x3b4d,0x3b4d],[0x4e00,0x4e00],[0x4e03,0x4e03],[0x4e07,0x4e07],[0x4e09,0x4e09],[0x4e5d,0x4e5d],[0x4e8c,0x4e8c],[0x4e94,0x4e94],[0x4e96,0x4e96],[0x4ebf,0x4ec0],[0x4edf,0x4edf],[0x4ee8,0x4ee8],[0x4f0d,0x4f0d],[0x4f70,0x4f70],[0x5104,0x5104],[0x5146,0x5146],[0x5169,0x5169],[0x516b,0x516b],[0x516d,0x516d],[0x5341,0x5341],[0x5343,0x5345],[0x534c,0x534c],[0x53c1,0x53c4],[0x56db,0x56db],[0x58f1,0x58f1],[0x58f9,0x58f9],[0x5e7a,0x5e7a],[0x5efe,0x5eff],[0x5f0c,0x5f0e],[0x5f10,0x5f10],[0x62fe,0x62fe],[0x634c,0x634c],[0x67d2,0x67d2],[0x6f06,0x6f06],[0x7396,0x7396],[0x767e,0x767e],[0x8086,0x8086],[0x842c,0x842c],[0x8cae,0x8cae],[0x8cb3,0x8cb3],[0x8d30,0x8d30],[0x9621,0x9621],[0x9646,0x9646],[0x964c,0x964c],[0x9678,0x9678],[0x96f6,0x96f6],[0xf96b,0xf96b],[0xf973,0xf973],[0xf978,0xf978],[0xf9b2,0xf9b2],[0xf9d1,0xf9d1],[0xf9d3,0xf9d3],[0xf9fd,0xf9fd],[0x20001,0x20001],[0x20064,0x20064],[0x200e2,0x200e2],[0x20121,0x20121],[0x2092a,0x2092a],[0x20983,0x20983],[0x2098c,0x2098c],[0x2099c,0x2099c],[0x20aea,0x20aea],[0x20afd,0x20afd],[0x20b19,0x20b19],[0x22390,0x22390],[0x22998,0x22998],[0x23b1b,0x23b1b],[0x2626d,0x2626d],[0x2f890,0x2f890]];
const inRanges = (ranges, cp) => ranges.some(([lo, hi]) => cp >= lo && cp <= hi);
const RE_ND = /^\p{Nd}$/u;
const RE_N = /^\p{N}$/u;
const isAlpha = (c) => RE_ALPHA.test(c);
const isDigit = (c) => RE_ND.test(c) || inRanges(DIGIT_EXTRA, c.codePointAt(0));
const isNumeric = (c) => RE_N.test(c) || inRanges(NUMERIC_EXTRA, c.codePointAt(0));
const RE_UPPER = /^(?:\p{Lu}|\p{Uppercase})$/u;
const RE_LOWER_OR_TITLE = /^(?:\p{Ll}|\p{Lowercase}|\p{Lt})$/u;
// str.isupper(): ha ao menos um caractere "cased" e nenhum minusculo/titlecase.
function isUpper(s) {
let cased = false;
for (const c of s) {
if (RE_LOWER_OR_TITLE.test(c)) return false;
if (RE_UPPER.test(c)) cased = true;
}
return cased;
}
const charIsUpper = (c) => RE_UPPER.test(c) && !RE_LOWER_OR_TITLE.test(c);
// list[i] do Python: IndexError (excecao) fora do intervalo.
function at(list, index) {
if (index < 0 || index >= list.length) throw new RangeError("list index out of range");
return list[index];
}
// list.index(x): ValueError (excecao) se nao existir.
function indexOrThrow(list, value, from = 0) {
const i = list.indexOf(value, from);
if (i === -1) throw new Error("value not in list");
return i;
}
// ---------- encoders ----------
export function encodeNorthAmerican(text) {
let braille = "";
for (const phrase of text.split("\n")) {
const words = phrase.replaceAll('"', "'").split(" ");
for (const word of words) {
for (const letter of word) {
try {
const i = indexOrThrow(NORTH_AMERICAN.normal, letter.toLowerCase());
braille += at(NORTH_AMERICAN.braille, i);
} catch (e) { /* pass */ }
}
braille += " ";
}
braille = rstrip(braille) + "\n";
}
return rstrip(braille);
}
export function encodeBrazilian(text) {
let treated = "";
for (const phrase of text.split("\n")) {
for (const word of phrase.split(" ")) {
const w = chars(word);
for (let index = 0; index < w.length; index++) {
const letter = w[index];
let lastAlpha = false;
let nextAlpha = false;
if (index > 0) lastAlpha = isAlpha(w[index - 1]);
if (index < w.length - 1) nextAlpha = isAlpha(w[index + 1]);
if (lastAlpha && letter === "(") treated += " ";
treated += letter;
if (nextAlpha && letter === ")") treated += " ";
}
treated += " ";
}
treated = rstrip(treated) + "\n";
}
let braille = "";
for (const phrase of treated.split("\n")) {
for (const word of phrase.split(" ")) {
try {
const w = chars(word);
if (w.length === 0) throw new RangeError("string index out of range");
const isNumber = isNumeric(w[0]);
if (isNumber) braille += "⠼";
const upperWord = isUpper(word);
if (!isNumber && upperWord) braille += "⠨⠨";
let lastLetter = null;
for (const letter of w) {
const i = indexOrThrow(BRAZILIAN.normal, letter.toLowerCase());
if (!isNumber && isNumeric(letter)) braille += "⠼";
if (charIsUpper(letter) && !upperWord) braille += "⠨";
if (lastLetter) {
if (isNumeric(lastLetter) && isAlpha(letter)) braille += "⠂";
}
braille += at(BRAZILIAN.braille, i);
lastLetter = letter;
}
braille += " ";
} catch (e) { /* pass */ }
}
braille = rstrip(braille) + "\n";
}
return rstrip(braille).replaceAll(" ", "⠀");
}
// ---------- pinyin (porte do pypinyin 0.55: mmseg + TONE3 + neutro 5) ----------
let pinyinData = null; // { singles: {}, keys: string[] (ordenado UTF-16), diffs: Map }
let pinyinPromise = null;
async function loadPinyinData() {
if (pinyinData) return pinyinData;
if (!pinyinPromise) {
pinyinPromise = (async () => {
const url = new URL("./pinyin_data.json", import.meta.url);
let json;
if (typeof process !== "undefined" && process.versions && process.versions.node && url.protocol === "file:") {
const fs = await import("node:fs/promises");
json = JSON.parse(await fs.readFile(url, "utf8"));
} else {
const res = await fetch(url);
if (!res.ok) throw new Error("pinyin data unavailable");
json = await res.json();
}
pinyinData = { singles: json.s, keys: json.k.split("\n"), diffs: new Map(Object.entries(json.d)) };
return pinyinData;
})().catch((err) => { pinyinPromise = null; throw err; });
}
return pinyinPromise;
}
// Primeiro indice cuja chave >= word (ordem por unidades UTF-16, igual a do arquivo).
function lowerBound(keys, word) {
let lo = 0;
let hi = keys.length;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (keys[mid] < word) lo = mid + 1; else hi = mid;
}
return lo;
}
const isPhrase = (d, word) => { const i = lowerBound(d.keys, word); return i < d.keys.length && d.keys[i] === word; };
const isPrefix = (d, word) => { const i = lowerBound(d.keys, word); return i < d.keys.length && d.keys[i].startsWith(word); };
// pypinyin.seg.mmseg.Seg.cut com seg = Seg(p_set, no_non_phrases=True) (modo estrito).
function mmsegCut(d, text) {
const out = [];
const t = chars(text);
let pos = 0;
while (pos < t.length) {
let lastValid = "";
let lastValidIndex = 0;
let broke = false;
for (let index = 0; pos + index < t.length; index++) {
const word = t.slice(pos, pos + index + 1).join("");
if (isPrefix(d, word)) {
if (isPhrase(d, word)) {
lastValid = word;
lastValidIndex = index + 1;
}
} else {
if (lastValid) {
out.push(lastValid);
pos += lastValidIndex;
} else {
out.push(t[pos]);
pos += 1;
}
broke = true;
break;
}
}
if (!broke) {
if (lastValid) {
out.push(lastValid);
pos += lastValidIndex;
} else {
// estrito: o resto nao e uma frase, sai caractere a caractere
for (const c of t.slice(pos)) out.push(c);
break;
}
}
}
return out;
}
function singleReading(d, han) {
const r = d.singles[han];
return r === undefined ? han : r;
}
// pypinyin.pinyin(chunk, style=TONE3, neutral_tone_with_five=True) achatado e unido.
function hanChunkToPinyin(d, chunk) {
let out = "";
for (const segment of mmsegCut(d, chunk)) {
if (isPhrase(d, segment)) {
const diff = d.diffs.get(segment);
if (diff !== undefined) {
out += diff.split(" ").join("");
continue;
}
}
for (const han of segment) out += singleReading(d, han);
}
return out;
}
const RE_HAN_TEST = /[一-鿿]/u;
const RE_HAN_CHAR = /^[一-鿿]$/u;
async function hanziToPinyin(text) {
const d = await loadPinyinData();
const parts = [];
const t = chars(text);
if (t.length === 0) return "";
let key = RE_HAN_CHAR.test(t[0]);
let buffer = t[0];
const flush = () => parts.push(key ? hanChunkToPinyin(d, buffer) : buffer);
for (const c of t.slice(1)) {
const k = RE_HAN_CHAR.test(c);
if (k === key) buffer += c;
else { flush(); key = k; buffer = c; }
}
flush();
return parts.join("");
}
// ---------- encoder chines ----------
function encodeWord(word) {
const cells = [];
let buffer = "";
for (const ch of word) {
if (PUNCTUATION_CELLS.has(ch)) {
if (buffer) { cells.push(encodePinyin(buffer)); buffer = ""; }
cells.push(PUNCTUATION_CELLS.get(ch));
} else {
buffer += ch;
}
}
if (buffer) cells.push(encodePinyin(buffer));
return cells.join("");
}
function stripAccents(segment) {
const plain = [];
const tones = new Map();
for (const ch of segment) {
if (ACCENTS.has(ch)) {
const [base, tone] = ACCENTS.get(ch);
if (tone) tones.set(plain.length, tone);
plain.push(base);
} else {
plain.push(ch);
}
}
return [plain, tones];
}
// plain e uma lista de caracteres (code points), como a str do Python.
function parseSyllables(plain, tones) {
const slice = (a, b) => plain.slice(a, b).join("");
const result = [];
let index = 0;
const length = plain.length;
while (index < length) {
const ch = plain[index];
if (ch === "'" || ch === "’" || ch === " " || isDigit(ch)) { index += 1; continue; }
const start = index;
let initial = "";
if (INITIAL_CELLS.has(slice(index, index + 2))) {
initial = slice(index, index + 2);
index += 2;
} else if (INITIAL_CELLS.has(slice(index, index + 1))) {
initial = plain[index];
index += 1;
}
let rest = slice(index, plain.length);
if (YU_INITIALS.includes(initial) && chars(rest).slice(0, 1).join("") === "u") {
rest = "ü" + chars(rest).slice(1).join("");
}
const restChars = chars(rest);
let final = "";
for (let size = 4; size >= 1; size--) {
const candidate = restChars.slice(0, size).join("");
if (FINAL_CELLS.has(candidate)) {
final = candidate;
index += size;
break;
}
}
let tone = "";
if (index < length && "012345".includes(plain[index])) {
tone = plain[index];
index += 1;
}
if (!tone) {
for (let position = start; position < index; position++) {
if (tones.has(position)) { tone = tones.get(position); break; }
}
}
if (!initial && !final) { index = start + 1; continue; }
result.push([initial, final, tone]);
}
return result;
}
function encodePinyin(segment) {
const [plain, tones] = stripAccents(segment);
return parseSyllables(plain, tones).map(([i, f, t]) => encodeSyllable(i, f, t)).join("");
}
function encodeSyllable(initial, final, tone) {
let cells = "";
if (BUZZING_INITIALS.includes(initial) && final === "i") {
cells += INITIAL_CELLS.get(initial);
} else if (initial === "d" && final === "e" && (tone === "" || tone === "5" || tone === "0")) {
cells += INITIAL_CELLS.get("d");
tone = "";
} else {
if (initial) cells += INITIAL_CELLS.get(initial);
if (final) cells += FINAL_CELLS.get(final);
}
if (TONE_CELLS.has(tone)) cells += TONE_CELLS.get(tone);
return cells;
}
export async function encodeChinese(text) {
if (RE_HAN_TEST.test(text)) text = await hanziToPinyin(text);
return text.split("\n").map((line) => line.split(" ").map(encodeWord).join(BLANK)).join("\n");
}
// ---------- decoders ----------
export function decodeNorthAmerican(braille) {
let text = "";
for (const phrase of braille.split("\n")) {
const words = phrase.replaceAll('"', "'").split(" ");
for (const word of words) {
for (const letter of word) {
try {
const i = indexOrThrow(NORTH_AMERICAN.braille, letter);
text += at(NORTH_AMERICAN.normal, i);
} catch (e) { /* pass */ }
}
text += " ";
}
text = rstrip(text) + "\n";
}
return rstrip(text);
}
export function decodeBrazilian(braille) {
let text = "";
for (const phrase of braille.split("\n")) {
for (const word of phrase.split("⠀")) {
try {
let treated = word;
if (word.length === 0) throw new RangeError("string index out of range");
let isNumber = word[0] === "⠼";
if (isNumber) treated = treated.slice(1);
if (word.startsWith("⠣⠄")) { text += "("; treated = treated.slice(2); }
if (word.startsWith("⠷⠄")) { text += "["; treated = treated.slice(2); }
if (word.endsWith("⠄⠜")) treated = treated.slice(0, -2);
if (word.endsWith("⠄⠾")) treated = treated.slice(0, -2);
let isUpperCase = false;
if (!isNumber && word.startsWith("⠨⠨")) {
isUpperCase = true;
treated = treated.slice(2);
}
let lastLetter = null;
for (const letter of treated) {
if (letter === "⠠" || letter === "⠨") { lastLetter = letter; continue; }
if (letter === "⠂" && lastLetter === "⠠") { text += "/"; continue; }
if (letter === "⠴" && lastLetter === "⠸") { text += "%"; continue; }
if (isNumber && letter === "⠂") { isNumber = false; continue; }
let index = BRAZILIAN.braille.indexOf(letter);
if (index === -1) { lastLetter = letter; continue; }
if (isNumber) index = indexOrThrow(BRAZILIAN.braille.slice(38), letter) + 38;
if (isUpperCase || lastLetter === "⠨") text += at(BRAZILIAN.normal, index).toUpperCase();
else text += at(BRAZILIAN.normal, index);
lastLetter = letter;
}
if (word.endsWith("⠄⠜")) text += ")";
if (word.endsWith("⠄⠾")) text += "]";
} catch (e) { /* pass */ }
text += " ";
}
text = rstrip(text) + "\n";
}
return rstrip(text);
}
const YU_FINALS = new Map([["ü", "u"], ["üe", "ue"], ["üan", "uan"], ["ün", "un"]]);
const I_OR_YU_START = ["i", "ü"];
function matchPunctuation(w, index) {
for (const size of [3, 2, 1]) {
const chunk = w.slice(index, index + size).join("");
if (PUNCTUATION_BY_CELL.has(chunk)) return [PUNCTUATION_BY_CELL.get(chunk), size];
}
return null;
}
function renderSyllable(initial, finalCell, tone) {
if (initial && finalCell === null) {
let syllable;
if (BUZZING_INITIALS.includes(initial)) syllable = initial + "i";
else if (initial === "d") syllable = "de";
else syllable = initial;
return syllable + tone;
}
if (finalCell === null) return tone;
if (!initial) {
const f = FINAL_STANDALONE_BY_CELL.has(finalCell) ? FINAL_STANDALONE_BY_CELL.get(finalCell) : FINAL_BY_CELL.get(finalCell);
return f + tone;
}
let final = FINAL_BY_CELL.get(finalCell);
if (I_OR_YU_START.includes(final[0])) {
initial = ({ g: "j", k: "q", h: "x" })[initial] ?? initial;
}
if (YU_INITIALS.includes(initial) && YU_FINALS.has(final)) final = YU_FINALS.get(final);
if (final === "o" && !["", "b", "p", "m", "f"].includes(initial)) final = "e";
return initial + final + tone;
}
function decodeWord(word) {
const w = chars(word);
let output = "";
let index = 0;
const length = w.length;
while (index < length) {
const punctuation = matchPunctuation(w, index);
if (punctuation) { output += punctuation[0]; index += punctuation[1]; continue; }
const initial = INITIAL_BY_CELL.get(w[index]);
const hasInitial = initial !== undefined;
if (hasInitial) index += 1;
let finalCell = null;
if (index < length && FINAL_BY_CELL.has(w[index])) { finalCell = w[index]; index += 1; }
let tone = "";
if (index < length && TONE_BY_CELL.has(w[index])) { tone = TONE_BY_CELL.get(w[index]); index += 1; }
if (!hasInitial && finalCell === null) { index += 1; continue; }
output += renderSyllable(initial || "", finalCell, tone);
}
return output;
}
export function decodeChinese(braille) {
return braille.split("\n")
.map((line) => line.split(BLANK).filter((word) => word).map(decodeWord).join(" "))
.join("\n");
}
// ---------- fachada (translator.py) ----------
export async function encodeText(text, alphabet = "North American") {
switch (alphabet) {
case "Brazilian": return encodeBrazilian(text);
case "North American": return encodeNorthAmerican(text);
case "Chinese": return encodeChinese(text);
default: return "Alphabet not found.";
}
}
export function decodeText(braille, alphabet = "North American") {
switch (alphabet) {
case "Brazilian": return decodeBrazilian(braille);
case "North American": return decodeNorthAmerican(braille);
case "Chinese": return decodeChinese(braille);
default: return "Alphabet not found.";
}
}
// ---------- API compativel com POST /api/encode e /api/decode ----------
export class ValidationError extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
this.status = 422;
}
}
function validate(payload, field) {
if (payload === null || typeof payload !== "object") throw new ValidationError("invalid payload");
const value = payload[field];
if (typeof value !== "string") throw new ValidationError(`${field}: string required`);
if (chars(value).length > MAX_TEXT_LENGTH) throw new ValidationError(`${field}: at most ${MAX_TEXT_LENGTH} characters`);
const alphabet = payload.alphabet;
if (alphabet !== undefined && alphabet !== null && !ALPHABETS.includes(alphabet)) {
throw new ValidationError("alphabet: invalid value");
}
return [value, alphabet ? alphabet : "North American"];
}
/** @returns {Promise<{encoded: string}>} mesmo objeto de response.data de /api/encode */
export async function encode(payload) {
const [text, alphabet] = validate(payload, "text");
return { encoded: await encodeText(text, alphabet) };
}
/** @returns {Promise<{decoded: string}>} mesmo objeto de response.data de /api/decode */
export async function decode(payload) {
const [braille, alphabet] = validate(payload, "braille");
return { decoded: decodeText(braille, alphabet) };
}