Jump to content
Sign in to follow this  
mattw112

Which function to use?

Recommended Posts

mattw112

Is there a function available that would do this:

Compare 1 string to another string and let me know if ANY word in one string is also in the other string?

So for example:

String1 = "This is a string"

String2 = "The dog is black"

Both strings have "is" in common, so would like a TRUE returned. Or even an array of matching words??

Basically I want to compare various strings against a bunch of other strings and find ones that have similar words because those most likely would be a match.

Another examples would be:

String1 = "Microsoft Office"

String2 = "Office 2007"

These two would be a match on the word Office.

Thanks,

Terry

Share this post


Link to post
Share on other sites
Zedna

Such function doesn't exists but you may make your own UDF.

- StringSplit both strings with space as separator to get array of words

- go through these arrays and compare them

- make third output array with results based on previous comparison

Share this post


Link to post
Share on other sites
tAKTelapis

Hmm, good thinking Zedna.. A recursive search.

Word 1 from array 1 compared to each word in array 2

Word 2 from array 1 compared to each word in array 2

and so forth.

Should be reasonably simple.

Share this post


Link to post
Share on other sites
tAKTelapis

Geez, that code confuses me, i like this simplistic.. i also wanted something to kill a few minutes and make it look like i was working (code on your screen has the ability to do that, so this is what i came up with:

(note: It will place doubles in the array.. so if your 2 sentences contain the same word twice, it will be added to the array twice)

#include <Array.au3>

$string1 = "This is a string"
$string2 = "The dog is black"
$test = _StringCompareWords($string1, $string2)
_ArrayDisplay($test, "test")

Func _StringCompareWords($scw_string1, $scw_string2)
    Dim $scw_wordarray[1]
    $scw_array1 = StringSplit($scw_string1, " ")
    $scw_array2 = StringSplit($scw_string2, " ")
    For $i = 1 to $scw_array1[0]
        For $x = 1 to $scw_array2[0]
            If $scw_array1[$i] = $scw_array2[$x] Then
                _ArrayAdd($scw_wordarray, $scw_array1[$i])
            Else
                ContinueLoop
            EndIf
        Next
    Next
    Return $scw_wordarray
EndFunc ;==> _StringCompareWords()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.