Markov chain text generator: Difference between revisions

m (→‎{{header|Wren}}: Changed to Wren S/H)
 
Line 871:
<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}}
2,442

edits