Markov chain text generator: Difference between revisions

Added Wren
No edit summary
(Added Wren)
Line 1,613:
<pre>$ ./main /path/to/alice_oz.txt
with a crash, whereupon the Scarecrow's clothes fell out of the clouds to rule over us. Still, for many days they grieved over the loss of my heart. While I was in love I was the happiest man on earth; but no one came near them nor spoke to them because of the great beam the house rested on, two feet were sticking out, shod in silver shoes with pointed toes. Oh, dear! Oh, dear! cried Dorothy, clasping her hands together in dismay. The house must have fallen on her. Whatever shall we do? There is nothing to be done, I wonder?' As she said this, she looked up, but it was the first to break the silence. 'What day of the month is it?' he said, turning to Alice as it spoke. 'As wet as ever,' said Alice in a piteous tone. And she thought of herself, 'I wish the creatures wouldn't be so stingy about it, you know-' She had quite forgotten the Duchess by this time, and was going to dive in among the leaves, which she found to be nothing but the stars over them; and they rested very well indeed. In the morning they traveled on until they came to the great Throne Room, where he saw, sitting in the emerald throne, a most lovely Lady. She was dressed in a green uniform and wearing a long green beard. Here are strangers, said the Guardian of the Gates lived. This officer unlocked their spectacles to put them back in his great box, and then he struck at the Tin Woodman passed safely under it. Come on! he shouted to the others. These she also led to rooms, and each one of them can explain it,' said the King, 'and don't look at me like that!' He got behind</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-str}}
<lang ecmascript>import "io" for File
import "random" for Random
import "/str" for Strs
 
var markov = Fn.new { |filePath, keySize, outputSize|
if (keySize < 1) Fiber.abort("Key size can't be less than 1")
var words = File.read(filePath).trimEnd().split(" ")
var wordCount = words.count
if (outputSize < keySize || outputSize > wordCount) Fiber.abort("Output size is out of range")
var dict = {}
for (i in 0..(wordCount - keySize)) {
var prefix = Strs.join(words[i...i + keySize], " ")
var suffix = (i + keySize < wordCount) ? words[i + keySize] : ""
if (!dict.containsKey(prefix)) dict[prefix] = []
dict[prefix].add(suffix)
}
var output = []
var r = Random.new()
var prefix = r.sample(dict.keys.toList)
output.addAll(prefix.split(" "))
for (n in 1..wordCount) {
var nextWord = r.sample(dict[prefix].toList)
if (nextWord.isEmpty) break
output.add(nextWord)
if (output.count >= outputSize) break
prefix = Strs.join(output[n...n + keySize], " ")
}
return Strs.join(output.take(outputSize).toList, " ")
}
 
System.print(markov.call("alice_oz.txt", 3, 100))</lang>
 
{{out}}
<pre>
Alice with one finger; and the whole party swam to the shore. They were indeed a queer-looking party that assembled on the bank-the birds with draggled feathers, the animals with their fur clinging close to them, and all dripping wet, cross, and uncomfortable. The first question of course was, how to get back to the road from which the river had carried them. It was a very difficult game indeed. The players all played at once without waiting for the end of the table. 'Have some wine,' the March Hare said to itself in a whisper.) 'That would be grand,
</pre>
9,485

edits