Markov chain text generator: Difference between revisions

Content deleted Content added
Added Fennel.
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 49: Line 49:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F makerule(data, context)
<syntaxhighlight lang="11l">F makerule(data, context)
‘Make a rule dict for given data.’
‘Make a rule dict for given data.’
[String = [String]] rule
[String = [String]] rule
Line 95: Line 95:
cur_line_size += word.len + 1
cur_line_size += word.len + 1
print(word, end' ‘ ’)
print(word, end' ‘ ’)
print()</lang>
print()</syntaxhighlight>


{{out}}
{{out}}
Line 108: Line 108:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.IO;
Line 178: Line 178:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>axes,' said the Duchess, who seemed ready to agree to everything that Alice said; 'there's a large mustard-mine near here. And the moral of that is-Be what you would seem to be-or if you'd like it put more simply-Never imagine yourself not to be alive. Those creatures frightened me so badly that I cannot keep my promises. I think you are wrong to want a heart. It makes most people unhappy. If you only knew it, you are in luck not to have a heart. I have played Wizard for so many years that I may be as other men are. Why should I give you fair warning,' shouted the Queen, stamping on the ground as she spoke; 'either you or your head must be off, and that the pail was lying in several small pieces, while the poor milkmaid had a nick in her left elbow. There! cried the milkmaid angrily. See what you have done! she screamed. In a minute I shall melt away. I'm very sorry, returned Dorothy. Please forgive us. But the pretty milkmaid was much too wise not to swim, and he was obliged to call to her to help him up again. Why didn't</pre>
<pre>axes,' said the Duchess, who seemed ready to agree to everything that Alice said; 'there's a large mustard-mine near here. And the moral of that is-Be what you would seem to be-or if you'd like it put more simply-Never imagine yourself not to be alive. Those creatures frightened me so badly that I cannot keep my promises. I think you are wrong to want a heart. It makes most people unhappy. If you only knew it, you are in luck not to have a heart. I have played Wizard for so many years that I may be as other men are. Why should I give you fair warning,' shouted the Queen, stamping on the ground as she spoke; 'either you or your head must be off, and that the pail was lying in several small pieces, while the poor milkmaid had a nick in her left elbow. There! cried the milkmaid angrily. See what you have done! she screamed. In a minute I shall melt away. I'm very sorry, returned Dorothy. Please forgive us. But the pretty milkmaid was much too wise not to swim, and he was obliged to call to her to help him up again. Why didn't</pre>
Line 185: Line 185:
In this implementation there is no repeated suffixes!
In this implementation there is no repeated suffixes!


<lang cpp>
<syntaxhighlight lang="cpp">
#include <ctime>
#include <ctime>
#include <iostream>
#include <iostream>
Line 258: Line 258:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 279: Line 279:


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang ruby>class Markov(N)
<syntaxhighlight lang="ruby">class Markov(N)
@dictionary = Hash(StaticArray(String, N), Array(String)).new { [] of String }
@dictionary = Hash(StaticArray(String, N), Array(String)).new { [] of String }


Line 321: Line 321:
chain = Markov(3).new
chain = Markov(3).new
chain.parse("alice_oz.txt")
chain.parse("alice_oz.txt")
puts chain.generate(200)</lang>
puts chain.generate(200)</syntaxhighlight>
{{out}}
{{out}}
<pre>tired. I'll tell you my story. So they sat down and listened while he told the following story: I was born in Omaha-- Why, that isn't very far from Kansas! cried Dorothy. No, but I am sure we shall sometime come to some place. But day by day passed away, and they found themselves in the midst of a strange people, who, seeing me come from the big Head; so she took courage and answered: I am Dorothy, answered the girl, if he will see you, said the soldier who had taken her message to the Wizard, although he does not like to go back to Oz, and claim his promise. Yes, said the Woodman, so get behind me and I will tell you my story. So they sat down upon the bank and gazed wistfully at the Scarecrow until a Stork flew by, who, upon seeing them, stopped to rest at the water's edge. Who are you and where are you going? My name is Dorothy, said the girl, let us go. And she handed the basket to the Scarecrow. There were no fences at all by the roadside now, and the land was rough and untilled. Toward evening</pre>
<pre>tired. I'll tell you my story. So they sat down and listened while he told the following story: I was born in Omaha-- Why, that isn't very far from Kansas! cried Dorothy. No, but I am sure we shall sometime come to some place. But day by day passed away, and they found themselves in the midst of a strange people, who, seeing me come from the big Head; so she took courage and answered: I am Dorothy, answered the girl, if he will see you, said the soldier who had taken her message to the Wizard, although he does not like to go back to Oz, and claim his promise. Yes, said the Woodman, so get behind me and I will tell you my story. So they sat down upon the bank and gazed wistfully at the Scarecrow until a Stork flew by, who, upon seeing them, stopped to rest at the water's edge. Who are you and where are you going? My name is Dorothy, said the girl, let us go. And she handed the basket to the Scarecrow. There were no fences at all by the roadside now, and the land was rough and untilled. Toward evening</pre>
Line 327: Line 327:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang D>import std.file;
<syntaxhighlight lang="d">import std.file;
import std.random;
import std.random;
import std.range;
import std.range;
Line 377: Line 377:
void main() {
void main() {
writeln(markov("alice_oz.txt", 3, 200));
writeln(markov("alice_oz.txt", 3, 200));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>neighbour to tell him. 'A nice muddle their slates'll be in before the trial's over!' thought Alice. One of the jurors had a pencil that squeaked. This of course, Alice could not think of any good reason, and as the monster crawls through the forest he seizes an animal with a leg and drags it to his ear. Alice considered a little, and then said 'The fourth.' 'Two days wrong!' sighed the Hatter. 'I deny it!' said the March Hare. 'Sixteenth,' added the Dormouse. 'Write that down,' the King replied. Here the other guinea-pig cheered, and was immediately suppressed by the officers of the court, all dressed in green clothes and had greenish skins. They looked at Dorothy again. Why should I do this for you? asked the Scarecrow. You are quite welcome to take my head off, as long as the tiger had said, and its body covered with coarse black hair. It had a great longing to have for her own the Silver Shoes had fallen off in her flight through the air, and on the morning of the second day I awoke and found the oil-can, and then she had to stop and untwist it. After a</pre>
<pre>neighbour to tell him. 'A nice muddle their slates'll be in before the trial's over!' thought Alice. One of the jurors had a pencil that squeaked. This of course, Alice could not think of any good reason, and as the monster crawls through the forest he seizes an animal with a leg and drags it to his ear. Alice considered a little, and then said 'The fourth.' 'Two days wrong!' sighed the Hatter. 'I deny it!' said the March Hare. 'Sixteenth,' added the Dormouse. 'Write that down,' the King replied. Here the other guinea-pig cheered, and was immediately suppressed by the officers of the court, all dressed in green clothes and had greenish skins. They looked at Dorothy again. Why should I do this for you? asked the Scarecrow. You are quite welcome to take my head off, as long as the tiger had said, and its body covered with coarse black hair. It had a great longing to have for her own the Silver Shoes had fallen off in her flight through the air, and on the morning of the second day I awoke and found the oil-can, and then she had to stop and untwist it. After a</pre>


=={{header|F Sharp}}==
=={{header|F Sharp}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
let ngram len source i = source |> List.skip i |> List.take len
let ngram len source i = source |> List.skip i |> List.take len
let trailing len source = ngram len source (Seq.length source - len)
let trailing len source = ngram len source (Seq.length source - len)
Line 420: Line 420:
|> List.ofSeq
|> List.ofSeq
|> markov 2 100
|> markov 2 100
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 427: Line 427:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
{{works with|Factor|0.99 2019-10-06}}
<lang factor>USING: assocs fry grouping io io.encodings.ascii io.files kernel
<syntaxhighlight lang="factor">USING: assocs fry grouping io io.encodings.ascii io.files kernel
make math random sequences splitting ;
make math random sequences splitting ;


Line 445: Line 445:
] { } make 2nip " " join print ;
] { } make 2nip " " join print ;


"alice_oz.txt" 3 200 markov</lang>
"alice_oz.txt" 3 200 markov</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 452: Line 452:


=={{header|Fennel}}==
=={{header|Fennel}}==
<lang Fennel>(fn string.split [self sep]
<syntaxhighlight lang="fennel">(fn string.split [self sep]
"Tokenize a string using the given separator."
"Tokenize a string using the given separator."
(let [pattern (string.format "([^%s]+)" sep)
(let [pattern (string.format "([^%s]+)" sep)
Line 527: Line 527:


;;; Test the generator.
;;; Test the generator.
(print (make-string (make-rule (read-file "alice_oz.txt") 2) 300))</lang>
(print (make-string (make-rule (read-file "alice_oz.txt") 2) 300))</syntaxhighlight>


{{out}}
{{out}}
Line 546: Line 546:


=={{header|Go}}==
=={{header|Go}}==
<lang Go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 735: Line 735:
}
}
return append(slice, value)
return append(slice, value)
}</lang>
}</syntaxhighlight>
{{out|input=-n=3 -runs=3 -words=12 -capital -sentence}}
{{out|input=-n=3 -runs=3 -words=12 -capital -sentence}}
<pre>Alice could speak again. The Mock Turtle went on at last, more calmly, though still sobbing a little now and then, and holding it to his ear.
<pre>Alice could speak again. The Mock Turtle went on at last, more calmly, though still sobbing a little now and then, and holding it to his ear.
Line 746: Line 746:
This seems to be reasonably close to the specification:
This seems to be reasonably close to the specification:


<lang J>require'web/gethttp'
<syntaxhighlight lang="j">require'web/gethttp'


setstats=:dyad define
setstats=:dyad define
Line 779: Line 779:
end.
end.
;:inv phrase { words
;:inv phrase { words
)</lang>
)</syntaxhighlight>


{{out}}<lang J> 2 1 50 setstats 'http://paulo-jorente.de/text/alice_oz.txt'
{{out}}<syntaxhighlight lang="j"> 2 1 50 setstats 'http://paulo-jorente.de/text/alice_oz.txt'
genphrase''
genphrase''
got in as alice alice
got in as alice alice
Line 787: Line 787:
perhaps even alice
perhaps even alice
genphrase''
genphrase''
pretty milkmaid alice</lang>
pretty milkmaid alice</syntaxhighlight>


And, using 8 word suffixes (but limiting results to a bit over 50 words):
And, using 8 word suffixes (but limiting results to a bit over 50 words):


{{out}}<lang J> 2 8 50 setstats 'http://paulo-jorente.de/text/alice_oz.txt'
{{out}}<syntaxhighlight lang="j"> 2 8 50 setstats 'http://paulo-jorente.de/text/alice_oz.txt'
genphrase''
genphrase''
added it alice was beginning to get very tired of this i vote the young lady tells us alice was beginning to get very tired of being such a tiny little thing it did not take her long to find the one paved with yellow bricks within a short time
added it alice was beginning to get very tired of this i vote the young lady tells us alice was beginning to get very tired of being such a tiny little thing it did not take her long to find the one paved with yellow bricks within a short time
Line 797: Line 797:
the raft through the water they got along quite well alice was beginning to get very tired of this i vote the young lady tells us alice was beginning to get very tired of being all alone here as she said this last word two or three times over to
the raft through the water they got along quite well alice was beginning to get very tired of this i vote the young lady tells us alice was beginning to get very tired of being all alone here as she said this last word two or three times over to
genphrase''
genphrase''
gown that alice was beginning to get very tired of sitting by her sister on the bank and alice was beginning to get very tired of being such a tiny little thing it did so indeed and much sooner than she had accidentally upset the week before oh i beg</lang>
gown that alice was beginning to get very tired of sitting by her sister on the bank and alice was beginning to get very tired of being such a tiny little thing it did so indeed and much sooner than she had accidentally upset the week before oh i beg</syntaxhighlight>


(see talk page for discussion of odd line wrapping with some versions of Safari)
(see talk page for discussion of odd line wrapping with some versions of Safari)
Line 804: Line 804:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{works with|Java|8}}
{{works with|Java|8}}
<lang Java>import java.io.IOException;
<syntaxhighlight lang="java">import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Path;
Line 867: Line 867:
System.out.println(markov("alice_oz.txt", 3, 200));
System.out.println(markov("alice_oz.txt", 3, 200));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Emerald City is? Certainly, answered the Queen; but it is a lucky thing I am not, for my mouth is only painted, and if I ever get my heart? Or I my courage? asked the Lion in surprise, as he watched her pick up the Scarecrow and said: Come with me, for Oz has made me its ruler and the people gazed upon it with much curiosity. The Tin Woodman appeared to think deeply for a moment. Then he said, The Winkies were sorry to have them go, and they had grown so large in the last few minutes that she wasn't a bit afraid of interrupting him,) 'I'll give him sixpence. I don't believe there's an atom of meaning in it.' The jury all wrote down on their slates, and then added them up, and reduced the answer to it?' said the Mock Turtle. 'No, no! The adventures first,' said the Gryphon hastily. 'Go on with the next verse,' the Gryphon repeated impatiently: 'it begins I passed by his garden, and marked, with one eye,</pre>
<pre>Emerald City is? Certainly, answered the Queen; but it is a lucky thing I am not, for my mouth is only painted, and if I ever get my heart? Or I my courage? asked the Lion in surprise, as he watched her pick up the Scarecrow and said: Come with me, for Oz has made me its ruler and the people gazed upon it with much curiosity. The Tin Woodman appeared to think deeply for a moment. Then he said, The Winkies were sorry to have them go, and they had grown so large in the last few minutes that she wasn't a bit afraid of interrupting him,) 'I'll give him sixpence. I don't believe there's an atom of meaning in it.' The jury all wrote down on their slates, and then added them up, and reduced the answer to it?' said the Mock Turtle. 'No, no! The adventures first,' said the Gryphon hastily. 'Go on with the next verse,' the Gryphon repeated impatiently: 'it begins I passed by his garden, and marked, with one eye,</pre>
Line 874: Line 874:
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>function markovtext(txt::AbstractString, klen::Integer, maxchlen::Integer)
<syntaxhighlight lang="julia">function markovtext(txt::AbstractString, klen::Integer, maxchlen::Integer)
words = matchall(r"\w+", txt)
words = matchall(r"\w+", txt)
dict = Dict()
dict = Dict()
Line 899: Line 899:


txt = readstring(download("http://paulo-jorente.de/text/alice_oz.txt"))
txt = readstring(download("http://paulo-jorente.de/text/alice_oz.txt"))
println(markovtext(txt, 3, 200))</lang>
println(markovtext(txt, 3, 200))</syntaxhighlight>


{{out}}
{{out}}
Line 906: Line 906:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.3.10
<syntaxhighlight lang="scala">// version 1.3.10


import java.io.File
import java.io.File
Line 944: Line 944:
fun main() {
fun main() {
println(markov("alice_oz.txt", 3, 100))
println(markov("alice_oz.txt", 3, 100))
}</lang>
}</syntaxhighlight>


Sample output:
Sample output:
Line 957: Line 957:
Computes keys of all lengths <= N. During text generation, if a key does not exist in the dictionary, the first (least recent) word is removed, until a key is found (if no key at all is found, the program terminates).
Computes keys of all lengths <= N. During text generation, if a key does not exist in the dictionary, the first (least recent) word is removed, until a key is found (if no key at all is found, the program terminates).


<lang Lua>local function pick(t)
<syntaxhighlight lang="lua">local function pick(t)
local i = math.ceil(math.random() * #t)
local i = math.ceil(math.random() * #t)
return t[i]
return t[i]
Line 1,044: Line 1,044:
end
end
io.write('\n')
io.write('\n')
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,067: Line 1,067:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang="nim">
<lang Nim>
import random, sequtils, strutils, tables
import random, sequtils, strutils, tables


Line 1,106: Line 1,106:
if idx < 0: idx = result.high
if idx < 0: idx = result.high
echo result[start..idx]
echo result[start..idx]
start = idx + 1</lang>
start = idx + 1</syntaxhighlight>


{{out}}
{{out}}
Line 1,117: Line 1,117:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;


Line 1,155: Line 1,155:
}
}


print join(' ', @chain) . "\n";</lang>
print join(' ', @chain) . "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>Alice was thoroughly puzzled. 'Does the boots and shoes,' the Gryphon whispered in a fight with another hedgehog, which seemed to extend to the South Country? To see the Great Oz was ready to sink into the garden, where Alice could see or feel was the end of you, as she chose, she ran after her. 'I've something important to say!' This sounded promising, certainly: Alice turned and walked through the forest very thick on this side, and it seemed to Alice as she chose, she ran out and shone brightly. So they all spoke at once, I'll chop it down, and the Dormouse sulkily remarked, 'If you please, sir-' The Rabbit started violently, dropped the white kid gloves and the four travelers walked up to the Land of Oz in less than no time to think about stopping herself before she made a dreadfully ugly child: but it is a man,' said the Stork, as she spoke. 'I must be a person of authority among them, called out, 'Sit down, all of them expected to come out of breath, and till the Pigeon in a sorrowful tone; 'at least there's no use to me they flew away with me,' thought Alice,</pre>
<pre>Alice was thoroughly puzzled. 'Does the boots and shoes,' the Gryphon whispered in a fight with another hedgehog, which seemed to extend to the South Country? To see the Great Oz was ready to sink into the garden, where Alice could see or feel was the end of you, as she chose, she ran after her. 'I've something important to say!' This sounded promising, certainly: Alice turned and walked through the forest very thick on this side, and it seemed to Alice as she chose, she ran out and shone brightly. So they all spoke at once, I'll chop it down, and the Dormouse sulkily remarked, 'If you please, sir-' The Rabbit started violently, dropped the white kid gloves and the four travelers walked up to the Land of Oz in less than no time to think about stopping herself before she made a dreadfully ugly child: but it is a man,' said the Stork, as she spoke. 'I must be a person of authority among them, called out, 'Sit down, all of them expected to come out of breath, and till the Pigeon in a sorrowful tone; 'at least there's no use to me they flew away with me,' thought Alice,</pre>
Line 1,161: Line 1,161:
=={{header|Phix}}==
=={{header|Phix}}==
This was fun! (easy, but fun)
This was fun! (easy, but fun)
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"now he is gone she said he is gone for good"</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"now he is gone she said he is gone for good"</span>
Line 1,197: Line 1,197:
<span style="color: #0000FF;">?</span><span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
from the alice_oz.txt file:
from the alice_oz.txt file:
Line 1,215: Line 1,215:
=={{header|PHP}}==
=={{header|PHP}}==


<lang PHP><?php
<syntaxhighlight lang="php"><?php


// Automated text generator using markov chain
// Automated text generator using markov chain
Line 1,283: Line 1,283:
);
);


echo wordwrap($text, 100, PHP_EOL) . PHP_EOL;</lang>
echo wordwrap($text, 100, PHP_EOL) . PHP_EOL;</syntaxhighlight>


{{out}}
{{out}}
Line 1,308: Line 1,308:
Usage: markov.py source.txt context length
Usage: markov.py source.txt context length


<lang python>import random, sys
<syntaxhighlight lang="python">import random, sys


def makerule(data, context):
def makerule(data, context):
Line 1,352: Line 1,352:
rule = makerule(data, int(sys.argv[2]))
rule = makerule(data, int(sys.argv[2]))
string = makestring(rule, int(sys.argv[3]))
string = makestring(rule, int(sys.argv[3]))
print(string)</lang>
print(string)</syntaxhighlight>


{{out}}
{{out}}
Line 1,376: Line 1,376:


{{Works with|Python|3.7}}
{{Works with|Python|3.7}}
<lang python>'''Markov chain text generation'''
<syntaxhighlight lang="python">'''Markov chain text generation'''


from os.path import (expanduser)
from os.path import (expanduser)
Line 1,507: Line 1,507:


if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Markov chain text generation:
<pre>Markov chain text generation:
Line 1,529: Line 1,529:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define (pick xs) (list-ref xs (random (length xs))))
(define (pick xs) (list-ref xs (random (length xs))))
Line 1,555: Line 1,555:
(values (append (rest prefix) (list suffix)) (cons suffix out))))
(values (append (rest prefix) (list suffix)) (cons suffix out))))


