Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.01 KB | None | 0 0
  1. open System.IO
  2. open System.Text.RegularExpressions
  3.  
  4. let readFile (path : string) : string =
  5.     use stream = new StreamReader(path)
  6.    
  7.     let mutable keepReading = true
  8.     let mutable text = ""
  9.     while (keepReading) do
  10.         let line = stream.ReadLine()
  11.         if (line = null) then
  12.             keepReading <- false
  13.         else
  14.             text <- text + line
  15.     text
  16.  
  17. let createStats (text : string) =
  18.     // text.ToLower().Split([|',';' ';'\n'|])
  19.     Regex.Split (text.ToLower(), "[^A-Za-zæøåÆØÅ]")
  20.     |> Seq.where    (fun w -> w.Length > 0)
  21.     |> Seq.countBy  (fun s -> s.Trim())
  22.     |> Seq.sortBy   (snd >> (~-))
  23.  
  24.    
  25. let writeToFile (wordSeq : seq<string * int>) (sw:StreamWriter) =
  26.     let wordList = Seq.toList wordSeq    
  27.     for pair in wordList do
  28.         sw.WriteLine( string (fst pair) + " : " + string (snd pair) )
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. let text = readFile "./storeClausLilleClaus.txt"
  37. let seq = createStats text
  38. let sw = new StreamWriter("hyppighed.txt")
  39. writeToFile seq sw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement