Execute a Markov algorithm: Difference between revisions

Added EchoLisp
(Added EchoLisp)
Line 1,481:
 
!. markov markov-parse get-from !args 1</lang>
 
=={{header|EchoLisp}}==
<lang scheme>
;; rule := (pattern replacement [#t terminal])
 
(define-syntax-rule (pattern rule) (first rule))
(define-syntax-rule (repl sule) (second rule))
(define-syntax-rule (term? rule) (!empty? (cddr rule)))
 
;; (alpha .beta )--> (alpha beta #t)
(define (term-rule rule)
(if (string=? (string-first (repl rule)) ".")
(list (pattern rule) (string-rest (repl rule)) #t)
rule ))
 
;; returns list of rules
(define (parse-rules lines)
(map term-rule
(for/list [(line (string-split lines "\n"))]
#:continue (string=? (string-first line) "#")
(map string-trim
(string-split (string-replace line "/\\t/g" " ") " -> ")))))
;; markov machine
(define (markov i-string rules)
(while
(for/fold (run #f) ((rule rules))
#:when (string-index (pattern rule) i-string)
(set! i-string (string-replace i-string (pattern rule) (repl rule)))
;;(writeln rule i-string) ;; uncomment for trace
#:break (term? rule) => #f
#:break #t => #t ))
i-string)
(define (task i-string RS)
(markov i-string (parse-rules RS)))
</lang>
{{out}}
<pre>
(define RS1 #<<
# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
A -> apple
B -> bag
C -> chinchard
S -> shop
T -> the
the shop -> my brother
a never used -> .terminating rule
>>#)
;; [ Other rules sets here ...]
 
(define i-string-1 "I bought a B of As and Cs from T S.")
(define i-string-2 "I bought a B of As from T S.")
(define i-string-3 "I bought a B of As W my Bgage from T S.")
(define i-string-4 "_1111*11111_")
(define i-string-5 "000000A000000")
 
(task i-string-1 RS1)
→ "I bought a bag of apples and chinchards from my brother."
(task i-string-2 RS2)
→ "I bought a bag of apples from T shop."
(task i-string-3 RS3)
→ "I bought a bag of apples with my money from T shop."
(task i-string-4 RS4)
→ "11111111111111111111"
(task i-string-5 RS5)
→ "00011H1111000"
</pre>
 
 
=={{header|Go}}==