Jump to content

Comparing strings


Recommended Posts

I've just written a small script that compares two strings and returns the similarity of those two in %. I know of StringCompare, but I want to get a percentage and I also want to get in touch with Autoit.

Compiling doesn't cause any problems, but actually running it does. In line 20 it has a problem with the index and says "Subscript used on non-accessible variable". What's causing that problem, and how can I solve it? Thanks! And sorry for my ugly style

 

Similarity.au3

Link to comment
Share on other sites

Try this:
 

#include <MsgBoxConstants.au3>

Global $sTitle = "Similarity"
Global $tTextA = InputBox($sTitle, "Data 1: ")
Global $tTextB = InputBox($sTitle, "Data 2: ")
If $tTextA = $tTextB Then Exit MsgBox($MB_TOPMOST, $sTitle, "Smiler: 100%")
Local $nStringLenTextA = StringLen($tTextA)
Local $nStringLenTextB = StringLen($tTextB)
$tTextA = StringReplace($tTextA, " " & " ", " ")
Local $cSpit = StringInStr($tTextA, " ") ? " " : ""
Local $sSpitA = StringSplit($tTextA, $cSpit)
Local $Smiler = 0, $px = UBound($sSpitA)
For $i = 1 To $px - 1
    If StringInStr($tTextB, $sSpitA[$i]) Then $Smiler += 1
    ConsoleWrite($sSpitA[$i] & " : " & $tTextB & @CRLF)
Next
ConsoleWrite($px & @CRLF)
If $px > 3 Then $px -= 1
Local $SmilerPercent = ($Smiler * 100) / ($px - 1);$nStringLenTextB
If $SmilerPercent > 199 Then $SmilerPercent = 99
MsgBox($MB_TOPMOST, $sTitle, "Smiler: " & $SmilerPercent & "%")

Exit

 

Regards,
 

Link to comment
Share on other sites

Wicked_Caty,

$a and $b are NOT arrays.  Try it using stringmid like this...

#include <MsgBoxConstants.au3>

$title = "Similarity"
;~ $a = InputBox($title, "Data 1")
;~ $b = InputBox($title, "Data 2")
local $a = 'abcd'

local $b = 'abce'

$n = 0
$la = StringLen($a)
$lb = StringLen($b)
$lc = ($la + $lb) / 2

If $la > $lb Then
   $l = $la
ElseIf $lb > $la Then
   $l = $lb

Else
   $l = $lc

EndIf



For $i = 1 To $l
    ConsoleWrite($i & @CRLF)
   If (stringmid($a,$i,1) = stringmid($b,$i,1)) Then
      $n = $n + 1
   EndIf

Next

$p = $n / $l * 100

MsgBox($MB_TOPMOST, $title, $a & @CRLF & $b & @CRLF & $title & ":" & @TAB & $p)

Exit

note: only strings of equal length tested...input supplied as strings, not user input

edit: also your loop should start at position "1" for strings

Edited by kylomas
additional info

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

I've just written a small script that compares two strings and returns the similarity of those two in %. I know of StringCompare, but I want to get a percentage and I also want to get in touch with Autoit.

Compiling doesn't cause any problems, but actually running it does. In line 20 it has a problem with the index and says "Subscript used on non-accessible variable". What's causing that problem, and how can I solve it? Thanks! And sorry for my ugly style

 

Similarity.au3

The problem is you're trying to use a variable that is not there.

You can solve it by ensuring you do not try to access a non existent variable.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Wicked_Caty,

Streamlined the code a bit.  If the string lengths are equal just use one or the other as the length value, no need to add them and divide by two.  I used a ternary expression to replace all of your length determination code...

#include <MsgBoxConstants.au3>

$title = "Similarity"
;~ $a = InputBox($title, "Data 1")
;~ $b = InputBox($title, "Data 2")
local $a = 'abcdf'

local $b = 'abce'



local $len_to_check = (stringlen($a) > stringlen($b)) ? stringlen($a) : stringlen($b), $p = 0, $n = 0

For $i = 1 To $len_to_check
   If (stringmid($a,$i,1) = stringmid($b,$i,1)) Then
      $n = $n + 1
   EndIf

Next

$p = $n / $len_to_check * 100

MsgBox($MB_TOPMOST, $title, $a & @CRLF & $b & @CRLF & $title & ":" & @TAB & $p)

Exit

Good Luck,

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

jguinch,

I believe you refer to my unifuzz SQLite extension; start reading here, then have luck finding the actual post where the extension resides later in this same thread. The new search feature is terrible. For the matter of string comparison outside of SQLite, this post may give something to work with.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

According to the Levenshtein algorithm (https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#VBScript), from the VBS code :

$s1 = "My String 1"
$s2 = "My String 2 !"



Local $Levenshtein = Levenshtein($s1, $s2)

Local $iMaxLen = StringLen (  StringLen($s1) > StringLen($s2) ? $s1 : $s2 )
Local $percent = Round(($iMaxLen - $Levenshtein) * 100 / $iMaxLen, 2)

ConsoleWrite($percent & "%")


Func levenshtein( $a, $b )
    Local $i, $j, $cost, $d[1], $min1, $min2, $min3

    If StringLen( $a ) = 0 Then Return StringLen( $b )
    If StringLen( $b ) = 0 Then Return StringLen( $a )

    ReDim $d[ StringLen( $a ) + 1][ StringLen( $b ) + 1]

    For $i = 0 To StringLen( $a )
        $d[$i][0] = $i
    Next

    For $j = 0 To StringLen( $b )
        $d[ 0][$j] = $j
    Next

    For $i = 1 To StringLen( $a )
        For $j = 1 To StringLen( $b )
            $cost =  ( StringMid($a, $i, 1) = StringMid($b, $j, 1) ? 0 : 1)

            $min1 = $d[$i - 1][$j] + 1
            $min2 = $d[$i][$j - 1] + 1
            $min3 = $d[$i - 1][$j - 1] + $cost

            If $min1 <= $min2 And $min1 <= $min3 Then
                $d[$i][$j] = $min1
            ElseIf $min2 <= $min1 And $min2 <= $min3 Then
                $d[$i][$j] = $min2
            Else
                $d[$i][$j] = $min3
            EndIf
        Next
    Next

    Return $d[StringLen( $a )][StringLen( $b )]
EndFunc

 

Link to comment
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
 Share

×
×
  • Create New...