-
Notifications
You must be signed in to change notification settings - Fork 0
/
Similar.cs
218 lines (212 loc) · 7.01 KB
/
Similar.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
namespace Utility
{
public class Similar
{
public static string GetMaxSameLengthString(string s1, string s2)
{
int len = s1.Length;
string previousNode = string.Empty;
for (int i = 0; i < len; i++)
{
for (int j = i, k = 1; j < len; j++)
{
string ss = s1.Substring(i, k);
if (s2.Contains(ss))
{
if (ss.Length > previousNode.Length)
{
previousNode = ss;
}
if (j == len - 1)
{
i = len - 1;
break;
}
k++;
}
else
{
break;
}
}
}
return previousNode;
}
public static double GetSimilar(string s1, string s2)
{
int len = s1.Length;
string previousNode = string.Empty;
for (int i = 0; i < len; i++)
{
for (int j = i, k = 1; j < len; j++)
{
string ss = s1.Substring(i, k);
if (s2.Contains(ss))
{
if (ss.Length > previousNode.Length)
{
previousNode = ss;
}
if (j == len - 1)
{
i = len - 1;
break;
}
k++;
}
else
{
break;
}
}
}
return previousNode.Length * 2 / (s1.Length + s2.Length);
}
public static List<string> GetAllSameLengthString(string s1, string s2)
{
List<string> list = new List<string>();
int len = s1.Length;
for (int i = 0; i < len; i++)
{
string previousNode = string.Empty;
for (int j = i, k = 1; j < len; j++)
{
string ss = s1.Substring(i, k);
if (s2.Contains(ss))
{
previousNode = ss;
if (j == len - 1)
{
i = len - 1;
break;
}
k++;
}
else
{
break;
}
}
if (previousNode.Length > 0)
{
if (!list.Contains(previousNode))
{
list.Add(previousNode);
}
}
}
return list;
}
public static double getSimilarity(string doc1, string doc2)
{
if (!string.IsNullOrEmpty(doc1) && !string.IsNullOrEmpty(doc2))
{
Dictionary<char, int[]> dict = new Dictionary<char, int[]>();
for (int i = 0, len = doc1.Length; i < len; i++)
{
char key = doc1[i];
if (!dict.ContainsKey(key))
{
int[] arr = new int[2];
arr[0] = 1;
dict.Add(key, arr);
}
else
{
int[] arr = dict[key];
arr[0]++;
dict[key] = arr;
}
}
for (int i = 0, len = doc2.Length; i < len; i++)
{
char key = doc2[i];
if (!dict.ContainsKey(key))
{
int[] arr = new int[2];
arr[1] = 1;
dict.Add(key, arr);
}
else
{
int[] arr = dict[key];
arr[1]++;
dict[key] = arr;
}
}
double sqdoc1 = 0;
double sqdoc2 = 0;
double denominator = 0;
foreach (KeyValuePair<char, int[]> kvp in dict)
{
int[] c = kvp.Value;
denominator += c[0] * c[1];
sqdoc1 += c[0] * c[0];
sqdoc2 += c[1] * c[1];
}
return denominator / Math.Sqrt(sqdoc1 * sqdoc2);
}
else
{
return 0;
}
}
public static double levenshtein(string str1, string str2)
{
//计算两个字符串的长度。
int len1 = str1.Length;
int len2 = str2.Length;
//建立上面说的数组,比字符长度大一个空间
int[,] dif = new int[len1 + 1, len2 + 1];
//赋初值,步骤B。
for (int a = 0; a <= len1; a++)
{
dif[a, 0] = a;
}
for (int a = 0; a <= len2; a++)
{
dif[0, a] = a;
}
//计算两个字符是否一样,计算左上的值
int temp;
for (int i = 1; i <= len1; i++)
{
for (int j = 1; j <= len2; j++)
{
if (str1[i - 1] == str2[j - 1])
{
temp = 0;
}
else
{
temp = 1;
}
int[] arr = new int[] { dif[i - 1, j - 1] + temp, dif[i, j - 1] + 1, dif[i - 1, j] + 1 };
int min = arr[0];
for (int k = 0; k < arr.Length; k++)
{
if (arr[k] < min)
{
min = arr[k];
}
}
dif[i, j] = min;
}
}
//Console.WriteLine("字符串\"" + str1 + "\"与\"" + str2 + "\"的比较");
//取数组右下角的值,同样不同位置代表不同字符串的比较
//Console.WriteLine("差异步骤:" + dif[len1, len2]);
//计算相似度
double similarity = 1 - (double)dif[len1, len2] / (double)Math.Max(str1.Length, str2.Length);
//Console.WriteLine("相似度:" + similarity);
return similarity;
}
}
}