1 #Region "Microsoft.VisualBasic::2752fb765f45d7cba9688fcd6c357296, Microsoft.VisualBasic.Core\ComponentModel\DataStructures\Tree\BinaryTree\Extensions.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module Extensions
35     
36     '         Function: NameCompare, NameFuzzyMatch
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports sys = System.Math
44
45 Namespace ComponentModel.DataStructures.BinaryTree
46
47     Public Module Extensions
48
49         ''' <summary>
50         ''' 字符串名字的比较规则:
51         ''' 
52         ''' 假若字符串是空值或者空字符串,则该变量小
53         ''' 假若字符串相等(忽略大小写),则变量值一样
54         ''' 最后逐个字符进行比较,按照字母的charcode大小来比较,第一个charcode大的变量大
55         ''' </summary>
56         ''' <param name="a$"></param>
57         ''' <param name="b$"></param>
58         ''' <returns></returns>
59         Public Function NameCompare(a$, b$) As Integer
60             Dim null1 = String.IsNullOrEmpty(a)
61             Dim null2 = String.IsNullOrEmpty(b)
62
63             If null1 AndAlso null2 Then
64                 Return 0
65             ElseIf null1 Then
66                 Return -1
67             ElseIf null2 Then
68                 Return 1
69             ElseIf String.Equals(a, b, StringComparison.OrdinalIgnoreCase) Then
70                 Return 0
71             Else
72
73                 Dim minl = sys.Min(a.Length, b.Length)
74                 Dim c1, c2 As Char
75
76                 For i As Integer = 0 To minl - 1
77                     c1 = Char.ToLower(a.Chars(i))
78                     c2 = Char.ToLower(b.Chars(i))
79
80                     If c1 <> c2 Then
81                         Return c1.CompareTo(c2)
82                     End If
83                 Next
84
85                 If a.Length < b.Length Then
86                     Return -1
87                 Else
88                     Return 1
89                 End If
90             End If
91         End Function
92
93         ''' <summary>
94         ''' The term index search engine.
95         ''' 
96         ''' If the string similarity less than threshold, then will returns negative value
97         ''' If the string similarity greater than threshold, then will returns positive value
98         ''' If the string text equals to other, then will reutrns ZERO
99         ''' </summary>
100         ''' <param name="a$"></param>
101         ''' <param name="b$"></param>
102         ''' <returns></returns>
103         Public Function NameFuzzyMatch(a$, b$) As Integer
104             Dim similarity = Text.Levenshtein.ComputeDistance(a, b)
105
106             If a.TextEquals(b) Then
107                 Return 0
108             ElseIf similarity Is Nothing Then
109                 Return -1
110             ElseIf similarity.MatchSimilarity < 0.6 Then
111                 Return -1
112             Else
113                 Return 1
114             End If
115         End Function
116     End Module
117 End Namespace