Markov chain text generator: Difference between revisions

(Minor edits to my old Python implementation.)
 
(9 intermediate revisions by 6 users not shown)
Line 45:
pretty random text but...) and create output text also in any length.
Probably you want to call your program passing those numbers as parameters. Something like: markov( "text.txt", 3, 300 )
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F makerule(data, context)
‘Make a rule dict for given data.’
[String = [String]] rule
V words = data.split(‘ ’)
V index = context
 
L(word) words[index ..]
V key = (words[index - context .< index]).join(‘ ’)
I key C rule
rule[key].append(word)
E
rule[key] = [word]
index++
 
R rule
 
F makestring(rule, length)
‘Use a given rule to make a string.’
V oldwords = random:choice(Array(rule.keys())).split(‘ ’)
V string = oldwords.join(‘ ’)‘ ’
 
L 0 .< length
X.try
V key = oldwords.join(‘ ’)
V newword = random:choice(rule[key])
string ‘’= newword‘ ’
 
L(word) 0 .< oldwords.len
oldwords[word] = oldwords[(word + 1) % oldwords.len]
oldwords.last = newword
 
X.catch KeyError
R string
R string
 
V data = File(‘alice_oz.txt’).read()
V rule = makerule(data, 3)
V string = makestring(rule, 100)
V max_line_size = 100
V cur_line_size = 0
L(word) string.split(‘ ’)
I cur_line_size + word.len >= max_line_size
print()
cur_line_size = 0
cur_line_size += word.len + 1
print(word, end' ‘ ’)
print()</syntaxhighlight>
 
{{out}}
<pre>
awakened from his sleep, and seeing all these mice around him he gave one bark of delight and
jumped right into the middle of a river. I am afraid I shall never have any brains, after all! Down
the stream the raft floated, and the poor little thing sat down and looked along the passage into
the loveliest garden you ever saw. How she longed to get out again. Suddenly she came upon a little
three-legged table, all made of china, even to their clothes, and were so small that he dropped
almost as many as he put in the basket. Then, being
</pre>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.IO;
Line 117 ⟶ 178:
}
}
}</langsyntaxhighlight>
{{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>
Line 124 ⟶ 185:
In this implementation there is no repeated suffixes!
 
<langsyntaxhighlight lang="cpp">
#include <ctime>
#include <iostream>
Line 197 ⟶ 258:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 218 ⟶ 279:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">class Markov(N)
@dictionary = Hash(StaticArray(String, N), Array(String)).new { [] of String }
 
Line 260 ⟶ 321:
chain = Markov(3).new
chain.parse("alice_oz.txt")
puts chain.generate(200)</langsyntaxhighlight>
{{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>
Line 266 ⟶ 327:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.file;
import std.random;
import std.range;
Line 316 ⟶ 377:
void main() {
writeln(markov("alice_oz.txt", 3, 200));
}</langsyntaxhighlight>
{{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>
 
=={{header|F Sharp}}==
<langsyntaxhighlight lang="fsharp">
let ngram len source i = source |> List.skip i |> List.take len
let trailing len source = ngram len source (Seq.length source - len)
Line 359 ⟶ 420:
|> List.ofSeq
|> markov 2 100
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 366 ⟶ 427:
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
<langsyntaxhighlight lang="factor">USING: assocs fry grouping io io.encodings.ascii io.files kernel
make math random sequences splitting ;
 
Line 384 ⟶ 445:
] { } make 2nip " " join print ;
 
"alice_oz.txt" 3 200 markov</langsyntaxhighlight>
{{out}}
<pre>
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}}==
<syntaxhighlight 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))</syntaxhighlight>
 
{{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}}==
<langsyntaxhighlight Golang="go">package main
 
import (
Line 580 ⟶ 735:
}
return append(slice, value)
}</langsyntaxhighlight>
{{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.
Line 591 ⟶ 746:
This seems to be reasonably close to the specification:
 
<langsyntaxhighlight Jlang="j">require'web/gethttp'
 
setstats=:dyad define
Line 624 ⟶ 779:
end.
;:inv phrase { words
)</langsyntaxhighlight>
 
{{out}}<langsyntaxhighlight Jlang="j"> 2 1 50 setstats 'http://paulo-jorente.de/text/alice_oz.txt'
genphrase''
got in as alice alice
Line 632 ⟶ 787:
perhaps even alice
genphrase''
pretty milkmaid alice</langsyntaxhighlight>
 
And, using 8 word suffixes (but limiting results to a bit over 50 words):
 
{{out}}<langsyntaxhighlight Jlang="j"> 2 8 50 setstats 'http://paulo-jorente.de/text/alice_oz.txt'
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
Line 642 ⟶ 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
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</langsyntaxhighlight>
 
(see talk page for discussion of odd line wrapping with some versions of Safari)
Line 649 ⟶ 804:
{{trans|Kotlin}}
{{works with|Java|8}}
<langsyntaxhighlight Javalang="java">import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Line 712 ⟶ 867:
System.out.println(markov("alice_oz.txt", 3, 200));
}
}</langsyntaxhighlight>
{{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>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
In the following, the initial word phrase is chosen so that it might
look like the beginning of a sentence.
 
Since jq does not include a PRN generator, an external source of entropy such as /dev/urandom is assumed.
A suitable invocation of jq would be along the lines of:
<pre>
< /dev/urandom tr -cd '0-9' | fold -w 1 | $jq -Rcnr --rawfile text alice_oz.txt -f mc.jq
</pre>
<syntaxhighlight lang="jq">
# If using gojq:
def keys_unsorted: keys;
 
 
### Pseuo-random numbers
 
# Output: a prn in range(0;$n) where $n is `.`
def prn:
if . == 1 then 0
else . as $n
| ([1, (($n-1)|tostring|length)]|max) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def sample:
if length == 0 # e.g. null or []
then null
else .[length|prn]
end;
 
 
### The tasks
 
# The words in $text
def words:
[$text | sub("\\s*$";"") | splits(" *")];
 
def markov($keySize; $outputSize):
if $keySize < 1
then "Key size can't be less than 1" | error
else words as $words
| ($words|length|length) as $wordCount
| if $outputSize < $keySize or $outputSize > $wordCount
then "Requested output size is greater than the number of words in the given text" | error
else
(reduce range(0; 1+$wordCount - $keySize) as $i ( {};
($words[$i:$i + $keySize] | join(" ")) as $prefix
| (if ($i + $keySize < $wordCount) then $words[$i + $keySize] else "" end) as $suffix
| .[$prefix] += [$suffix] )) as $dict
| { output: [] }
| ($dict|keys_unsorted) as $keys
# Start with a capitalized word, possibly following a quotation mark:
| .prefix = ($keys | map(select(test("^['A-Z][^.]*$"))) | sample)
| .output += (.prefix|split(" "))
| last(label $out
| foreach range(1; 1+$wordCount) as $n (.;
($dict[.prefix]|sample) as $nextWord
| if $nextWord | length == 0
then ., break $out
else .output += [$nextWord]
| if (.output|length) >= $outputSize
then ., break $out
else .prefix = (.output[$n:$n + $keySize] | join(" "))
end
end ))
| .output[:$outputSize] | join(" ")
end
end ;
 
markov(3; 100)
</syntaxhighlight>
{{output}}
<pre>
I am made of straw and cannot be easily damaged. There are worse
things in the world. Dorothy did not know this, and was full of smoke
from one end to the truck. Of course the truck was all ready for
them. They came from all directions, and there were thousands of them:
big mice and little mice and middle-sized mice; and each one of them
found himself lodged in a very humble tone, going down on one knee as
he spoke, and added 'It isn't a letter, after all: it's a set of
verses.' 'Are they in the prisoner's
</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function markovtext(txt::AbstractString, klen::Integer, maxchlen::Integer)
words = matchall(r"\w+", txt)
dict = Dict()
Line 744 ⟶ 988:
 
txt = readstring(download("http://paulo-jorente.de/text/alice_oz.txt"))
println(markovtext(txt, 3, 200))</langsyntaxhighlight>
 
{{out}}
Line 751 ⟶ 995:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.3.10
 
import java.io.File
Line 789 ⟶ 1,033:
fun main() {
println(markov("alice_oz.txt", 3, 100))
}</langsyntaxhighlight>
 
Sample output:
Line 802 ⟶ 1,046:
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).
 
<langsyntaxhighlight Lualang="lua">local function pick(t)
local i = math.ceil(math.random() * #t)
return t[i]
Line 889 ⟶ 1,133:
end
io.write('\n')
</syntaxhighlight>
</lang>
 
{{out}}
Line 909 ⟶ 1,153:
she comes, to-' At this moment Five, who had been greatly interested in
</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">
import random, sequtils, strutils, tables
 
proc markov(filePath: string; keySize, outputSize: Positive): string =
 
let words = filePath.readFile().strip(false, true).splitWhitespace()
doAssert outputSize in keySize..words.len, "Output size is out of range"
 
var dict: Table[string, seq[string]]
 
for i in 0..(words.len - keySize):
let prefix = words[i..<(i+keySize)].join(" ")
let suffix = if i + keySize < words.len: words[i + keySize] else: ""
dict.mgetOrPut(prefix, @[]).add suffix
 
var output: seq[string]
var prefix = toSeq(dict.keys).sample()
output.add prefix.split(' ')
 
for n in 1..words.len:
let nextWord = dict[prefix].sample()
if nextWord.len == 0: break
output.add nextWord
if output.len >= outputSize: break
prefix = output[n..<(n + keySize)].join(" ")
 
result = output[0..<outputSize].join(" ")
 
 
const MaxLineSize = 100
 
randomize()
let result = markov("alice_oz.txt", 3, 100)
var start = 0
while start < result.len:
var idx = if start + MaxLineSize <= result.high: result.rfind(' ', start, start + MaxLineSize)
else: result.high
if idx < 0: idx = result.high
echo result[start..idx]
start = idx + 1</syntaxhighlight>
 
{{out}}
<pre>ground was carpeted with them. There were big yellow and white and blue and purple blossoms, besides
great clusters of scarlet poppies, which were so brilliant in color they almost dazzled Dorothy's
eyes. Aren't they beautiful? the girl asked, as she breathed in the spicy scent of the flowers, and
here they laid her gently on the forehead. Where her lips touched the girl they left a round,
shining mark, as Dorothy found out soon after. The road to the City of Emeralds. Perhaps Oz will
help you. Where is this great spider of yours now? asked the Tin Woodman.</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 949 ⟶ 1,244:
}
 
print join(' ', @chain) . "\n";</langsyntaxhighlight>
{{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>
Line 955 ⟶ 1,250:
=={{header|Phix}}==
This was fun! (easy, but fun)
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer fn = open("alice_oz.txt","rb")
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
string text = get_text(fn)
<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>
close(fn)
<span style="color: #004080;">string</span> <span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">s</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"alice_oz.txt"</span><span style="color: #0000FF;">))</span>
sequence words = split(text)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">)</span>
 
function markov(integer n, m)
<span style="color: #008080;">function</span> <span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
integer dict = new_dict(), ki
<span style="color: #004080;">integer</span> <span style="color: #000000;">dict</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">ki</span>
sequence key, data, res
<span style="color: #004080;">sequence</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">res</span>
string suffix
<span style="color: #004080;">string</span> <span style="color: #000000;">suffix</span>
for i=1 to length(words)-n do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
key = words[i..i+n-1]
<span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
suffix = words[i+n]
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span>
ki = getd_index(key,dict)
<span style="color: #000000;">ki</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
if ki=0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">ki</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
data = {}
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
else
<span data style="color: getd_by_index(ki,dict)#008080;">else</span>
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ki</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
setd(key,append(data,suffix),dict)
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">),</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">),</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
integer start = rand(length(words)-n)
<span style="color: #004080;">integer</span> <span style="color: #000000;">start</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
key = words[start..start+n-1]
<span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">start</span><span style="color: #0000FF;">..</span><span style="color: #000000;">start</span><span style="color: #0000FF;">+</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
res = key
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
for i=1 to m do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">m</span> <span style="color: #008080;">do</span>
ki = getd_index(key,dict)
<span style="color: #000000;">ki</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
if ki=0 then exit end if
<span style="color: #008080;">if</span> <span style="color: #000000;">ki</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
data = getd_by_index(ki,dict)
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ki</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
suffix = data[rand(length(data))]
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">))]</span>
res = append(res,suffix)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">)</span>
key = append(key[2..$],suffix)
<span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$],</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return join(res)
<span style="color: #008080;">return</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
?markov(2,100)</lang>
<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>
<!--</syntaxhighlight>-->
{{out}}
from the alice_oz.txt file:
Line 998 ⟶ 1,295:
I beg your pardon!\' cried Alice hastily, afraid that she was shrinking rapidly; so she felt lonely among all these strange people. Her
tears seemed to Alice a good dinner."
</pre>
Under pwa/p2js with more limited input the output could be anything between and beyond
<pre>
`gone for good`
`said he is gone she said he is gone she said he is gone she said he is gone she said he is gone for good`
</pre>
 
=={{header|PHP}}==
 
<langsyntaxhighlight PHPlang="php"><?php
 
// Automated text generator using markov chain
Line 1,070 ⟶ 1,372:
);
 
echo wordwrap($text, 100, PHP_EOL) . PHP_EOL;</langsyntaxhighlight>
 
{{out}}
Line 1,095 ⟶ 1,397:
Usage: markov.py source.txt context length
 
<langsyntaxhighlight lang="python">import random, sys
 
def makerule(data, context):
Line 1,139 ⟶ 1,441:
rule = makerule(data, int(sys.argv[2]))
string = makestring(rule, int(sys.argv[3]))
print(string)</langsyntaxhighlight>
 
{{out}}
Line 1,163 ⟶ 1,465:
 
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Markov chain text generation'''
 
from os.path import (expanduser)
Line 1,294 ⟶ 1,596:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Markov chain text generation:
Line 1,316 ⟶ 1,618:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define (pick xs) (list-ref xs (random (length xs))))
Line 1,342 ⟶ 1,644:
(values (append (rest prefix) (list suffix)) (cons suffix out))))
 
(markov "alice_oz.txt" 3 300)</langsyntaxhighlight>
 
{{out}}
Line 1,352 ⟶ 1,654:
(formerly Perl 6)
{{Works with|rakudo|2017-01}}
<syntaxhighlight lang="raku" perl6line>unit sub MAIN ( :$text=$*IN, :$n=2, :$words=100, );
 
sub add-to-dict ( $text, :$n=2, ) {
Line 1,366 ⟶ 1,668:
 
put @generated-text.head: $words;
</syntaxhighlight>
</lang>
<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
Line 1,372 ⟶ 1,674:
 
=={{header|REXX}}==
<langsyntaxhighlight 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*/
if ord=='' | ord=="," then ord= 3 /*Not specified? Then use the default.*/
Line 1,409 ⟶ 1,711:
end /*k*/
if g\=='' then say g /*There any residual? Then display it.*/
return /* [↑] limits G to terminal width.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,430 ⟶ 1,732:
{{trans|Python}}
{{works with|Rust|1.32 nightly (2018 edition)}}
<langsyntaxhighlight lang="rust">use std::env;
use std::fs;
 
Line 1,510 ⟶ 1,812:
sample(&mut rng, max, 1).into_vec()[0]
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,518 ⟶ 1,820:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func build_dict (n, words) {
var dict = Hash()
words.each_cons(n+1, {|*prefix|
Line 1,545 ⟶ 1,847:
}
 
say chain.join(' ')</langsyntaxhighlight>
 
{{out}}
Line 1,555 ⟶ 1,857:
{{trans|Python}}
{{works with|Swift|4.2}}
<langsyntaxhighlight lang="swift">import Foundation
 
func makeRule(input: String, keyLength: Int) -> [String: [String]] {
Line 1,599 ⟶ 1,901:
let str = makeString(rule: rule, length: 300)
 
print(str)</langsyntaxhighlight>
 
{{out}}
Line 1,608 ⟶ 1,910:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "random" for Random
import "./str" for Strs
 
var markov = Fn.new { |filePath, keySize, outputSize|
Line 1,638 ⟶ 1,940:
}
 
System.print(markov.call("alice_oz.txt", 3, 100))</langsyntaxhighlight>
 
{{out}}
2,460

edits