Skip to content

Commit 7836689

Browse files
committed
move freeform node yet again
1 parent 14594ca commit 7836689

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

src/translator/ast.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ export type Preposition = Readonly<{
9090
object: NounPhrase;
9191
emphasis: boolean;
9292
}>;
93-
export type Sentence =
93+
export type Sentence = Readonly<{
94+
clauses: ReadonlyArray<Clause>;
95+
punctuation: string;
96+
}>;
97+
export type Sentences =
9498
| Readonly<{ type: "free form"; text: string }>
95-
| Readonly<{
96-
type: "sentence";
97-
clauses: ReadonlyArray<Clause>;
98-
punctuation: string;
99-
}>;
99+
| Readonly<{ type: "sentences"; sentences: ReadonlyArray<Sentence> }>;

src/translator/composer.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,22 @@ function clause(ast: English.Clause): string {
132132
}
133133
}
134134
function sentence(sentence: English.Sentence): string {
135-
let text: string;
136-
switch (sentence.type) {
135+
const capitalized = capitalize(sentence.clauses.map(clause).join(", "));
136+
return `${capitalized}${sentence.punctuation}`;
137+
}
138+
export function multipleSentences(
139+
sentences: English.Sentences,
140+
): string {
141+
switch (sentences.type) {
137142
case "free form":
138-
text = sentence.text;
139-
break;
140-
case "sentence":
141-
text = `${
142-
sentence.clauses.map(clause).join(", ")
143-
}${sentence.punctuation}`;
144-
break;
143+
return capitalize(sentences.text);
144+
case "sentences":
145+
return sentences.sentences.map(sentence).join(" ");
145146
}
147+
}
148+
function capitalize(text: string): string {
146149
return text.replace(
147150
/(?<![<&\p{Alpha}\p{Nd}\p{Nl}\p{No}])[\p{Alpha}\p{Nd}\p{Nl}\p{No}]/u,
148151
(character) => character.toLocaleUpperCase(),
149152
);
150153
}
151-
export function multipleSentences(
152-
sentences: ReadonlyArray<English.Sentence>,
153-
): string {
154-
return sentences.map(sentence).join(" ");
155-
}

src/translator/sentence.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,23 @@ function sentence(
177177
}
178178
export function multipleSentences(
179179
sentences: TokiPona.MultipleSentences,
180-
): ArrayResult<ReadonlyArray<English.Sentence>> {
180+
): ArrayResult<English.Sentences> {
181181
switch (sentences.type) {
182182
case "single word": {
183183
const { word } = sentences;
184184
return new ArrayResult(dictionary.get(word)!.definitions)
185185
.flatMap(definitionAsPlainString)
186-
.map<English.Sentence>((definition) => ({
186+
.map((definition) => ({
187187
type: "free form",
188188
text: definition,
189-
}))
190-
.map((definition) => [definition]);
189+
}));
191190
}
192191
case "sentences":
193192
return ArrayResult.combine(
194193
...sentences.sentences.map((value, i) =>
195194
sentence(value, i === sentences.sentences.length - 1)
196195
),
197-
);
196+
)
197+
.map((sentences) => ({ type: "sentences", sentences }));
198198
}
199199
}

0 commit comments

Comments
 (0)