Execute a Markov algorithm: Difference between revisions

Added Wren
(Add APL)
(Added Wren)
Line 5,298:
00011H1111000
</lang>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-ioutil}}
{{libheader|wren-pattern}}
<lang ecmascript>import "/ioutil" for FileUtil, File
import "/pattern" for Pattern
 
var lb = FileUtil.lineBreak
 
/* rulesets assumed to be separated by a blank line in file */
var readRules = Fn.new { |path|
return File.read(path).trimEnd().split("%(lb)%(lb)").map { |rs| rs.split(lb) }.toList
}
 
/* tests assumed to be on consecutive lines */
var readTests = Fn.new { |path| File.read(path).trimEnd().split(lb) }
 
var rules = readRules.call("markov_rules.txt")
var tests = readTests.call("markov_tests.txt")
var pattern = Pattern.new("+0/s[~.][+0/z]", Pattern.start)
var ix = 0
for (origTest in tests) {
var captures = []
for (rule in rules[ix]) {
if (rule.startsWith("#")) continue
var splits = rule.split(" -> ")
var m = pattern.find(splits[1])
if (m) captures.add([splits[0].trimEnd()] + m.capsText)
}
var test = origTest
while (true) {
var copy = test
var redo = false
for (c in captures) {
test = test.replace(c[0], c[2])
if (c[1] == ".") break
if (test != copy) {
redo = true
break
}
}
if (!redo) break
}
System.print("%(origTest)\n%(test)\n")
ix = ix + 1
}</lang>
 
{{out}}
<pre>
I bought a B of As from T S.
I bought a bag of apples from my brother.
 
I bought a B of As from T S.
I bought a bag of apples from T shop.
 
I bought a B of As W my Bgage from T S.
I bought a bag of apples with my money from T shop.
 
_1111*11111_
11111111111111111111
 
000000A000000
00011H1111000
</pre>
 
=={{header|zkl}}==
9,483

edits