2016-11-03 12 views
0

タイトルの示唆しているように、辞書を別の辞書と比較しようとしています。これは、1つの辞書から別の辞書とバイグラムを比較するためのものです。辞書を他の辞書と比較する

私はコンソールアプリケーションを使用してファイルをロードしています。アプリケーションはこのファイルを読み込み、2文字ごとに(例えばheyとheとなるように)キーを追加します。値はバイグラムがパーセンテージで表示される頻度です(この場合、彼は50%、eyは50%です)。 )。私はこれを行うことができる部分に着いた。

最近、追加されたバイグラムを2番目のファイル(テキスト)と比較するのが難しい部分です。 2番目のファイルを別の辞書に追加します。だから私は2つの辞書(最初のファイルのために1つと2番目のファイルのための2つ)を持っています。

ここで、これらの2つの辞書を比較して、どのバイグラムがどちらにないのかを見てみましょう(たとえば、最初のディレクトリには、ey、2番目のディレクトリにhe、ey、yg、gu、uy、ysがあります) 。だから、yg、gu、uy、ysが来るはずです。ここで

は私のコードです:

私が試した何
static StringBuilder newText = new StringBuilder(); 
static Dictionary<string, int> d = new Dictionary<string, int>(); 
static Dictionary<string, double> dNL = new Dictionary<string, double>(); 
static Dictionary<string, double> dDE = new Dictionary<string, double>(); 
static Dictionary<string, double> dFR = new Dictionary<string, double>(); 
static Dictionary<string, double> dSP = new Dictionary<string, double>(); 
static Dictionary<string, double> dEN = new Dictionary<string, double>(); 
static Dictionary<string, double> dIT = new Dictionary<string, double>(); 

static List<string> fileList = new List<string>(); 
static List<string> dConverted = new List<string>(); 
static List<string> dConvertedNL = new List<string>(); 
static List<string> dConvertedDE = new List<string>(); 
static List<string> dConvertedFR = new List<string>(); 
static List<string> dConvertedSP = new List<string>(); 
static List<string> dConvertedEN = new List<string>(); 
static List<string> dConvertedIT = new List<string>(); 

static string text; 
static string languageChosen; 
static string gecombineerdeLetters; 
static int value = 0; 
static int totaleNGram = 0; 
static string[] fileRead; 
static char[] tekst; 

[STAThread] 
static void Main(string[] args) 
{ 
    Start: 
    Console.WriteLine("Welcome to this program. We need some text to start getting results. Please enter a Language your text is in:"); 
    Console.WriteLine("press 1 - 7/enter 'exit' to close the program"); 
    Console.WriteLine("1. Dutch/2. German/3. French/4. Spanish/5. English/6. Italian/7. Enter unknown language"); 
    Console.WriteLine(); 
    languageChosen = Console.ReadLine(); 
    if (languageChosen == "1" || languageChosen == "2" || languageChosen == "3" || languageChosen == "4" || languageChosen == "5" || languageChosen == "6") 
    { 
     calculateChanceKnown(); 
     Console.WriteLine(); 
     goto Start; 
    } 
    else if (languageChosen == "7") 
    { 
     OpenReadFile(); 
     // compare to every language 
     // first convert to list 
     dConverted = d.Keys.ToList(); 
     dConvertedNL = dNL.Keys.ToList(); 
     dConvertedDE = dDE.Keys.ToList(); 
     dConvertedFR = dFR.Keys.ToList(); 
     dConvertedSP = dSP.Keys.ToList(); 
     dConvertedEN = dEN.Keys.ToList(); 
     dConvertedIT = dIT.Keys.ToList(); 
     // compare d against each other list to see which has the most simularities 
     //List<string> firstNotSecond = dConverted.Except(dConvertedNL).ToList(); 

     Console.WriteLine(); 
     goto Start; 
    } 
    else if(languageChosen == "exit") 
    { 
     Environment.Exit(0); 
    } 
    else 
    { 
     Console.WriteLine("Wrong input, try again!"); 
     Console.WriteLine(); 
     goto Start; 
    } 

    Console.Read(); 
} 

