-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNameGenerator.cs
More file actions
57 lines (47 loc) · 1.73 KB
/
NameGenerator.cs
File metadata and controls
57 lines (47 loc) · 1.73 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicMau.ProceduralNameGenerator
{
public class NameGenerator : Generator
{
public NameGenerator(IEnumerable<string> trainingData, int order, double smoothing) : base(trainingData, order, smoothing)
{
}
public string GenerateName(int minLength, int maxLength, int maxDistance, string similarTo, Random rnd)
{
string name = Generate(rnd).Replace("#", "");
if (name.Length < minLength || name.Length > maxLength)
{
Console.WriteLine("Rejected because of length: " + name);
return null;
}
if (similarTo != null && LevenshteinDistance.Compute(similarTo, name) > maxDistance)
{
Console.WriteLine("Rejected because not similar: " + name);
return null;
}
return name;
}
public Task<List<string>> GenerateNames(int count, int length, Random rnd)
{
return GenerateNames(count, length, length, 0, null, rnd);
}
public async Task<List<string>> GenerateNames(int count, int minLength, int maxLength, int maxDistance, string similarTo, Random rnd)
{
return await Task.Run(() =>
{
var names = new List<string>();
while (names.Count < count)
{
string name = GenerateName(minLength, maxLength, maxDistance, similarTo, rnd);
if (name != null)
names.Add(name);
}
return names;
});
}
}
}