forked from asmaloney/naming-language-gen
/
syllable.go
64 lines (55 loc) · 1.23 KB
/
syllable.go
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
package naming
import (
"github.com/dlclark/regexp2"
"math/rand"
)
func (lang Language) spell(syllable string) string {
if !lang.ApplyOrtho {
return syllable
}
var s []rune
for _, c := range syllable {
str := string(c)
if val, ok := lang.ConsOrtho[str]; ok {
s = append(s, []rune(val)...)
} else if val, ok := lang.VowelOrtho[str]; ok {
s = append(s, []rune(val)...)
} else if val, ok := defaultOrtho[str]; ok {
s = append(s, []rune(val)...)
} else {
s = append(s, c)
}
}
return string(s)
}
func (lang Language) makeSyllable(structure string) string {
for {
syllable := ""
structureLen := len(structure)
for i := 0; i < structureLen; i++ {
ptype := string(structure[i])
if (i < structureLen-1) && structure[i+1] == '?' {
i++
if rand.Float32() < 0.5 {
continue
}
}
syllable += RandomRuneFromString(lang.Phonemes[ptype])
}
// Check if the syllable satisfies our criteria.
var bad bool
for _, restriction := range lang.SyllableRestrictions {
exp := regexp2.MustCompile(restriction, 0)
matched, _ := exp.MatchString(syllable)
if matched {
bad = true
break
}
}
// Didn't work out, try again.
if bad {
continue
}
return lang.spell(syllable)
}
}