Markov chain text generator: Difference between revisions

Added Fennel.
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
(Added Fennel.)
Line 450:
sight, they were getting so far off). 'Oh, my poor little feet, I wonder who will put on your shoes and stockings for you now, dears? I'm sure I don't want to live here, cried Dorothy. I want to go! Let me see: four times five is twelve, and four times six is thirteen, and four times six is thirteen, and four times seven is-oh dear! I shall be glad to give you some. And I want him to give me a heart, said the Woodman. So he walked to the edge of the desert, so she may know a way to carry you to the City of Oz in less than no time she'd have everybody executed, all round. (It was this last remark that had made her so savage when they met in the house, Let us both go to law: I will prosecute you. -Come, I'll take no denial; We must have a prize herself, you know,' said Alice to herself. 'Shy, they seem to put everything upon Bill! I wouldn't be in Bill's place for a good deal: this fireplace is narrow, to be sure; but I think I could, if I only knew how to begin.'
</pre>
 
=={{header|Fennel}}==
<lang Fennel>(fn string.split [self sep]
"Tokenize a string using the given separator."
(let [pattern (string.format "([^%s]+)" sep)
fields {}]
(self:gsub pattern (fn [c] (tset fields (+ 1 (length fields)) c)))
fields))
 
(fn read-file [filename]
"Read a file as a string. Doesn't do any error checking."
(with-open [fin (io.open filename)]
(fin:read :a)))
 
(fn random-key [table]
"Pick a random key from a non-sequential table."
;Collect keys into sequential table.
(let [keys (icollect [k _ (pairs table)] k)]
;Return a random key.
(. keys (math.random (length keys)))))
 
(fn cat-tables [t1 t2]
"Concatenate two sequential tables into a new table."
;Copy table 1 to create a result table.
(var result [(table.unpack t1)])
;Append each item of table 2 into the result.
(each [_ v (ipairs t2)]
(table.insert result v))
;Return the result.
result)
 
(fn make-rule [str context]
"Make a rule table for a given string and context length."
(var key "")
(var rule {})
(let [words (str:split " ")]
;Loop through word list.
(each [index word (ipairs [(table.unpack words (+ 1 context))])]
;Join previous words into key.
(set key (table.concat words " " index (+ index (- context 1))))
;Append word to existing key or add to a new key.
(if (. rule key)
(table.insert (. rule key) word)
(tset rule key [word]))))
;Return final rule.
rule)
 
(fn make-string [rule size]
"Make a string from a rule table up to the given size. May break early."
(var result [])
(var words [])
(var old-words (: (random-key rule) :split " "))
(var key (table.concat old-words " "))
;Loop until early break or size reached.
(for [i 1 size :until (= nil (. rule key))]
;Add to the result a random word from the list words for that key.
(set words (. rule key))
(table.insert result (. words (math.random (length words))))
;Shift the key to the next word.
(set old-words (cat-tables [(table.unpack old-words 2)]
[(. result (length result))]))
(set key (table.concat old-words " ")))
;Return the final string.
(table.concat result " "))
 
;;; Test the generator.
(print (make-string (make-rule (read-file "alice_oz.txt") 2) 300))</lang>
 
{{out}}
(Newlines added for readability.)
<pre>'Come, let's try Geography. London is the use of a hen that had a vague sort of
chance of a big room with a melancholy tone: 'it doesn't seem to be no worse off
than before, as the March Hare interrupted in a moment: she looked down at my
best one day, for I have to eat the comfits: this caused some noise and
confusion, as the rest of the legs and fierce eyes and roll off the fire, since
your tastes are so very angry that she could do that in a way to kill the Wicked
Witch of the East was dead the Munchkins sent a swift messenger to me, for Oz so
long and tiresome walk through the trees and began an account of the Munchkins,
who had expected her to begin.' For, you see, Alice had got its head to it, by
means of the Gates. No one has ever crossed the desert, so she made a hearty
supper and was going on. There was a little uneasily. Oh, yes; but one Wicked
Witch dies you will be the lovely Lady I shall get on my toes or sticks a pin
into me, it doesn't understand</pre>
 
=={{header|Go}}==
Anonymous user