|
1 | 1 | import type { Action } from "../components/types"; |
2 | 2 | import { FileEditor } from "./fileEditor"; |
3 | 3 |
|
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 = { |
13 | 5 | text: string; |
14 | 6 | action?: Action; |
15 | 7 | }; |
16 | 8 |
|
| 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 | + |
17 | 62 | export class Cat { |
18 | 63 | private fileEditor = new FileEditor(); |
19 | 64 |
|
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 { |
81 | 66 | const lowerMessage = message.toLowerCase(); |
82 | 67 |
|
83 | | - for (const [emotion, keywords] of Object.entries(this.emotionKeywords)) { |
| 68 | + for (const [emotion, keywords] of Object.entries(emotionKeywords)) { |
84 | 69 | if (keywords.some((keyword) => lowerMessage.includes(keyword))) { |
85 | | - return emotion; |
| 70 | + return emotion as Emotion; |
86 | 71 | } |
87 | 72 | } |
88 | 73 |
|
89 | | - return "default"; |
| 74 | + return null; |
90 | 75 | } |
91 | 76 |
|
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] : ["ニャー", "ニャン", "ミャー"]; |
97 | 79 | const index = Math.floor(Math.random() * responses.length); |
98 | 80 | return responses[index] as string; |
99 | 81 | } |
|
0 commit comments