Markov chain text generator: Difference between revisions

m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
 
(4 intermediate revisions by 3 users not shown)
Line 49:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F makerule(data, context)
‘Make a rule dict for given data.’
[String = [String]] rule
Line 95:
cur_line_size += word.len + 1
print(word, end' ‘ ’)
print()</langsyntaxhighlight>
 
{{out}}
Line 108:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.IO;
Line 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 185:
In this implementation there is no repeated suffixes!
 
<langsyntaxhighlight lang="cpp">
#include <ctime>
#include <iostream>
Line 258:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 279:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">class Markov(N)
@dictionary = Hash(StaticArray(String, N), Array(String)).new { [] of String }
 
Line 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 327:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.file;
import std.random;
import std.range;
Line 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 420:
|> List.ofSeq
|> markov 2 100
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 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 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 641 ⟶ 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 652 ⟶ 746:
This seems to be reasonably close to the specification:
 
<langsyntaxhighlight Jlang="j">require'web/gethttp'
 
setstats=:dyad define
Line 685 ⟶ 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 693 ⟶ 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 703 ⟶ 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 710 ⟶ 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 773 ⟶ 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 805 ⟶ 988:
 
txt = readstring(download("http://paulo-jorente.de/text/alice_oz.txt"))
println(markovtext(txt, 3, 200))</langsyntaxhighlight>
 
{{out}}
Line 812 ⟶ 995:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.3.10
 
import java.io.File
Line 850 ⟶ 1,033:
fun main() {
println(markov("alice_oz.txt", 3, 100))
}</langsyntaxhighlight>
 
Sample output:
Line 863 ⟶ 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 950 ⟶ 1,133:
end
io.write('\n')
</syntaxhighlight>
</lang>
 
{{out}}
Line 973 ⟶ 1,156:
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">
<lang Nim>
import random, sequtils, strutils, tables
 
Line 1,012 ⟶ 1,195:
if idx < 0: idx = result.high
echo result[start..idx]
start = idx + 1</langsyntaxhighlight>
 
{{out}}
Line 1,023 ⟶ 1,206:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 1,061 ⟶ 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 1,067 ⟶ 1,250:
=={{header|Phix}}==
This was fun! (easy, but fun)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 1,103 ⟶ 1,286:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
from the alice_oz.txt file:
Line 1,121 ⟶ 1,304:
=={{header|PHP}}==
 
<langsyntaxhighlight PHPlang="php"><?php
 
// Automated text generator using markov chain
Line 1,189 ⟶ 1,372:
);
 
echo wordwrap($text, 100, PHP_EOL) . PHP_EOL;</langsyntaxhighlight>
 
{{out}}
Line 1,214 ⟶ 1,397:
Usage: markov.py source.txt context length
 
<langsyntaxhighlight lang="python">import random, sys
 
def makerule(data, context):
Line 1,258 ⟶ 1,441:
rule = makerule(data, int(sys.argv[2]))
string = makestring(rule, int(sys.argv[3]))
print(string)</langsyntaxhighlight>
 
{{out}}
Line 1,282 ⟶ 1,465:
 
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Markov chain text generation'''
 
from os.path import (expanduser)
Line 1,413 ⟶ 1,596:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Markov chain text generation:
Line 1,435 ⟶ 1,618:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define (pick xs) (list-ref xs (random (length xs))))
Line 1,461 ⟶ 1,644:
(values (append (rest prefix) (list suffix)) (cons suffix out))))
 
(markov "alice_oz.txt" 3 300)</langsyntaxhighlight>
 
{{out}}
Line 1,471 ⟶ 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,485 ⟶ 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,491 ⟶ 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,528 ⟶ 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,549 ⟶ 1,732:
{{trans|Python}}
{{works with|Rust|1.32 nightly (2018 edition)}}
<langsyntaxhighlight lang="rust">use std::env;
use std::fs;
 
Line 1,629 ⟶ 1,812:
sample(&mut rng, max, 1).into_vec()[0]
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,637 ⟶ 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,664 ⟶ 1,847:
}
 
say chain.join(' ')</langsyntaxhighlight>
 
{{out}}
Line 1,674 ⟶ 1,857:
{{trans|Python}}
{{works with|Swift|4.2}}
<langsyntaxhighlight lang="swift">import Foundation
 
func makeRule(input: String, keyLength: Int) -> [String: [String]] {
Line 1,718 ⟶ 1,901:
let str = makeString(rule: rule, length: 300)
 
print(str)</langsyntaxhighlight>
 
{{out}}
Line 1,727 ⟶ 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,757 ⟶ 1,940:
}
 
System.print(markov.call("alice_oz.txt", 3, 100))</langsyntaxhighlight>
 
{{out}}
2,442

edits