Функция, которая обнаруживает, если твиты похожи - PullRequest
0 голосов
/ 25 марта 2020

Я пытаюсь создать функцию в 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, поэтому есть некоторые ошибки, особенно я продолжаю сталкиваться с Ожидаемой: Ошибка массива. Любая помощь будет принята с благодарностью, спасибо!

1 Ответ

1 голос
/ 25 марта 2020

Вот быстрое переписывание, исправляющее вещи, которые я отметил в моих комментариях выше. Если это не совсем то, что вы хотели, это должно привести вас в стадион.

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
...