static void calculateChanceKnown() 
{ 
    OpenReadFile(); 

    switch (languageChosen) 
    { 
     case "1": 
      foreach (KeyValuePair<string, int> kvp in d) 
      { 
       //Console.WriteLine("Key = {0}, Value = {1}, Chance = {2}%", kvp.Key, kvp.Value, ((double)kvp.Value/totaleNGram) * 100); 
       dNL.Add(kvp.Key, ((double)kvp.Value/totaleNGram) * 100); 
       //if key already exists -> add +1 to totaleNGram -> d[gecombineerdeletters] +1 -> calculate % again 
      } 
      d.Clear(); 
      //foreach (KeyValuePair<string, double> kvp in dNL) 
      //{ 
      // Console.WriteLine("Bigram = {0}, Chance = {1}%", kvp.Key, kvp.Value); 
      //} 
      break; 
     case "2": 
      foreach (KeyValuePair<string, int> kvp in d) 
      { 
       //Console.WriteLine("Key = {0}, Value = {1}, Chance = {2}%", kvp.Key, kvp.Value, ((double)kvp.Value/totaleNGram) * 100); 
       dDE.Add(kvp.Key, ((double)kvp.Value/totaleNGram) * 100); 
      } 
      foreach (KeyValuePair<string, double> kvp in dDE) 
      { 
       Console.WriteLine("Bigram = {0}, Chance = {1}%", kvp.Key, kvp.Value); 
      } 
      break; 
     case "3": 
      foreach (KeyValuePair<string, int> kvp in d) 
      { 
       //Console.WriteLine("Key = {0}, Value = {1}, Chance = {2}%", kvp.Key, kvp.Value, ((double)kvp.Value/totaleNGram) * 100); 
       dFR.Add(kvp.Key, ((double)kvp.Value/totaleNGram) * 100); 
      } 
      foreach (KeyValuePair<string, double> kvp in dFR) 
      { 
       Console.WriteLine("Bigram = {0}, Chance = {1}%", kvp.Key, kvp.Value); 
      } 
      break; 
     case "4": 
      foreach (KeyValuePair<string, int> kvp in d) 
      { 
       //Console.WriteLine("Key = {0}, Value = {1}, Chance = {2}%", kvp.Key, kvp.Value, ((double)kvp.Value/totaleNGram) * 100); 
       dSP.Add(kvp.Key, ((double)kvp.Value/totaleNGram) * 100); 
      } 
      foreach (KeyValuePair<string, double> kvp in dSP) 
      { 
       Console.WriteLine("Bigram = {0}, Chance = {1}%", kvp.Key, kvp.Value); 
      } 
      break; 
     case "5": 
      foreach (KeyValuePair<string, int> kvp in d) 
      { 
       //Console.WriteLine("Key = {0}, Value = {1}, Chance = {2}%", kvp.Key, kvp.Value, ((double)kvp.Value/totaleNGram) * 100); 
       dEN.Add(kvp.Key, ((double)kvp.Value/totaleNGram) * 100); 
      } 
      foreach (KeyValuePair<string, double> kvp in dEN) 
      { 
       Console.WriteLine("Bigram = {0}, Chance = {1}%", kvp.Key, kvp.Value); 
      } 
      break; 
     case "6": 
      foreach (KeyValuePair<string, int> kvp in d) 
      { 
       //Console.WriteLine("Key = {0}, Value = {1}, Chance = {2}%", kvp.Key, kvp.Value, ((double)kvp.Value/totaleNGram) * 100); 
       dIT.Add(kvp.Key, ((double)kvp.Value/totaleNGram) * 100); 
      } 
      foreach (KeyValuePair<string, double> kvp in dIT) 
      { 
       Console.WriteLine("Bigram = {0}, Chance = {1}%", kvp.Key, kvp.Value); 
      } 
      break; 
     default: 
      break; 
    } 
} 

