//Private Methods //Removing of unwanthed characters //Calculating final probability document belonging to category private double CalculatePCatDoc(CategoryTable tableConservative, double totalUniqueWords, double PCatParty, List <double> PCatPartyDoc) { double CatLogTotal = 0; for (int i = 0; i < fileContents.Count; i++) { PCatPartyDoc.Add(tableConservative.GetPWordCat(fileContents[i], totalUniqueWords)); } if (PCatPartyDoc.Any()) { PCatPartyDoc.Add(PCatParty); } else { PCatPartyDoc.Add(0); PCatPartyDoc.Add(PCatParty); } for (int i = 0; i < PCatPartyDoc.Count() - 1; i++) { CatLogTotal += PCatPartyDoc[i]; } return(CatLogTotal); }
//Removing stopwords and combining lemmatizations //Public Methods public Classification(string conservativeDoc, string LabourDoc, string libDemConDoc, string master, string stopWordDoc, string lemmatizationDoc) { try { SetSourceDirectory(); Console.WriteLine("Please enter the name of the .txt document you wish to predict."); Console.WriteLine("Name of included test documents: 'test1', 'test2', 'test3'"); string FileDirectory = srcDirectory + Console.ReadLine() + ".txt"; if (File.Exists(FileDirectory)) { ParseTextDocument(FileDirectory, stopWordDoc, lemmatizationDoc); StreamReader sr = new StreamReader(srcDirectory + master); double totalConservative = 0; double totalLabour = 0; double totalLibDemCon = 0; double totalUniqueWords = 0; string[] tempMaster = sr.ReadToEnd().Split(' ', '\n', '\t'); for (int i = 0; i < tempMaster.Length; i++) { if (tempMaster[i] == "Conservative") { totalConservative = double.Parse(tempMaster[i + 1]); } if (tempMaster[i] == "Labour") { totalLabour = double.Parse(tempMaster[i + 1]); } if (tempMaster[i] == "LibDemCon") { totalLibDemCon = double.Parse(tempMaster[i + 1]); } if (tempMaster[i] == "Count") { totalUniqueWords = int.Parse(tempMaster[i + 1]); } sr.Close(); } if (Math.Abs((totalConservative + totalLabour + totalLibDemCon)) < 1e-11) { Console.WriteLine("\nNo words were found in the network"); Console.ReadKey(); return; } double PCatConservative = Math.Log((totalConservative / (totalConservative + totalLabour + totalLibDemCon))); double PCatLabour = Math.Log((totalLabour / (totalConservative + totalLabour + totalLibDemCon))); double PCatConservativeLibDemCoalition = Math.Log((totalLibDemCon / (totalConservative + totalLabour + totalLibDemCon))); tableConservative = new CategoryTable(srcDirectory + conservativeDoc, totalUniqueWords); tableLabour = new CategoryTable(srcDirectory + LabourDoc, totalUniqueWords); tableLibDem = new CategoryTable(srcDirectory + libDemConDoc, totalUniqueWords); double consLogTotal = CalculatePCatDoc(tableConservative, totalUniqueWords, PCatConservative, PCatConDoc); double LabourLogTotal = CalculatePCatDoc(tableLabour, totalUniqueWords, PCatLabour, PCatLabourDoc); double libDemConLogTotal = CalculatePCatDoc(tableLibDem, totalUniqueWords, PCatConservativeLibDemCoalition, PCatConservativeConseravtiveCoalitionDoc); if (consLogTotal > LabourLogTotal && consLogTotal > libDemConLogTotal) { Console.WriteLine("\nMost Likely: Conservative"); } else if (LabourLogTotal > consLogTotal && LabourLogTotal > libDemConLogTotal) { Console.WriteLine("\nMost Likely: Labour"); } else if (libDemConLogTotal > consLogTotal && libDemConLogTotal > LabourLogTotal) { Console.WriteLine("\nMost Likely: Liberal Democrats / Conservative Coalition"); } else { Console.WriteLine("\nCannot Determine Party!"); } Console.WriteLine("\nConservative : LogE(Probability) = " + consLogTotal); Console.WriteLine("Labour : LogE(Probability) = " + LabourLogTotal); Console.WriteLine("Conservative/Liberal Democrats Coalition : LogE(Probability) = " + libDemConLogTotal); Console.WriteLine("\nNB: The unknown document is predicted by the most positive LogE(Probability)."); Console.WriteLine("\nPress any KEY to continue."); Console.ReadKey(); } else { Console.Clear(); Console.WriteLine(FileDirectory + " Doesn't exist!"); Console.WriteLine("Please enter the name of a valid .txt file."); Console.WriteLine("Press any key to restart."); Console.WriteLine("Or Press ESC to exit."); if (Console.ReadKey().Key == ConsoleKey.Escape) { Environment.Exit(0); } } } catch (Exception e) { ExeptionMethod(e); } }