Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,27 @@
|
|||
# Output: the input string with all its regex-special characters suitably escaped
|
||||
def deregex:
|
||||
reduce ("\\\\", "\\*", "\\^", "\\?", "\\+", "\\.", "\\!", "\\{", "\\}", "\\[", "\\]", "\\$", "\\|",
|
||||
"\\(", "\\)" ) as $c
|
||||
(.; gsub( $c; $c));
|
||||
|
||||
# line-break
|
||||
def lb: "\n";
|
||||
|
||||
def split2($s):
|
||||
index($s) as $ix
|
||||
| if $ix then [ .[:$ix], .[$ix + ($s|length):]] else null end;
|
||||
|
||||
def trim: sub("^ *";"") | sub(" *$";"");
|
||||
|
||||
# rulesets are assumed to be separated by a blank line
|
||||
# input: a string
|
||||
def readRules:
|
||||
trim | split("\(lb)\(lb)") | map(split(lb)) ;
|
||||
|
||||
# tests are assumed to be on consecutive lines via `inputs`
|
||||
# Output: an array
|
||||
def readTests: [inputs | trim | select(length>0) ];
|
||||
|
||||
def rules: $markov_rules | readRules;
|
||||
|
||||
def tests: readTests;
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
def parseRules($rules):
|
||||
"^ *(?<period>[.]?) *(?<rule>.*)" as $pattern
|
||||
| reduce $rules[] as $rule ([];
|
||||
if $rule | (startswith("#") or (test(" -> ")|not)) then .
|
||||
else ($rule|split2(" -> ")) as $splits
|
||||
| ($splits[1] | capture($pattern)) as $re
|
||||
| . + [[($splits[0]|trim|deregex), $re.period, ($re.rule | trim)]]
|
||||
end );
|
||||
|
||||
# applyRules applies $rules to . recursively,
|
||||
# where $rules is the set of applicable rules in the form of an array-of-triples.
|
||||
# Input and output: a string
|
||||
def applyRules($rules):
|
||||
# The inner function has arity-0 for efficiency
|
||||
# input and output: {stop, string}
|
||||
def apply:
|
||||
if .stop then .
|
||||
else .string as $copy
|
||||
| first( foreach $rules[] as $c (.;
|
||||
.string |= sub($c[0]; $c[2])
|
||||
| if $c[1] == "."
|
||||
then .stop=true
|
||||
elif .string != $copy
|
||||
then (apply | .stop = true)
|
||||
else .
|
||||
end;
|
||||
if .stop then . else empty end))
|
||||
// .
|
||||
end;
|
||||
{stop: false, string: .} | apply | .string;
|
||||
|
||||
def proceed:
|
||||
rules as $rules
|
||||
| tests as $tests
|
||||
| range(0; $tests|length) as $ix
|
||||
| $tests[$ix]
|
||||
| " \(.)\n=>\(applyRules( parseRules( $rules[$ix] ) ))\n" ;
|
||||
|
||||
proceed
|
||||
Loading…
Add table
Add a link
Reference in a new issue