Skip to content

Commit 27ffc54

Browse files
committed
implement translation of o clause, closes #60
1 parent a83cf05 commit 27ffc54

File tree

3 files changed

+136
-7
lines changed

3 files changed

+136
-7
lines changed

src/translator/clause.ts

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { nullableAsArray, throwError } from "../misc.ts";
33
import * as TokiPona from "../parser/ast.ts";
44
import * as English from "./ast.ts";
55
import { FilteredOutError, TranslationTodoError } from "./error.ts";
6+
import { perspective } from "./noun.ts";
67
import { multiplePhrases, multiplePhrasesAsNoun } from "./phrase.ts";
78
import { predicate } from "./predicate.ts";
89
import { nounAsPreposition } from "./preposition.ts";
9-
import { verb } from "./verb.ts";
10+
import { addModalToAll, verb } from "./verb.ts";
1011
import { unemphasized } from "./word.ts";
1112

1213
function phraseClause(
@@ -85,10 +86,7 @@ function liClause(
8586
predicate(clause.predicates, "li"),
8687
)
8788
.flatMap(([subject, predicate]) => {
88-
const perspective = subject.type === "simple"
89-
? subject.perspective
90-
: "third";
91-
return verb(predicate, perspective, subject.quantity)
89+
return verb(predicate, perspective(subject), subject.quantity)
9290
.map((verb) => ({
9391
type: "default",
9492
subject,
@@ -97,6 +95,89 @@ function liClause(
9795
}));
9896
});
9997
}
98+
function iWish(
99+
subject: English.NounPhrase,
100+
verb: English.VerbPhrase,
101+
): English.Clause {
102+
return {
103+
type: "default",
104+
subject: {
105+
type: "simple",
106+
determiner: [],
107+
adjective: [],
108+
noun: unemphasized("I"),
109+
quantity: "singular",
110+
perspective: "first",
111+
postAdjective: null,
112+
preposition: [],
113+
emphasis: false,
114+
},
115+
verb: {
116+
type: "default",
117+
adverb: [],
118+
verb: {
119+
modal: null,
120+
first: unemphasized("wish"),
121+
rest: [],
122+
},
123+
subjectComplement: null,
124+
contentClause: {
125+
type: "default",
126+
subject,
127+
verb,
128+
hideSubject: false,
129+
},
130+
object: null,
131+
objectComplement: null,
132+
preposition: [],
133+
hideVerb: false,
134+
},
135+
hideSubject: false,
136+
};
137+
}
138+
function oClause(
139+
clause: TokiPona.Clause & { type: "o clause" },
140+
): ArrayResult<English.Clause> {
141+
const subject = clause.subjects != null
142+
? multiplePhrasesAsNoun({
143+
phrases: clause.subjects,
144+
place: "subject",
145+
includeGerund: true,
146+
andParticle: "en",
147+
})
148+
: new ArrayResult<English.NounPhrase>([{
149+
type: "simple",
150+
determiner: [],
151+
adjective: [],
152+
noun: unemphasized("you"),
153+
quantity: "plural",
154+
perspective: "second",
155+
postAdjective: null,
156+
preposition: [],
157+
emphasis: false,
158+
}]);
159+
return ArrayResult.concat(
160+
ArrayResult.combine(subject, predicate(clause.predicates, "o"))
161+
.flatMap(([subject, predicate]) => {
162+
return verb(predicate, perspective(subject), subject.quantity)
163+
.map<English.Clause>((verb) => iWish(subject, verb));
164+
}),
165+
ArrayResult.combine(
166+
subject,
167+
predicate(clause.predicates, "o")
168+
.map((verb) => addModalToAll(unemphasized("should"), verb)),
169+
)
170+
.flatMap(([subject, predicate]) => {
171+
return verb(predicate, perspective(subject), subject.quantity)
172+
.map((verb) => ({
173+
type: "default",
174+
subject,
175+
verb,
176+
hideSubject: false,
177+
}));
178+
}),
179+
);
180+
}
100181
export function clause(clause: TokiPona.Clause): ArrayResult<English.Clause> {
101182
switch (clause.type) {
102183
case "phrases":
@@ -118,8 +199,9 @@ export function clause(clause: TokiPona.Clause): ArrayResult<English.Clause> {
118199
);
119200
case "li clause":
120201
return liClause(clause);
121-
case "prepositions":
122202
case "o clause":
203+
return oClause(clause);
204+
case "prepositions":
123205
return new ArrayResult(new TranslationTodoError(clause.type));
124206
}
125207
}

src/translator/noun.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,11 @@ export function nounAsPlainString(
126126
return noun({ definition, reduplicationCount: 1, emphasis: false })
127127
.map((noun) => EnglishComposer.noun(noun, 0));
128128
}
129+
export function perspective(noun: English.NounPhrase): Dictionary.Perspective {
130+
switch (noun.type) {
131+
case "simple":
132+
return noun.perspective;
133+
case "compound":
134+
return "third";
135+
}
136+
}

src/translator/verb.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as Dictionary from "../../dictionary/type.ts";
22
import { ArrayResult } from "../array_result.ts";
3-
import { mapNullable } from "../misc.ts";
3+
import { mapNullable, nullableAsArray } from "../misc.ts";
44
import { settings } from "../settings.ts";
55
import * as English from "./ast.ts";
6+
import { FilteredOutError } from "./error.ts";
67
import { condense } from "./misc.ts";
78
import { noun } from "./noun.ts";
89
import { nounAsPreposition } from "./preposition.ts";
@@ -43,6 +44,44 @@ export function condenseVerb(present: string, past: string): string {
4344
const second = past.split(" ")[0];
4445
return [condense(first, second), ...rest].join(" ");
4546
}
47+
export function addModal(modal: English.Word, verb: PartialVerb): PartialVerb {
48+
if (verb.modal == null) {
49+
const newRest = nullableAsArray(verb.first)
50+
.map(({ presentPlural }) => presentPlural)
51+
.map((verb) => verb === "are" ? "be" : verb)
52+
.map((newVerb) =>
53+
word({
54+
word: newVerb,
55+
reduplicationCount: verb.reduplicationCount,
56+
emphasis: verb.wordEmphasis,
57+
})
58+
);
59+
return {
60+
...verb,
61+
modal,
62+
first: null,
63+
rest: [...newRest, ...verb.rest],
64+
reduplicationCount: 1,
65+
wordEmphasis: false,
66+
};
67+
} else {
68+
throw new FilteredOutError("nested modal verb");
69+
}
70+
}
71+
export function addModalToAll(
72+
modal: English.Word,
73+
verb: PartialCompoundVerb,
74+
): PartialCompoundVerb {
75+
switch (verb.type) {
76+
case "simple":
77+
return { ...addModal(modal, verb), type: "simple" };
78+
case "compound":
79+
return {
80+
...verb,
81+
verb: verb.verb.map((verb) => addModalToAll(modal, verb)),
82+
};
83+
}
84+
}
4685
export function partialVerb(
4786
options: Readonly<{
4887
definition: Dictionary.Verb;

0 commit comments

Comments
 (0)