Skip to content

Commit cbc41fd

Browse files
committed
implement preposition
1 parent d41fd27 commit cbc41fd

File tree

5 files changed

+78
-9
lines changed

5 files changed

+78
-9
lines changed

src/parser/composer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ export function phrase(value: Phrase): string {
9090
return preposition(value);
9191
}
9292
}
93-
function particle(type: "and conjunction" | "anu", particle: string): string {
93+
function particle(
94+
type: "and conjunction" | "anu",
95+
particle: null | string,
96+
): string {
9497
if (type === "and conjunction") {
95-
return particle;
98+
return particle!;
9699
} else {
97100
return "anu;";
98101
}
99102
}
100103
export function multiplePhrases(
101104
phrases: MultiplePhrases,
102-
andParticle: string,
105+
andParticle: null | string,
103106
): string {
104107
switch (phrases.type) {
105108
case "single":

src/translator/ast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export type Preposition = Readonly<{
8888
adverb: ReadonlyArray<Word>;
8989
preposition: Word;
9090
object: NounPhrase;
91+
emphasis: boolean;
9192
}>;
9293
export type Sentence = Readonly<{
9394
clauses: ReadonlyArray<Clause>;

src/translator/composer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ export function adjective(
6868
return word({ word: text, emphasis: phrases.emphasis });
6969
}
7070
function preposition(preposition: English.Preposition): string {
71-
return `${word(preposition.preposition)} ${noun(preposition.object, 0)}`;
71+
return word({
72+
word: `${word(preposition.preposition)} ${noun(preposition.object, 0)}`,
73+
emphasis: preposition.emphasis,
74+
});
7275
}
7376
function complement(
7477
complement: English.Complement,

src/translator/phrase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ export function multiplePhrases(
325325
phrases: TokiPona.MultiplePhrases;
326326
place: Place;
327327
includeGerund: boolean;
328-
andParticle: string;
328+
andParticle: null | string;
329329
includeVerb: boolean;
330330
}>,
331331
): ArrayResult<PhraseTranslation> {

src/translator/preposition.ts

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,74 @@
1+
import { throwError } from "../../misc/misc.ts";
12
import { ArrayResult } from "../array_result.ts";
3+
import { dictionary } from "../dictionary.ts";
24
import * as TokiPona from "../parser/ast.ts";
5+
import { fixAdverb } from "./adverb.ts";
36
import * as English from "./ast.ts";
4-
import { TranslationTodoError } from "./error.ts";
5-
import { noEmphasis } from "./word.ts";
7+
import { FilteredError, TranslationTodoError } from "./error.ts";
8+
import { multipleModifiers } from "./modifier.ts";
9+
import { multiplePhrases } from "./phrase.ts";
10+
import { noEmphasis, word } from "./word.ts";
11+
import { getReduplicationCount } from "./word_unit.ts";
612

713
export function preposition(
8-
_preposition: TokiPona.Preposition,
14+
preposition: TokiPona.Preposition,
915
): ArrayResult<English.Preposition> {
10-
throw new TranslationTodoError("preposition");
16+
return ArrayResult.combine(
17+
prepositionAsWord(preposition.preposition),
18+
multipleModifiers(preposition.modifiers)
19+
.filterMap((modifier) =>
20+
modifier.type === "adverbial"
21+
? (modifier.inWayPhrase == null ? modifier.adverb : throwError(
22+
new FilteredError(
23+
'"in [adjective] way" prepositional phrase modifying preposition',
24+
),
25+
))
26+
: throwError(new FilteredError("adjectives modifying preposition"))
27+
)
28+
.map(fixAdverb),
29+
multiplePhrases({
30+
phrases: preposition.phrases,
31+
place: "object",
32+
includeGerund: true,
33+
andParticle: null,
34+
includeVerb: false,
35+
})
36+
.filterMap((phrases) =>
37+
phrases.type === "noun"
38+
? phrases.noun
39+
: throwError(new FilteredError(`${phrases.type} as indirect object`))
40+
),
41+
)
42+
.map<English.Preposition>(([preposition, adverb, object]) => ({
43+
adverb,
44+
preposition,
45+
object,
46+
emphasis: preposition.emphasis != null,
47+
}));
48+
}
49+
function prepositionAsWord(
50+
preposition: TokiPona.HeadedWordUnit,
51+
): ArrayResult<English.Word> {
52+
switch (preposition.type) {
53+
case "x ala x":
54+
return new ArrayResult(
55+
new TranslationTodoError("preposition ala preposition"),
56+
);
57+
case "default":
58+
case "reduplication":
59+
return new ArrayResult(
60+
dictionary.get(preposition.word)!.definitions,
61+
)
62+
.filterMap((definition) =>
63+
definition.type === "preposition"
64+
? word({
65+
word: definition.preposition,
66+
reduplicationCount: getReduplicationCount(preposition),
67+
emphasis: preposition.emphasis != null,
68+
})
69+
: null
70+
);
71+
}
1172
}
1273
export function nounAsPreposition(
1374
phrase: English.NounPhrase,
@@ -17,5 +78,6 @@ export function nounAsPreposition(
1778
adverb: [],
1879
preposition: noEmphasis(preposition),
1980
object: phrase,
81+
emphasis: false,
2082
};
2183
}

0 commit comments

Comments
 (0)