Fuzzy
퍼지.. 1과 1은 어느 정도 같은가????
의 해답…
Public Class CompareWords #Region "Properties" Private totalPointsValue As Int32 ''' <summary> ''' Holds the total match points for the search. ''' </summary> Public ReadOnly Property TotalPoints() As Int32 Get Return Me.totalPointsValue End Get End Property Private matchTypeValue As MatchTypes ''' <summary> ''' Specifies the type of search to use when matching. ''' </summary> ''' <value>Sets the match type.</value> Public Property MatchType() As MatchTypes Get Return Me.matchTypeValue End Get Set(ByVal value As MatchTypes) Me.matchTypeValue = value End Set End Property Private minimumSubstringLengthValue As Int32 ''' <summary> ''' Specifies the minimum substring length to use when matching. ''' Larger numbers return lower scores. Must be greater than zero. ''' </summary> ''' <value>Sets the minimum substring length.</value> Public Property MinimumSubstringLength() As Int32 Get Return Me.minimumSubstringLengthValue End Get Set(ByVal value As Int32) If value > 0 Then Me.minimumSubstringLengthValue = value Else Throw New System.ArgumentOutOfRangeException("Minimum substring length must be greater than zero.") End If End Set End Property Private ignoreCaseValue As Boolean ''' <summary> ''' Specifies if you want to ignore case when matching. ''' </summary> ''' <value>Sets the ignore case value.</value> Public Property IgnoreCase() As Boolean Get Return Me.ignoreCaseValue End Get Set(ByVal value As Boolean) Me.ignoreCaseValue = value End Set End Property #End Region #Region "Constructors" ''' <summary> ''' Initializes this class. Defaults minimum substring length to 4. Defaults ignore case to true. ''' </summary> ''' <param name="matchType">Specifies if you want fast or accurate matching.</param> Public Sub New(ByVal matchType As MatchTypes) Me.MatchType = matchType Me.MinimumSubstringLength = 4 Me.IgnoreCase = True End Sub #End Region #Region "Methods" ''' <summary> ''' Returns a score based on how similar the words are. ''' A score of 100 indicates the strings are identical. ''' </summary> ''' <param name="word1">The first word to compare.</param> ''' <param name="word2">The second word to compare.</param> Public Function GetMatch(ByVal word1 As String, ByVal word2 As String) As Int32 'process caseing preference If Me.IgnoreCase Then word1 = word1.ToLower word2 = word2.ToLower End If 'strings are equal If word1 = word2 Then Return 100 ElseIf word1 = Nothing OrElse word2 = Nothing Then Return 0 Else 'Clear variable for new compare totalPointsValue = 0 'Start calculation FindCommon(word1, word2) 'Return percentage Return Convert.ToInt32((totalPointsValue * 200) / (word1.Length + word2.Length)) End If End Function ''' <summary> ''' Returns True when match score is greater or equal to the minimum score specified. ''' </summary> ''' <param name="word1">The first word to compare.</param> ''' <param name="word2">The second word to compare.</param> ''' <param name="minimumScore">The minimum score of the match in order to determine a successful match.</param> Public Function GetMatch(ByVal word1 As String, ByVal word2 As String, ByVal minimumScore As Int32) As Boolean Dim matchPercentage As Int32 = Me.GetMatch(word1, word2) Return matchPercentage >= minimumScore End Function ''' <summary> ''' Recursively substrings the first word, and compares it to the second. ''' </summary> ''' <param name="word1">The first word to compare.</param> ''' <param name="word2">The second word to compare.</param> Private Sub FindCommon(ByVal word1 As String, ByVal word2 As String) 'Make sure name1 is the longest string If word1.Length < word2.Length Then Dim buf As String = word2 word2 = word1 word1 = buf End If Dim placeHolder As Int32 Dim tempBuffer As String Dim commonString As String = String.Empty For i As Int32 = 0 To word1.Length - 1 'reset substring length placeHolder = Me.MinimumSubstringLength While placeHolder <= (word1.Length - i) AndAlso placeHolder <= word2.Length tempBuffer = word1.Substring(i, placeHolder) If word2.IndexOf(tempBuffer) >= 0 Then If tempBuffer.Length > commonString.Length Then commonString = tempBuffer End If placeHolder += 1 Else Exit While End If End While Next totalPointsValue += commonString.Length 'Stop recursion when strings get too small If commonString <> Nothing Then Select Case Me.MatchType Case MatchTypes.Fast 'Run routine for concatonated data (faster but less accurate) FindCommon(word1.Remove(word1.IndexOf(commonString), commonString.Length), word2.Remove(word2.IndexOf(commonString), commonString.Length)) Case MatchTypes.Accurate 'Run routine for data on the right FindCommon(word1.Substring(0, word1.IndexOf(commonString)), word2.Substring(0, word2.IndexOf(commonString))) 'Run routine for data on the left FindCommon(word1.Substring(word1.IndexOf(commonString) + commonString.Length), word2.Substring(word2.IndexOf(commonString) + commonString.Length)) End Select End If End Sub #End Region Public Enum MatchTypes Fast = 1 Accurate = 2 End Enum End Class
http://www.vbforums.com/showthread.php?t=540094
* 출처: Fuzzy
누구나 수정하실 수 있습니다. 문법은 Formatting Syntax참조하세요. |
---|