2012-12-07 12 views
3

我正在對可視C# 來計算字錯誤率 我有一個文本框的refrence這是正確的誹謗 和一個用於假設是錯誤的。在字符串中匹配字符在視覺C#

爲了計算WER我需要計算: 替代:已被改變的話這是我的第一個問題 插入:已被插入刪除句子 的話:那已經從刪除的話原句

對於EX:

參考:這是一個不良貸款計劃。 假設:這是一個非常酷的。

它:替代 是:正確 的:替代 NPL:正確 程序:刪除 涼:插

我試過dasblinkenlight建議(非常感謝你的方式)的算法 我的工作但有一個運行時錯誤,我找不出來,在行

int x= Compute(buffer[j], buffer_ref[i]); 

索引超出了數組的界限。

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     string [] hyp = new string[20]; 
     string [] refrence = new string[20]; 
     string [] Anser= new string[20]; 
     string[] buffer = new string[20]; 
     string[] buffer_ref = new string[20]; 
     int count = 0; // number of words 
     string ref2=" " ; 
     string hyp2 = " "; 
     string Anser2 = " "; 
     string buffer2 = " "; 

     int corecct_c=0; 
     int corecct_d = 0; 
     int corecct_i = 0; 

     //==================================================================== 

     public Form1() 
     { 
      InitializeComponent(); 
      for (int i = 0; i <= 19; ++i) 
      { 
       hyp[i] = null; 
       buffer[i] = null; 
      } 
     } 

     private void textBox2_TextChanged(object sender, EventArgs e) 
     { 
      refrence = this.textBox2.Text.Split(' '); 
      buffer_ref = this.textBox2.Text.Split(' '); 


     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
      hyp = this.textBox1.Text.Split(' '); 
      buffer = this.textBox1.Text.Split(' '); 
      //hyp = this.textBox1.Text; 
      // fname1.Add(this.textBox1.Text); 


     } 

     public void correct(string[] R) 
     { 

      for (int i = 0; (i <= 19) && (R[i] != "."); ++i) 
      { 

       if (buffer[i] == refrence[i]) 
       { buffer[i] = "0"; 
       buffer_ref[i] = "0"; 
        corecct_c = corecct_c + 1; 
        Anser[i] = "C"; 
       } 
      } 

     } 

     // function that compute 2 strings 
     public static int Compute(string s, string t) 
     { 
      int n = s.Length; 
      int m = t.Length; 
      int[,] d = new int[n + 1, m + 1]; 

      // Step 1 
      if (n == 0) 
      { 
       return m; 
      } 

      if (m == 0) 
      { 
       return n; 
      } 

      // Step 2 
      for (int i = 0; i <= n; d[i, 0] = i++) 
      { 
      } 

      for (int j = 0; j <= m; d[0, j] = j++) 
      { 
      } 

      // Step 3 
      for (int i = 1; i <= n; i++) 
      { 
       //Step 4 
       for (int j = 1; j <= m; j++) 
       { 
        // Step 5 
        int cost = (t[j - 1] == s[i - 1]) ? 0 : 1; 

        // Step 6 
        d[i, j] = Math.Min(
         Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), 
         d[i - 1, j - 1] + cost); 
       } 
      } 
      // Step 7 
      return d[n, m]; 
     } 


     public void sub(){ 

      for (int j = 0;j<=19;j++) 
     { 
      if (buffer[j].IndexOf("0") != -1) 
      { 


       for (int i = 0; i <= 19; i++) 
       { 

        if (buffer_ref[j].IndexOf("0") != -1) 
        { 

         int x= Compute(buffer[j], buffer_ref[i]); 
         if (x > 3) 
         { 
          buffer[j] = "0"; 
          Anser[j] = "S"; 

         } 


        }//end if 

       } 

      }//end if 


     }//end for 

     }// end fun 

     private void button1_Click(object sender, EventArgs e) 
     { 


      correct(refrence); 
      sub(); 
      for (int i = 0; (i <= 19) && (refrence[i] != "."); ++i) 
      { 
       //loop intialize 
       ref2 = ref2 + " " + refrence[i]; 
       hyp2 = hyp2 + " " + hyp[i]; 
       Anser2 = Anser2 + " " + Anser[i]; 
       buffer2 = buffer2 + " " + buffer[i]; 
       count++; 
          } 

      listBox1.Items.Add(" Refrence :" + ref2); 
      listBox1.Items.Add(" HYp :" + hyp2); 
      listBox1.Items.Add(" Anser:" + Anser2); 
      listBox1.Items.Add(" buffer:" + buffer2); 
      listBox1.Items.Add(count); 

     } 




     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 



     private void button2_Click(object sender, EventArgs e) 
     { 

     } 

     private void label2_Click(object sender, EventArgs e) 
     { 

     } 

     private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

    } 
} 

你能幫助我嗎?

+0

歡迎,只是擡頭,我們已經有一些類似的問題,http://stackoverflow.com/questions/5344514/testing-for-similar-string-content,http://stackoverflow.com/questions/ 747169/c-sharp-comparison-similar-strings和http://stackoverflow.com/questions/1918838/match-similar-names-in-c-sharp –

+1

什麼程度是「相似」?爲了確定兩個字符串「相似」,有多少個字母必須相同? – Mikk

+0

謝謝@NathanKoop只是想把它提起來:) –

回答

5

還有就是要測試,如果兩行相同內置的方式,但沒有內置的方式說,如果兩行是類似的。您需要實施一種測量字符串相似性的算法,例如Levenshtein Distance - 一種非常常見的算法。具有小編輯距離的行可以根據您的要求特定的某個閾值聲明相似。

+2

+1你更快:) AC#執行萊文斯坦距離算法:http://www.dotnetperls.com/levenshtein – GolfWolf

+0

你可以請檢查我的代碼,我相信我做錯了什麼,但我現在不是什麼 – Glory

+0

@ user1885980'sub()'從哪裏來? – dasblinkenlight

2

你需要使用一個algorithm比較兩個字符串之間的「距離」:

一個匹配的緊密度在 必要的基本操作的數量方面被測量到的字符串轉換成精確匹配 。這個數字被稱爲字符串和 模式之間的編輯距離。通常的基本操作是:

insertion: cot → coat 
deletion: coat → cot 
substitution: coat → cost 
+0

請問您可以檢查我的代碼,我相信我做錯了什麼,但我現在不是什麼 – Glory

+0

緩衝在你的代碼中做什麼?要計算'a1'和'a2'之間的「距離」,您需要這樣做:'var distance = LevenshteinDistance.Compute(a1,a2)' –

+0

緩衝區是一個溫度,我可以刪除正確的單詞,以便代碼可以跳過他們 – Glory