我想比较两个文件或文本框,找出它们之间的相似程度

本文关键字:之间 程度 相似 文本 比较 文件 两个 | 更新日期: 2023-09-27 18:28:11

很抱歉打扰您。我已经删除了代码并编辑了帖子。。。

真正的问题是我试图找出两个文本或文件之间的相似程度或抄袭行为。我该怎么做?如果你引导我。。。

我需要上面算法的代码包含在我的项目中。

使用visualstudio2013。。。c#

编辑时间:k到目前为止我已经做到了。。。

        int i = 0;
        int j = 0;
        long lena1 = txtFile1.Text.Length;
        long lenb1 = lena1;
        long len2 = txtFile2.Text.Length;
        string str1 = txtFile1.Text;
        string str2 = txtFile2.Text;
        string str3;
        bool match = false;
        int count = 0;
        int nowords1 = 0;
        int nowords2 = 0;
        string str4;
        int k = 0;
        int m = 0;
        int nowords_match = 0;

        char[] array1 = str1.ToArray();
        char[] array2 = str2.ToArray();
        int[] loc1 = new int[1048576];
        int[] loc2 = new int[1048576];
        while (i < array1.Length)
        {
            if (array1[i] == ' ')
            {
                nowords1++;
                loc1[j] = i;
                j++;
            }
            i++;
        }
        i = j = 0;
        while (i < array2.Length)
        {
            if (array2[i] == ' ')
            {
                nowords2++;
                loc2[j] = i;
                j++;
            }
            i++;
        }
        i = j = 0;
        m = 0;
        for (k = 0; k < loc1.Length-2; k++)
        {
            str3 = str1.Substring(loc1[m], loc1[m + 1] - loc1[m]);
            match = true;
            if (match == true && count > 3)
            {
               txtPlagiarism.Text += " " + loc1[i-3] + loc1[i-2] + " " + loc1[i];
            }
            else
            {
                count = 0;
                match = false;
            }
            j = 0;
            i = 0;
            while (i < nowords2)
            {
                if (j != nowords2)
                {
                    str4 = str2.Substring(loc2[j], loc2[j + 1] - (loc2[j]));
                }
                else
                {
                    break;
                }
                if (str4.Equals(str3)) 
                {
                    nowords_match++;
                    count ++;
                }
                j++;
                i++;
            }
            m++;
        }

我只是在计算匹配的字数,这样我就可以从first_file文本到copy case文本中选择这个字数。但是我得到了一个运行时错误。

**System.ArgumentOutOfRangeException was unhandled
  HResult=-2146233086
  Message=Length cannot be less than zero.
Parameter name: length
  Source=mscorlib
  ParamName=length
  StackTrace:
       at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
   at System.String.Substring(Int32 startIndex, Int32 length)
   at Calculate_File_Checksum.Form1.btnDetectPlagiairism_Click(Object sender, EventArgs e) in c:'Users'BLOOM'Documents'Visual Studio 2013'App2Test'Calculate_File_Checksum'Calculate_File_Checksum'Form1.cs:line 363
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Calculate_File_Checksum.Program.Main() in c:'Users'BLOOM'Documents'Visual Studio 2013'App2Test'Calculate_File_Checksum'Calculate_File_Checksum'Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
  InnerException:** 

我不明白为什么会这样??因为我在里面给出了正确的值。。。请帮助任何人。

我想比较两个文件或文本框,找出它们之间的相似程度

有许多方法可以比较字符串的相似性。以下是Martin为Levenstein距离制定的算法

在我的一个项目中,我必须检测对象数组中的变化,并确定插入了哪些对象,删除了哪些对象。也许这个算法可以用于您的任务。这里有一些伪代码,您可以将其改编为C#。

最简单的方法是逐字符比较字符串。如果你发现效果不好,你可以试着逐字、逐行或逐段进行比较。

想法是:

  • 创建两个"指针",每个指针指向相应字符串的开头
  • 如果这些指针下的字符(或单词或其他)不同,则将两个指针提前一个位置(一个字符或一个单词等)
  • 当指针移动时,它会将所有跨越的字符(或单词等)存储在地图(字典)中
  • 在每个步骤之后,每个指针都会检查其当前字符(或单词)是否存在于另一个指针的映射中
  • 如果发现了这样一个,那么这是在一定程度的差异之后发现的巧合。在这种情况下,我们将此字符(单词)标记为重合,增加找到的重合字符(单词(word)的数量,将两个指针移动到此字符(word),并清除这两个映射,以从头开始搜索下一个重合
  • 否则,将两个指针再移动一步,然后重复检查

请注意,逐个字符的搜索可能会导致单个字符被检测到,并且结果可能很奇怪。但无论如何,比较任意字符串并不是一件容易的事。

String str1="...";
String str2="...";
int i=-1,j=-1,p=-1,q=-1,coincidence=0;
TreeMap<char,int> map1=new TreeMap<char,int>();
TreeMap<char,int> map2=new TreeMap<char,int>();
while(true){
    if(i<str1.length) i++;
    if(j<str2.length) j++;
    char c1 = (i==str1.length) ? 0 : str1[i] ;
    char c2 = (j==str2.length) ? 0 : str2[j] ;
    if(c1!=0) map1.put(c1,i);
    if(c2!=0) map2.put(c2,j);
    int i2 = (c2==0) ? 0 : map1.get(c2) ;
    int j2 = (c1==0) ? 0 : map2.get(c1) ;
    if( i2!=0 || j2!=0 || ( i==str1.length && j==str2.length ) ){
        if(i2!=0) j2=0;
        if(i2!=0) i=i2;
        if(j2!=0) j=j2;
        if( i2!=0 || j2!=0 ) coincidence++;
        p=i;
        q=j;
        map1.clear();
        map2.clear();
    }
    if( i==str1.length && j==str2.length ) break;
}
print("Length of text 1: "+str1.length);
print("Length of text 2: "+str2.length);
print("Amount of coincidence: "+coincidence);