| 1 | #Region "Microsoft.VisualBasic::ca487e112ea0bd80e3585708d1ca0aac, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\Levenshtein\LevenshteinModel.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 DistResult |
| 35 | ' |
| 36 | ' Properties: Distance, DistEdits, DistTable, Hypotheses, Matches |
| 37 | ' MatchSimilarity, NumMatches, Path, Reference, Score |
| 38 | ' |
| 39 | ' Function: __getReference, __getSubject, __innerInsert, CopyTo, IsPath |
| 40 | ' ToString, TrimMatrix |
| 41 | ' |
| 42 | ' |
| 43 | ' /********************************************************************************/ |
| 44 | |
| 45 | #End Region |
| 46 | |
| 47 | Imports System.Drawing |
| 48 | Imports System.Text.RegularExpressions |
| 49 | Imports Microsoft.VisualBasic.Linq.Extensions |
| 50 | Imports Microsoft.VisualBasic.Net.Protocols |
| 51 | Imports Microsoft.VisualBasic.Text.Xml.Models |
| 52 | Imports sys = System.Math |
| 53 | |
| 54 | Namespace Text.Levenshtein |
| 55 | |
| 56 | Public Class DistResult |
| 57 | |
| 58 | Public Property Reference As String |
| 59 | Public Property Hypotheses As String |
| 60 | Public Property DistTable As Streams.Array.Double() |
| 61 | ''' <summary> |
| 62 | ''' How doest the <see cref="Hypotheses"/> evolve from <see cref="Reference"/>.(这个结果描述了subject是如何变化成为Query的) |
| 63 | ''' </summary> |
| 64 | ''' <returns></returns> |
| 65 | Public Property DistEdits As String |
| 66 | Public Property Path As Coordinate() |
| 67 | Public Property Matches As String |
| 68 | |
| 69 | Public Overrides Function ToString() As String |
| 70 | Return $"{Reference} => {Hypotheses} // {DistEdits}" |
| 71 | End Function |
| 72 | |
| 73 | Public Function IsPath(i As Integer, j As Integer) As Boolean |
| 74 | Dim pt As New Point With {.X = i, .Y = j} |
| 75 | Dim LQuery% = (From c As Coordinate |
| 76 | In Path |
| 77 | Where c = pt |
| 78 | Select 100).FirstOrDefault |
| 79 | Return LQuery > 50 |
| 80 | End Function |
| 81 | |
| 82 | Public ReadOnly Property Distance As Double |
| 83 | Get |
| 84 | If DistTable.IsNullOrEmpty Then |
| 85 | Return 0 |
| 86 | End If |
| 87 | |
| 88 | Dim reference As String = __getReference() |
| 89 | Dim hypotheses As String = __getSubject() |
| 90 | |
| 91 | Return DistTable(reference.Length) _ |
| 92 | .Values(hypotheses.Length) |
| 93 | End Get |
| 94 | End Property |
| 95 | |
| 96 | ''' <summary> |
| 97 | ''' 可以简单地使用这个数值来表述所比较的两个对象之间的相似度 |
| 98 | ''' </summary> |
| 99 | ''' <returns></returns> |
| 100 | Public ReadOnly Property Score As Double |
| 101 | Get |
| 102 | If String.IsNullOrEmpty(DistEdits) Then |
| 103 | Return 0 |
| 104 | End If |
| 105 | |
| 106 | Dim view As String = DistEdits.ToLower |
| 107 | Dim m As Integer = view.Count("m"c) |
| 108 | Dim i As Integer = view.Count("i"c) |
| 109 | Dim d As Integer = view.Count("d"c) |
| 110 | Dim s As Integer = view.Count("s"c) |
| 111 | Dim len As Integer = view.Length |
| 112 | |
| 113 | Return (m - (i * 0.5 + d * 0.3 + s * 0.2)) / len |
| 114 | End Get |
| 115 | End Property |
| 116 | |
| 117 | ''' <summary> |
| 118 | ''' ``m+`` scores.(0-1之间) |
| 119 | ''' </summary> |
| 120 | ''' <returns></returns> |
| 121 | Public ReadOnly Property MatchSimilarity As Double |
| 122 | Get |
| 123 | Dim ms As String() = Regex.Matches(DistEdits, "m+").ToArray |
| 124 | Dim mg = (From x In ms Select x Group x By x Into Count).ToArray |
| 125 | Dim len = DistEdits.Length |
| 126 | Dim score As Double |
| 127 | |
| 128 | For Each x In mg |
| 129 | score += (x.x.Length / len) * x.Count |
| 130 | Next |
| 131 | |
| 132 | Return score |
| 133 | End Get |
| 134 | End Property |
| 135 | |
| 136 | ''' <summary> |
| 137 | ''' 比对上的对象的数目 |
| 138 | ''' </summary> |
| 139 | ''' <returns></returns> |
| 140 | Public ReadOnly Property NumMatches As Integer |
| 141 | Get |
| 142 | If String.IsNullOrEmpty(DistEdits) Then |
| 143 | Return 0 |
| 144 | End If |
| 145 | |
| 146 | Dim view As String = DistEdits.ToLower |
| 147 | Dim m As Integer = view.Count("m"c) |
| 148 | Return m |
| 149 | End Get |
| 150 | End Property |
| 151 | |
| 152 | Public Function CopyTo(Of T As DistResult)(ByRef obj As T) As T |
| 153 | obj.Path = Path |
| 154 | obj.DistEdits = DistEdits |
| 155 | obj.DistTable = DistTable |
| 156 | obj.Hypotheses = Hypotheses |
| 157 | obj.Matches = Matches |
| 158 | obj.Reference = Reference |
| 159 | Return obj |
| 160 | End Function |
| 161 | |
| 162 | Public Function TrimMatrix(l As Integer) As Streams.Array.Double() |
| 163 | Me.DistTable = Me.DistTable _ |
| 164 | .Select(Function(row) |
| 165 | Dim values#() = row _ |
| 166 | .Values _ |
| 167 | .Select(Function(n) sys.Round(n, l)) _ |
| 168 | .ToArray |
| 169 | |
| 170 | Return New Streams.Array.Double With { |
| 171 | .Values = values |
| 172 | } |
| 173 | End Function) _ |
| 174 | .ToArray |
| 175 | |
| 176 | Return Me.DistTable |
| 177 | End Function |
| 178 | |
| 179 | Protected Friend Overridable Function __innerInsert() As String |
| 180 | Return "" |
| 181 | End Function |
| 182 | |
| 183 | Protected Friend Overridable Function __getReference() As String |
| 184 | Return Reference |
| 185 | End Function |
| 186 | |
| 187 | Protected Friend Overridable Function __getSubject() As String |
| 188 | Return Hypotheses |
| 189 | End Function |
| 190 | End Class |
| 191 | End Namespace |