-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfern.go
119 lines (97 loc) · 2.85 KB
/
fern.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
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
package fern
import (
"strings"
"math/rand"
// "fmt"
)
type Successor struct {
successor string
weight float32
leftContext string
rightContext string
// TODO successorFn
// TODO conditionFn
}
type L struct {
axiom string
rules map[string][]Successor
}
func generate(axiom string) L {
return L {
axiom,
make(map[string][]Successor),
}
}
func setAxiom(currL L, newAxiom string) L {
return L {
newAxiom,
currL.rules,
}
}
func setRules(currL L, newRules map[string][]Successor) L {
currLNewRules := make(map[string][]Successor)
for from, currSuccessors := range currL.rules {
currLNewRules[from] = currSuccessors
}
for from, newSuccessors := range newRules {
currLNewRules[from] = newSuccessors
}
return L {
currL.axiom,
currLNewRules,
}
}
func iterate(currL L, numIterations int) string {
return rewrite(currL.axiom, currL.rules, numIterations)
}
func rewrite(str string, rules map[string][]Successor, numIterations int) string {
if numIterations == 0 {
return str
}
var newStr strings.Builder
for _, char := range str {
var charStr string = string(char)
var charIndex int = strings.Index(str, charStr)
// check if charStr has a valid successors
charStrSuccessors := rules[charStr]
if len(charStrSuccessors) > 0 {
var successorIndex = -1
// calculate total weight of all successors, find deterministic successor
var totalWeight float32 = 0.0
for i, charStrSuccessor := range charStrSuccessors {
totalWeight += charStrSuccessor.weight
if charStrSuccessor.weight == 1.0 {
successorIndex = i
}
}
// random successor
if successorIndex == -1 {
var randWeight float32 = rand.Float32()
var cumulativeWeight float32 = 0.0
for i, charStrSuccessor := range charStrSuccessors {
if randWeight < cumulativeWeight / totalWeight {
successorIndex = i
}
cumulativeWeight += charStrSuccessor.weight
}
}
validSuccessor := charStrSuccessors[successorIndex]
// ensure context is correct
if validSuccessor.leftContext != "" && (charIndex - len(validSuccessor.leftContext) < 0 || str[charIndex - len(validSuccessor.leftContext) : charIndex] != validSuccessor.leftContext) {
successorIndex = -1
}
if validSuccessor.rightContext != "" && (charIndex + len(validSuccessor.rightContext) >= len(str) || str[charIndex + 1 : charIndex + len(validSuccessor.rightContext) + 1] != validSuccessor.rightContext) {
successorIndex = -1
}
if successorIndex > -1 {
newStr.WriteString(charStrSuccessors[0].successor)
} else {
newStr.WriteString(charStr)
}
} else {
newStr.WriteString(charStr)
}
}
return rewrite(newStr.String(), rules, numIterations - 1)
}
// TODO final