-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
stringgenerator.ts
170 lines (142 loc) · 3.96 KB
/
stringgenerator.ts
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
import RNG from "./rng.js";
interface Options {
/** Use word mode? */
words: boolean;
/** Order, default = 3 */
order: number;
/** Prior value, default = 0.001 */
prior: number;
}
type Events = { [key:string]: number };
/**
* @class (Markov process)-based string generator.
* Copied from a <a href="http://roguebasin.com/index.php/Names_from_a_high_order_Markov_Process_and_a_simplified_Katz_back-off_scheme">RogueBasin article</a>.
* Offers configurable order and prior.
*/
export default class StringGenerator {
_options: Options;
_boundary: string;
_suffix: string;
_prefix: string[];
_priorValues: { [key:string]: number };
_data: { [key:string]: Events };
constructor(options: Partial<Options>) {
this._options = {
words: false,
order: 3,
prior: 0.001
};
Object.assign(this._options, options);
this._boundary = String.fromCharCode(0);
this._suffix = this._boundary;
this._prefix = [];
for (let i=0;i<this._options.order;i++) { this._prefix.push(this._boundary); }
this._priorValues = {};
this._priorValues[this._boundary] = this._options.prior;
this._data = {};
}
/**
* Remove all learning data
*/
clear() {
this._data = {};
this._priorValues = {};
}
/**
* @returns {string} Generated string
*/
generate() {
let result = [this._sample(this._prefix)];
while (result[result.length-1] != this._boundary) {
result.push(this._sample(result));
}
return this._join(result.slice(0, -1));
}
/**
* Observe (learn) a string from a training set
*/
observe(string: string) {
let tokens = this._split(string);
for (let i=0; i<tokens.length; i++) {
this._priorValues[tokens[i]] = this._options.prior;
}
tokens = this._prefix.concat(tokens).concat(this._suffix); /* add boundary symbols */
for (let i=this._options.order; i<tokens.length; i++) {
let context = tokens.slice(i-this._options.order, i);
let event = tokens[i];
for (let j=0; j<context.length; j++) {
let subcontext = context.slice(j);
this._observeEvent(subcontext, event);
}
}
}
getStats() {
let parts = [];
let priorCount = Object.keys(this._priorValues).length;
priorCount--; // boundary
parts.push("distinct samples: " + priorCount);
let dataCount = Object.keys(this._data).length;
let eventCount = 0;
for (let p in this._data) {
eventCount += Object.keys(this._data[p]).length;
}
parts.push("dictionary size (contexts): " + dataCount);
parts.push("dictionary size (events): " + eventCount);
return parts.join(", ");
}
/**
* @param {string}
* @returns {string[]}
*/
_split(str: string) {
return str.split(this._options.words ? /\s+/ : "");
}
/**
* @param {string[]}
* @returns {string}
*/
_join(arr: string[]) {
return arr.join(this._options.words ? " " : "");
}
/**
* @param {string[]} context
* @param {string} event
*/
_observeEvent(context: string[], event: string) {
let key = this._join(context);
if (!(key in this._data)) { this._data[key] = {}; }
let data = this._data[key];
if (!(event in data)) { data[event] = 0; }
data[event]++;
}
/**
* @param {string[]}
* @returns {string}
*/
_sample(context: string[]) {
context = this._backoff(context);
let key = this._join(context);
let data = this._data[key];
let available : Events = {};
if (this._options.prior) {
for (let event in this._priorValues) { available[event] = this._priorValues[event]; }
for (let event in data) { available[event] += data[event]; }
} else {
available = data;
}
return RNG.getWeightedValue(available) as string;
}
/**
* @param {string[]}
* @returns {string[]}
*/
_backoff(context: string[]) {
if (context.length > this._options.order) {
context = context.slice(-this._options.order);
} else if (context.length < this._options.order) {
context = this._prefix.slice(0, this._options.order - context.length).concat(context);
}
while (!(this._join(context) in this._data) && context.length > 0) { context = context.slice(1); }
return context;
}
}