Wicked_Caty

Comparing strings

8 posts in this topic

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

Share this post


Link to post
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,
 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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
1 person likes this

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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
1 person likes this

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

Monkey's are, like, natures humans.

Share this post


Link to post
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

Share this post


Link to post
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.

1 person likes this

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)

Share this post


Link to post
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

 

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

  • Similar Content

    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1+ System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • FMS
      By FMS
      Hello,
      I'm trying to read a binary file to an array but couln't get it to work.
      Also I coul not find any help in the forum around this subject whish was helpfull.
      Is there any way it could be done?
      I tried a lot of ways but maybe somebody know's the right way?
      #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> #include <Array.au3> #include <AutoItConstants.au3> Local $in=FileOpen("TEST_labels.idx1-ubyte",16) ; 16+0=Read binary Local $data = FileRead($in) Local $FileArray = BinaryToString($data,4) ;~ $FileArray = StringSplit($BinarydData, @CRLF, 1+2) ;~ Local $FileArray = StringRegExp($BinarydData, "[^\r\n]+", 3) FileClose($in) _ArrayDisplay($FileArray,"$FileArray","",32) MsgBox($MB_SYSTEMMODAL, "", "$FileArray = " & $FileArray )  
      TEST_labels.idx1-ubyte
    • ur
      By ur
      I am reading a CSV file which is tab seperated as below.
      Local $aArray = FileReadToArray($file) And now, I am splitting this main array record wise so that Array contains internally another arrow to represent each row.
      For $i = 0 to (UBound($aArray) - 1) ;MsgBox(0,"",$aArray[$i]) $aArray[$i] = StringSplit(StringStripCR($aArray[$i]), Chr(9),2);Chr(9) for tab ;_ArrayDisplay($aArray[$i]) Next Afther that, _ArrayDIsplay is able to see the individual internal arrays.
      _ArrayDisplay($aArray[1]) But If I try to access the individual element of it as below.It is not showing any result.
      MsgBox(0,"",$aArray[1][1]) Any suggestion, below is the sample csv file.
      New Text Document.csv
    • Jibsbrown
      By Jibsbrown
      Need some help understanding why the ConsoleWrite works inside 2nd For loop but not out side. Between Audit Wiki, Help file , Forum searching (lots of code reading), and YouTube ( shout out to TutsTeach), I have not been able to find the reason why. 
      $sIniPath = "installLog.ini" ; - Get section name $iniSctionNames = IniReadSectionNames($sIniPath) ; - Get Keys and Vaules For $a = 1 to UBound($iniSctionNames) - 1 $keys = IniReadSection($sIniPath , $iniSctionNames[$a]) For $b = 1 to UBound($keys) - 1 $oldSysInfo = IniRead($sIniPath , $iniSctionNames[1], $keys[$b][0], "") $PntIPInfo = IniRead($sIniPath , $iniSctionNames[2], $keys[$b][0], "") $NewPCInfor = IniRead($sIniPath , $iniSctionNames[3], $keys[$b][0], "") ;ConsoleWrite($oldSysInfo & @LF) Next ;ConsoleWrite($oldSysInfo & @LF) Next ConsoleWrite($oldSysInfo) My intention is to use the variables later for Listboxes. Any explanation, forum post links or whatever would help. Sorry also very very new to Autoit.
      Also here's the ini file.
      [OldSysInfo] 4=192.168.0.4|DESKTOP-RDIU2SN|R90M05Q8 5=192.168.0.5|SD0123456789101|R9WGP9P 6=192.168.0.6|SD0123456789102|R9WGP9PT 3=192.168.0.3|DESKTOP-3RS4LKL|R9WGP9P 23=192.168.0.23|SD0123456789102|MXL1234P5I [PrinterIp] 50=192.168.0.50 48=192.168.0.48 47=192.168.0.47 [NewSysInfo] newPC = SD0123456789adfs|192.168.0.185|2UA1234FTR Thank you for your time.
    • XinYoung
      By XinYoung
      Hello again,
      I'm trying to use the FileMove function to rename a bunch of text files.
      Some of the files have "_1" at the end of their name, for example, "File123_1.txt". I want to remove the "_1" so it would become "File123.txt".
      What I currently have...
              FileMove($sSource & "\*_1.txt", $sDestination & "\*.txt")
      But since I'm using a wildcard, it doesn't appear to be working. I think it's just replacing .txt with .txt. Am I going about this the wrong way? How can I use wildcards and still accomplish this?
      Any help is greatly appreciated ^__^