miller/docs/ngrams/ng1.mlr
2020-09-27 23:20:46 -04:00

39 lines
863 B
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);
for (int i = 0; i < n; i += 1) {
str a = substr(v, i, i);
@a_histo[a] += 1;
}
}
# ================================================================
end {
# Define this in this scope else it'll be scoped to the for-loop.
map a_cmf = compute_cmf_from_histo(@a_histo);
for (int oi = 0; oi < @ocount; oi += 1) {
str out = "";
for (int i = 0; i < @olen; i += 1) {
str oa = sample_from_cmf(a_cmf);
if (oa == "") {
break;
}
out .= oa;
}
print out;
}
}