Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.85 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 (sent : String) (names : Map<String, Set<int>>) index : Map<String, Set<int>> =
  7.     let words = sent.Split([|' '; ','; ';'; '"';'\r'; '\n'; '\t'; ':'; '-'|])
  8.     let rec search_names_rec (names_rec : Map<String, Set<int>>) i =
  9.         match i with
  10.         | a when a = words.Length -> names_rec
  11.         | _ -> words.[i].Trim([|'.'; '!'; '?'; ' '; ')'; '('; ','; ';'; '"'; '\'' ;'\r'; '\n'; '\t'|])  |> (fun word ->  
  12.             if word.Length > 1 && Char.IsUpper(word.[0]) && not (Char.IsUpper(word.[1]))
  13.             then                                        
  14.                 let w =
  15.                     if word.[word.Length - 2] = '\'' && word.[word.Length - 1] = 's'
  16.                     then word.Remove(word.Length - 2)
  17.                     else word                              
  18.                 if names_rec.ContainsKey(w)
  19.                 then search_names_rec (names_rec.Add(w, names_rec.[w].Add(index))) (i + 1)
  20.                 else search_names_rec (names_rec.Add(w, Set.empty.Add(index))) (i + 1)                                    
  21.             else search_names_rec names_rec (i + 1))
  22.     search_names_rec names 1
  23.  
  24. type pair = string * string * int
  25.  
  26. let exclusion = ["Russia"; "Russian"; "French"; "Princess"; "Prince"; "Emperor"; "Moscow"; "Count"; "Smolensk"; "Petersburg"; "Mademoiselle"; "His"; "Her"; "Project"; "Hills"; "Guards"; "The"; "Uncle"; "Countess"; "Tsar"; "German"; "Austrian"; "General"; "God"; "Charmee"; "Go"; "Get"; "From"; "However"; "When"; "Where"; "How"; "What"; "King"; "Kiss"; "King"; "A"; "Your"; "We"; "About"; "After"; "Street"; "Ah"; "An";"War";"And";"You";"That";"They";"These";"This";"She"]
  27.  
  28. let main (text : String) : pair list =
  29.     let sentences = text.Split([|'.'; '!'; '?'|]);
  30.    
  31.     let rec global_search (names : Map<string, Set<int>>) i : Map<string, Set<int>> =
  32.         if (i < sentences.Length)
  33.         then global_search (search_names (sentences.[i].Trim([|'.'; '!'; '?'; ' '; '’'; '\''|])) names i) (i + 1)
  34.         else names
  35.  
  36.     let map = Map.filter (fun word _ -> not (List.exists (fun elem -> elem = word) exclusion)) (global_search Map.empty 0)
  37.    
  38.     let pairs = [ for n1 in map do
  39.                       for n2 in map do
  40.                       let c = Set.count(Set.intersect n1.Value n2.Value)
  41.                       if (n1.Key < n2.Key) && (c > 0) then yield (n1.Key, n2.Key, c)]
  42.  
  43.     let sort (p1 : pair) (p2 : pair) =
  44.         let n (_, _, c) = c
  45.         if (n p1 > n p2)
  46.         then -1
  47.         else 1
  48.     List.sortWith sort pairs
  49.  
  50. if not file_write.Exists then
  51.     using (file_write.CreateText()) (fun stream ->
  52.                                 for str in (main file_read) do
  53.                                     stream.WriteLine(str.ToString()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement