Skip to content

Commit 1e852cf

Browse files
committed
move final translator
1 parent e5337d2 commit 1e852cf

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { dictionaryParser } from "../dictionary/parser.ts";
1313
import PROJECT_DATA from "../project_data.json" with { type: "json" };
1414
import { loadCustomDictionary } from "./dictionary.ts";
1515
import { checkLocalStorage, setIgnoreError } from "./local_storage.ts";
16-
import { translate } from "./translator.ts";
1716
import { PositionedError } from "./parser/parser_lib.ts";
1817
import { settings } from "./settings.ts";
1918
import {
@@ -22,6 +21,7 @@ import {
2221
resetElementsToCurrent,
2322
resetElementsToDefault,
2423
} from "./settings_frontend.ts";
24+
import { translate } from "./translator/translator.ts";
2525

2626
const DICTIONARY_AUTO_PARSE_THRESHOLD = 9000;
2727

src/repl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { unescape } from "@std/html/entities";
44
import entityList from "@std/html/named-entity-list.json" with { type: "json" };
55
import { repeatArray } from "../misc/misc.ts";
6-
import { translate } from "./translator.ts";
6+
import { translate } from "./translator/translator.ts";
77

88
if (import.meta.main) {
99
// deno-lint-ignore no-console

src/translator.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/translator/translator.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
import { ArrayResult } from "../array_result.ts";
1+
import { distinct } from "@std/collections/distinct";
2+
import { shuffle } from "@std/random/shuffle";
3+
import { deduplicateErrors } from "../../misc/deduplicate_errors.ts";
4+
import { errors } from "../../telo_misikeke/telo_misikeke.js";
5+
import { ArrayResult, ArrayResultError } from "../array_result.ts";
26
import { parser } from "../parser/parser.ts";
7+
import { settings } from "../settings.ts";
38
import * as EnglishComposer from "./composer.ts";
49
import { multipleSentences } from "./sentence.ts";
510

6-
export function translate(text: string): ArrayResult<string> {
7-
return parser
8-
.parse(text)
11+
export function translate(tokiPona: string): ArrayResult<string> {
12+
const arrayResult = parser
13+
.parse(tokiPona)
914
.flatMap(multipleSentences)
1015
.map(EnglishComposer.multipleSentences);
16+
if (!arrayResult.isError()) {
17+
const values = distinct(arrayResult.array);
18+
if (settings.randomize) {
19+
return new ArrayResult(shuffle(values));
20+
} else {
21+
return new ArrayResult(values);
22+
}
23+
} else {
24+
const teloMisikekeErrors = settings.teloMisikeke
25+
? errors(tokiPona)
26+
.map((message) => new ArrayResultError(message, { isHtml: true }))
27+
: [];
28+
const error = teloMisikekeErrors.length === 0
29+
? deduplicateErrors(arrayResult.errors)
30+
: teloMisikekeErrors;
31+
return ArrayResult.errors(error);
32+
}
1133
}

0 commit comments

Comments
 (0)