Advertisement
Guest User

Untitled

a guest
May 28th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.08 KB | None | 0 0
  1. open FSharp.Data
  2. open System
  3. open System.IO
  4. open System.Net
  5. open System.Text
  6. open System.Collections.Specialized
  7.  
  8. type Word =
  9.     Id of int
  10.     | String of string
  11.  
  12. type Text = (Word seq) * (string array)
  13.  
  14. let task1 (s:string) =
  15.     let rec process (tmp : char * int) (result : (char * int) list) (pos:int) (s:string) : ((char * int) list) =
  16.         if (s.Length <= pos) then
  17.             tmp::result
  18.         elif (s.[pos] = (fst tmp)) then
  19.             process (fst tmp, (snd tmp) + 1) result (pos + 1) s
  20.         else
  21.             process (s.[pos], 1) (tmp::result) (pos + 1) s
  22.     if (s.Length = 0) then
  23.         []
  24.     else
  25.         process (s.[0], 1) [] 1 s
  26.         |> List.rev
  27.  
  28. let rec task2 (compressed: (char * int) list) =
  29.     let rec decompress (part: char * int) =
  30.         if (snd part <= 0) then
  31.             ""
  32.         else
  33.             string(fst part) + (decompress (fst part, (snd part) - 1))
  34.     if (compressed.Length <= 0) then
  35.         ""
  36.     else
  37.         let a::b = compressed
  38.         (decompress a) + (task2 b)
  39.  
  40. let task3 (path:string) =
  41.     let lines = File.ReadAllLines(path)
  42.     lines
  43.     |> Array.map (fun x -> x.Split([|' '; '.'; ','; ';'|]) |> Array.filter (fun x -> x.Length > 0))
  44.     |> Array.concat
  45.     |> Array.toSeq
  46.  
  47. let task4 (words: string seq) : Text =
  48.     let wordsCount = words |> Seq.countBy id
  49.     let dict =  wordsCount
  50.                 |> Seq.filter (fun x -> (snd x) > 5)
  51.                 |> Seq.map (fst)
  52.                 |> Seq.toArray
  53.     let text =  words
  54.                 |> Seq.map (fun x -> try Id(Array.findIndex (fun y -> y = x) dict) with | :? System.Collections.Generic.KeyNotFoundException -> String(x))
  55.     (text, dict)
  56.  
  57. let task5 (text:Text) =
  58.     let dict = snd text
  59.     let words = fst text
  60.     Seq.map (fun x -> match x with | Id(t) -> dict.[t] | String(s) -> s) words
  61.    
  62. let task6 (paths: string seq) =
  63.     paths
  64.     |> Seq.map (fun x -> async { return task4 (task3 x)})
  65.     |> Async.Parallel
  66.     |> Async.RunSynchronously
  67.  
  68.  
  69. [<EntryPoint>]
  70. let main argv =
  71.     let a = task6 [@"D:\1.txt"; @"D:\2.txt"]
  72.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement