1 | #Region "Microsoft.VisualBasic::7bf5437bac5dcd2ffe436e0ef16a8150, Microsoft.VisualBasic.Core\Text\StringSimilarity\Levenshtein\LevenshteinString.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 | ' Structure LevenshteinString |
35 | ' |
36 | ' Constructor: (+1 Overloads) Sub New |
37 | ' Function: ToString |
38 | ' Operators: (+6 Overloads) Like |
39 | ' |
40 | ' |
41 | ' /********************************************************************************/ |
42 | |
43 | #End Region |
44 | |
45 | Imports System.Runtime.CompilerServices |
46 | |
47 | Namespace Text.Levenshtein |
48 | |
49 | ''' <summary> |
50 | ''' The string like helper |
51 | ''' </summary> |
52 | Public Structure LevenshteinString |
53 | |
54 | Private _string$ |
55 | Private _chars%() |
56 | |
57 | Sub New(s$) |
58 | _string = s |
59 | _chars = s _ |
60 | .Select(AddressOf AscW) _ |
61 | .ToArray |
62 | End Sub |
63 | |
64 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
65 | Public Overrides Function ToString() As String |
66 | Return _string |
67 | End Function |
68 | |
69 | ''' <summary> |
70 | ''' String similarity compares |
71 | ''' </summary> |
72 | ''' <param name="s$"></param> |
73 | ''' <param name="subject"></param> |
74 | ''' <returns></returns> |
75 | ''' |
76 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
77 | Public Shared Operator Like(s$, subject As LevenshteinString) As DistResult |
78 | Return Levenshtein.ComputeDistance( |
79 | s.CharCodes, |
80 | subject._chars, |
81 | Function(a, b) a = b, |
82 | AddressOf ChrW) |
83 | End Operator |
84 | |
85 | ''' <summary> |
86 | ''' String similarity compares |
87 | ''' </summary> |
88 | ''' <param name="query"></param> |
89 | ''' <param name="s$"></param> |
90 | ''' <returns></returns> |
91 | ''' |
92 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
93 | Public Shared Operator Like(query As LevenshteinString, s$) As DistResult |
94 | Return Levenshtein.ComputeDistance( |
95 | query._chars, |
96 | s.CharCodes, |
97 | Function(a, b) a = b, |
98 | AddressOf ChrW) |
99 | End Operator |
100 | |
101 | ''' <summary> |
102 | ''' String similarity compares |
103 | ''' </summary> |
104 | ''' <param name="query"></param> |
105 | ''' <param name="subject"></param> |
106 | ''' <returns></returns> |
107 | ''' |
108 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
109 | Public Shared Operator Like(query As LevenshteinString, subject As LevenshteinString) As DistResult |
110 | Return Levenshtein.ComputeDistance( |
111 | query._chars, |
112 | subject._chars, |
113 | Function(a, b) a = b, |
114 | AddressOf ChrW) |
115 | End Operator |
116 | |
117 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
118 | Public Shared Narrowing Operator CType(s As LevenshteinString) As String |
119 | Return s._string |
120 | End Operator |
121 | |
122 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
123 | Public Shared Widening Operator CType(s$) As LevenshteinString |
124 | Return New LevenshteinString With { |
125 | ._string = s, |
126 | ._chars = s.Select(AddressOf AscW).ToArray |
127 | } |
128 | End Operator |
129 | End Structure |
130 | End Namespace |