static void OpenReadFile() 
{ 
    var fileDialog = new OpenFileDialog { Multiselect = false, Title = "Open Text File", Filter = "txt files (*txt)|*.txt| word files (*.doc, *.docx)|*.doc; *docx" }; 
    using (fileDialog) 
    { 
     if (fileDialog.ShowDialog() == DialogResult.OK) 
     { 
      System.IO.StreamReader sr = new System.IO.StreamReader(fileDialog.FileName); 
      string line; 
      while ((line = sr.ReadLine()) != null) 
      { 
       fileList.Add(line); 
      } 
     } 
    } 
    fileRead = fileList.ToArray(); 
    tekst = string.Join(string.Empty, fileRead).ToCharArray(); 

    foreach (char c in tekst) 
    { 
     if (!char.IsPunctuation(c)) newText.Append(c); 
    } 
    text = newText.ToString(); 
    text.ToLower(); 
    text = Regex.Replace(text, @"[\s+]", ""); 
    tekst = text.ToCharArray(); 

    for (int i = 0; i < tekst.Count() - 1; i += 1) 
    { 
     gecombineerdeLetters = string.Format("{0}{1}", tekst[i], tekst[i + 1]); 
     //Console.WriteLine(gecombineerdeLetters); 

     if (!d.TryGetValue(gecombineerdeLetters, out value)) 
     { 
      d.Add(gecombineerdeLetters, 1); 
      totaleNGram += 1; 
     } 
     else 
     { 
      d[gecombineerdeLetters] += 1; 
      totaleNGram += 1; 
     } 
    } 

} 

  • を使用して、私は同じことを比較してみました

たびSequenceEqual>

  • <以外それはどちらのディレクトリにもない1つのバイグラムを私に与えるだろう。

  • +0

    'dict1.Keys.Except(dict2.Keys)'とその逆を行うことを願って? – itsme86

    +0

    @ itsme86それは私も同様に考えましたが、何らかの理由で私はどちらのディレクトリにもないバイグラムを取得します。 – Remco1250

    +0

    それはまさに不可能です! – itsme86

    答えて

    1
    public class DictionaryComparer 
    { 
        public List<string> CompareDictionaries(IDictionary<string, double> first, IDictionary<string, double> second) 
        { 
         var dictionary = new Dictionary<string, int>(); 
         foreach (var f in first) 
         { 
          if (!dictionary.ContainsKey(f.Key)) 
          { 
           dictionary.Add(f.Key, 1); 
          } 
          else 
          { 
           dictionary[f.Key]++; 
          } 
         } 
    
         foreach (var f in second) 
         { 
          if (!dictionary.ContainsKey(f.Key)) 
          { 
           dictionary.Add(f.Key, 1); 
          } 
          else 
          { 
           dictionary[f.Key]++; 
          } 
         } 
    
         return dictionary.Where(s => s.Value == 1).Select(a => a.Key).ToList(); 
    
        } 
    } 
    

    とテスト:

    [TestFixture] 
    public class Test 
    { 
        [Test] 
        public void Compare() 
        { 
         IDictionary<string, double> dictionaryOne = new Dictionary<string, double>() 
         { 
          {"he", 0},{"ey", 0 } 
         }; 
         Dictionary<string, double> dictionaryTwo = new Dictionary<string, double>() 
         { 
          {"he", 0},{"ey", 0 },{"yg", 0 },{"gu", 0 },{"uy", 0 },{"ys", 0 } 
         }; 
         var comparer = new DictionaryComparer(); 
         var list = comparer.CompareDictionaries(dictionaryOne, dictionaryTwo); 
         Assert.That(4, Is.EqualTo(list.Count)); 
         Assert.That("yg", Is.EqualTo(list[0])); 
         Assert.That("gu", Is.EqualTo(list[1])); 
         Assert.That("uy", Is.EqualTo(list[2])); 
         Assert.That("ys", Is.EqualTo(list[3])); 
        } 
    } 
    

    は、それが意味

    +0

    申し訳ありませんが、テストはNunitで書かれています – xszaboj

    +0

    しかし、私は実際には、ちょっと、ちょっと男を使用していると仮定しています。私は欲しいテキストを挿入したい。 – Remco1250

    +0

    私はあなたの答えを使用すると思います!私はforeachループでうまくいくと思います。 – Remco1250

    関連する問題