Skip to content

Commit 8c4cee4

Browse files
committed
implement multiple phrase translator
1 parent 17f2ea4 commit 8c4cee4

1 file changed

Lines changed: 135 additions & 2 deletions

File tree

src/translator2/phrase.ts

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import { IterableResult } from "../compound.ts";
1414
import { wordUnit } from "./word_unit.ts";
1515
import { multipleModifiers } from "./modifier.ts";
1616
import * as Composer from "../parser/composer.ts";
17-
import { verb } from "./verb.ts";
18-
import { word } from "./word.ts";
17+
import { CONJUNCTION } from "../translator/misc.ts";
1918

2019
export type PhraseTranslation =
2120
| Readonly<{ type: "noun"; noun: English.NounPhrase }>
@@ -307,3 +306,137 @@ export function phrase(
307306
}));
308307
}
309308
}
309+
export function phraseAsVerb(
310+
phrase: PhraseTranslation,
311+
): English.VerbPhrase {
312+
// TODO: on grammar fixer, extract noun and adjective negative modifier and put it on the verb
313+
switch (phrase.type) {
314+
case "noun":
315+
case "adjective": {
316+
let subjectComplement: English.Complement;
317+
let inWayPhrase: null | English.NounPhrase;
318+
switch (phrase.type) {
319+
case "noun": {
320+
inWayPhrase = null;
321+
subjectComplement = {
322+
type: "noun",
323+
noun: phrase.noun,
324+
};
325+
break;
326+
}
327+
case "adjective": {
328+
inWayPhrase = phrase.inWayPhrase;
329+
subjectComplement = {
330+
type: "adjective",
331+
adjective: phrase.adjective,
332+
};
333+
break;
334+
}
335+
}
336+
return {
337+
type: "simple",
338+
verb: [
339+
{
340+
verb: {
341+
type: "non-modal",
342+
presentPlural: "are",
343+
presentSingular: "is",
344+
past: "were",
345+
emphasis: false,
346+
reduplicationCount: 1,
347+
},
348+
preAdverbs: [],
349+
postAdverb: null,
350+
},
351+
],
352+
subjectComplement,
353+
contentClause: null,
354+
object: null,
355+
objectComplement: null,
356+
prepositions: nullableAsArray(inWayPhrase)
357+
.map((noun) => nounAsPreposition(noun, "in")),
358+
forObject: false,
359+
predicateType: null,
360+
emphasis: false,
361+
hideVerb: false,
362+
};
363+
}
364+
case "verb":
365+
return phrase.verb;
366+
}
367+
}
368+
369+
export function multiplePhrases(
370+
options: Readonly<{
371+
phrases: TokiPona.MultiplePhrases;
372+
includeGerund: boolean;
373+
andParticle: null | string;
374+
}>,
375+
): IterableResult<PhraseTranslation> {
376+
const { phrases, andParticle } = options;
377+
switch (phrases.type) {
378+
case "simple":
379+
return phrase({ ...options, phrase: phrases.phrase });
380+
case "and":
381+
case "anu": {
382+
const conjunction = CONJUNCTION[phrases.type];
383+
return IterableResult.combine(
384+
...phrases.phrases
385+
.map((phrases) => multiplePhrases({ ...options, phrases })),
386+
)
387+
.filterMap((phrase): null | PhraseTranslation => {
388+
if (
389+
phrase.some((phrase) =>
390+
phrase.type === "adjective" && phrase.inWayPhrase != null
391+
)
392+
) {
393+
throw new FilteredError(
394+
"in [adjective] way phrase within compound",
395+
);
396+
}
397+
if (phrase.every((phrase) => phrase.type === "noun")) {
398+
return {
399+
type: "noun",
400+
// TODO: flatten compound nouns on the grammar fixer
401+
noun: {
402+
type: "compound",
403+
conjunction,
404+
nouns: phrase.map(({ noun }) => noun),
405+
},
406+
};
407+
} else if (phrase.every((phrase) => phrase.type === "adjective")) {
408+
if (andParticle === "en" && conjunction === "and") {
409+
return null;
410+
} else {
411+
return {
412+
type: "adjective",
413+
// TODO: flatten compound adjective on the grammar fixer
414+
adjective: {
415+
type: "compound",
416+
conjunction,
417+
adjectives: phrase.map(({ adjective }) => adjective),
418+
emphasis: false,
419+
},
420+
inWayPhrase: null,
421+
};
422+
}
423+
} else {
424+
return {
425+
type: "verb",
426+
verb: {
427+
type: "compound",
428+
conjunction,
429+
verbs: phrase.map(phraseAsVerb),
430+
object: null,
431+
objectComplement: null,
432+
prepositions: [],
433+
},
434+
};
435+
}
436+
})
437+
.addErrorWhenNone(() =>
438+
new ExhaustedError(Composer.multiplePhrases(phrases, andParticle))
439+
);
440+
}
441+
}
442+
}

0 commit comments

Comments
 (0)