aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-07-03 12:46:22 +0200
committerlonkaars <loek@pipeframe.xyz>2023-07-03 12:46:22 +0200
commit5c9bebac4078ee053c5048e4de2a3ed5afed4e24 (patch)
tree6afc0bf8e0d8db373bb080567c6db56b78b4440a
parentfa68eb21bf1d0460303cae3a0233e2d6cefd00ca (diff)
fix furigana for known working sentence
-rw-r--r--api/word.ts21
-rw-r--r--examples/furigana-html.ts3
-rw-r--r--test/api-word.ts14
-rw-r--r--util/string.ts17
4 files changed, 48 insertions, 7 deletions
diff --git a/api/word.ts b/api/word.ts
index 3421f9f..13c7197 100644
--- a/api/word.ts
+++ b/api/word.ts
@@ -4,6 +4,7 @@ import { ParseToken } from "../language/types.ts";
import Japanese 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 */
@@ -15,16 +16,22 @@ export default class Word extends APIBase {
super();
if (typeof input === "string") {
input = input as string;
- this.text = new Japanese(input, input);
- this.base = this.text;
+ this.base = new Japanese(input, input);
+ this.text = this.base;
} else {
input = input as ParseToken;
- var common = Math.max(input.writing.cmpLen(input.source),
- input.reading.cmpLen(input.source));
- var conjugation = input.source.substring(common);
this.base = new Japanese(input.writing, input.reading);
- this.text = new Japanese(input.writing.substring(0, common) + conjugation,
- input.reading.substring(0, common) + conjugation);
+ if (input.tags.anyOf(Object.values(Tag.Class.Verb))) {
+ var writingCommon = input.writing.cmpLen(input.source);
+ var readingCommon = input.reading.cmpLen(input.source);
+ var stemLength = Math.max(writingCommon, readingCommon);
+ var base = input[writingCommon > readingCommon ? "writing" : "reading"].substring(stemLength);
+ var conjugation = input.source.substring(stemLength);
+ this.text = new Japanese(input.writing.replaceLast(base, conjugation),
+ input.reading.replaceLast(base, conjugation));
+ } else {
+ this.text = this.base;
+ }
}
}
diff --git a/examples/furigana-html.ts b/examples/furigana-html.ts
index f0ff067..6b6d2cd 100644
--- a/examples/furigana-html.ts
+++ b/examples/furigana-html.ts
@@ -14,6 +14,9 @@ var sentence = await api.sentence("日本に来て一番驚いたことは自動
// Copy the sentence verbatim but add furigana to each word's kanji
var furigana = sentence.furigana("HTML");
+// TODO: sentence is not copied verbatim, words are replaced by their kanji if they matched by kana only
console.log(furigana);
+// this sentence works :tada:
+// console.log((await api.sentence("浮上したハイラル城の下にてゼルダ様達の捜索を行うこととなった")).furigana("HTML"));
diff --git a/test/api-word.ts b/test/api-word.ts
new file mode 100644
index 0000000..c41e1ef
--- /dev/null
+++ b/test/api-word.ts
@@ -0,0 +1,14 @@
+import Word from "../api/word.ts";
+import { Tag } from "../language/tags.ts";
+import { ParseToken } from "../language/types.ts";
+
+var cases = [
+ { writing: "繰り返す", reading: "くりかえす", source: "繰り返した", start: 0, tags: [Tag.Class.Verb.U], term_id: 0 },
+ { writing: "繰り返す", reading: "くりかえす", source: "くりかえした", start: 0, tags: [Tag.Class.Verb.U], term_id: 0 },
+] satisfies Array<ParseToken>;
+
+for (var testcase of cases) {
+ var test = new Word(testcase);
+ console.log(test);
+}
+
diff --git a/util/string.ts b/util/string.ts
index 934963e..bb6bc0f 100644
--- a/util/string.ts
+++ b/util/string.ts
@@ -43,6 +43,12 @@ declare global {
* @param position index to search from (0 by default = start of string)
*/
cmpLen(searchString: string, position?: number): number;
+
+ /** @summary remove `length` characters from end of string */
+ removeEnd(length: number): string;
+
+ /** @summary replace last instance of `searchString` with `replaceValue` */
+ replaceLast(searchString: string, replaceValue: string): string;
}
}
@@ -129,7 +135,18 @@ String.prototype.cmpLen = function(searchString, position = 0) {
for (let i = 0; i < searchString.length; i++) {
if (i + position >= this.length) break;
if (this[i + position] == searchString[i]) len++;
+ else break;
}
return len;
}
+String.prototype.removeEnd = function(length) {
+ return this.substring(0, this.length - length);
+}
+
+String.prototype.replaceLast = function(find, replace) {
+ var i = this.lastIndexOf(find);
+ if (i == -1) return this as string; // not found
+ return this.substring(0, i) + replace + this.substring(i + find.length);
+}
+