LoginSignup
2
0

More than 1 year has passed since last update.

お疲れ様です。たなしょです。
今日はカノジョのフォームのデザイン部分や返答する部分を作成していきます。
今日でカノジョが完成する予定です。

パターンマッチングした場合の返答

Responder(返答部分)は以前のプロトタイプモデルで作成したのとほぼほぼ変わらないのですが、
パターンマッチングした場合のReponderがないので追加しました。

foreach (ParseItem parseItem in Cdictionary.Pattern)
{
    string mtc = parseItem.Match(input);
    if (String.IsNullOrEmpty(mtc) == false)
    {
        string resp = parseItem.Choice(mood);
        if (resp != null)
            return resp.Replace("%match%", mtc);
    }
}

パターンがマッチングした場合Choice()を実行する。Choice()がnullなら%match%が存在するかチェックしています。

感情モデルをクラス化する。

感情の起伏をクラス化してフォームに反映できるようにしていきます。

1.ランダムで返答を返す分岐でPatternResponderが追加しています。

public string Dialogue(string input)
{
    _emotion.Update(input);
    Random rnd = new();
    int num = rnd.Next();
    if (num < 6)
        _responder = _res_pattern;
    else if(num < 9)
        _responder = _res_random;
    else
        _responder = _res_repeat;
    return _responder.Response(
        input,
        _emotion.Mood);
}

2.ボタンを押して表情を変わるようにif文で調整してます。

int em = _chan.Emotion.Mood;
if ((-5 <= em) && (em <= 5))
{
    this.pictureBox1.Image = Properties.Resources.normal;
}
else if (-10 <= em & em < -5)
{
    this.pictureBox1.Image = Properties.Resources.what;
}
else if (-15 <= em & em < -10)
{
    this.pictureBox1.Image = Properties.Resources.angry;
}
else if (5 <= em & em <= 15)
{
    this.pictureBox1.Image = Properties.Resources.smile;
}
label2.Text = Convert.ToString(_chan.Emotion.Mood);

ちょっとここで

いざ実行してみてもエラーになってしまいました。
デバッグしてみるとCdictionary.csのpattern.txtを読み込んでパターン分割するところでエラーが起きていました。

string[] carveLine = rep_line.Split(new char[] { '\t' });
    _patternList.Add(
        new ParseItem(
        carveLine[0],
        carveLine[1])
    );

pattern.txtで空白だったのがよくなかったのかと思い下記のようにpattern.txtを変更しました。

こん(ちは|にちは)$\tこんにちは|やほー|ハーイ|どうもー|まああなた?
おはよう|おはよー|オハヨウ\tおはよ!|まだ眠い…|さっき寝たばかりなんだー
こんばん(は|わ)\tこんばんわ|わんばんこ|今何時?

実行してまたエラーが出てしまったのでパラメータをよくて見ると

こん(ちは|にちは)$\\tこんにちは|やほー|ハーイ|どうもー|まああなた?

上記のようにwindows特有の\tが\\tになる事象が発生していたので置換する処理を分割処理の前に付けました。

string rep_line = line.Replace("\\t", "\t");

カノジョができた

メリークリスマスと入力すると「メリークリスマス」と言ってくれるカノジョが僕にもできました🎉
image.png

最後に

感情の起伏があるカノジョができたので、
次はちょっとAIぽく言葉を学習するカノジョを作っていければなと思います。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0