Untitled
raw download clone
TEXT
views 22
,
size 971 b
let bigramify (text: string) =
    text.Replace(',', ' ').Replace('\n', ' ').Split ' '
    |> Seq.windowed 2
    |> Seq.toArray

let nextWords (bigrams: string [] seq) (word: string) =
    bigrams
    |> Seq.filter (fun pair -> pair.[0] = word)
    |> Seq.map (fun pair -> pair.[1])
    |> Seq.toArray

let generateWords (sample: string) (firstWord: string) times =
    let bigrams = bigramify sample
    let takeNext (bigrams) (word: string) = 
        let next = nextWords bigrams word
        next.[System.Random().Next(0, next.Length)]
    let rec generateWords sentence word counter =
        if counter > times then
            sentence
        else
            let next = takeNext bigrams word
            let updated = sentence + " " + next
            generateWords updated next (counter+1)
    generateWords firstWord firstWord 1

let words = System.IO.File.ReadAllLines("tarzan.txt") |> String.concat ", "
generateWords words "A" 10
close fullscreen
Login or Register to edit or fork this paste. It's free.