Execute a Markov algorithm: Difference between revisions

m
→‎{{header|J}}: remove superflouous indentation
No edit summary
m (→‎{{header|J}}: remove superflouous indentation)
Line 104:
'''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+' (rxmatches 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.'
Anonymous user