Gumanitariy

lab3

Nov 12th, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.43 KB | None | 0 0
  1. // Functional Programming: Lab 3
  2. // Student: ***Lyaysan Iskhakova***
  3. // N = 4
  4. open System
  5. open System.IO
  6. open System.Net
  7. open System.Text.RegularExpressions  
  8.  
  9. let http (url:string) =
  10.    let rq = WebRequest.Create(url)
  11.    use res = rq.GetResponse()
  12.    use rd = new StreamReader(res.GetResponseStream())
  13.    rd.ReadToEnd()
  14.  
  15.  
  16. // Task 1
  17. type FormattedText =
  18.     | Str of string
  19.     | Paragraph of FormattedText
  20.     | List of FormattedText seq
  21.     | Bold of FormattedText
  22.     | Italic of FormattedText
  23.     | Header of FormattedText
  24.  
  25. let p = "t"
  26. let e = [Bold(Str(p));Str(p);Str(p)] |> Seq.ofList
  27. let t = List(e)
  28.  
  29. // Task 2
  30. let rec format (x : FormattedText) =
  31.     let s1 = match x with
  32.         | Paragraph t -> "\n" + (format t) + "\n"
  33.         | Header t -> "# " + (format t)
  34.         | Italic t -> "*" + (format t) + "*"
  35.         | Bold t -> "**" + (format t) + "**"
  36.         | List t -> t |> Seq.map( fun i -> "* " + (format i)) |> Seq.fold (fun r s -> r + s + "\n")  ""
  37.         | Str t -> t
  38.     s1.TrimEnd([|'\n'|])
  39. ///////////////////////////////
  40. let tf = format t
  41.  
  42. // Task 3
  43. let (|Regex|_|) pattern input =
  44.         let m = Regex.Match(input, pattern)
  45.         if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
  46.         else None
  47.  
  48. let parseTxt (r:string): FormattedText =
  49.     match r with
  50.     | Regex @"\*\*(.+)\*\*" [t] -> Bold(Str(t))
  51.     | Regex @"\*(.+)\*" [t] -> Italic(Str(t))
  52.     | Regex @"(.+)" [t] -> Str(t)
  53.     | _ -> Str("")
  54.  
  55. let rec parse1 (r:string): FormattedText =
  56.     match r with
  57.     | Regex @"#(.+)" [t] -> Header(Str(t))
  58.     | Regex @"\* (.+)" [t] -> [parseTxt t] |> Seq.ofList |> List        
  59.     | Regex @"\*\*(.+)\*\*" [t] -> Bold(Str(t))
  60.     | Regex @"\*(.+)\*" [t] -> Italic(Str(t))
  61.     | Regex @"(.+)" [t] -> Str(t)
  62.     | _ -> Str("")
  63.  
  64. let isStr x =
  65.     match x with
  66.     | Str t -> true
  67.     | _ -> false
  68.  
  69. let isList x =
  70.     match x with
  71.     | List t -> true
  72.     | _ -> false
  73.  
  74. let getListStr a =
  75.     match a with
  76.     |List s -> s
  77.     | _ -> null
  78.  
  79. let group (x : FormattedText seq): FormattedText seq =  
  80.     let mutable x1 = x |> Seq.toArray
  81.     let mutable y = []
  82.     let mutable i = 0
  83.    
  84.     while i < x1.Length do
  85.         let mutable s = ""
  86.         let mutable f = false      
  87.         while (i + 1 < x1.Length) && (isList x1.[i]) && (isList x1.[i + 1]) do
  88.             let mutable a = getListStr x1.[i]
  89.             x1.[i+1] <- Seq.append a (getListStr x1.[i+1]) |> List
  90.             f <- true
  91.             i <- i + 1
  92.         match f with
  93.         | true ->
  94.             y <- List.append y [x1.[i]]
  95.             i <- i + 1
  96.         | false ->
  97.         while (i < x1.Length) && (isStr x1.[i]) do
  98.             s <- s + " " + (format x1.[i])
  99.             i <- i + 1
  100.         match s with
  101.         | "" ->
  102.             y <- List.append y [x1.[i]]
  103.             i <- i + 1
  104.         | _ ->
  105.             y <- List.append y [Paragraph(Str(s))]
  106.            
  107.     Seq.ofList y
  108.        
  109. let parse (x : string seq) : FormattedText seq = x |> Seq.map(parse1) |> group
  110. //////////////////////////////////////////////////////////////////////////////////////
  111. let splitLines (s:string) = List.ofSeq(s.Split([|'\n'|]))
  112. let y = splitLines tf
  113. let ttt = parse y
  114. let dw = ["# Header"; "* one"; "* **two**"; "* *three*"; "      "; "hello."; "wa"; "JHfv?"] |> Seq.ofList
  115. let t1 = dw|>Seq.map(parse1)
  116. let t2 = parse dw
  117. t2
  118.  
  119.  
  120. // Task 4
  121. let rec formatText : (FormattedText -> string) = fun s ->
  122.     match s with
  123.     | Paragraph t | Header t | Italic t | Bold t -> formatText t
  124.     | List t -> t |> Seq.map(formatText) |> Seq.fold (+) ""
  125.     | Str t -> t
  126. /////////////////////////
  127.  
  128. t2 |> Seq.map(formatText)
  129. // let strip : string -> string = parse >> formatText
  130.  
  131. // Task 5
  132. let count (s : string) =
  133.     let t1 = s.Split(',',' ','.',':','!','-','—','(',')','/',''', '')
  134.    let t2 = [1.0..1.0..(float t1.Length)]
  135.    let comb = Seq.zip t1 t2 |> Seq.groupBy fst |> Seq.map (fun (key, values) -> (key, values |> Seq.averageBy snd))
  136.    comb
  137.  
  138. let analyze (x : string) =
  139.    (Seq.collect count (x.Split('.', '?', '!')
  140.    |> Seq.ofArray
  141.    |> Seq.map(fun s -> s.ToLower())))
  142.    |> Seq.groupBy fst
  143.    |> Seq.map (fun (key, values) -> (key, values |> Seq.averageBy snd))
  144.    |> Seq.sortByDescending snd
  145.  
  146.  
  147. // let strip : string -> string = parse |> Seq.concat >> formatText
  148. //////////////////////  
  149. let b =  http "http://www.gutenberg.org/files/11/11-0.txt"
  150. analyze b
Add Comment
Please, Sign In to add comment