Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- let file_read = System.IO.File.ReadAllText @"D:\input.txt"
- let file_write = new System.IO.FileInfo @"D:\output.txt"
- let search_names (sentence : String) (i : int) (names : Map<string, Set<int>>) : Map<string, Set<int>> =
- let words = sentence.Split([|' '; ','; ';'; '"'; '\r'; '\n'; '\t'; ':'; '-'; '*'; '#'|])
- let rec search_names_rec (names_rec : Map<string, Set<int>>) i =
- let symbols = [|'.'; '!'; '?'; ' '; ')'; '('; ','; ';'; '"'; '\'' ;'\r'; '\n'; '\t'; '*'; '#'|]
- match i with
- | l when l = words.Length -> names_rec
- | _ -> words.[i].Trim(symbols) |> (fun word -> if word.Length > 1 && Char.IsUpper(word.[0]) && not (Char.IsUpper(word.[1])) then
- let word_1 =
- if word.[word.Length - 2] = '\'' && word.[word.Length - 1] = 's'
- then word.Remove(word.Length - 2)
- else word
- if names_rec.ContainsKey(word_1) then
- search_names_rec (names_rec.Add(word_1, names_rec.[word_1].Add(i))) (i + 1)
- else
- search_names_rec (names_rec.Add(word_1, Set.empty.Add(i))) (i + 1)
- else search_names_rec names_rec (i + 1))
- search_names_rec names 0
- type relation = string * string * int
- let badWords = ["Space";
- ]
- let main (text : String) : relation list =
- let sentences = text.Split([|'.'; '!'; '?'|]);
- let rec global_search (names : Map<string, Set<int>>) i : Map<string, Set<int>> =
- if (i < sentences.Length)
- then global_search (search_names (sentences.[i].Trim([|'.'; '!'; '?'; ' '; '’'; '\''|])) i names) (i + 1)
- else names
- let map = Map.filter (fun word _ -> not (List.exists (fun elem -> elem = word) badWords)) (global_search Map.empty 0)
- let relations = [ for n1 in map do
- for n2 in map do
- let c = Set.count(Set.intersect n1.Value n2.Value)
- if (n1.Key < n2.Key) && (c > 0) then yield (n1.Key, n2.Key, c)]
- let sort (r1 : relation) (r2 : relation) =
- let n (_, _, c) = c
- if (n r1 > n r2)
- then -1
- else 1
- List.sortWith sort relations
- if not file_write.Exists then
- using (file_write.CreateText()) (fun stream ->
- for str in (main file_read) do
- stream.WriteLine(str.ToString()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement