Skip to content

Commit

Permalink
fix parsing for languages other than english
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-zibert committed Dec 10, 2023
1 parent 879c07c commit d832f65
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
43 changes: 35 additions & 8 deletions frontend/src/parsing/__test__/turing-copy-paste.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ test("A4BL4O", () => {
});
});

test("A4BL4O lowercase", () => {
test("B51EHF9 firefox", () => {
expect(
parse(
`op #A4B L4O Share Get Criteria and Verification cards in the box. A 5 578 B 10 376 C 11 566 D 17 618 Back to Homepage`.toLowerCase()
`logoTMSolo/Co-op#B51 EHF 9 ShareGet Criteria and Verification cards in the box.a2587b12319c17496d21523e22536Back to Homepage`
)
).toStrictEqual({
ind: [5, 10, 11, 17],
crypt: [578, 376, 566, 618],
color: 0,
ind: [2, 12, 17, 21, 22],
crypt: [587, 319, 496, 523, 536],
color: 1,
m: 0,
code: "A4BL4O",
code: "B51EHF9",
});
});

Expand Down Expand Up @@ -85,8 +85,6 @@ test("E52 NBU A", () => {
});
});

//

test("H5K M7S", () => {
expect(
parse(
Expand All @@ -100,3 +98,32 @@ test("H5K M7S", () => {
code: "H5KM7S",
});
});

test("all languages work", () => {
const problems = [
`logoTM Solo/Co-op #B5V VGO Share Get Criteria and Verification cards in the box. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Back to Homepage`,
`logoTM Solo / Coop #B5V VGO Partager Récupérer les cartes Critères et Vérification dans la boîte. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Retour à la page d'accueil`,
`logoTM #B5V VGO 分享 从游戏盒中拿取标准卡牌和验证卡牌。 A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 返回主页`,
`logoTM #B5V VGO 分享 從遊戲盒中拿取準則卡和驗證卡。 A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 返回主畫面`,
`logoTM #B5V VGO 공유하기 다음 검증기 카드와 테스트 카드를 준비하세요. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 첫 화면으로 돌아가기`,
`logoTM #B5V VGO Teilen Nimm folgende Prüf- und Ergebniskarten aus der Schachtel: A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Zurück zur Startseite`,
`logoTM #B5V VGO MEGOSZTÁS Keresd ki a feltétel- és igazolókártyákat a dobozból. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Vissza a főoldalra`,
`logoTM #B5V VGO Condividi Prendi le carte criterio e le carte verifica indicate. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Torna alla Homepage`,
`logoTM #B5V VGO Comparte Toma las siguientes cartas de Criterio y de Verificación. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Volver a la página principal`,
`logoTM #B5V VGO ΜΟΙΡΑΣΤΕΙΤΕ Βρείτε κάρτες Κριτηρίων και Ελεγκτών στο κουτί. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Πίσω στην Αρχική Σελίδα`,
`logoTM #B5V VGO Compartilhar Pegue as cartas de Critério e Verificação na caixa. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Voltar para Home`,
`logoTM #B5V VGO シェアする 下記の要件カードと判定カードを 用意してください。 A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 トップに戻る`,
`logoTM #B5V VGO PODZIEL SIĘ Weź karty kryteriów i weryfikacji z pudełka. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Wróć do strony głównej`,
`logoTM #B5V VGO DELEN Pak de criterium- en controlekaarten uit de doos. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Terug naar homepage`,
`logoTM #B5V VGO ПОДІЛИТИСЯ Візьміть карти критеріїв та верифікації з коробки. A 4 447 B 14 596 C 18 220 D 19 233 E 22 485 Повернутися на головну`,
];
for (const problem of problems) {
expect(parse(problem)).toStrictEqual({
ind: [4, 14, 18, 19, 22],
crypt: [447, 596, 220, 233, 485],
m: 0,
color: 0,
code: "B5VVGO",
});
}
});
36 changes: 26 additions & 10 deletions frontend/src/parsing/turing-copy-paste.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import { range, getColor, parseCode, type ParsedGame } from "./util";

function getCode(parts: string[]) {
let start = 0;
for (let i = 0; i < parts.length; i += 1) {
if (parts[i].startsWith("#")) {
function getCode(text: string) {
let start = null;
let currentPart = "";
let result = "";
for (let i = 0; i < text.length; i += 1) {
if (text[i] === "#") {
start = i;
} else if (parts[i].toLowerCase().includes("share")) {
return parts.slice(start, i);
continue;
}
if (start === null) {
continue;
}
if (text[i] === " ") {
if (currentPart.length > 0 && currentPart.length <= 3) {
result += currentPart;
currentPart = "";
continue;
} else {
return result || null;
}
}
if (text[i].match(/[A-Z0-9]/)) {
currentPart += text[i];
} else {
return result || null;
}
}
return null;
Expand Down Expand Up @@ -79,12 +97,10 @@ function parseNightmareInd(text: string, numVerifiers: number) {
}

export function parse(text: string): ParsedGame | null {
const parts = text.replaceAll(/\s+/g, " ").trim().split(" ");
const codeParts = getCode(parts);
if (codeParts === null) {
const code = getCode(text);
if (code === null) {
return null;
}
const code = codeParts.join("").slice(1).toUpperCase();
const parsedCode = parseCode(code);
if (parsedCode === null) {
return null;
Expand Down

0 comments on commit d832f65

Please sign in to comment.