Execute a Markov algorithm: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|J}}: remove superflouous indentation)
Line 104: Line 104:
'''Solution''':<lang j> require'strings regex'
'''Solution''':<lang j> require'strings regex'


NB. Lex a Markov program
NB. Lex a Markov program
markovLexer =: verb define
markovLexer =: verb define
rules =. LF cut TAB&=`(,:&' ')}y
rules =. LF cut TAB&=`(,:&' ')}y
rules =. a: -.~ (dltb@:{.~ i:&'#')&.> rules
rules =. a: -.~ (dltb@:{.~ i:&'#')&.> rules
0 _1 {"1 '\s+->\s+' (rxmatches rxcut ])S:0 rules
0 _1 {"1 '\s+->\s+' (rxmatches rxcut ])S:0 rules
)
)


NB. Given ruleset and target string, output
NB. Given ruleset and target string, output
NB. result.
NB. result.
markov =: markovLexer@[ stringreplace^:_ ]
markov =: markovLexer@[ stringreplace^:_ ]


NB. Same as above, but output all intermediate
NB. Same as above, but output all intermediate
NB. evaluations
NB. evaluations
markovDebug =: markovLexer@[ stringreplace^:a: ]</lang>
markovDebug =: markovLexer@[ stringreplace^:a: ]</lang>


'''Example''':<lang j>
'''Example''':<lang j>
m1 =. noun define
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
A -> apple
A -> apple
B -> bag
B -> bag
S -> shop
S -> shop
T -> the
T -> the
the shop -> my brother
the shop -> my brother
a never used -> .terminating rule
a never used -> .terminating rule
)
)


m1 markov 'I bought a B of As from T S.'
m1 markov 'I bought a B of As from T S.'