Execute a Markov algorithm: Difference between revisions

m
alphabetized
(→‎{{header|J}}: only cut on first -> and leave any remaining as replacement string (trivial fix))
m (alphabetized)
Line 26:
the shop -> my brother
a never used -> .terminating rule</pre>If you input "I bought a B of As from T S." this code should output "I bought a bag of apples from T shop." This one tests that terminating rules work.
 
=={{header|J}}==
{{incorrect|lang|Quick & dirty solution, see [[Talk:Markov Algorithm#J|discussion of gaps]]}}
 
'''Solution''':<lang j>require'strings regex'
 
NB. Lex a Markov program
markovLexer =: verb define
rules =. LF cut TAB&=`(,:&' ')}y
rules =. a: -.~ (dltb@:{.~ i:&'#')&.> rules
0 _1 {"1 '\s+->\s+' (rxmatch rxcut ])S:0 rules
 
NB. Given ruleset and target string, output
NB. result.
markov =: markovLexer@[ stringreplace^:_ ]
 
NB. Same as above, but output all intermediate
NB. evaluations
markovDebug =: markovLexer@[ stringreplace^:a: ]</lang>
 
'''Example''':<lang j> m1 =. noun define
# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
A -> apple
B -> bag
S -> shop
T -> the
the shop -> my brother
a never used -> .terminating rule
 
m1 markov 'I bought a B of As from T S.'
I bought a bag of apples from my brother.
 
m1 markovDebug 'I bought a B of As from T S.'
I bought a B of As from T S.
I bought a bag of apples from the shop.
I bought a bag of apples from my brother.</lang>
'''Discussion''': This solution implemented in 20 seconds and doesn't fully implement a Markov algorithm. More details on [[Talk:Markov Algorithm#J|the talk page]].
 
=={{header|Ruby}}==
Line 98 ⟶ 138:
}
close $out</lang>
 
=={{header|J}}==
{{incorrect|lang|Quick & dirty solution, see [[Talk:Markov Algorithm#J|discussion of gaps]]}}
 
'''Solution''':<lang j>require'strings regex'
 
NB. Lex a Markov program
markovLexer =: verb define
rules =. LF cut TAB&=`(,:&' ')}y
rules =. a: -.~ (dltb@:{.~ i:&'#')&.> rules
0 _1 {"1 '\s+->\s+' (rxmatch rxcut ])S:0 rules
 
NB. Given ruleset and target string, output
NB. result.
markov =: markovLexer@[ stringreplace^:_ ]
 
NB. Same as above, but output all intermediate
NB. evaluations
markovDebug =: markovLexer@[ stringreplace^:a: ]</lang>
 
'''Example''':<lang j> m1 =. noun define
# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
A -> apple
B -> bag
S -> shop
T -> the
the shop -> my brother
a never used -> .terminating rule
 
m1 markov 'I bought a B of As from T S.'
I bought a bag of apples from my brother.
 
m1 markovDebug 'I bought a B of As from T S.'
I bought a B of As from T S.
I bought a bag of apples from the shop.
I bought a bag of apples from my brother.</lang>
'''Discussion''': This solution implemented in 20 seconds and doesn't fully implement a Markov algorithm. More details on [[Talk:Markov Algorithm#J|the talk page]].
Anonymous user