Advertisement
Guest User

bayes

a guest
Jan 20th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace baes //Math.Log(s.Length / k);
  8. {
  9.     class Program
  10.     {
  11.         /*static double bayes(int Dc, int D, int v, string[][] spl, string[] Q, int i)
  12.         {
  13.             double answer = 0;
  14.             for (int j = 0; j < Q.Length; j++)
  15.                 answer += Math.Log((count_word(Q[j], spl[i]) + 1) / (v + spl[i].Length));
  16.             answer += Math.Log(Dc, D);
  17.             return answer;
  18.         }
  19.  
  20.         static int find_max(int[] Dc, int D, int v, string[][] spl, string[] Q)
  21.         {
  22.             double max = 0;
  23.             int k = 0;
  24.             for(int i = 0; i < spl.Length; i++)
  25.                 if (bayes(Dc[i], D, v, spl, Q, i) > max)
  26.                 {
  27.                     max = bayes(Dc[i], D, v, spl, Q, i);
  28.                     k = i;
  29.                 }
  30.             return k;
  31.         }*/
  32.  
  33.         static int count_word(string word, string[] s)
  34.         {
  35.             int count = 0;
  36.             foreach (string item in s)
  37.                 if (item.Contains(word))//(item == word)
  38.                 {
  39.                     //Console.Write(word + " " + item + " " + s.Contains(item) + "\n");
  40.                     count++;
  41.                 }
  42.             //Console.Write(count + "\n");
  43.             return count;
  44.         }
  45.  
  46.         static void Main()
  47.         {
  48.             Console.OutputEncoding = Encoding.UTF8;
  49.             string[] lines = System.IO.File.ReadAllLines(@"D:\Study\news_trainC2.txt");
  50.             /*string[] lines = System.IO.File.ReadAllLines(@"D:\Study\news_output_example.txt");
  51.             Console.Write(lines.Length+"\n");
  52.             for(int i = 0; i < lines.Length; i++)
  53.                 Console.Write(lines[i] + "\n");*/
  54.             string[][] split = new string[lines.Length][];
  55.             char[] delimiter = { '.', ' ', '!', '"', '-', '\t', '\n', ',', '(', ')', '\"', '?', ':', ';', '«', '»', '/' };
  56.             Console.Write(1 + "\n");
  57. //выделяем слова.            
  58.             for (int i = 0; i < split.Length; i++)
  59.                 split[i] = lines[i].Split(delimiter);
  60.             Console.Write(2 + "\n");
  61.  
  62. //объединяем в темы.
  63.             string[] theme = new string[10];
  64.             for (int i = 0; i < 10; i++)
  65.                 theme[i] = null;  
  66.             int[] Dc = new int[10];
  67.             for (int i = 0; i < split.Length; i++)
  68.             {
  69.                 if (string.Compare(split[i][0], "science") == 0)
  70.                 {
  71.                     theme[0] = string.Concat(theme[0], lines[i]);
  72.                     Dc[0]++;
  73.                 }
  74.                 if (string.Compare(split[i][0], "style") == 0)
  75.                 {
  76.                     theme[1] = string.Concat(theme[1], lines[i]);
  77.                     Dc[1]++;
  78.                 }
  79.                 if (string.Compare(split[i][0], "culture") == 0)
  80.                 {
  81.                     theme[2] = string.Concat(theme[2], lines[i]);
  82.                     Dc[2]++;
  83.                 }
  84.                 if (string.Compare(split[i][0], "life") == 0)
  85.                 {
  86.                     theme[3] = string.Concat(theme[3], lines[i]);
  87.                     Dc[3]++;
  88.                 }
  89.                 if (string.Compare(split[i][0], "economics") == 0)
  90.                 {
  91.                     theme[4] = string.Concat(theme[4], lines[i]);
  92.                     Dc[4]++;
  93.                 }
  94.                 if (string.Compare(split[i][0], "business") == 0)
  95.                 {
  96.                     theme[5] = string.Concat(theme[5], lines[i]);
  97.                     Dc[5]++;
  98.                 }
  99.                 if (string.Compare(split[i][0], "travel") == 0)
  100.                 {
  101.                     theme[6] = string.Concat(theme[6], lines[i]);
  102.                     Dc[6]++;
  103.                 }
  104.                 if (string.Compare(split[i][0], "forces") == 0)
  105.                 {
  106.                     theme[7] = string.Concat(theme[7], lines[i]);
  107.                     Dc[7]++;
  108.                 }
  109.                 if (string.Compare(split[i][0], "media") == 0)
  110.                 {
  111.                     theme[8] = string.Concat(theme[8], lines[i]);
  112.                     Dc[8]++;
  113.                 }
  114.                 if (string.Compare(split[i][0], "sport") == 0)
  115.                 {
  116.                     theme[9] = string.Concat(theme[9], lines[i]);
  117.                     Dc[9]++;
  118.                 }
  119.             }
  120.             for (int i = 0; i < Dc.Length; i++)
  121.                 Console.Write("Dc[{0}]: {1}; ", i,  Dc[i]);
  122.             Console.Write("\n" + 3 + "\n");
  123.             Console.Write(theme[0]);
  124.             int D = lines.Length;
  125.             string[][] spl = new string[theme.Length][];
  126.             for (int i = 0; i < spl.Length; i++)
  127.                 spl[i] = theme[i].Split(delimiter);
  128.             Console.Write("D = " +D + "\n" + 4 + "\n");
  129.  
  130. //удаляем последний символ в кажом слове.
  131.             for (int i = 0; i < spl.Length; i++)
  132.                  for (int j = 1; j < spl[i].Length; j++)
  133.                      if (spl[i][j].Length > 1)
  134.                      {
  135.                          spl[i][j] = spl[i][j].Substring(0, spl[i][j].Length - 1);
  136.                          //Console.Write(spl[i][j] + "\n");
  137.                      }
  138.              Console.Write(5 + "\n");
  139.  
  140.  //убираем слова, сост. из 1, 2, 3 символов  
  141.              int[] k = new int[10];
  142.              for (int i = 0; i < spl.Length; i++)
  143.              {
  144.                  k[i] = 0;
  145.                  for (int j = 0; j < spl[i].Length; j++)
  146.                      if (spl[i][j].Length > 3)
  147.                      {
  148.                          spl[i][k[i]] = spl[i][j];
  149.                          k[i]++;
  150.                      }
  151.                  Array.Resize<string>(ref spl[i], k[i]);
  152.              }
  153.              //foreach (string item in spl[0])
  154.              //    Console.Write(item + " ");
  155.              Console.Write(6 + "\n");
  156.  
  157. //считаем количество уникальных слов во всех документах обучающей выборки
  158.             int v = 0;
  159.             for (int i = 0; i < spl.Length; i++)
  160.             {
  161.                 IEnumerable<string> distinct = spl[i].Distinct();
  162.                 foreach (string item in distinct)
  163.                     v++;
  164.             }
  165.             Console.Write("v = " + v + "\n" + 7 + "\n");
  166.  
  167. //читаем test
  168.             //int q = 0;
  169.             string[] test = System.IO.File.ReadAllLines(@"D:\Study\news_test.txt");
  170.             string[][] split_test = new string[test.Length][];
  171.             //Console.Write(split_test.Length);
  172.             for (int i = 0; i < split_test.Length; i++)
  173.                 split_test[i] = test[i].Split(delimiter);
  174.             Console.Write(8 + "\n");
  175.  
  176. //удаляем первый и 2 последних символ в кажом слове.            
  177.             for (int i = 0; i < split_test.Length; i++)
  178.                 for (int j = 1; j < split_test[i].Length; j++)
  179.                     if (split_test[i][j].Length > 3)
  180.                     {
  181.                         split_test[i][j] = split_test[i][j].Substring(1, split_test[i][j].Length - 2);
  182.                     }
  183.             Console.Write(9 + "\n");
  184.  
  185. //убираем слова, сост. из 1, 2, 3 символов  
  186.             int[] k1 = new int[split_test.Length];
  187.             for (int i = 0; i < split_test.Length; i++)
  188.             {
  189.                 k1[i] = 0;
  190.                 for (int j = 0; j < split_test[i].Length; j++)
  191.                     if (split_test[i][j].Length > 3)
  192.                     {
  193.                         split_test[i][k1[i]] = split_test[i][j];
  194.                         //Console.Write(split_test[i][k1[i]] + "\n");
  195.                         k1[i]++;
  196.                     }
  197.                 Array.Resize<string>(ref split_test[i], k1[i]);
  198.             }
  199.             Console.Write(10 + "\n");
  200.  
  201. //гадаем
  202.             //Console.Write(spl[0].Length+"\n\n");
  203.             //Console.Write(count_word(split_test[0][1], spl[0])+"\n\n");
  204.             string[] answer = new string[test.Length];
  205.             for (int i = 0; i < test.Length; i++)
  206.             {
  207.                 Console.Write("{0}-ый шаг: ", i);
  208.                 double max = 0;
  209.                 int a = 0;
  210.                 for (int them = 0; them < spl.Length; them++)
  211.                 {
  212.                     double bayes = 0;
  213.                     for (int j = 0; j < split_test[i].Length; j++)
  214.                     {
  215.                         //Console.Write("them=" + them + ";  " + "j=" + j + ";  " + "count = {0};   spl[them.Length] = {1}",count_word(split_test[i][j], spl[them]), spl[them].Length + "\n");
  216.                         //bayes += Math.Log((count_word(split_test[i][j], spl[them]) + 1) / (v + spl[them].Length));
  217.                         if (count_word(split_test[i][j], spl[them]) != 0)
  218.                             bayes += Math.Log((count_word(split_test[i][j], spl[them]) + 1) * 100000 / (v + spl[them].Length));
  219.                         //Console.Write("bayes = " + bayes + "\n");
  220.                     }
  221.                     bayes += Math.Log(Dc[them] * 100000 / D);
  222.                     //bayes *= Dc[them] / D;
  223.                     //Console.Write(bayes + " ");
  224.                     if (bayes > max)
  225.                     {
  226.                         max = bayes;
  227.                         a = them;
  228.                     }
  229.                 }
  230.                 //Console.Write(a + " ");
  231.                 if (a == 0)
  232.                 {
  233.                     answer[i] = "science";
  234.                     //answer = string.Concat(answer, "science");
  235.                     Console.Write("science\n");
  236.                 }
  237.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "science");
  238.                 if (a == 1)
  239.                 {
  240.                     answer[i] = "style";
  241.                     //answer = string.Concat(answer, "style");
  242.                     Console.Write("style\n");
  243.                 }
  244.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "style");
  245.                 if (a == 2)
  246.                 {
  247.                     answer[i] = "culture";
  248.                     //answer = string.Concat(answer, "culture");
  249.                     Console.Write("culture\n");
  250.                 }
  251.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "culture");
  252.                 if (a == 3)
  253.                 {
  254.                     answer[i] = "life";
  255.                     //answer = string.Concat(answer, "life");
  256.                     Console.Write("life\n");
  257.                 }
  258.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "life");
  259.                 if (a == 4)
  260.                 {
  261.                     answer[i] = "economics";
  262.                     //answer = string.Concat(answer, "economics");
  263.                     Console.Write("economics\n");
  264.                 }
  265.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "economics");
  266.                 if (a == 5)
  267.                 {
  268.                     answer[i] = "business";
  269.                     //answer = string.Concat(answer, "business");
  270.                     Console.Write("business\n");
  271.                 }
  272.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "business");
  273.                 if (a == 6)
  274.                 {
  275.                     answer[i] = "travel";
  276.                     //answer = string.Concat(answer, "travel");
  277.                     Console.Write("travel\n");
  278.                 }
  279.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "travel");
  280.                 if (a == 7)
  281.                 {
  282.                     answer[i] = "forces";
  283.                     //answer = string.Concat(answer, "forces");
  284.                     Console.Write("forces\n");
  285.                 }
  286.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "forces");
  287.                 if (a == 8)
  288.                 {
  289.                     answer[i] = "media";
  290.                     //answer = string.Concat(answer, "media");
  291.                     Console.Write("media\n");
  292.                 }
  293.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "media");
  294.                 if (a == 9)
  295.                 {
  296.                     answer[i] = "sport";
  297.                     //answer = string.Concat(answer, "sport");
  298.                     Console.Write("sport\n");
  299.                 }
  300.                     //System.IO.File.WriteAllText(@"D:\Study\outC.txt", "sport");
  301.                 //Console.Write(find_max(Dc, D, v, spl, split_test[i]) + " ");
  302.             }
  303.             //Console.Write(answer);
  304.             System.IO.File.WriteAllLines(@"D:\Study\out.txt", answer, Encoding.UTF8);
  305.         }
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement