使用C#获得最高百分比相似度 [英] Get the highest percentage similarity using C#

查看:197
本文介绍了使用C#获得最高百分比相似度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hye这是我第一次使用c#所以我对它不是很熟悉。我有一个编码来获取文件的百分比相似性。在这个程序中,我已经在记事本中写了文件的所有路径,我将它与输入文件进行比较。输入文件然后运行后,它会将输入文件与记事本中文件的列表路径进行比较,然后列出所有文件的所有百分比相似度。我想知道,我想如何创建编码以从文件比较中获得最高的百分比相似度?然后它会弹出消息,显示所有文件列表中的最高百分比。我的记事本文件名是inputlist.txt。这个程序有双模式。首先在两个输入文件之间进行比较,然后在记事本中将输入文件与路径文件进行比较。这是我的编码:

hye It's the first time I'm using c# so I'm not very familiar with it.I have a coding to get the percentage similarity of files. In this program, I have written all the paths of file in notepad which I will compare it with the input file. After I input file and then run, it will compare the input file with a list path of files in notepad and then it will list all the percentage similarity for all the file. I want to know, How I want to create coding to get the highest percentage similarity from the comparison of files? And then it will pop up message that will show the highest percentage from all the list of files.My notepad file name is inputlist.txt. this program have dual mode. first compare between two input file and second is compare input file with path file in notepad. this is my coding :

private void buttonbrowse1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
        textBoxfile1.Text = openFileDialog1.FileName;
}

private void buttonbrowse2_Click(object sender, EventArgs e)
{
    if (openFileDialog2.ShowDialog() == DialogResult.OK)
        textBoxfile2.Text = openFileDialog2.FileName;
}

private void buttoncompute_Click(object sender, EventArgs e)
{
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    textBox3.Text = "";
    if (!checkBox1.Checked)
    {
       FileInfo f1 = new FileInfo(textBoxfile1.Text);
       FileInfo f2 = new FileInfo(textBoxfile2.Text);
       if (f2.Length < f1.Length)
       {
           string tmp = textBoxfile1.Text;
           textBoxfile1.Text = textBoxfile2.Text;
           textBoxfile2.Text = tmp;
       }
       Process p = new Process();
       p.StartInfo.FileName = "bsdiff.exe";
       p.StartInfo.Arguments = "\"" + textBoxfile1.Text + "\" \"" + textBoxfile2.Text + "\" diff.bin";



       p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
       p.Start();
       p.WaitForExit();
       FileInfo fi = new FileInfo("diff.bin");


       long lenf3 = f1.Length > f2.Length ? f1.Length : f2.Length;
       long filen = (fi.Length - 140) > 0 ? (fi.Length - 140) : 0;
       float diff = (lenf3 - (filen));

       textBox3.Text = "\r\nDifference of two files:" + (filen) + " bytes ";
       float similar = (diff / lenf3) * 100;
       textBox3.Text += "\r\nPercentage of similarity : " + similar + "%";
       timer1.Stop();
     }
     else
     {
        var lines = File.ReadAllLines("inputlist.txt");
        textBox3.Text += "\r\n\r\nComparing\r\nFile1:" + textBoxfile1.Text+"\r\n----------------------------------\r\n";
        foreach (var line in lines)
        {
           textBoxfile2.Text = line;
           FileInfo f1 = new FileInfo(textBoxfile1.Text);
           FileInfo f2 = new FileInfo(textBoxfile2.Text);
           string file1 = textBoxfile1.Text;
           string file2 = textBoxfile2.Text;
           if (f2.Length < f1.Length)
           {
              string tmp = file1;
              file1 = file2;
              file2 = tmp;
           }
           f1 = new FileInfo(file1);
           f2 = new FileInfo(file2);
           Process p = new Process();
           p.StartInfo.FileName = "bsdiff.exe";
           p.StartInfo.Arguments = "\"" + file1 + "\" \"" + file2 + "\" diff.bin";



           p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
           p.Start();
           p.WaitForExit();
           FileInfo fi = new FileInfo("diff.bin");


           long lenf3 = f1.Length > f2.Length ? f1.Length : f2.Length;
           long filen = (fi.Length - 140)>0?(fi.Length - 140):0;
           float diff = (lenf3 - (filen));


           textBox3.Text += "\r\n\r\n" + file1+","+file2;
           textBox3.Text += "\r\nDifference of two files:" + (filen) + " bytes ";
           float similar = (diff / lenf3) * 100;
           textBox3.Text += "\r\nPercentage of similarity : " + similar + "%";

         }
         timer1.Stop();
     }
  }

private void MainForm_Load(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
   groupBox1.Visible = !checkBox1.Checked;
}

private void textBox3_TextChanged(object sender, EventArgs e)
{

}

推荐答案

如果它是纯文本,那么Google的diff-match-patch库应该做你想要的(它有一个C#版本)。

google-diff-match-patch



如果是二进制数据,请查看人们做的事情是将更新应用于可执行文件(bsdiff和Courgette)。他们寻找两个文件之间的最小差异,以便可以向最终用户发送较小的更新。听起来与您的需求类似。



二进制差异/补丁实用程序



Courgette
If it's plain text, then Google's diff-match-patch library ought to do what you want (it has a C# version).
google-diff-match-patch

If it's binary data, then look into the things people do to apply updates to executables (bsdiff and Courgette). They look for the minimum difference between two files so that a smaller update can be sent out to end users. Sounds similar to your needs.

Binary diff/patch utility

Courgette


首先要做的事情。确定最适合您需求的算法。一个好的开始是查看 Levenshtein [ + ]



这里 [ + ]是您使用的C#代码的一个很好的例子。
First things first. Decide which algorithm you want to use that will best suite your needs. A good start will be to look at Levenshtein[+]

Here [+] is a good example of C# code for you to use.


这篇关于使用C#获得最高百分比相似度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