检测推文是否相似的功能



我正试图在VBA中创建一个函数,该函数接受2个字符串和一个阈值(十进制形式的百分比(,如果字符串包含的相同单词的百分比高于阈值,则返回true。这是我迄今为止的代码。。。

Function isDup(tweet1 As String, tweet2 As String, threshold As Double) As Boolean
'Declare variables to store words from each tweet
Dim C1 As String
Dim C2 As String
'Use split function to split each tweet into single words. The " " is the delimiter, each space creates a new word
C1 = Split(tweet1, " ")
C2 = Split(tweet2, " ")
'Loop through each word from tweet1 and each word from tweet2
For i = LBound(C1) To UBound(C1)
For j = LBound(C2) To UBound(C2)
'Declare variable to store result from StrComp Function
Dim Cresult As Double
'Use StrComp Function to compare the current word from tweet1 to the current word from tweet2
Cresult = StrComp(i, j, vbTextCompare)
Next i
Next j
'Use If Then to return true if the tweets are more similar than the percentage given by the threshold
If Cresult > threshold Then
isDup = True
End Function

我是VBA的新手,所以有一些错误,特别是我一直遇到预期的:数组错误。如有任何帮助,我们将不胜感激,谢谢!

下面是一个快速重写,修复了我在上面评论中提到的内容。如果这不完全是你想要的,它应该会让你进入正轨。

Function isDup(tweet1 As String, tweet2 As String, threshold As Double) As Boolean
'Declare variables to store words from each tweet
Dim C1 As Variant
Dim C2 As Variant
'Use split function to split each tweet into single words. The " " is the delimiter, each space creates a new word
C1 = Split(tweet1, " ")
C2 = Split(tweet2, " ")
'Declare variable to store result from StrComp Function
Dim Cresult As Double
'Loop through each word from tweet1 and each word from tweet2
For i = LBound(C1) To UBound(C1)
For j = LBound(C2) To UBound(C2)
'Use StrComp Function to compare the current word from tweet1 to the current word from tweet2
If StrComp(C1(i), C2(j), vbTextCompare) = 0 Then
Cresult = Cresult + 1
End If
Next j
Next i
'Use If Then to return true if the tweets are more similar than the percentage given by the threshold
If Cresult > threshold Then
isDup = True
End If
End Function

最新更新