Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.83 KB | None | 0 0
  1. open System
  2.  
  3. let file_read = System.IO.File.ReadAllText @"D:\input.txt"
  4. let file_write = new System.IO.FileInfo @"D:\output.txt"
  5.  
  6. let search_names (sentence : String) (i : int) (names : Map<string, Set<int>>) : Map<string, Set<int>> =
  7.     let words = sentence.Split([|' '; ','; ';'; '"'; '\r'; '\n'; '\t'; ':'; '-'; '*'; '#'|])
  8.     let rec search_names_rec (names_rec : Map<string, Set<int>>) i =
  9.         let symbols = [|'.'; '!'; '?'; ' '; ')'; '('; ','; ';'; '"'; '\'' ;'\r'; '\n'; '\t'; '*'; '#'|]
  10.         match i with
  11.         | l when l = words.Length -> names_rec
  12.         | _ -> words.[i].Trim(symbols) |> (fun word ->  if word.Length > 1 && Char.IsUpper(word.[0]) && not (Char.IsUpper(word.[1])) then
  13.                                                             let word_1 =
  14.                                                                 if word.[word.Length - 2] = '\'' && word.[word.Length - 1] = 's'
  15.                                                                 then word.Remove(word.Length - 2)
  16.                                                                 else word
  17.                                                             if names_rec.ContainsKey(word_1) then
  18.                                                                     search_names_rec (names_rec.Add(word_1, names_rec.[word_1].Add(i))) (i + 1)
  19.                                                             else
  20.                                                                 search_names_rec (names_rec.Add(word_1, Set.empty.Add(i))) (i + 1)
  21.                                                         else search_names_rec names_rec (i + 1))
  22.     search_names_rec names 0
  23.  
  24. type relation = string * string * int
  25.  
  26. let badWords = ["Space";
  27.  
  28.  
  29.                         ]
  30.  
  31. let main (text : String) : relation list =
  32.     let sentences = text.Split([|'.'; '!'; '?'|]);
  33.    
  34.     let rec global_search (names : Map<string, Set<int>>) i : Map<string, Set<int>> =
  35.         if (i < sentences.Length)
  36.         then global_search (search_names (sentences.[i].Trim([|'.'; '!'; '?'; ' '; '’'; '\''|])) i names) (i + 1)
  37.         else names
  38.  
  39.     let map = Map.filter (fun word _ -> not (List.exists (fun elem -> elem = word) badWords)) (global_search Map.empty 0)
  40.    
  41.     let relations = [ for n1 in map do
  42.                       for n2 in map do
  43.                       let c = Set.count(Set.intersect n1.Value n2.Value)
  44.                       if (n1.Key < n2.Key) && (c > 0) then yield (n1.Key, n2.Key, c)]
  45.  
  46.     let sort (r1 : relation) (r2 : relation) =
  47.         let n (_, _, c) = c
  48.         if (n r1 > n r2)
  49.         then -1
  50.         else 1
  51.     List.sortWith sort relations
  52.  
  53. if not file_write.Exists then
  54.     using (file_write.CreateText()) (fun stream ->
  55.                                 for str in (main file_read) do
  56.                                     stream.WriteLine(str.ToString()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement