-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessTalkHistory.ts
More file actions
174 lines (137 loc) · 4.98 KB
/
ProcessTalkHistory.ts
File metadata and controls
174 lines (137 loc) · 4.98 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
HEADER_PREFIX,
HEADER_SUFFIX,
STAMP_EMOTICON,
IMAGE,
VIDEO,
} from "./MetaCharacters";
import kuromoji from "kuromoji";
const TinySegmenter = require("tiny-segmenter");
type DataDict = {
date: Array<string>,
time: Array<string>,
name: Array<string>,
message: Array<string>
}
type WordChain = {
[key: string]: {
[key2: string]: {
[key3: string]: number
}
}
};
class ProcessTalkHistory {
opponentName: string = "";
protected dataDict?: DataDict;
private opponentMessageList: Array<string> = [];
private myMessageList: Array<string> = [];
private tinySegmenter = new TinySegmenter();
protected opponentMarkovChain?: WordChain;
protected myMarkovChain?: WordChain;
// constructor(filename: string, data_: string) { // DEBUG:
// const data = data_;
constructor(talkData: string) {
// FIXME:
// reading file...
const data:string = talkData;
const firstLine:string = talkData.split("\n")[0];
this.opponentName = firstLine.replace(HEADER_PREFIX, "").replace(HEADER_SUFFIX, "");
// FIXME:
this.makeDataDict(data);
this.getMessageList();
this.run();
}
// remove metaCharacters
removeNoneString(message: string) {
if ([HEADER_PREFIX, HEADER_SUFFIX, STAMP_EMOTICON, IMAGE, VIDEO].includes(message)) {
return "NONE";
}
return message;
}
makeDataDict(data: string) {
let dateList: Array<string> = [];
let timeList: Array<string> = [];
let nameList: Array<string> = [];
let messageList: Array<string> = [];
let date: string = "";
let time: string = "";
let name: string = "";
let message: string = "";
for (var datum of data.split("\n\n").slice(1)) { // slice[1:]: remove header
if (datum === "") continue;
const lineList = datum.split("\n");
if (lineList[0] != "")
date = lineList[0];
for (var line of lineList.slice(1)) { // slice[1:]: remove date
try {
// if line === [time, name, message]
if (line.split("\t").length === 3) {
time = line.split("\t")[0];
name = line.split("\t")[1];
message = line.split("\t")[2];
}
} catch {
continue;
}
dateList.push(date);
timeList.push(time);
nameList.push(name);
messageList.push(this.removeNoneString(message));
}
}
this.dataDict = {
date: dateList, time: timeList,
name: nameList, message: messageList,
}
}
getMessageList() {
if (this.dataDict) {
for (var i = 0; i < this.dataDict.name.length; i++) {
let name = this.dataDict.name[i];
let message = this.dataDict.message[i];
if (name === this.opponentName) {
this.opponentMessageList.push(message + "^"); // ^ as end of sentence
} else {
this.myMessageList.push(message + "^"); // ^ as end of sentence
}
}
}
}
addWordChain(dict: WordChain, tmpList: Array<string>) {
const w1: string = tmpList[0];
const w2: string = tmpList[1];
const w3: string = tmpList[2];
if (!Object.keys(dict).includes(w1))
dict[w1] = {};
if (!Object.keys(dict[w1]).includes(w2))
dict[w1][w2] = {};
if (!Object.keys(dict[w1][w2]).includes(w3))
dict[w1][w2][w3] = 0;
dict[w1][w2][w3] += 1;
return dict;
}
makeMarkovChain(messageList?: Array<string>) {
let dict: WordChain = {};
if (messageList) {
for (var message of messageList) {
if (message !== "NONE^"){ // if not image, video...
let tmpList = ["@"]; // @ as beginning of sentence
for (var word of this.tinySegmenter.segment(message)) {
tmpList.push(word);
if (tmpList.length > 3) // 3 words dict
tmpList = tmpList.slice(1);
else if (tmpList.length < 3)
continue;
dict = this.addWordChain(dict, tmpList)
}
}
}
return dict;
}
}
run() {
this.opponentMarkovChain = this.makeMarkovChain(this.opponentMessageList);
this.myMarkovChain = this.makeMarkovChain(this.myMessageList);
}
}
export default ProcessTalkHistory;