Skip to content

Commit 9d61a3e

Browse files
committed
chore: Refactor cat.ts
1 parent f44fe5f commit 9d61a3e

File tree

1 file changed

+60
-78
lines changed

1 file changed

+60
-78
lines changed

src/lib/cat.ts

Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,81 @@
11
import type { Action } from "../components/types";
22
import { FileEditor } from "./fileEditor";
33

4-
type CatVocabulary = {
5-
[key: string]: string[];
6-
};
7-
8-
type EmotionKeywords = {
9-
[key: string]: string[];
10-
};
11-
12-
type CatResponse = {
4+
export type CatResponse = {
135
text: string;
146
action?: Action;
157
};
168

9+
const vocabulary = {
10+
greeting: ["ニャッ", "ミャッ", "ニャ"],
11+
affection: ["ニャオ~ン", "ミャオ~", "ニャンニャン"],
12+
satisfaction: ["ゴロゴロ", "ニャ~"],
13+
excitement: ["ニャニャニャ!", "ミャー!", "ニャッニャッ"],
14+
playful: ["ニャーン", "ミャミャ", "ニャオッ"],
15+
sleepy: ["ニャ…", "フニャ~", "ニャ~ン"],
16+
hungry: ["ニャオォン", "ミャーオ", "ニャンニャン!"],
17+
} as const;
18+
19+
type Emotion = keyof typeof vocabulary;
20+
21+
const emotionKeywords: Record<Emotion, string[]> = {
22+
greeting: [
23+
"こんにちは",
24+
"おはよう",
25+
"こんばんは",
26+
"はじめまして",
27+
"やあ",
28+
"どうも",
29+
],
30+
affection: ["好き", "愛", "かわいい", "可愛い", "撫でて", "なでて", "甘えて"],
31+
satisfaction: [
32+
"ありがとう",
33+
"嬉しい",
34+
"うれしい",
35+
"よかった",
36+
"素晴らしい",
37+
"最高",
38+
],
39+
excitement: [
40+
"遊ぼう",
41+
"あそぼう",
42+
"楽しい",
43+
"たのしい",
44+
"わーい",
45+
"やったー",
46+
"すごい",
47+
],
48+
playful: ["ゲーム", "おもちゃ", "ボール", "遊び", "じゃれる"],
49+
sleepy: ["眠い", "ねむい", "疲れた", "つかれた", "おやすみ", "寝る"],
50+
hungry: [
51+
"お腹",
52+
"おなか",
53+
"食べ",
54+
"ごはん",
55+
"餌",
56+
"えさ",
57+
"フード",
58+
"美味しい",
59+
],
60+
} as const;
61+
1762
export class Cat {
1863
private fileEditor = new FileEditor();
1964

20-
private vocabulary: CatVocabulary = {
21-
greeting: ["ニャッ", "ミャッ", "ニャ"],
22-
affection: ["ニャオ~ン", "ミャオ~", "ニャンニャン"],
23-
satisfaction: ["ゴロゴロ", "ニャ~"],
24-
excitement: ["ニャニャニャ!", "ミャー!", "ニャッニャッ"],
25-
playful: ["ニャーン", "ミャミャ", "ニャオッ"],
26-
sleepy: ["ニャ…", "フニャ~", "ニャ~ン"],
27-
hungry: ["ニャオォン", "ミャーオ", "ニャンニャン!"],
28-
default: ["ニャー", "ニャン", "ミャー"],
29-
};
30-
31-
private emotionKeywords: EmotionKeywords = {
32-
greeting: [
33-
"こんにちは",
34-
"おはよう",
35-
"こんばんは",
36-
"はじめまして",
37-
"やあ",
38-
"どうも",
39-
],
40-
affection: [
41-
"好き",
42-
"愛",
43-
"かわいい",
44-
"可愛い",
45-
"撫でて",
46-
"なでて",
47-
"甘えて",
48-
],
49-
satisfaction: [
50-
"ありがとう",
51-
"嬉しい",
52-
"うれしい",
53-
"よかった",
54-
"素晴らしい",
55-
"最高",
56-
],
57-
excitement: [
58-
"遊ぼう",
59-
"あそぼう",
60-
"楽しい",
61-
"たのしい",
62-
"わーい",
63-
"やったー",
64-
"すごい",
65-
],
66-
playful: ["ゲーム", "おもちゃ", "ボール", "遊び", "じゃれる"],
67-
sleepy: ["眠い", "ねむい", "疲れた", "つかれた", "おやすみ", "寝る"],
68-
hungry: [
69-
"お腹",
70-
"おなか",
71-
"食べ",
72-
"ごはん",
73-
"餌",
74-
"えさ",
75-
"フード",
76-
"美味しい",
77-
],
78-
};
79-
80-
private detectEmotion(message: string): string {
65+
private detectEmotion(message: string): Emotion | null {
8166
const lowerMessage = message.toLowerCase();
8267

83-
for (const [emotion, keywords] of Object.entries(this.emotionKeywords)) {
68+
for (const [emotion, keywords] of Object.entries(emotionKeywords)) {
8469
if (keywords.some((keyword) => lowerMessage.includes(keyword))) {
85-
return emotion;
70+
return emotion as Emotion;
8671
}
8772
}
8873

89-
return "default";
74+
return null;
9075
}
9176

92-
private getRandomResponse(emotion: string): string {
93-
const responses = this.vocabulary[emotion] || this.vocabulary.default;
94-
if (!responses || responses.length === 0) {
95-
return "ニャー";
96-
}
77+
private getRandomResponse(emotion: Emotion | null): string {
78+
const responses = emotion ? vocabulary[emotion] : ["ニャー", "ニャン", "ミャー"];
9779
const index = Math.floor(Math.random() * responses.length);
9880
return responses[index] as string;
9981
}

0 commit comments

Comments
 (0)