Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Functional Programming: Lab 3
- // Student: ***Lyaysan Iskhakova***
- // N = 4
- open System
- open System.IO
- open System.Net
- open System.Text.RegularExpressions
- let http (url:string) =
- let rq = WebRequest.Create(url)
- use res = rq.GetResponse()
- use rd = new StreamReader(res.GetResponseStream())
- rd.ReadToEnd()
- // Task 1
- type FormattedText =
- | Str of string
- | Paragraph of FormattedText
- | List of FormattedText seq
- | Bold of FormattedText
- | Italic of FormattedText
- | Header of FormattedText
- let p = "t"
- let e = [Bold(Str(p));Str(p);Str(p)] |> Seq.ofList
- let t = List(e)
- // Task 2
- let rec format (x : FormattedText) =
- let s1 = match x with
- | Paragraph t -> "\n" + (format t) + "\n"
- | Header t -> "# " + (format t)
- | Italic t -> "*" + (format t) + "*"
- | Bold t -> "**" + (format t) + "**"
- | List t -> t |> Seq.map( fun i -> "* " + (format i)) |> Seq.fold (fun r s -> r + s + "\n") ""
- | Str t -> t
- s1.TrimEnd([|'\n'|])
- ///////////////////////////////
- let tf = format t
- // Task 3
- let (|Regex|_|) pattern input =
- let m = Regex.Match(input, pattern)
- if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
- else None
- let parseTxt (r:string): FormattedText =
- match r with
- | Regex @"\*\*(.+)\*\*" [t] -> Bold(Str(t))
- | Regex @"\*(.+)\*" [t] -> Italic(Str(t))
- | Regex @"(.+)" [t] -> Str(t)
- | _ -> Str("")
- let rec parse1 (r:string): FormattedText =
- match r with
- | Regex @"#(.+)" [t] -> Header(Str(t))
- | Regex @"\* (.+)" [t] -> [parseTxt t] |> Seq.ofList |> List
- | Regex @"\*\*(.+)\*\*" [t] -> Bold(Str(t))
- | Regex @"\*(.+)\*" [t] -> Italic(Str(t))
- | Regex @"(.+)" [t] -> Str(t)
- | _ -> Str("")
- let isStr x =
- match x with
- | Str t -> true
- | _ -> false
- let isList x =
- match x with
- | List t -> true
- | _ -> false
- let getListStr a =
- match a with
- |List s -> s
- | _ -> null
- let group (x : FormattedText seq): FormattedText seq =
- let mutable x1 = x |> Seq.toArray
- let mutable y = []
- let mutable i = 0
- while i < x1.Length do
- let mutable s = ""
- let mutable f = false
- while (i + 1 < x1.Length) && (isList x1.[i]) && (isList x1.[i + 1]) do
- let mutable a = getListStr x1.[i]
- x1.[i+1] <- Seq.append a (getListStr x1.[i+1]) |> List
- f <- true
- i <- i + 1
- match f with
- | true ->
- y <- List.append y [x1.[i]]
- i <- i + 1
- | false ->
- while (i < x1.Length) && (isStr x1.[i]) do
- s <- s + " " + (format x1.[i])
- i <- i + 1
- match s with
- | "" ->
- y <- List.append y [x1.[i]]
- i <- i + 1
- | _ ->
- y <- List.append y [Paragraph(Str(s))]
- Seq.ofList y
- let parse (x : string seq) : FormattedText seq = x |> Seq.map(parse1) |> group
- //////////////////////////////////////////////////////////////////////////////////////
- let splitLines (s:string) = List.ofSeq(s.Split([|'\n'|]))
- let y = splitLines tf
- let ttt = parse y
- let dw = ["# Header"; "* one"; "* **two**"; "* *three*"; " "; "hello."; "wa"; "JHfv?"] |> Seq.ofList
- let t1 = dw|>Seq.map(parse1)
- let t2 = parse dw
- t2
- // Task 4
- let rec formatText : (FormattedText -> string) = fun s ->
- match s with
- | Paragraph t | Header t | Italic t | Bold t -> formatText t
- | List t -> t |> Seq.map(formatText) |> Seq.fold (+) ""
- | Str t -> t
- /////////////////////////
- t2 |> Seq.map(formatText)
- // let strip : string -> string = parse >> formatText
- // Task 5
- let count (s : string) =
- let t1 = s.Split(',',' ','.',':','!','-','—','(',')','/',''', '’')
- let t2 = [1.0..1.0..(float t1.Length)]
- let comb = Seq.zip t1 t2 |> Seq.groupBy fst |> Seq.map (fun (key, values) -> (key, values |> Seq.averageBy snd))
- comb
- let analyze (x : string) =
- (Seq.collect count (x.Split('.', '?', '!')
- |> Seq.ofArray
- |> Seq.map(fun s -> s.ToLower())))
- |> Seq.groupBy fst
- |> Seq.map (fun (key, values) -> (key, values |> Seq.averageBy snd))
- |> Seq.sortByDescending snd
- // let strip : string -> string = parse |> Seq.concat >> formatText
- //////////////////////
- let b = http "http://www.gutenberg.org/files/11/11-0.txt"
- analyze b
Add Comment
Please, Sign In to add comment