summary refs log tree commit diff stats
path: root/dialogue.cpp
blob: 47a761df4731f6baadffd1e5037505d33350b76a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "vendor/csv.h"
#include "identifier.h"
#include "histogram.h"
#include <rawr.h>
#include <cstdlib>
#include <ctime>
#include <map>
#include <string>
#include <iostream>
#include <sstream>



using speakerstore = identifier<std::string>;
using speaker_id = speakerstore::key_type;


struct speaker_data {

  std::string name;
  histogram<speaker_id> nextSpeaker;
  rawr chain;
  size_t count = 0;

};




int main(int, char**)
{
  srand(time(NULL));
  rand(); rand(); rand(); rand();

  speakerstore speakers;
  std::map<speaker_id, speaker_data> speakerData;
  histogram<speaker_id> allSpeakers;



  io::CSVReader<2,io::trim_chars<' ', '\t'>,io::double_quote_escape<',', '"'>> in("../dialogue.csv");
  std::string speaker;
  std::string line;

  bool hadPrev = false;
  speaker_id prevSpeaker;

  while (in.read_row(speaker, line))
  {
    speaker_id spId = speakers.add(speaker);
    speaker_data& myData = speakerData[spId];
    myData.name = speaker;
    myData.count++;

    allSpeakers.add(spId);

    if (hadPrev && prevSpeaker != spId)
    {
      speaker_data& psd = speakerData[prevSpeaker];
      psd.nextSpeaker.add(spId);
    }

    myData.chain.addCorpus(line);

    hadPrev = true;
    prevSpeaker = spId;
  }

  for (auto& sp : speakerData)
  {
    sp.second.chain.compile(4);
    sp.second.nextSpeaker.compile();
  }

  std::cout << "Speakers:" << std::endl;
  for (auto& sp : speakerData)
  {
    std::cout << "  " << sp.second.name << ": " << sp.second.count << std::endl;
  }
  std::cout << std::endl;

  allSpeakers.compile();

  for (;;)
  {
    std::set<speaker_id> pastSpeakers;


    speaker_id curSpeaker = allSpeakers.next();

    std::ostringstream theEnd;
    int maxLines = rand() % 4 + 3;

    for (int i = 0; i < maxLines; i++)
    {
      pastSpeakers.insert(curSpeaker);

      speaker_data& curSd = speakerData.at(curSpeaker);

      if (curSd.name != "")
      {
        theEnd << curSd.name << ": ";
      }

      std::string curLine = curSd.chain.randomSentence(rand() % 30 + 1);

      if (curSd.name == "" &&
          curLine[0] != '[' &&
          curLine[0] != '(' &&
          curLine[0] != '*')
      {
        theEnd << "[" << curLine << "]";
      } else {
        theEnd << curLine;
      }

      theEnd << std::endl;

      speaker_id repeatSpeaker = *std::next(std::begin(pastSpeakers), rand() % pastSpeakers.size());
      if (repeatSpeaker != curSpeaker &&
          rand() % 3 == 0)
      {
        curSpeaker = repeatSpeaker;
      } else {
        curSpeaker = curSd.nextSpeaker.next();
      }
    }

    std::string output = theEnd.str();
    output.resize(280);
    output = output.substr(0, output.find_last_of('\n'));
    std::cout << output;

    std::cout << std::endl;
    std::cout << std::endl;

    getc(stdin);
  }
}