The purpose of this repository is to provide a way for people to generate random "placeholder text" -- with a Markov Chain. https://www.craigoates.net/Software/project/12
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
1.7 KiB

module internal DataServices
open SystemServices
open DataStructuring
open DataProcessing
open DataCleaning
open System.Threading
open System
let rec GenerateMarkovChain (map: Map<string, string List>) (state:string) chain =
if map.ContainsKey state then
let nextChoice = map.[state] |> PickRandomItem
if MatchText @"\." nextChoice then nextChoice :: chain
else
let currentWords =
state
|> SplitText @"\s+"
|> Seq.skip 1
|> ConcatToString
GenerateMarkovChain map (CombineWords currentWords nextChoice) (nextChoice :: chain)
else
let fallbackChoice = (PickRandomItem map).Key
String.Format("{0}.", fallbackChoice) :: chain
let GenerateMarkovSentence map start =
GenerateMarkovChain map start [start]
|> List.rev
|> ConcatToString
(* Thread.Sleep is needed for a new random number to generate.
Without it, the same sentence tends to repeat itself.
When debugging (I.E. the observer effect),
enough time passes for a new random number to generate --
meaning a new seed or "start word" for each sentence.
This isn't ideal but needed so please be careful when attempting to remove this line. *)
let GenerateMarkovText noOfSentences map =
let startWords = fst(SeperateStartWords map)
let result =
seq {
for i in 1 .. noOfSentences do
Thread.Sleep(100)
yield GenerateMarkovSentence map (PickRandomItem startWords).Key
}
result |> ConcatToString