miller/docs6/docs/ngrams/ng2.mlr

52 lines
1.1 KiB
Text
Executable file

# ================================================================
begin {
if (is_absent(@olen)) {
@olen = 16;
}
if (is_absent(@ocount)) {
@ocount = 16;
}
}
# ================================================================
for (_, v in $*) {
if (string(v) == "inf" || string(v) == "nan") {
continue;
}
int n = strlen(v);
if (n >= 1) {
str a = substr(v, 0, 0);
@a_histo[a] += 1;
for (int i = 0; i < n-1; i += 1) {
str a = substr(v, i, i);
str b = substr(v, i+1, i+1);
@ab_histo[a][b] += 1;
}
}
}
# ================================================================
end {
map a_cmf = compute_cmf_from_histo(@a_histo);
# Define this in this scope else they'll be scoped to the for-loops.
map ab_cmf = {};
for (a in @ab_histo) {
ab_cmf[a] = compute_cmf_from_histo(@ab_histo[a]);
}
for (int oi = 0; oi < @ocount; oi += 1) {
str oa = sample_from_cmf(a_cmf);
str out = oa;
for (int i = 1; i < @olen; i += 1) {
str ob = sample_from_cmf(ab_cmf[oa]);
if (ob == "") {
break;
}
out .= ob;
oa = ob;
}
print out;
}
}