c# - 在 Visual C# 中匹配字符串中的字符

标签 c#

我正在研究可视化 C# 计算单词错误率 我有一个文本框供引用,这是正确的句子 一个是错误的假设。

为了计算 WER,我需要计算: substitution :被改变的词,这是我的第一个问题 插入:句子中插入的单词 Deleted:原句中被删除的词

对于 EX:

引用:这是一个不良贷款计划。 假设:是一个NPL cool。

它:替换 是正确的 一个:替换 不良贷款:正确 程序:已删除 酷:插入

我尝试了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)
        {

        }

    }
}

你能帮帮我吗?

最佳答案

有一种内置方法可以测试两条线是否相同,但没有内置方法可以判断两条线是否相似。你需要实现一个衡量字符串相似度的算法,比如 Levenshtein Distance - 一个很常见的Edit Distance算法。根据特定于您的要求的某些阈值,可以将具有小编辑距离的行声明为相似。

关于c# - 在 Visual C# 中匹配字符串中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13766956/

相关文章:

c# - EF 代码首先使用空查找表设置外键

c# - 尝试上传时 Controller 操作未从 View 中抓取文件

c# - 是否可以从内部用常量代码包装一个方法?

c# - if 语句中的数据绑定(bind)

c# - 无法读取 dotnet 核心中链接的 appsettings.json 文件中的值

c# - 在 ListView 中显示数据表中的图像

c# - ManageUserViewModel 类在哪里?

c# - 为什么推荐继承Exception而不是ApplicationException

c# - Linq 条件 .Any() 选择

c# - Selenium Chrome C# 更改现有 Web 驱动程序上的用户代理而不创建新驱动程序?