Skip to content

Commit 7eb9769

Browse files
committed
implement pi, nanpa, and preposition
1 parent e6c6656 commit 7eb9769

4 files changed

Lines changed: 133 additions & 8 deletions

File tree

src/translator2/modifier.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { pronoun } from "./pronoun.ts";
1010
import { adjective, compoundAdjective } from "./adjective.ts";
1111
import * as Composer from "../parser/composer.ts";
1212
import { word } from "./word.ts";
13+
import { phrase, PhraseTranslation } from "./phrase.ts";
14+
import { nanpa } from "./nanpa.ts";
1315

1416
export type ModifierTranslation =
1517
| Readonly<{ type: "noun"; noun: English.NounPhrase }>
@@ -143,6 +145,20 @@ function defaultModifier(wordUnit: TokiPona.WordUnit) {
143145
}
144146
}
145147
}
148+
function pi(
149+
insidePhrase: TokiPona.Phrase,
150+
): IterableResult<ModifierTranslation> {
151+
return phrase({
152+
phrase: insidePhrase,
153+
includeGerund: true,
154+
})
155+
.filter((modifier) =>
156+
modifier.type !== "verb" &&
157+
(modifier.type !== "adjective" || modifier.inWayPhrase == null)
158+
) as IterableResult<
159+
PhraseTranslation & { type: Exclude<PhraseTranslation["type"], "verb"> }
160+
>;
161+
}
146162
function modifier(modifier: TokiPona.Modifier) {
147163
switch (modifier.type) {
148164
case "simple":
@@ -153,8 +169,13 @@ function modifier(modifier: TokiPona.Modifier) {
153169
name: modifier.words,
154170
});
155171
case "pi":
172+
return pi(modifier.phrase);
156173
case "nanpa":
157-
return IterableResult.errors([new TranslationTodoError(modifier.type)]);
174+
return nanpa(modifier)
175+
.map((noun): ModifierTranslation => ({
176+
type: "noun",
177+
noun: { ...noun, type: "simple" },
178+
}));
158179
}
159180
}
160181
export function multipleModifiers(

src/translator2/nanpa.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as TokiPona from "../parser/ast.ts";
2+
import { IterableResult } from "../compound.ts";
3+
import * as English from "./ast.ts";
4+
import { phrase } from "./phrase.ts";
5+
import { throwError } from "../../misc/misc.ts";
6+
import { FilteredError } from "./error.ts";
7+
8+
export function nanpa(
9+
nanpa: TokiPona.Nanpa,
10+
): IterableResult<English.SimpleNounPhrase> {
11+
return phrase({
12+
phrase: nanpa.phrase,
13+
includeGerund: true,
14+
})
15+
.map((phrase): English.SimpleNounPhrase =>
16+
phrase.type !== "noun"
17+
? throwError(
18+
new FilteredError(
19+
`${phrase.type} within "position X" phrase`,
20+
),
21+
)
22+
: {
23+
determiners: [],
24+
adjectives: [],
25+
singular: { subject: "position", object: "position" },
26+
plural: null,
27+
reduplicationCount: 1,
28+
wordEmphasis: false,
29+
perspective: "third",
30+
postCompound: phrase.noun,
31+
adjectiveName: null,
32+
prepositions: [],
33+
phraseEmphasis: false,
34+
}
35+
);
36+
}

src/translator2/phrase.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import * as English from "./ast.ts";
22
import { AdjectiveWithInWay } from "./adjective.ts";
33
import { AdjectivalModifier } from "./modifier.ts";
4-
import {
5-
ExhaustedError,
6-
FilteredError,
7-
TranslationTodoError,
8-
} from "./error.ts";
4+
import { ExhaustedError, FilteredError } from "./error.ts";
95
import { mapNullable, nullableAsArray } from "../../misc/misc.ts";
106
import { nounAsPreposition } from "./preposition.ts";
117
import { AdverbialModifier } from "./modifier.ts";
@@ -15,6 +11,7 @@ import { wordUnit } from "./word_unit.ts";
1511
import { multipleModifiers } from "./modifier.ts";
1612
import * as Composer from "../parser/composer.ts";
1713
import { CONJUNCTION } from "./misc.ts";
14+
import { preposition } from "./preposition.ts";
1815

1916
export type PhraseTranslation =
2017
| Readonly<{ type: "noun"; noun: English.NounPhrase }>
@@ -297,7 +294,12 @@ export function phrase(
297294
case "simple":
298295
return defaultPhrase({ ...options, phrase });
299296
case "preposition":
300-
return IterableResult.errors([new TranslationTodoError("preposition")]);
297+
return preposition(phrase)
298+
.map(prepositionAsVerb)
299+
.map((verb): PhraseTranslation => ({
300+
type: "verb",
301+
verb: { ...verb, type: "simple" },
302+
}));
301303
case "preverb":
302304
return preverb(phrase)
303305
.map((verb): PhraseTranslation => ({

src/translator2/preposition.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,72 @@
11
import * as English from "./ast.ts";
2-
import { noEmphasis } from "./word.ts";
2+
import { noEmphasis, word } from "./word.ts";
3+
import * as TokiPona from "../parser/ast.ts";
4+
import { IterableResult } from "../compound.ts";
5+
import { multipleModifiers } from "./modifier.ts";
6+
import { throwError } from "../../misc/misc.ts";
7+
import { FilteredError, TranslationTodoError } from "./error.ts";
8+
import { multiplePhrases } from "./phrase.ts";
9+
import { dictionary } from "../dictionary.ts";
10+
import { getReduplicationCount } from "./word_unit.ts";
311

12+
export function preposition(
13+
preposition: TokiPona.Preposition,
14+
): IterableResult<English.Preposition> {
15+
return IterableResult.combine(
16+
prepositionAsWord(preposition.preposition),
17+
multipleModifiers(preposition.modifiers)
18+
.map((modifier) =>
19+
modifier.type === "adverbial"
20+
? (modifier.inWayPhrase == null ? modifier.adverbs : throwError(
21+
new FilteredError(
22+
'"in [adjective] way" prepositional phrase modifying ' +
23+
"preposition",
24+
),
25+
))
26+
: throwError(new FilteredError("adjectives modifying preposition"))
27+
),
28+
multiplePhrases({
29+
phrases: preposition.phrases,
30+
includeGerund: true,
31+
andParticle: null,
32+
})
33+
.map((phrases) =>
34+
phrases.type === "noun"
35+
? phrases.noun
36+
: throwError(new FilteredError(`${phrases.type} as indirect object`))
37+
),
38+
)
39+
.map(([preposition, adverbs, object]): English.Preposition => ({
40+
adverbs,
41+
preposition,
42+
object,
43+
emphasis: preposition.emphasis,
44+
}));
45+
}
46+
function prepositionAsWord(
47+
preposition: TokiPona.HeadedWordUnit,
48+
): IterableResult<English.Word> {
49+
switch (preposition.type) {
50+
case "x ala x":
51+
return IterableResult.errors([
52+
new TranslationTodoError("preposition ala preposition"),
53+
]);
54+
case "simple":
55+
case "reduplication":
56+
return IterableResult.fromArray(
57+
dictionary.get(preposition.word)!.definitions,
58+
)
59+
.filterMap((definition) =>
60+
definition.type === "preposition"
61+
? word({
62+
word: definition.preposition,
63+
reduplicationCount: getReduplicationCount(preposition),
64+
emphasis: preposition.emphasis != null,
65+
})
66+
: null
67+
);
68+
}
69+
}
470
export function nounAsPreposition(
571
phrase: English.NounPhrase,
672
preposition: string,

0 commit comments

Comments
 (0)