-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameGen.cs
180 lines (171 loc) · 4 KB
/
NameGen.cs
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
180
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kawa.Json;
namespace SBCharCreator
{
static class NameGen
{
private static Random rand;
private static System.Globalization.TextInfo ti = System.Globalization.CultureInfo.InvariantCulture.TextInfo;
private static string TitleCase(string text, JsonObj part)
{
if (part.ContainsKey("titleCase") && (bool)part["titleCase"])
{
return ti.ToTitleCase(text.ToLowerInvariant());
}
return text;
}
private static string Markov(JsonObj settings)
{
var order = (int)((double)settings["prefixSize"]);
var minLength = (int)((double)settings["endSize"]);
var samples = ((List<object>)settings["sourceNames"]).Cast<string>().ToList();
var chains = new Dictionary<string, List<char>>();
var getLetter = new Func<string, char>(token =>
{
if (!chains.ContainsKey(token))
{
return '?';
}
List<char> letters = chains[token];
int n = rand.Next(letters.Count);
return letters[n];
});
foreach (string word in samples)
{
for (int letter = 0; letter < word.Length - order; letter++)
{
var token = word.Substring(letter, order);
List<char> entry = null;
if (chains.ContainsKey(token))
{
entry = chains[token];
}
else
{
entry = new List<char>();
chains[token] = entry;
}
entry.Add(word[letter + order]);
}
}
var ret = string.Empty;
do
{
var n = rand.Next(samples.Count);
int nameLength = samples[n].Length;
ret = (samples[n].Substring(rand.Next(0, samples[n].Length - order), order));
while (ret.Length < nameLength)
{
string token = ret.Substring(ret.Length - order, order);
char c = getLetter(token);
if (c != '?')
{
ret += getLetter(token);}
else{
break;}
}
if (ret.Contains(" "))
{
string[] tokens = ret.Split(' ');
ret = "";
for (int t = 0; t < tokens.Length; t++)
{
if (tokens[t] == "")
{
continue;
}
if (tokens[t].Length == 1)
{
tokens[t] = tokens[t].ToUpperInvariant();
}
else
{
tokens[t] = tokens[t].Substring(0, 1) + tokens[t].Substring(1).ToLowerInvariant();
}
if (ret != "")
{
ret += " ";
}
ret += tokens[t];
}
}
else
{
ret = ret.Substring(0, 1) + ret.Substring(1).ToLowerInvariant();
}
}
while (ret.Length < minLength);
return ret;
}
private static string Parse(List<object> part)
{
if (part[0] is JsonObj)
{
var mode = (string)((JsonObj)part[0])["mode"];
if (mode == "alts")
{
var alt = part[rand.Next(1, part.Count)];
if (alt is string)
{
return TitleCase((string)alt, (JsonObj)part[0]);
}
else
{
return TitleCase(Parse((List<object>)alt), (JsonObj)part[0]);
}
}
else if (mode == "serie")
{
var sb = new System.Text.StringBuilder();
for (var i = 1; i < part.Count; i++)
{
if (part[i] is string)
{
sb.Append(part[i]);
}
else
{
sb.Append(Parse((List<object>)part[i]));
}
}
return TitleCase(sb.ToString(), (JsonObj)part[0]);
}
else if (mode == "markov")
{
var name = Markov((JsonObj)Assets.GetJson("/names/" + (string)((JsonObj)part[0])["source"] + ".namesource"));
return TitleCase(name, (JsonObj)part[0]);
}
}
else if (part is List<object> || part[0] is string)
{
//Assume alts
var alt = part[rand.Next(0, part.Count)];
if (alt is string)
{
return (string)alt;
}
else
{
return Parse((List<object>)alt);
}
}
throw new InvalidCastException("Expected a mode object.");
}
public static string Generate(JsonObj source, string key)
{
if (rand == null)
{
rand = new Random();
}
var part = source[key];
if (part is List<object>)
{
return Parse((List<object>)part);
}
throw new InvalidCastException("Expected a list.");
}
}
}