Markov chain text generator: Difference between revisions

no edit summary
m (→‎{{header|Raku}}: Fix code: Perl 6 --> Raku)
No edit summary
Line 320:
<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}}==
<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)
let leading = List.take
 
let buildMap prefixLen source =
let add map sequence =
let prefix = leading (Seq.length sequence - 1) sequence
let suffix = Seq.last sequence
let existingSuffixes = map |> Map.tryFind prefix |> Option.defaultValue []
// add a suffix to existing suffixes, probably creating duplicated entries
Map.add prefix (suffix :: existingSuffixes) map
let ngramCount = Seq.length source - prefixLen
Seq.init ngramCount (ngram (prefixLen + 1) source) |> Seq.fold add (Map[])
 
let generate outputLenLimit seed map =
let random = System.Random()
let pickSuffix prefix =
let randomItem suffixes = List.item (List.length suffixes |> random.Next) suffixes
// as the list of suffixes contains duplicates, probability of any distinct word
// is proportional to its frequency in the source text
map |> Map.tryFind prefix |> Option.map randomItem
let prefixLen = Seq.length seed
let add list word = list @ [word]
let append output _ =
trailing prefixLen output |> pickSuffix |> Option.map (add output) |> Option.defaultValue output
Seq.init outputLenLimit id |> Seq.fold append seed
 
let markov prefixLen outputLenLimit source =
let map = buildMap prefixLen source
let seed = leading prefixLen source
generate outputLenLimit seed map |> String.concat " "
 
"alice_oz.txt"
|> System.IO.File.ReadAllText
|> (fun t -> t.Split ' ')
|> List.ofSeq
|> markov 2 100
</lang>
{{out}}
<pre>
Alice was more dreadful than any of them. Brains are the only things in the forest a few happy days at the Scarecrow replied; but I must admit. Can't you see these are my friends, for all we know. But the girl in any other city, replied Oz; but when oil was poured upon it with a laugh, and that I may become as much as she passed; it was just beginning to feel very uneasy: to be said. At last the Gryphon whispered in reply, 'for fear they should forget them before the door, she ran out to look over their"
</pre>
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
Anonymous user