aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/sentence.ts2
-rw-r--r--api/word.ts20
-rw-r--r--examples/furigana-html.ts8
3 files changed, 26 insertions, 4 deletions
diff --git a/api/sentence.ts b/api/sentence.ts
index 6d9fc6d..d0cc434 100644
--- a/api/sentence.ts
+++ b/api/sentence.ts
@@ -50,7 +50,7 @@ export default class Sentence extends APIBase {
furigana(format: JapaneseFormatter = "HTML"): string {
return this.words.reduce((out, word) => {
- return out + word.text.furigana(format);
+ return out + word.furigana(format);
}, "");
}
}
diff --git a/api/word.ts b/api/word.ts
index 13c7197..4d77d97 100644
--- a/api/word.ts
+++ b/api/word.ts
@@ -1,24 +1,31 @@
import Glossary from "./glossary.ts";
import APIBase from "./base.ts";
import { ParseToken } from "../language/types.ts";
-import Japanese from "./japanese.ts";
+import Japanese, { JapaneseFormatter } from "./japanese.ts";
import "../util/string.ts";
import { Tag } from "../language/tags.ts";
export default class Word extends APIBase {
/** @prop dictionary form of verb if this word is a verb */
- public base: Japanese;
+ protected base: Japanese;
/** @prop word as written in parent sentence */
- public text: Japanese;
+ protected text: Japanese;
+ /** @prop this.furigana should output kanji with reading */
+ protected outputKanji: boolean = true;
+ /** @prop this word represents an unrecognized sentence part between recognized terms */
+ protected filler: boolean;
constructor(input: string | ParseToken) {
super();
if (typeof input === "string") {
+ this.filler = true;
input = input as string;
this.base = new Japanese(input, input);
this.text = this.base;
+ this.outputKanji = false;
} else {
+ this.filler = false;
input = input as ParseToken;
this.base = new Japanese(input.writing, input.reading);
if (input.tags.anyOf(Object.values(Tag.Class.Verb))) {
@@ -32,10 +39,17 @@ export default class Word extends APIBase {
} else {
this.text = this.base;
}
+ this.outputKanji = !input.source.kanaOnly(); // only output kanji if input also uses kanji
}
}
+ furigana(format: JapaneseFormatter) {
+ if (!this.outputKanji) return this.text.reading;
+ else return this.text.furigana(format);
+ }
+
async glossary() {
+ // TODO: output nothing if this.filler == true
return new Glossary().withParent(await this.api);
}
}
diff --git a/examples/furigana-html.ts b/examples/furigana-html.ts
index 6b6d2cd..6a0e801 100644
--- a/examples/furigana-html.ts
+++ b/examples/furigana-html.ts
@@ -20,3 +20,11 @@ console.log(furigana);
// this sentence works :tada:
// console.log((await api.sentence("浮上したハイラル城の下にてゼルダ様達の捜索を行うこととなった")).furigana("HTML"));
+
+var test = "日本に来て一番驚いたことは自動販売機の多さだ。";
+console.log(test);
+console.log((await api.sentence(test)).furigana("parenthesis"));
+
+test = "にほんに来て一番驚いたことは自動販売機の多さだ。";
+console.log(test);
+console.log((await api.sentence(test)).furigana("parenthesis"));