-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.js
179 lines (165 loc) · 4.02 KB
/
generator.js
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
175
176
177
178
179
/* This names generator was made by Alexey Izvalov (Airapport) for own strategy game called Conquicktory.
It utilizes Markov chains approach to generate names for anything.
It defines the frequencies of symbols and their combinations from the learning sample (function init), and then
generates the names which will sound naturally alike to those from the learning sample (function generate).
Feel free to use it in your projects of any type and mention Airapport in the Credits.
*/
class MarkovGenerator{
constructor(){
this.probsOb = {}
this.markovOb = {}
this.seedsLengths = [];
this.seedsAr = [];
this.minWordLength = 0;
this.maxWordLength = 30;
this.settings={mustCreateOnlyNewNames:true,mustKeepSmapleLength:true}
}
//call this function let the generator find out the rules from the learning sample (which is array of strings)
init(ar){
this.seedsLengths = [];
this.seedsAr = ar.slice();
this.probsOb = {};
for (var i = 0; i < this.seedsAr.length; i++ ){
var str = this.seedsAr[i];
var prevPrevCh ="|";
var prevCh ="|";
var len = str.length;
for (var j = 0; j <= len; j++ ){
if(j<len){
var ch = str.charAt(j);
}else{
ch ="|";
}
if (prevPrevCh in this.probsOb){
var ob = this.probsOb[prevPrevCh];
}else{
ob = {};
this.probsOb[prevPrevCh] = ob;
}
if (prevCh in ob){
var ob2 = ob[prevCh];
}else{
ob2 = {};
ob[prevCh] = ob2;
}
if (ch in ob2){
ob2[ch]++;
}else{
ob2[ch] = 1;
}
prevPrevCh = prevCh;
prevCh = ch;
}
while (len>=this.seedsLengths.length){
this.seedsLengths.push(0);
}
this.seedsLengths[len]++;
}
this.markovOb = {};
for (var ch1 in this.probsOb){
ob2 = this.probsOb[ch1];
for (var ch2 in ob2){
var ob3 = ob2[ch2];
var charsAr = [];
var weightAr = [];
for (var ch3 in ob3){
charsAr.push(ch3);
weightAr.push(ob3[ch3]);
}
this.markovOb[ch1 + ch2] = {charsAr:charsAr, weightAr:weightAr};
}
}
//defining the lengths of 95% smaple strings
var percVal = 0.025;
var percNum = Math.round(percVal * this.seedsAr.length);
var id = 0;
var sum = 0;
while(sum<percNum){
sum += this.seedsLengths[id];
id++;
}
this.minWordLength = id - 1;
id = this.seedsLengths.length-1;
sum = 0;
while(sum<percNum){
sum += this.seedsLengths[id];
id--;
}
this.maxWordLength = id + 1;
}
//call this function to generate a name
generate(){
var numAttempts = 0;
var mustMakeNextAttempt = true;
while (mustMakeNextAttempt){
mustMakeNextAttempt = false;
numAttempts++;
var res ="";
var prevPrevCh ="|";
var prevCh ="|";
var need1More = true;
while (need1More){
var ob = this.markovOb[prevPrevCh + prevCh];
var id = this.getRandomIndexFromWeightedAr(ob.weightAr);
var ch = ob.charsAr[id];
if (ch!="|"){
res += ch;
}else{
need1More = false;
}
prevPrevCh = prevCh;
prevCh = ch;
//if (res.length>30){
// break;
//}
}
if (numAttempts<10){
if (this.settings.mustCreateOnlyNewNames){
if (this.seedsAr.indexOf(res)!=-1){
mustMakeNextAttempt = true;
}
}
if (!mustMakeNextAttempt){
if (this.settings.mustKeepSmapleLength){
if ((res.length < this.minWordLength) || (res.length > this.maxWordLength)){
mustMakeNextAttempt = true;
}
}
}
}
}
return res;
}
//service function for weighted random generation
getRandomIndexFromWeightedAr(ar){
if (ar.length == 1){
if (ar[0]==0){
return -1
}else{
return 0;
}
}
var res = -1;
var s = 0;
for (var i = 0; i < ar.length; i++)
{
s += ar[i];
}
if (s > 0)
{
var rnd = s * Math.random();
var rid = 0;
while (rnd >= ar[rid])
{
rnd -= ar[rid];
rid++;
}
res = rid;
}
else
{
res = -1;
}
return res;
}
}