Advertisement
Guest User

Replace words

a guest
Dec 1st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.06 KB | None | 0 0
  1. open System
  2.  
  3. let rng = new Random()
  4. let sentence = "The quick brown fox jumped over the lazy dog"
  5. let businessWords = ["synergy"; "buzz"; "overflow"; "capital"; "corporate";]
  6.  
  7. let splitToPairs (str:string) =
  8.     let rec pairOff arr =
  9.         match arr with
  10.         | h::s::t -> (h, s)::(pairOff t)
  11.         | h::[] -> (h, "")::[]
  12.         | [] -> []
  13.     let words = str.Split [|' '|]
  14.     let pairs = pairOff (List.ofArray words)
  15.     pairs
  16.  
  17. let replaceSecondWords pairs words =
  18.     let random (words:string list) =
  19.         let max = words.Length
  20.         let i = rng.Next(0, max)
  21.         words.Item i
  22.  
  23.     let replacedPairs =
  24.         pairs
  25.         |> List.map(fun p ->
  26.                     match p with
  27.                     | (f, s) when s = "" -> (f, "")
  28.                     | (f, s) -> (f, (random words))
  29.                 )
  30.  
  31.     replacedPairs
  32.     |> List.map(fun p -> String.Join(" ", (fst p), (snd p)))
  33.     |> List.fold (fun state acc -> state + " " + acc) ""
  34.  
  35.  
  36. let newSentence = replaceSecondWords (splitToPairs sentence) businessWords
  37. (newSentence)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement