Markov chain text generator: Difference between revisions

no edit summary
No edit summary
Line 221:
{{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>
 
=={{header|Crystal}}==
<lang ruby>class Markov(N)
@dictionary = Hash(StaticArray(String, N), Array(String)).new { [] of String }
 
def parse(filename : String)
File.open(filename) do |file|
parse(file)
end
end
 
private def prefix_from(array)
StaticArray(String, N).new { |i| array[-(N - i)] }
end
 
def parse(input : IO)
sequence = [] of String
loop do
word = input.gets(' ', true)
break unless word
if sequence.size == N
prefix = prefix_from(sequence)
@dictionary[prefix] = (@dictionary[prefix] << word)
end
sequence << word
sequence.shift if sequence.size > N
end
end
 
def generate(count)
prefix = @dictionary.keys.sample
result = Array(String).new(prefix.size) { |i| prefix[i] }
(count - N).times do
prefix = prefix_from(result)
values = @dictionary[prefix]
break if values.size == 0
result << values.sample
end
result.join(' ')
end
end
 
chain = Markov(3).new
chain.parse("alice_oz.txt")
puts chain.generate(200)</lang>
{{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>
 
 
=={{header|D}}==
Anonymous user