Markov chain text generator: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: elided the need for the RAND function, changed a comment..
Line 565:
 
=={{header|Kotlin}}==
<lang scala>// version 1.13.5110
 
import java.io.File
import java.util.Random
 
val r = Random()
 
fun markov(filePath: String, keySize: Int, outputSize: Int): String {
if require(keySize <>= 1) throw{ IllegalArgumentException("Key size can't be less than 1") }
 
val words = File(filePath).readText().trimEnd().split(' ')
if require(outputSize !in keySize..words.size) { "Output size is out of range" }
 
throw IllegalArgumentException("Output size is out of range")
}
val dict = mutableMapOf<String, MutableList<String>>()
 
for (i in 0..(words.size - keySize)) {
val keyprefix = words.subList(i, i + keySize).joinToString(" ")
val valuesuffix = if (i + keySize < words.size) words[i + keySize] else ""
ifval suffixes = (!dict.containsKeygetOrPut(keyprefix) { mutableListOf() }
suffixes += suffix
dict.put(key, mutableListOf(value))
else
dict[key]!!.add(value)
}
 
val output = mutableListOf<String>()
var nprefix = 0dict.keys.random()
var rnoutput += rprefix.nextIntsplit(dict.size' ')
 
var prefix = dict.keys.toList()[rn]
for (n in 1..words.size) {
output.addAll(prefix.split(' '))
val nextWord = dict[keyprefix]!!.addrandom(value)
if (suffixnextWord.size == 1isEmpty()) {break
 
output += nextWord
if (output.size >= outputSize) return output.take(outputSize).joinToString(" ")break
 
while (true) {
var suffix = dict[prefix]!!
if (suffix.size == 1) {
if (suffix[0] == "") return output.joinToString(" ")
output.add(suffix[0])
}
else {
rn = r.nextInt(suffix.size)
output.add(suffix[rn])
}
if (output.size >= outputSize) return output.take(outputSize).joinToString(" ")
n++
prefix = output.subList(n, n + keySize).joinToString(" ")
}
 
return output.take(outputSize).joinToString(" ")
}
 
fun main(args: Array<String>) {
println(markov("alice_oz.txt", 3, 200100))
}</lang>
 
Sample output:
<pre>
jump? asked the Scarecrow. Never. He sits day after day in the great fields. They kept on walking, however, and at night the moon came out and shone brightly. So they lay down among the sweet smelling yellow flowers and slept soundly until morning. When it was daylight, the girl bathed her face in her hands, and she set to work in one of the shelves as she passed; it was labelled 'ORANGE MARMALADE', but to her great delight it fitted! Alice opened the door and closer to one another, for the stillness of the empty room was more dreadful
but now, to her surprise, she found it was no longer green, but pure white. The ribbon around Toto's neck, and they started off in the right way. But at noon, when the sun was up, they started on their way, and soon saw a beautiful green glow in the sky just before them. On the other side of the garden, where Alice could see this, as she was near enough to look over their slates; 'but it doesn't matter which way you go,' said the King, looking round the court and got behind him, and very soon finished off the cake. 'Curiouser and curiouser!' cried Alice (she was so much disappointed; and the eyes winked again and looked upon her anxiously, as if the Great Oz himself, and driven him out of the room. The cook threw a frying-pan after her as she spoke. Alice did not quite know what to do next, when suddenly a White Rabbit with pink eyes ran close by her. There was nothing else to do, so Alice soon began talking again. 'Dinah'll miss me very much to-night, I should think!' (Dinah was the cat.) 'I hope they'll remember her saucer of milk at
</pre>