(markov "alice_oz.txt" 3 300)</lang>
(markov "alice_oz.txt" 3 300)</syntaxhighlight>


{{out}}
{{out}}
Line 1,565: Line 1,565:
(formerly Perl 6)
(formerly Perl 6)
{{Works with|rakudo|2017-01}}
{{Works with|rakudo|2017-01}}
<lang perl6>unit sub MAIN ( :$text=$*IN, :$n=2, :$words=100, );
<syntaxhighlight lang="raku" line>unit sub MAIN ( :$text=$*IN, :$n=2, :$words=100, );


sub add-to-dict ( $text, :$n=2, ) {
sub add-to-dict ( $text, :$n=2, ) {
Line 1,579: Line 1,579:


put @generated-text.head: $words;
put @generated-text.head: $words;
</syntaxhighlight>
</lang>
<pre>>raku markov.raku <alice_oz.txt --n=3 --words=200
<pre>>raku markov.raku <alice_oz.txt --n=3 --words=200
Scarecrow. He can't hurt the straw. Do let me carry that basket for you. I shall not mind it, for I can't get tired. I'll tell you what I think, said the little man. Give me two or three pairs of tiny white kid gloves: she took up the fan and gloves, and, as the Lory positively refused to tell its age, there was no use in saying anything more till the Pigeon had finished. 'As if it wasn't trouble enough hatching the eggs,' said the Pigeon; 'but I must be very careful. When Oz gives me a heart of course I needn't mind so much. They were obliged to camp out that night under a large tree in the wood,' continued the Pigeon, raising its voice to a whisper. He is more powerful than they themselves, they would surely have destroyed me. As it was, I lived in deadly fear of them for many years; so you can see for yourself. Indeed, a jolly little clown came walking toward them, and Dorothy could see that in spite of all her coaxing. Hardly knowing what she did, she picked up a little bit of stick, and held it out to
Scarecrow. He can't hurt the straw. Do let me carry that basket for you. I shall not mind it, for I can't get tired. I'll tell you what I think, said the little man. Give me two or three pairs of tiny white kid gloves: she took up the fan and gloves, and, as the Lory positively refused to tell its age, there was no use in saying anything more till the Pigeon had finished. 'As if it wasn't trouble enough hatching the eggs,' said the Pigeon; 'but I must be very careful. When Oz gives me a heart of course I needn't mind so much. They were obliged to camp out that night under a large tree in the wood,' continued the Pigeon, raising its voice to a whisper. He is more powerful than they themselves, they would surely have destroyed me. As it was, I lived in deadly fear of them for many years; so you can see for yourself. Indeed, a jolly little clown came walking toward them, and Dorothy could see that in spite of all her coaxing. Hardly knowing what she did, she picked up a little bit of stick, and held it out to
Line 1,585: Line 1,585:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program produces a Markov chain text from a training text using a text generator.*/
<syntaxhighlight lang="rexx">/*REXX program produces a Markov chain text from a training text using a text generator.*/
parse arg ord fin iFID seed . /*obtain optional arguments from the CL*/
parse arg ord fin iFID seed . /*obtain optional arguments from the CL*/
if ord=='' | ord=="," then ord= 3 /*Not specified? Then use the default.*/
if ord=='' | ord=="," then ord= 3 /*Not specified? Then use the default.*/
Line 1,622: Line 1,622:
end /*k*/
end /*k*/
if g\=='' then say g /*There any residual? Then display it.*/
if g\=='' then say g /*There any residual? Then display it.*/
return /* [↑] limits G to terminal width.*/</lang>
return /* [↑] limits G to terminal width.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,643: Line 1,643:
{{trans|Python}}
{{trans|Python}}
{{works with|Rust|1.32 nightly (2018 edition)}}
{{works with|Rust|1.32 nightly (2018 edition)}}
<lang rust>use std::env;
<syntaxhighlight lang="rust">use std::env;
use std::fs;
use std::fs;


Line 1,723: Line 1,723:
sample(&mut rng, max, 1).into_vec()[0]
sample(&mut rng, max, 1).into_vec()[0]
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,731: Line 1,731:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>func build_dict (n, words) {
<syntaxhighlight lang="ruby">func build_dict (n, words) {
var dict = Hash()
var dict = Hash()
words.each_cons(n+1, {|*prefix|
words.each_cons(n+1, {|*prefix|
Line 1,758: Line 1,758:
}
}


say chain.join(' ')</lang>
say chain.join(' ')</syntaxhighlight>


{{out}}
{{out}}
Line 1,768: Line 1,768:
{{trans|Python}}
{{trans|Python}}
{{works with|Swift|4.2}}
{{works with|Swift|4.2}}
<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


func makeRule(input: String, keyLength: Int) -> [String: [String]] {
func makeRule(input: String, keyLength: Int) -> [String: [String]] {
Line 1,812: Line 1,812:
let str = makeString(rule: rule, length: 300)
let str = makeString(rule: rule, length: 300)


print(str)</lang>
print(str)</syntaxhighlight>


{{out}}
{{out}}
Line 1,821: Line 1,821:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-str}}
{{libheader|Wren-str}}
<lang ecmascript>import "io" for File
<syntaxhighlight lang="ecmascript">import "io" for File
import "random" for Random
import "random" for Random
import "/str" for Strs
import "/str" for Strs
Line 1,851: Line 1,851:
}
}


System.print(markov.call("alice_oz.txt", 3, 100))</lang>
System.print(markov.call("alice_oz.txt", 3, 100))</syntaxhighlight>


{{out}}
{{out}}