Execute a Markov algorithm: Difference between revisions
Content added Content deleted
(→{{header|jq}}: tidy) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 153: | Line 153: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">T Rule |
||
String pattern |
String pattern |
||
String replacement |
String replacement |
||
Line 282: | Line 282: | ||
L(ruleset) RuleSets |
L(ruleset) RuleSets |
||
V rules = parse(ruleset) |
V rules = parse(ruleset) |
||
print(apply(SampleTexts[L.index], rules))</ |
print(apply(SampleTexts[L.index], rules))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 295: | Line 295: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
markov.ads: |
markov.ads: |
||
< |
<syntaxhighlight lang="ada">with Ada.Strings.Unbounded; |
||
package Markov is |
package Markov is |
||
Line 320: | Line 320: | ||
Entries : Entry_Array (1 .. Length); |
Entries : Entry_Array (1 .. Length); |
||
end record; |
end record; |
||
end Markov;</ |
end Markov;</syntaxhighlight> |
||
markov.adb: |
markov.adb: |
||
< |
<syntaxhighlight lang="ada">package body Markov is |
||
function Parse (S : String_Array) return Ruleset is |
function Parse (S : String_Array) return Ruleset is |
||
Line 403: | Line 403: | ||
end Apply; |
end Apply; |
||
end Markov;</ |
end Markov;</syntaxhighlight> |
||
test_markov.adb: |
test_markov.adb: |
||
< |
<syntaxhighlight lang="ada">with Ada.Command_Line; |
||
with Ada.Text_IO.Unbounded_IO; |
with Ada.Text_IO.Unbounded_IO; |
||
with Ada.Strings.Unbounded; |
with Ada.Strings.Unbounded; |
||
Line 453: | Line 453: | ||
end; |
end; |
||
end; |
end; |
||
end Test_Markov;</ |
end Test_Markov;</syntaxhighlight> |
||
Output (rulesX contains the ruleset of above examples and testX the example text): |
Output (rulesX contains the ruleset of above examples and testX the example text): |
||
Line 469: | Line 469: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">markov←{ |
||
trim←{(~(∧\∨⌽∘(∧\)∘⌽)⍵∊⎕UCS 9 32)/⍵} |
trim←{(~(∧\∨⌽∘(∧\)∘⌽)⍵∊⎕UCS 9 32)/⍵} |
||
rules←(~rules∊⎕UCS 10 13)⊆rules←80 ¯1 ⎕MAP ⍺ |
rules←(~rules∊⎕UCS 10 13)⊆rules←80 ¯1 ⎕MAP ⍺ |
||
Line 488: | Line 488: | ||
} |
} |
||
rules apply ⍵ |
rules apply ⍵ |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 'f:\ruleset1.mkv' markov 'I bought a B of As from T S.' |
<pre> 'f:\ruleset1.mkv' markov 'I bought a B of As from T S.' |
||
Line 502: | Line 502: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">;--------------------------------------------------------------------------- |
||
; Markov Algorithm.ahk |
; Markov Algorithm.ahk |
||
; by wolf_II |
; by wolf_II |
||
Line 855: | Line 855: | ||
;---------- end of file ----------------------------------------------------</ |
;---------- end of file ----------------------------------------------------</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> PRINT FNmarkov("ruleset1.txt", "I bought a B of As from T S.") |
||
PRINT FNmarkov("ruleset2.txt", "I bought a B of As from T S.") |
PRINT FNmarkov("ruleset2.txt", "I bought a B of As from T S.") |
||
PRINT FNmarkov("ruleset3.txt", "I bought a B of As W my Bgage from T S.") |
PRINT FNmarkov("ruleset3.txt", "I bought a B of As W my Bgage from T S.") |
||
Line 892: | Line 892: | ||
UNTIL EOF#rules% OR done% |
UNTIL EOF#rules% OR done% |
||
CLOSE #rules% |
CLOSE #rules% |
||
= text$</ |
= text$</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 904: | Line 904: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Save the following text to a file "markov.bra": |
Save the following text to a file "markov.bra": |
||
< |
<syntaxhighlight lang="bracmat"> |
||
markov= |
markov= |
||
{ |
{ |
||
Line 1,119: | Line 1,119: | ||
& ok |
& ok |
||
| failure; |
| failure; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test: |
Test: |
||
Line 1,138: | Line 1,138: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 1,340: | Line 1,340: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight>output<syntaxhighlight lang="text">Rules from 'rule1' ok |
||
text: I bought a B of As from T S. |
text: I bought a B of As from T S. |
||
markoved: I bought a bag of apples from my brother. |
markoved: I bought a bag of apples from my brother. |
||
Line 1,359: | Line 1,359: | ||
text: 000000A000000 |
text: 000000A000000 |
||
markoved: 00011H1111000 |
markoved: 00011H1111000 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Note: Non-use of <code>iswhite</code> is intentional, since depending on the locale, other chars besides space and tab might be detected by that function. |
Note: Non-use of <code>iswhite</code> is intentional, since depending on the locale, other chars besides space and tab might be detected by that function. |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <cstdlib> |
#include <cstdlib> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,476: | Line 1,476: | ||
std::cout << output << "\n"; |
std::cout << output << "\n"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">markov = cluster is make, run |
||
rule = struct[from, to: string, term: bool] |
rule = struct[from, to: string, term: bool] |
||
rep = array[rule] |
rep = array[rule] |
||
Line 1,578: | Line 1,578: | ||
when invalid(s: string): stream$putl(eo, "Parse error: " || s) |
when invalid(s: string): stream$putl(eo, "Parse error: " || s) |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ ./markov ruleset1.mkv "I bought a B of As from T S." |
<pre>$ ./markov ruleset1.mkv "I bought a B of As from T S." |
||
Line 1,597: | Line 1,597: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
I should mention that this uses the regular expression machinery present in Allegro Lisp but not Common Lisp generally (though there are public domain Lisp libraries). |
I should mention that this uses the regular expression machinery present in Allegro Lisp but not Common Lisp generally (though there are public domain Lisp libraries). |
||
< |
<syntaxhighlight lang="lisp">;;; Keeps track of all our rules |
||
(defclass markov () |
(defclass markov () |
||
((rules :initarg :rules :initform nil :accessor rules))) |
((rules :initarg :rules :initform nil :accessor rules))) |
||
Line 1,664: | Line 1,664: | ||
(setf ret (adjust rule-info ret)) |
(setf ret (adjust rule-info ret)) |
||
(if (terminal (car rule-info)) (return ret)) |
(if (terminal (car rule-info)) (return ret)) |
||
(setf rule-info (get-rule markov ret)))))</ |
(setf rule-info (get-rule markov ret)))))</syntaxhighlight> |
||
Testing: |
Testing: |
||
<lang>(defparameter |
<syntaxhighlight lang="text">(defparameter |
||
*rules1* |
*rules1* |
||
"# This rules file is extracted from Wikipedia: |
"# This rules file is extracted from Wikipedia: |
||
Line 1,693: | Line 1,693: | ||
00011H1111000 |
00011H1111000 |
||
NIL |
NIL |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
include "strings.coh"; |
include "strings.coh"; |
||
include "malloc.coh"; |
include "malloc.coh"; |
||
Line 1,892: | Line 1,892: | ||
print(ApplyRules(ReadFile(fname), &patbuf[0])); |
print(ApplyRules(ReadFile(fname), &patbuf[0])); |
||
print_nl();</ |
print_nl();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ ./markov.386 ruleset1.mkv "I bought a B of As from T S." |
<pre>$ ./markov.386 ruleset1.mkv "I bought a B of As from T S." |
||
Line 1,907: | Line 1,907: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.file, std.regex, std.string, std.range, |
import std.stdio, std.file, std.regex, std.string, std.range, |
||
std.functional; |
std.functional; |
||
Line 1,939: | Line 1,939: | ||
writefln("%s\n%s\n", origTest, test); |
writefln("%s\n%s\n", origTest, test); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>I bought a B of As from T S. |
<pre>I bought a B of As from T S. |
||
Line 1,958: | Line 1,958: | ||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
This implementation expect the initial text on the command line and the ruleset on STDIN. |
This implementation expect the initial text on the command line and the ruleset on STDIN. |
||
< |
<syntaxhighlight lang="dejavu">(remove-comments) text: |
||
] |
] |
||
for line in text: |
for line in text: |
||
Line 2,000: | Line 2,000: | ||
not (markov-tick) rules |
not (markov-tick) rules |
||
!. markov markov-parse get-from !args 1</ |
!. markov markov-parse get-from !args 1</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
;; rule := (pattern replacement [#t terminal]) |
;; rule := (pattern replacement [#t terminal]) |
||
Line 2,037: | Line 2,037: | ||
(define (task i-string RS) |
(define (task i-string RS) |
||
(markov i-string (parse-rules RS))) |
(markov i-string (parse-rules RS))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,074: | Line 2,074: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
<p>Using Partial Active Pattern to simplify pattern matching.</p> |
<p>Using Partial Active Pattern to simplify pattern matching.</p> |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
open System.IO |
open System.IO |
||
open System.Text.RegularExpressions |
open System.Text.RegularExpressions |
||
Line 2,126: | Line 2,126: | ||
|> run |
|> run |
||
|> printfn "%s" |
|> printfn "%s" |
||
0</ |
0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>H:\RosettaCode\ExecMarkovAlgo>echo I bought a B of As from T S. | Fsharp\RosettaCode\bin\Debug\RosettaCode.exe m1 |
<pre>H:\RosettaCode\ExecMarkovAlgo>echo I bought a B of As from T S. | Fsharp\RosettaCode\bin\Debug\RosettaCode.exe m1 |
||
Line 2,144: | Line 2,144: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,319: | Line 2,319: | ||
`00011H1111000`, |
`00011H1111000`, |
||
}, |
}, |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,327: | Line 2,327: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">def markovInterpreterFor = { rules -> |
||
def ruleMap = [:] |
def ruleMap = [:] |
||
rules.eachLine { line -> |
rules.eachLine { line -> |
||
Line 2,351: | Line 2,351: | ||
text |
text |
||
}] |
}] |
||
}</ |
}</syntaxhighlight> |
||
The test code is below (with the markov rulesets 2..5 elided): |
The test code is below (with the markov rulesets 2..5 elided): |
||
< |
<syntaxhighlight lang="groovy">def verify = { ruleset -> |
||
[withInput: { text -> |
[withInput: { text -> |
||
[hasOutput: { expected -> |
[hasOutput: { expected -> |
||
Line 2,385: | Line 2,385: | ||
def ruleset5 = markovInterpreterFor("""...""") |
def ruleset5 = markovInterpreterFor("""...""") |
||
verify ruleset5 withInput '000000A000000' hasOutput '00011H1111000'</ |
verify ruleset5 withInput '000000A000000' hasOutput '00011H1111000'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,399: | Line 2,399: | ||
This program expects a source file as an argument and uses the standard input and output devices for the algorithm's I/O. |
This program expects a source file as an argument and uses the standard input and output devices for the algorithm's I/O. |
||
< |
<syntaxhighlight lang="haskell">import Data.List (isPrefixOf) |
||
import Data.Maybe (catMaybes) |
import Data.Maybe (catMaybes) |
||
import Control.Monad |
import Control.Monad |
||
Line 2,441: | Line 2,441: | ||
then let new = reverse before ++ to ++ drop (length from) ahead |
then let new = reverse before ++ to ++ drop (length from) ahead |
||
in if terminating then new else f rules new |
in if terminating then new else f rules new |
||
else g (a : before) as</ |
else g (a : before) as</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="unicon">procedure main(A) |
||
rules := loadRules(open(A[1],"r")) |
rules := loadRules(open(A[1],"r")) |
||
every write(line := !&input, " -> ",apply(rules, line)) |
every write(line := !&input, " -> ",apply(rules, line)) |
||
Line 2,466: | Line 2,466: | ||
if (s == line) | \r.term then return s else line := s |
if (s == line) | \r.term then return s else line := s |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
Sample runs using above rule sets and test strings: |
Sample runs using above rule sets and test strings: |
||
Line 2,488: | Line 2,488: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''':< |
'''Solution''':<syntaxhighlight lang="j">require'strings regex' |
||
markovLexer =: verb define |
markovLexer =: verb define |
||
Line 2,517: | Line 2,517: | ||
end. |
end. |
||
y |
y |
||
)</ |
)</syntaxhighlight> |
||
'''Example''':< |
'''Example''':<syntaxhighlight lang="j"> m1 =. noun define |
||
# This rules file is extracted from Wikipedia: |
# This rules file is extracted from Wikipedia: |
||
# http://en.wikipedia.org/wiki/Markov_Algorithm |
# http://en.wikipedia.org/wiki/Markov_Algorithm |
||
Line 2,532: | Line 2,532: | ||
m1 markov 'I bought a B of As from T S.' |
m1 markov 'I bought a B of As from T S.' |
||
I bought a bag of apples from my brother. |
I bought a bag of apples from my brother. |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Discussion''': The J implementation correctly processes all the rulesets. More details are available on the [[Talk:Markov Algorithm#explicit_vs_tacit|the talk page]]. |
'''Discussion''': The J implementation correctly processes all the rulesets. More details are available on the [[Talk:Markov Algorithm#explicit_vs_tacit|the talk page]]. |
||
Line 2,538: | Line 2,538: | ||
{{trans|D}} |
{{trans|D}} |
||
{{works with|Java|7}} |
{{works with|Java|7}} |
||
< |
<syntaxhighlight lang="java">import java.io.IOException; |
||
import java.nio.charset.StandardCharsets; |
import java.nio.charset.StandardCharsets; |
||
import java.nio.file.Files; |
import java.nio.file.Files; |
||
Line 2,600: | Line 2,600: | ||
return rules; |
return rules; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,620: | Line 2,620: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">/** |
||
* Take a ruleset and return a function which takes a string to which the rules |
* Take a ruleset and return a function which takes a string to which the rules |
||
* should be applied. |
* should be applied. |
||
Line 2,779: | Line 2,779: | ||
console.log(markov(ruleset3)('I bought a B of As W my Bgage from T S.')); |
console.log(markov(ruleset3)('I bought a B of As W my Bgage from T S.')); |
||
console.log(markov(ruleset4)('_1111*11111_')); |
console.log(markov(ruleset4)('_1111*11111_')); |
||
console.log(markov(ruleset5)('000000A000000'));</ |
console.log(markov(ruleset5)('000000A000000'));</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>I bought a bag of apples from my brother. |
<pre>I bought a bag of apples from my brother. |
||
Line 2,802: | Line 2,802: | ||
'''Preliminaries''' |
'''Preliminaries''' |
||
< |
<syntaxhighlight lang="jq"># Output: the input string with all its regex-special characters suitably escaped |
||
def deregex: |
def deregex: |
||
reduce ("\\\\", "\\*", "\\^", "\\?", "\\+", "\\.", "\\!", "\\{", "\\}", "\\[", "\\]", "\\$", "\\|", |
reduce ("\\\\", "\\*", "\\^", "\\?", "\\+", "\\.", "\\!", "\\{", "\\}", "\\[", "\\]", "\\$", "\\|", |
||
Line 2,828: | Line 2,828: | ||
def rules: $markov_rules | readRules; |
def rules: $markov_rules | readRules; |
||
def tests: readTests;</ |
def tests: readTests;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">def parseRules($rules): |
||
"^ *(?<period>[.]?) *(?<rule>.*)" as $pattern |
"^ *(?<period>[.]?) *(?<rule>.*)" as $pattern |
||
| reduce $rules[] as $rule ([]; |
| reduce $rules[] as $rule ([]; |
||
Line 2,868: | Line 2,868: | ||
| " \(.)\n=>\(applyRules( parseRules( $rules[$ix] ) ))\n" ; |
| " \(.)\n=>\(applyRules( parseRules( $rules[$ix] ) ))\n" ; |
||
proceed</ |
proceed</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,892: | Line 2,892: | ||
'''Module''': |
'''Module''': |
||
< |
<syntaxhighlight lang="julia">module MarkovAlgos |
||
struct MarkovRule{F,T} |
struct MarkovRule{F,T} |
||
Line 2,939: | Line 2,939: | ||
end |
end |
||
end # module MarkovAlgos</ |
end # module MarkovAlgos</syntaxhighlight> |
||
'''Main''': |
'''Main''': |
||
< |
<syntaxhighlight lang="julia">include("module.jl") |
||
let rulesets = @.("data/markovrules0" * string(1:5) * ".txt"), |
let rulesets = @.("data/markovrules0" * string(1:5) * ".txt"), |
||
Line 2,952: | Line 2,952: | ||
println("Transformed:\n", MarkovAlgos.apply(ruletest[i], rules)) |
println("Transformed:\n", MarkovAlgos.apply(ruletest[i], rules)) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,992: | Line 2,992: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.51 |
||
import java.io.File |
import java.io.File |
||
Line 3,035: | Line 3,035: | ||
println("$origTest\n$test\n") |
println("$origTest\n$test\n") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,056: | Line 3,056: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- utility method to escape punctuation |
||
function normalize(str) |
function normalize(str) |
||
local result = str:gsub("(%p)", "%%%1") |
local result = str:gsub("(%p)", "%%%1") |
||
Line 3,240: | Line 3,240: | ||
do_markov(grammar3, text2, 'I bought a bag of apples with my money from T shop.') |
do_markov(grammar3, text2, 'I bought a bag of apples with my money from T shop.') |
||
do_markov(grammar4, text3, '11111111111111111111') |
do_markov(grammar4, text3, '11111111111111111111') |
||
do_markov(grammar5, text4, '00011H1111000')</ |
do_markov(grammar5, text4, '00011H1111000')</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">markov[ruleset_, text_] := |
||
Module[{terminating = False, output = text, |
Module[{terminating = False, output = text, |
||
rules = StringCases[ |
rules = StringCases[ |
||
Line 3,254: | Line 3,254: | ||
output = StringReplace[output, rule[[1]] -> rule[[2]]]; |
output = StringReplace[output, rule[[1]] -> rule[[2]]]; |
||
If[! rule[[3]], terminating = False]; Break[]], {rule, rules}]]; |
If[! rule[[3]], terminating = False]; Break[]], {rule, rules}]]; |
||
output];</ |
output];</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="mathematica">markov["# Turing machine: three-state busy beaver |
||
# |
# |
||
# state A, symbol 0 => write 1, move right, new state B |
# state A, symbol 0 => write 1, move right, new state B |
||
Line 3,273: | Line 3,273: | ||
# state C, symbol 1 => write 1, move left, halt |
# state C, symbol 1 => write 1, move left, halt |
||
0C1 -> H01 |
0C1 -> H01 |
||
1C1 -> H11", "000000A000000"]</ |
1C1 -> H11", "000000A000000"]</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>"00011H1111000"</pre> |
<pre>"00011H1111000"</pre> |
||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang> 9 П4 |
<syntaxhighlight lang="text"> 9 П4 |
||
КИП4 [x] П7 Вx {x} П8 |
КИП4 [x] П7 Вx {x} П8 |
||
ИП8 ИПE * П8 {x} x=0 08 |
ИП8 ИПE * П8 {x} x=0 08 |
||
Line 3,298: | Line 3,298: | ||
ИП3 ИПE / [x] П3 |
ИП3 ИПE / [x] П3 |
||
x=0 22 |
x=0 22 |
||
ИП4 ИП0 - 9 - x=0 02 С/П</ |
ИП4 ИП0 - 9 - x=0 02 С/П</syntaxhighlight> |
||
Under the rules of left 4 registers, under the word has 8 character cells, the alphabet of the digits from 1 to 8. Rules are placed in "123,456", where "123" is a fragment, and "456" is to be replaced, in the registers of the РA to РD. The number of rules is stored in Р0, the initial word is in Р9. Number triggered rule is the last digit registration Р4 (0 to 3), if no rule did not work, the indicator 0, otherwise the current word to be processed. In РE is stored 10. |
Under the rules of left 4 registers, under the word has 8 character cells, the alphabet of the digits from 1 to 8. Rules are placed in "123,456", where "123" is a fragment, and "456" is to be replaced, in the registers of the РA to РD. The number of rules is stored in Р0, the initial word is in Р9. Number triggered rule is the last digit registration Р4 (0 to 3), if no rule did not work, the indicator 0, otherwise the current word to be processed. In РE is stored 10. |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils, strscans |
||
type Rule = object |
type Rule = object |
||
Line 3,455: | Line 3,455: | ||
for n, ruleset in RuleSets: |
for n, ruleset in RuleSets: |
||
let rules = ruleset.parse() |
let rules = ruleset.parse() |
||
echo SampleTexts[n].apply(rules)</ |
echo SampleTexts[n].apply(rules)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,468: | Line 3,468: | ||
I'm not familiar with string processing, or parsing, so there are probably better ways to express this in OCaml. One might be with the mikmatch library which allows pattern-matching with regexps. Here I've only used the OCaml stdlib... |
I'm not familiar with string processing, or parsing, so there are probably better ways to express this in OCaml. One might be with the mikmatch library which allows pattern-matching with regexps. Here I've only used the OCaml stdlib... |
||
< |
<syntaxhighlight lang="ocaml">(* Useful for resource cleanup (such as filehandles) *) |
||
let try_finally x f g = |
let try_finally x f g = |
||
try let res = f x in g x; res |
try let res = f x in g x; res |
||
Line 3,515: | Line 3,515: | ||
print_endline (run rules (input_line stdin)); |
print_endline (run rules (input_line stdin)); |
||
translate () |
translate () |
||
in try translate () with End_of_file -> ()</ |
in try translate () with End_of_file -> ()</syntaxhighlight> |
||
With the above compiled to an executable 'markov', and the five rule-sets in files, strings are accepted on stdin for translation: |
With the above compiled to an executable 'markov', and the five rule-sets in files, strings are accepted on stdin for translation: |
||
Line 3,543: | Line 3,543: | ||
This program expects a source file as an argument and uses the standard input and output devices for the algorithm's I/O. |
This program expects a source file as an argument and uses the standard input and output devices for the algorithm's I/O. |
||
< |
<syntaxhighlight lang="perl">@ARGV == 1 or die "Please provide exactly one source file as an argument.\n"; |
||
open my $source, '<', $ARGV[0] or die "I couldn't open \"$ARGV[0]\" for reading. ($!.)\n"; |
open my $source, '<', $ARGV[0] or die "I couldn't open \"$ARGV[0]\" for reading. ($!.)\n"; |
||
my @rules; |
my @rules; |
||
Line 3,560: | Line 3,560: | ||
and ($terminating ? last OUTER : redo OUTER);}} |
and ($terminating ? last OUTER : redo OUTER);}} |
||
print $input;</ |
print $input;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">subs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">reps</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">subs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">reps</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
||
Line 3,689: | Line 3,689: | ||
"""</span> |
"""</span> |
||
<span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ruleset5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"000000A000000"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"00011H1111000"</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ruleset5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"000000A000000"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"00011H1111000"</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,701: | Line 3,701: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
function markov($text, $ruleset) { |
function markov($text, $ruleset) { |
||
Line 3,847: | Line 3,847: | ||
foreach ($conf AS $id => $rule) { |
foreach ($conf AS $id => $rule) { |
||
echo 'Ruleset ', $id, ' : ', markov($rule['text'], $rule['rule']), PHP_EOL; |
echo 'Ruleset ', $id, ' : ', markov($rule['text'], $rule['rule']), PHP_EOL; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,858: | Line 3,858: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de markov (File Text) |
||
(use (@A @Z R) |
(use (@A @Z R) |
||
(let Rules |
(let Rules |
||
Line 3,876: | Line 3,876: | ||
(T (= "." (cadr (setq R @))) |
(T (= "." (cadr (setq R @))) |
||
(append @A (cddr R) @Z) ) |
(append @A (cddr R) @Z) ) |
||
(setq Text (append @A (cdr R) @Z)) ) ) ) ) )</ |
(setq Text (append @A (cdr R) @Z)) ) ) ) ) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>: (markov "r1" "I bought a B of As from T S.") |
<pre>: (markov "r1" "I bought a B of As from T S.") |
||
Line 3,897: | Line 3,897: | ||
Module lambda can be found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl |
Module lambda can be found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl |
||
< |
<syntaxhighlight lang="prolog">:- module('markov.pl', [markov/3, apply_markov/3]). |
||
:- use_module(library(lambda)). |
:- use_module(library(lambda)). |
||
Line 3,985: | Line 3,985: | ||
replacement([X | R]) --> [X], {X \= '\n'}, replacement(R). |
replacement([X | R]) --> [X], {X \= '\n'}, replacement(R). |
||
replacement([]) --> []. |
replacement([]) --> []. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Code to test : |
Code to test : |
||
< |
<syntaxhighlight lang="prolog">:- use_module('markov.pl'). |
||
:- use_module(library(lambda)). |
:- use_module(library(lambda)). |
||
Line 4,104: | Line 4,104: | ||
writeln(B), |
writeln(B), |
||
writeln(R). |
writeln(R). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : |
Output : |
||
<pre> ?- markov. |
<pre> ?- markov. |
||
Line 4,132: | Line 4,132: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
The GUI used here allows a ruleset to be loaded from a text file or manually added one rule at a time. Symbol input can be tested anytime by selecting 'Interpret'. |
The GUI used here allows a ruleset to be loaded from a text file or manually added one rule at a time. Symbol input can be tested anytime by selecting 'Interpret'. |
||
< |
<syntaxhighlight lang="purebasic">Structure mRule |
||
pattern.s |
pattern.s |
||
replacement.s |
replacement.s |
||
Line 4,245: | Line 4,245: | ||
EndSelect |
EndSelect |
||
Until isDone |
Until isDone |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample output from loading Ruleset 1 and interpreting a symbol: |
Sample output from loading Ruleset 1 and interpreting a symbol: |
||
<pre>Comment: "# This rules file is extracted from Wikipedia:" |
<pre>Comment: "# This rules file is extracted from Wikipedia:" |
||
Line 4,264: | Line 4,264: | ||
The example gains flexibility by not being tied to specific files. The functions may be imported into other programs which then can provide textual input from their sources without the need to pass 'file handles' around. |
The example gains flexibility by not being tied to specific files. The functions may be imported into other programs which then can provide textual input from their sources without the need to pass 'file handles' around. |
||
< |
<syntaxhighlight lang="python">import re |
||
def extractreplacements(grammar): |
def extractreplacements(grammar): |
||
Line 4,397: | Line 4,397: | ||
== '11111111111111111111' |
== '11111111111111111111' |
||
assert replace(text4, extractreplacements(grammar5)) \ |
assert replace(text4, extractreplacements(grammar5)) \ |
||
== '00011H1111000'</ |
== '00011H1111000'</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 4,405: | Line 4,405: | ||
The <tt>Markov-algorithm</tt> for a set of rules returns a function which maps from a string to string and can be used as a first-class object. Rules are represented by abstract data structures. |
The <tt>Markov-algorithm</tt> for a set of rules returns a function which maps from a string to string and can be used as a first-class object. Rules are represented by abstract data structures. |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 4,434: | Line 4,434: | ||
(let loop ([x x0] [fx (f x0)]) |
(let loop ([x x0] [fx (f x0)]) |
||
(if (equal? x fx) fx (loop fx (f fx))))) |
(if (equal? x fx) fx (loop fx (f fx))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example of use: |
Example of use: |
||
< |
<syntaxhighlight lang="racket"> |
||
> (define MA |
> (define MA |
||
(Markov-algorithm |
(Markov-algorithm |
||
Line 4,450: | Line 4,450: | ||
> (MA "I bought a B of As from T S.") |
> (MA "I bought a B of As from T S.") |
||
"I bought a bag of apples from T shop." |
"I bought a bag of apples from T shop." |
||
</syntaxhighlight> |
|||
</lang> |
|||
===The source reader=== |
===The source reader=== |
||
Line 4,456: | Line 4,456: | ||
To read from a file just replace <tt>with-input-from-string</tt> ==> <tt>with-input-from-file</tt>. |
To read from a file just replace <tt>with-input-from-string</tt> ==> <tt>with-input-from-file</tt>. |
||
< |
<syntaxhighlight lang="racket"> |
||
;; the reader |
;; the reader |
||
(define (read-rules source) |
(define (read-rules source) |
||
Line 4,479: | Line 4,479: | ||
(define (read-Markov-algorithm source) |
(define (read-Markov-algorithm source) |
||
(apply Markov-algorithm (read-rules source))) |
(apply Markov-algorithm (read-rules source))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Examples: |
Examples: |
||
< |
<syntaxhighlight lang="racket"> |
||
(define R3 (read-Markov-algorithm " |
(define R3 (read-Markov-algorithm " |
||
# BNF Syntax testing rules |
# BNF Syntax testing rules |
||
Line 4,548: | Line 4,548: | ||
0C1 -> H01 |
0C1 -> H01 |
||
1C1 -> H11")) |
1C1 -> H11")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="racket"> |
||
> (R3 "I bought a B of As W my Bgage from T S.") |
> (R3 "I bought a B of As W my Bgage from T S.") |
||
"I bought a bag of apples with my money from T shop." |
"I bought a bag of apples with my money from T shop." |
||
Line 4,559: | Line 4,559: | ||
> (R5 "000000A000000") |
> (R5 "000000A000000") |
||
"00011H1111000" |
"00011H1111000" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 4,569: | Line 4,569: | ||
Add --verbose to see the replacements step-by-step. |
Add --verbose to see the replacements step-by-step. |
||
<lang |
<syntaxhighlight lang="raku" line>grammar Markov { |
||
token TOP { |
token TOP { |
||
^ [^^ [<rule> | <comment>] $$ [\n|$]]* $ |
^ [^^ [<rule> | <comment>] $$ [\n|$]]* $ |
||
Line 4,633: | Line 4,633: | ||
say "starting with: $start_value"; |
say "starting with: $start_value"; |
||
say run(:$ruleset, :$start_value, :$verbose); |
say run(:$ruleset, :$start_value, :$verbose); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Code was added to the REXX example to optionally list the contents of the ruleset and/or the Markov entries. |
Code was added to the REXX example to optionally list the contents of the ruleset and/or the Markov entries. |
||
<br>Also, blank lines in the ruleset were treated as comments. |
<br>Also, blank lines in the ruleset were treated as comments. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program executes a Markov algorithm(s) against specified entries. */ |
||
parse arg low high . /*allows which ruleset to process. */ |
parse arg low high . /*allows which ruleset to process. */ |
||
if low=='' | low=="," then low=1 /*Not specified? Then use the default.*/ |
if low=='' | low=="," then low=1 /*Not specified? Then use the default.*/ |
||
Line 4,679: | Line 4,679: | ||
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4) '───►' @.r |
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4) '───►' @.r |
||
end /*r*/ /* [↑] read and maybe echo the rule. */ |
end /*r*/ /* [↑] read and maybe echo the rule. */ |
||
return</ |
return</syntaxhighlight> |
||
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. |
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. |
||
<br><br> |
<br><br> |
||
Line 4,703: | Line 4,703: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{works with|Ruby|1.8.7}} |
{{works with|Ruby|1.8.7}} |
||
< |
<syntaxhighlight lang="ruby">def setup(ruleset) |
||
ruleset.each_line.inject([]) do |rules, line| |
ruleset.each_line.inject([]) do |rules, line| |
||
if line =~ /^\s*#/ |
if line =~ /^\s*#/ |
||
Line 4,722: | Line 4,722: | ||
end |
end |
||
input_data |
input_data |
||
end</ |
end</syntaxhighlight> |
||
'''Test:''' |
'''Test:''' |
||
< |
<syntaxhighlight lang="ruby">ruleset1 = <<EOS |
||
# This rules file is extracted from Wikipedia: |
# This rules file is extracted from Wikipedia: |
||
# http://en.wikipedia.org/wiki/Markov_Algorithm |
# http://en.wikipedia.org/wiki/Markov_Algorithm |
||
Line 4,820: | Line 4,820: | ||
EOS |
EOS |
||
puts morcov(ruleset5, "000000A000000")</ |
puts morcov(ruleset5, "000000A000000")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,834: | Line 4,834: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">use std::str::FromStr; |
||
#[derive(Clone, Debug)] |
#[derive(Clone, Debug)] |
||
Line 5,064: | Line 5,064: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{works with|Scala|2.8}} |
{{works with|Scala|2.8}} |
||
< |
<syntaxhighlight lang="scala">import scala.io.Source |
||
object MarkovAlgorithm { |
object MarkovAlgorithm { |
||
Line 5,097: | Line 5,097: | ||
println(algorithm(args(1))) |
println(algorithm(args(1))) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Script-style, and more concise: |
Script-style, and more concise: |
||
< |
<syntaxhighlight lang="scala">import scala.io.Source |
||
if (argv.size != 2 ) error("Syntax: MarkovAlgorithm inputFile inputPattern") |
if (argv.size != 2 ) error("Syntax: MarkovAlgorithm inputFile inputPattern") |
||
Line 5,116: | Line 5,116: | ||
println(argv(1)) |
println(argv(1)) |
||
println(algorithm(argv(1)))</ |
println(algorithm(argv(1)))</syntaxhighlight> |
||
Sample outputs: |
Sample outputs: |
||
Line 5,144: | Line 5,144: | ||
The following implementation uses several string-related procedures provided by SRFI-13 [http://srfi.schemers.org/srfi-13/srfi-13.html]. |
The following implementation uses several string-related procedures provided by SRFI-13 [http://srfi.schemers.org/srfi-13/srfi-13.html]. |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define split-into-lines |
(define split-into-lines |
||
(lambda (str) |
(lambda (str) |
||
Line 5,192: | Line 5,192: | ||
rules)) |
rules)) |
||
(loop (cdr remaining) result))))))) |
(loop (cdr remaining) result))))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
<syntaxhighlight lang="sequencel"> |
|||
<lang sequenceL> |
|||
import <Utilities/Sequence.sl>; |
import <Utilities/Sequence.sl>; |
||
Line 5,245: | Line 5,245: | ||
replaceSubString(str, original, new, n + 1); |
replaceSubString(str, original, new, n + 1); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Note that the run-time data is immediately after the "end" label. This works with CSNOBOL4, on a Unix (or Unix-like) platform. |
Note that the run-time data is immediately after the "end" label. This works with CSNOBOL4, on a Unix (or Unix-like) platform. |
||
The Markov rules are actually compiled into the program after parsing, and are then directly executed (self-modifying code). |
The Markov rules are actually compiled into the program after parsing, and are then directly executed (self-modifying code). |
||
<syntaxhighlight lang="snobol4"> |
|||
<lang SNOBOL4> |
|||
#!/bin/sh |
#!/bin/sh |
||
exec "snobol4" "-r" "$0" "$@" |
exec "snobol4" "-r" "$0" "$@" |
||
Line 5,379: | Line 5,379: | ||
000000A000000 |
000000A000000 |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func setup(ruleset: String) -> [(String, String, Bool)] { |
func setup(ruleset: String) -> [(String, String, Bool)] { |
||
Line 5,428: | Line 5,428: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,440: | Line 5,440: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.5}} |
{{works with|Tcl|8.5}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
if {$argc < 3} {error "usage: $argv0 ruleFile inputFile outputFile"} |
if {$argc < 3} {error "usage: $argv0 ruleFile inputFile outputFile"} |
||
lassign $argv ruleFile inputFile outputFile |
lassign $argv ruleFile inputFile outputFile |
||
Line 5,480: | Line 5,480: | ||
puts $out $line |
puts $out $line |
||
} |
} |
||
close $out</ |
close $out</syntaxhighlight> |
||
In the case where there are no terminating rules and no overlapping issues, the following is an alternative: |
In the case where there are no terminating rules and no overlapping issues, the following is an alternative: |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
if {$argc < 3} {error "usage: $argv0 ruleFile inputFile outputFile"} |
if {$argc < 3} {error "usage: $argv0 ruleFile inputFile outputFile"} |
||
lassign $argv ruleFile inputFile outputFile |
lassign $argv ruleFile inputFile outputFile |
||
Line 5,509: | Line 5,509: | ||
} |
} |
||
puts $out $data |
puts $out $data |
||
close $out</ |
close $out</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
====Implementation==== |
====Implementation==== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
class markovparser |
class markovparser |
||
Line 5,587: | Line 5,587: | ||
end function |
end function |
||
end class |
end class |
||
</syntaxhighlight> |
|||
</lang> |
|||
=====Invocation===== |
=====Invocation===== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
dim m1 |
dim m1 |
||
set m1 = new markovparser |
set m1 = new markovparser |
||
Line 5,651: | Line 5,651: | ||
m5.ruleset = fso.opentextfile("busybeaver.tur").readall |
m5.ruleset = fso.opentextfile("busybeaver.tur").readall |
||
wscript.echo m5.apply("000000A000000") |
wscript.echo m5.apply("000000A000000") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=====Output===== |
=====Output===== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
I bought a bag of apples from my brother. |
I bought a bag of apples from my brother. |
||
I bought a bag of apples from T shop. |
I bought a bag of apples from T shop. |
||
Line 5,660: | Line 5,660: | ||
11111111111111111111 |
11111111111111111111 |
||
00011H1111000 |
00011H1111000 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 5,666: | Line 5,666: | ||
{{libheader|Wren-ioutil}} |
{{libheader|Wren-ioutil}} |
||
{{libheader|wren-pattern}} |
{{libheader|wren-pattern}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/ioutil" for FileUtil, File |
||
import "/pattern" for Pattern |
import "/pattern" for Pattern |
||
Line 5,707: | Line 5,707: | ||
System.print("%(origTest)\n%(test)\n") |
System.print("%(origTest)\n%(test)\n") |
||
ix = ix + 1 |
ix = ix + 1 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,728: | Line 5,728: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn parseRuleSet(lines){ |
||
if(vm.numArgs>1) lines=vm.arglist; // lines or object |
if(vm.numArgs>1) lines=vm.arglist; // lines or object |
||
ks:=L(); vs:=L(); |
ks:=L(); vs:=L(); |
||
Line 5,753: | Line 5,753: | ||
}while(go); |
}while(go); |
||
text |
text |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">ruleSet:=parseRuleSet("# This rules file is extracted from Wikipedia:", |
||
"# http://en.wikipedia.org/wiki/Markov_Algorithm", |
"# http://en.wikipedia.org/wiki/Markov_Algorithm", |
||
"A\t->\tapple", "B -> bag", "S -> shop", "T -> the", |
"A\t->\tapple", "B -> bag", "S -> shop", "T -> the", |
||
"the shop -> my brother", "a never used -> .terminating rule"); |
"the shop -> my brother", "a never used -> .terminating rule"); |
||
ruleSet.println(); |
ruleSet.println(); |
||
markov("I bought a B of As from T S.",ruleSet).println();</ |
markov("I bought a B of As from T S.",ruleSet).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,765: | Line 5,765: | ||
I bought a bag of apples from my brother. |
I bought a bag of apples from my brother. |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="zkl">parseRuleSet( // rule set in a list |
||
T("# Slightly modified from the rules on Wikipedia", |
T("# Slightly modified from the rules on Wikipedia", |
||
"A -> apple", "B -> bag", "S -> .shop", "T -> the", |
"A -> apple", "B -> bag", "S -> .shop", "T -> the", |
||
Line 5,775: | Line 5,775: | ||
"W -> WW", "S -> .shop", "T -> the", |
"W -> WW", "S -> .shop", "T -> the", |
||
"the shop -> my brother", "a never used -> .terminating rule") : |
"the shop -> my brother", "a never used -> .terminating rule") : |
||
markov("I bought a B of As W my Bgage from T S.",_).println();</ |
markov("I bought a B of As W my Bgage from T S.",_).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,782: | Line 5,782: | ||
</pre> |
</pre> |
||
For the next two tasks, read the rule set from a file. |
For the next two tasks, read the rule set from a file. |
||
< |
<syntaxhighlight lang="zkl">parseRuleSet(File("ruleSet4")) : markov("_1111*11111_",_).println(); |
||
parseRuleSet(File("ruleSet5")) : markov("000000A000000",_).println();</ |
parseRuleSet(File("ruleSet5")) : markov("000000A000000",_).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |