aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-07-05 18:23:56 +0200
committerlonkaars <loek@pipeframe.xyz>2023-07-05 18:23:56 +0200
commit4e32a1942ff2478b557af1b900b3f3282d7ce55b (patch)
treedda6cc88d4113d691cf8a16c49f3e13890b9d67d
parent5b0b8c82a8477cfe49a538f267805488daa7f5bd (diff)
fix examples/furigana-html.ts
-rw-r--r--api/word.ts6
-rw-r--r--language/parser.ts9
-rw-r--r--language/tags.ts8
-rw-r--r--util/japanese.ts10
4 files changed, 23 insertions, 10 deletions
diff --git a/api/word.ts b/api/word.ts
index 4d77d97..b7fc3e6 100644
--- a/api/word.ts
+++ b/api/word.ts
@@ -4,7 +4,7 @@ import { ParseToken } from "../language/types.ts";
import Japanese, { JapaneseFormatter } from "./japanese.ts";
import "../util/string.ts";
-import { Tag } from "../language/tags.ts";
+import { Tag, TagGroup } from "../language/tags.ts";
export default class Word extends APIBase {
/** @prop dictionary form of verb if this word is a verb */
@@ -28,7 +28,7 @@ export default class Word extends APIBase {
this.filler = false;
input = input as ParseToken;
this.base = new Japanese(input.writing, input.reading);
- if (input.tags.anyOf(Object.values(Tag.Class.Verb))) {
+ if (input.tags.anyOf(TagGroup.Conjugable as string[])) {
var writingCommon = input.writing.cmpLen(input.source);
var readingCommon = input.reading.cmpLen(input.source);
var stemLength = Math.max(writingCommon, readingCommon);
@@ -39,7 +39,7 @@ export default class Word extends APIBase {
} else {
this.text = this.base;
}
- this.outputKanji = !input.source.kanaOnly(); // only output kanji if input also uses kanji
+ this.outputKanji = input.source.hasKanji(); // only output kanji if input also uses kanji
}
}
diff --git a/language/parser.ts b/language/parser.ts
index 40bdd81..6398595 100644
--- a/language/parser.ts
+++ b/language/parser.ts
@@ -1,15 +1,10 @@
-import { Tag } from "./tags.ts";
+import { Tag, TagGroup } from "./tags.ts";
import { ParseResult, InputSentenceProps, ParseDepth } from "./types.ts";
import DB from "../db/db.ts";
import "../util/array.ts";
import "../util/set.ts";
import { DeepPartial } from "../util/types.ts";
-const CONJUGABLE_TAGS = [
- ...Object.values(Tag.Class.Verb),
- ...Object.values(Tag.Class.Adjective),
-];
-
// TODO: rename Parser to Search
/** @summary main Parser class */
export default class Parser {
@@ -71,7 +66,7 @@ export default class Parser {
// deconjugated words
if (result.depth > 0) {
// check if this word can be conjugated at all
- if (!result.tags.anyOf(CONJUGABLE_TAGS)) return false;
+ if (!result.tags.anyOf(TagGroup.Conjugable as string[])) return false;
// ignore other wrong deconjugations
if (result.tags.includes(Tag.Class.Verb.U) &&
diff --git a/language/tags.ts b/language/tags.ts
index 3065c77..135de68 100644
--- a/language/tags.ts
+++ b/language/tags.ts
@@ -109,6 +109,14 @@ export const Tag = {
},
} as const;
+export const TagGroup = {
+ /** @constant array that contains all tags of word classes that can be conjugated */
+ Conjugable: [
+ ...Object.values(Tag.Class.Verb),
+ ...Object.values(Tag.Class.Adjective),
+ ],
+} as const;
+
export type TokenTag = string; // no way around it
export type TokenTags = Array<TokenTag>;
diff --git a/util/japanese.ts b/util/japanese.ts
index d398b60..8381608 100644
--- a/util/japanese.ts
+++ b/util/japanese.ts
@@ -59,6 +59,9 @@ declare global {
*/
japaneseOnly(strict?: boolean): boolean
+ /** @summary check if the string contains kanji characters */
+ hasKanji(): boolean;
+
/** @summary convert any half-width katakana to full-width */
widenKatakana(): string;
@@ -270,3 +273,10 @@ String.prototype.normalizeKana = function() {
return this.widenKatakana().katakanaToHiragana();
}
+String.prototype.hasKanji = function() {
+ for (var c of this)
+ if ([UnicodeRange.JapaneseKanji].includes(c.range()))
+ return true;
+ return false;
+}
+