-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.cs
102 lines (89 loc) · 3.08 KB
/
Search.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Threading;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var r1 = "Ïðåäñòàâèòåëü òîðãîâûé â êîìïàíèþ ýòê ôåððåðî";
var r2 = "Òîðãîâûé ïðåäñòàâèòåëü ";
var t = new Fuzzy();
for(int i = 1; i <= 10; i++)
{
Console.WriteLine("Äëèíà ïîäñòðîêè " + i + ": " + t.IndistinctMatching(i, r1, r2).ToString("#.##") + "%");
}
}
}
public class Fuzzy
{
//------------------------------------------------------------------------------
//MaxMatching - ìàêñèìàëüíàÿ äëèíà ïîäñòðîêè (äîñòàòî÷íî 3-4)
//strInputMatching - ñðàâíèâàåìàÿ ñòðîêà
//strInputStandart - ñòðîêà-îáðàçåö
// Ñðàâíèâàíèå áåç ó÷åòà ðåãèñòðà
// if IndistinctMatching(4, "ïîèñêîâàÿ ñòðîêà", "îðèãèíàëüíàÿ ñòðîêà - ýòàëîí") > 40 then ...
struct RetCount
{
public long lngSubRows;
public long lngCountLike;
}
RetCount Matching(string strInputA, string strInputB, int lngLen, bool isCase)
{
RetCount TempRet;
int PosStrA;
int PosStrB;
string strTempA;
string strTempB;
TempRet.lngCountLike = 0;
TempRet.lngSubRows = 0;
for (PosStrA = 0; PosStrA <= strInputA.Length - lngLen; PosStrA++)
{
strTempA = strInputA.Substring(PosStrA, lngLen);
for (PosStrB = 0; PosStrB <= strInputB.Length - lngLen; PosStrB++)
{
strTempB = strInputB.Substring(PosStrB, lngLen);
if (string.Compare(strTempA, strTempB, isCase) == 0)
{
TempRet.lngCountLike += 1;
break;
}
}
TempRet.lngSubRows += 1;
}
return TempRet;
}
public float IndistinctMatching(int MaxMatching, string strInputMatching, string strInputStandart, bool isCase = true)
{
if (strInputMatching == null || strInputStandart == null || MaxMatching == 0) return 0;
if (strInputMatching == strInputStandart) return 100;
strInputMatching = Regex.Replace(strInputMatching, @"[\W\s]", "");
strInputStandart = Regex.Replace(strInputStandart, @"[\W\s]", "");
RetCount gret;
RetCount tret;
int lngCurLen; //òåêóùàÿ äëèíà ïîäñòðîêè
gret.lngCountLike = 0;
gret.lngSubRows = 0;
// Öèêë ïðîõîäà ïî äëèíå ñðàâíèâàåìîé ôðàçû
for (lngCurLen = 1; lngCurLen <= MaxMatching; lngCurLen++)
{
//Ñðàâíèâàåì ñòðîêó A ñî ñòðîêîé B
tret = Matching(strInputMatching, strInputStandart, lngCurLen, isCase);
gret.lngCountLike += tret.lngCountLike;
gret.lngSubRows += tret.lngSubRows;
//Ñðàâíèâàåì ñòðîêó B ñî ñòðîêîé A
tret = Matching(strInputStandart, strInputMatching, lngCurLen, isCase);
gret.lngCountLike += tret.lngCountLike;
gret.lngSubRows += tret.lngSubRows;
}
if (gret.lngSubRows == 0) return 0;
return (float)(gret.lngCountLike * 100.0 / gret.lngSubRows);
}
}
}