| 1 | #Region "Microsoft.VisualBasic::6ae8cf02dcd96b1b68976fabbdf5cb7d, Microsoft.VisualBasic.Core\Text\StringSimilarity\IEqualityComparer.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 | ' Class StringEqualityHelper |
| 35 | ' |
| 36 | ' Properties: BinaryEquals, TextEquals |
| 37 | ' |
| 38 | ' Constructor: (+1 Overloads) Sub New |
| 39 | ' Function: Equals, GetHashCode, ToString |
| 40 | ' |
| 41 | ' |
| 42 | ' /********************************************************************************/ |
| 43 | |
| 44 | #End Region |
| 45 | |
| 46 | Imports System.Runtime.CompilerServices |
| 47 | Imports Microsoft.VisualBasic.Text.Levenshtein |
| 48 | |
| 49 | Namespace Text.Similarity |
| 50 | |
| 51 | ''' <summary> |
| 52 | ''' thresholds: |
| 53 | ''' |
| 54 | ''' + 0: text equals |
| 55 | ''' + 1: binary equals |
| 56 | ''' + (0, 1): similarity threshold |
| 57 | ''' </summary> |
| 58 | Public Class StringEqualityHelper : Implements IEqualityComparer(Of String) |
| 59 | |
| 60 | ''' <summary> |
| 61 | ''' + 0: text equals |
| 62 | ''' + 1: binary equals |
| 63 | ''' + (0, 1): similarity threshold |
| 64 | ''' </summary> |
| 65 | Dim threshold# |
| 66 | Dim compare As GenericLambda(Of String).IEquals |
| 67 | |
| 68 | ''' <summary> |
| 69 | ''' thresholds: |
| 70 | ''' |
| 71 | ''' + 0: text equals |
| 72 | ''' + 1: binary equals |
| 73 | ''' + (0, 1): similarity threshold |
| 74 | ''' </summary> |
| 75 | Sub New(threshold#) |
| 76 | Me.threshold = threshold |
| 77 | |
| 78 | If threshold = 0R Then |
| 79 | compare = Function(s1, s2) s1.TextEquals(s2) |
| 80 | ElseIf threshold = 1 Then |
| 81 | compare = Function(s1, s2) s1 = s2 |
| 82 | Else |
| 83 | compare = Function(s1, s2) |
| 84 | With LevenshteinDistance.ComputeDistance(s1, s2) |
| 85 | Return?.MatchSimilarity >= threshold |
| 86 | End With |
| 87 | End Function |
| 88 | End If |
| 89 | End Sub |
| 90 | |
| 91 | Public Shared ReadOnly Property TextEquals As New StringEqualityHelper(0) |
| 92 | Public Shared ReadOnly Property BinaryEquals As New StringEqualityHelper(1) |
| 93 | |
| 94 | Public Overrides Function ToString() As String |
| 95 | If threshold = 0R Then |
| 96 | Return "Text Equals Of the Two String" |
| 97 | ElseIf threshold = 1.0R Then |
| 98 | Return "Binary Equals Of the Two String" |
| 99 | Else |
| 100 | Return "String Similarity With threshold " & threshold |
| 101 | End If |
| 102 | End Function |
| 103 | |
| 104 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 105 | Public Overloads Function Equals(x As String, y As String) As Boolean Implements IEqualityComparer(Of String).Equals |
| 106 | Return compare(x, y) |
| 107 | End Function |
| 108 | |
| 109 | Public Overloads Function GetHashCode(obj As String) As Integer Implements IEqualityComparer(Of String).GetHashCode |
| 110 | If obj Is Nothing Then |
| 111 | Return 0 |
| 112 | End If |
| 113 | Return obj.GetHashCode |
| 114 | End Function |
| 115 | End Class |
| 116 | End Namespace |