LoginSignup
0
1

More than 1 year has passed since last update.

お疲れ様です。たなしょです。
今日はテンプレートの返答をさせるためのテンプレート返答用のテキストと辞書クラスにテンプレート用の処理を追加していきます。

テンプレートテキストを作成する

%noun%を置換することでテンプレートの言葉として登録します。
この%noun%の数が後ほど学習部分で役に立ちます。
下記がテキストの中身ね。左が%noun%の出現回数です。

1\t%noun%なのね
1\t%noun%がいいんだ
1\t%noun%はやだ!
1\t%noun%が問題だね
1\t%noun%が!

テンプレート辞書を作成する

テンプレート辞書を作成します。
1.テンプレート辞書をオープンします

string[] t_lines = File.ReadAllLines(
            @"dics\template.txt",
            System.Text.Encoding.UTF8
            );

2.一行ずつを加工します(改行をとりのぞく)

List<string> new_lines = new();

foreach (string line in t_lines)
{
    string str = line.Replace("\n", "");

    if (line != "")
    {
        new_lines.Add(str);
    }
}

3.\tでキーと文字列を分けて%noun%の出現回数のキーが存在する確認します。

foreach (string line in new_lines)
{
    // add code
    // line replace \\t -> \t
    string rep_line = line.Replace("\\t", "\t");
    string[] carveLine = rep_line.Split(new Char[] { '\t' });

    if (!_templateDictionary.ContainsKey(carveLine[0]))
    {
        _templateDictionary.Add(carveLine[0], new List<string> { });
    }

    _templateDictionary[carveLine[0]].Add(carveLine[1]);
}

4.学習メソッドにテンプレート学習メソッド追加します。

StudyTemplate(parts);

5.ユーザーの発言をテンプレート学習して、テンプレート辞書に登録します。
5-1.形態素と品詞情報に分けて名詞(一般|固有名詞|サ変接続|形容動詞語幹)にマッチングするか調べます。

foreach (string[] morpheme in parts)
{
    if (Analyzer.KeyWordCheck(morpheme[1]).Success)

5-2.形態素を%noun%に書き換えてcount+1します。

morpheme[0] = "%noun%";
count++;

5-3.%noun%が存在して、%noun%の出現回数がテンプレート辞書はキー値である出現回数を追加します。

if (count > 0)
{
    string num = Convert.ToString(count);

    if (!_templateDictionary.ContainsKey(num))
    {
        _templateDictionary.Add(num, new List<string>());
    }

5-4.テンプレート辞書の中にテンプレート文字列が存在しない場合、リストに追加します。

if (!_templateDictionary[num].Contains(tempStr))
{ 
    _templateDictionary[num].Add(tempStr);
}

6.テンプレートの連想配列を1行ずつキー値と文字列を取り出して\tでつなげてリストに格納してソートで%noun%の出現回数順にソートをかけてファイルに書き込みます。

List<string> templateLine = new();

foreach (var dic in _templateDictionary)
{
    foreach (var temp in dic.Value)
    {
        templateLine.Add(dic.Key + "\\t" + temp);
    }

    templateLine.Sort();

    File.WriteAllLines(
        @"dics\template.txt",
        templateLine,
        System.Text.Encoding.UTF8
        );
}

最後に

今回はテンプレートの会話をするための土台となるテンプレート辞書とその仕組みを作成しました。
明日はレスポンスを返す処理と一連の処理をフォームに反映させたいと思います。

0
1
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
0
1