Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(memory): recall Japanese words with a long vowel mark

Jialin Chen799b5cdba0f0
diff --git a/server/cjk-search.test.ts b/server/cjk-search.test.ts
index 370e8c92a7..860d379968 100644
--- a/server/cjk-search.test.ts
+++ b/server/cjk-search.test.ts
@@ -35,6 +35,19 @@ describe("CJK search text", () => {
expect(cjkTokenParts("猫的粮")).toEqual({ other: [], bigrams: ["猫", "粮"] });
expect(cjkTokenParts("を")).toEqual({ other: [], bigrams: [] });
});
+
+ it("keeps the long vowel and half-width voicing marks inside a kana word", () => {
+ expect(cjkTokenParts("コーヒー")).toEqual({ other: [], bigrams: ["コー", "ーヒ", "ヒー"] });
+ expect(cjkTokenParts("メール")).toEqual({ other: [], bigrams: ["メー", "ール"] });
+ expect(cjkTokenParts("バス")).toEqual({ other: [], bigrams: ["バ", "゙ス"] });
+ expect(cjkTokenParts("パン")).toEqual({ other: [], bigrams: ["パ", "゚ン"] });
+ });
+
+ it("leaves out a stretched vowel, so a drawn-out interjection recalls nothing", () => {
+ expect(cjkTokenParts("すごーーい")).toEqual({ other: [], bigrams: ["すご"] });
+ expect(cjkTokenParts("スゴーーイ")).toEqual({ other: [], bigrams: ["スコ", "ゴ"] });
+ expect(recallQuery("うーーーん")).toBeNull();
+ });
});
describe("the search index", () => {
@@ -103,7 +116,7 @@ const NOTES: Array<[note: string, question: string]> = [
["年假还剩六天,打算春节前后一起休。", "我还有几天年假"],
];
-describe("Chinese recall", () => {
+describe("Chinese and Japanese recall", () => {
const BOT = "bot-cjk-recall";
beforeEach(() => {
closeMessageDb();
@@ -125,4 +138,10 @@ describe("Chinese recall", () => {
writeMemoryTopic(BOT, "cat.md", "家里的猫每天早上吃一次。\n");
expect(memoryPassages(BOT, "猫").map((passage) => passage.label)).toContain("memory/cat.md");
});
+
+ it("recalls a Japanese note by a katakana word with a long vowel", () => {
+ writeMemoryTopic(BOT, "coffee.md", "好きな飲み物はコーヒー、ブラックで。\n");
+ expect(recallQuery("コーヒーは?")).not.toBeNull();
+ expect(memoryPassages(BOT, "コーヒーは?").map((passage) => passage.label)).toContain("memory/coffee.md");
+ });
});
diff --git a/server/cjk-search.ts b/server/cjk-search.ts
index c73d0fb423..169cc837d6 100644
--- a/server/cjk-search.ts
+++ b/server/cjk-search.ts
@@ -3,7 +3,8 @@ import type { DatabaseSync } from "node:sqlite";
// FTS5's unicode61 tokenizer has no CJK dictionary and indexes a whole Han or kana run as one token, so a word in the
// middle of a sentence can never match. Each CJK character becomes its own token instead (split by U+200B, which
// unicode61 treats as a separator and a reader never sees), and a query asks for its characters as phrases.
-const CJK = "\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}";
+// Unicode puts the long vowel mark (ー, ー) and the half-width voicing marks (゙, ゚) in the Common script, not kana.
+const CJK = "\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}\\u30fc\\uff70\\uff9e\\uff9f";
const CJK_CHAR = new RegExp(`[${CJK}]`, "u");
const HAN = /\p{Script=Han}/u;
const CJK_RUNS = new RegExp(`[${CJK}]+|[^${CJK}]+`, "gu");
@@ -11,9 +12,10 @@ const CJK_BOUNDARY = new RegExp(`(?<=[${CJK}])(?=[\\p{L}\\p{N}])|(?<=[\\p{L}\\p{
/** U+200B ZERO WIDTH SPACE, written escaped so no editor or formatter can drop it unseen. */
export const SEARCH_MARK = "\u200b";
-/** Particles and filler words say nothing about what a note is about; they also end a run, so no bigram straddles one. */
+/** Particles, filler words and a stretched vowel (えーーー) say nothing about what a note is about; they also end a run,
+ * so no bigram straddles one. */
const FILLER = new RegExp(
- "我们|你们|他们|她们|它们|这个|那个|一下|一个|可以|时候|现在|知道|告诉|记得|还是|就是|不是|没有|已经|因为|所以|但是|如果|然后|还有|请问|帮我|怎样|这样|那样|哪里|哪个|应该|一些|[的了是吗呢吧啊呀哦嗯么]",
+ "[ーー]{2,}|我们|你们|他们|她们|它们|这个|那个|一下|一个|可以|时候|现在|知道|告诉|记得|还是|就是|不是|没有|已经|因为|所以|但是|如果|然后|还有|请问|帮我|怎样|这样|那样|哪里|哪个|应该|一些|[的了是吗呢吧啊呀哦嗯么]",
"gu",
);