|
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"; |
2 | 6 | import { parser } from "../parser/parser.ts";
|
| 7 | +import { settings } from "../settings.ts"; |
3 | 8 | import * as EnglishComposer from "./composer.ts";
|
4 | 9 | import { multipleSentences } from "./sentence.ts";
|
5 | 10 |
|
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) |
9 | 14 | .flatMap(multipleSentences)
|
10 | 15 | .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 | + } |
11 | 33 | }
|
0 commit comments