-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPTGG.sc
153 lines (128 loc) · 3.17 KB
/
PTGG.sc
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
// port of Donya Quick's PTGG (probabilistic temporal graph grammar) from Haskell
PTGG {
classvar <term_NT = \nt, <term_LET = \let, <term_VAR = \var;
var <>ruleDict; // Dictionary[Symbol -> (Prob -> Func)]
var <>symbolSet; // [Symbol]
var <>pExpand;
*new {
arg symbolSet, ruleDict, pExpand;
^super.new.init(symbolSet, ruleDict, pExpand);
}
init {
arg symbolSet, ruleDict, pExpand;
this.symbolSet = symbolSet;
this.ruleDict = this.normalizeRules(ruleDict);
this.pExpand = pExpand;
}
normalizeRules {
arg ruleDict;
var result = Dictionary[];
ruleDict.pairsDo {
|symbol, probFuncAssocs|
var sum = probFuncAssocs.collect(_.key).sum;
probFuncAssocs.do({
|assoc|
assoc.key = assoc.key / sum;
});
result = result.add(symbol -> probFuncAssocs);
};
^result;
}
update {
arg sentence;
var result = [];
if(sentence.isKindOf(Dictionary)) {
if(sentence.collect(_.isKindOf(Event)).reduce('&&').not) {
"PTGG.update: non-Event found in sentence input".error;
} {
"PTGG.update: non-Dictionary input".error
}
};
sentence.do {
|term, i|
var subsentence = term.type.switch
{term_NT} {this.applyRules(term)}
{term_LET} {pExpand.coin.if {
this.expand(term);
} {
[PTGG.let(this.update(term.val), this.update(term.expr))]
}
}
{term_VAR} {[term]}
{"PTGG.update: term found with impossible type".error};
if(subsentence.isKindOf(Collection).not) {
("PTGG.update: non-collection subsentence returned from term" + term.asString + "|" + subsentence.asString).error;
};
result = result ++ subsentence;
};
^result;
}
applyRules {
arg term;
var symbol = term.symbol;
var rules, ruleFunc;
if(symbolSet.includes(symbol).not) {
("PTGG.applyRules: not in symbol set ("++symbol++"). term:"+term.asString).error;
};
rules = ruleDict[symbol];
if(rules.isNil) {
("PTGG.applyRules: symbol not in ruleDict:"+symbol.asString).error;
};
ruleFunc = rules.collect(_.value).wchoose(rules.collect(_.key));
^ruleFunc.value(term);
}
expand {
arg term;
// term.postln;
^if(term.type == term_LET) {
var val = term.val;
var expr = term.expr;
var result = [];
if(val.isNil || expr.isNil) {
("PTGG.expand: term" + term.asString + "has a nil parameter").error;
};
expr.do({
|term|
result = result ++(term.type==term_VAR).if {
val
} {
[term]
}
});
result;
} {
[term];
};
}
expandAll {
arg sentence;
while {sentence.any({|term| term.type==term_LET})} {
// "loop".postln;
sentence = sentence.collect(this.expand(_)).reduce('++');
};
^sentence;
}
// convenience
*nt {
arg symbol, param = ();
^(type:\nt, symbol:symbol, param:param);
}
*vari {
^(type:\var);
}
*let {
arg val, expr;
^(type:\let, val:val, expr:expr);
}
*sentenceToString {
arg sentence, paramfunc = _.asString;
^sentence.collect(PTGG.termToString(_,paramfunc));
}
*termToString {
arg term,paramfunc = _.asString;
^switch(term.type)
{term_NT} {term.symbol.asString++paramfunc.value(term.param)}
{term_LET} {"LET "++PTGG.sentenceToString(term.val,paramfunc)++" IN "++PTGG.sentenceToString(term.expr,paramfunc)}
{term_VAR} {"var"};
}
}