Jump to content

Comparing two strings with various parameters and get Percentage of matching


Recommended Posts

I read various threads and theories regarding two string matching and convert matching value into Percentage.

there are some algorithms to calculate similarity like Lowenstein, but sometimes you need more then that

So, I written this code, and you will use it at your convenience

script matches two strings with word by word OR character by character

also match by case OR non case format

you can also find difference with (100 – Result)

If any suggestion then please write here

 

#include <Array.au3>
#include <File.au3>
#include <Math.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

$sStr01 = "My string Software v1.0"
$sStr02 = "My String Software v1.1"

ConsoleWrite("Standard: " & _StringMatchPercent($sStr01, $sStr02) & @CRLF)      ; Return Standard: 95.65
ConsoleWrite("Case ON (Char lvl): " & _StringMatchPercent($sStr01, $sStr02, 1, 0) & @CRLF)      ; Return "Case ON (Char lvl): 91.3"
ConsoleWrite("Case ON (word lvl): " & _StringMatchPercent($sStr01, $sStr02, 1, 1) & @CRLF)      ; Return "Case ON (word lvl): 50"
ConsoleWrite("Case Off (Char lvl): " & _StringMatchPercent($sStr01, $sStr02, 0, 0) & @CRLF)     ; Return "Case Off (Char lvl): 95.65"
ConsoleWrite("Case Off (word lvl): " & _StringMatchPercent($sStr01, $sStr02, 0, 1) & @CRLF)     ; Return "Case Off (word lvl): 75"


Func _StringMatchPercent($FirstString = "", $SecondString = "", $iCaseSense = 0, $iMatchByWord = 0)
;~ ($iCaseSense : [0 = not case sensitive [, 1 = case sensitive]])
;~ $iSplitByWord : [0 = Match Character by Character [, 1 = Match word by Word]]
;~ ### Check/Set function header parameters
    Local $sMatchBy = ($iMatchByWord = 0) ? "" : " "
    Local $inCaseSense = ($iCaseSense = 0) ? 0 : 1
    If $FirstString = "" Or $SecondString = "" Then Return "0"

    Local $aFirstArray = StringSplit($FirstString, $sMatchBy, 2)
    _ArraySort($aFirstArray) ; Sort Array to Ascending Order
;~ _ArrayDisplay($aFirstArray,"Show First Array")
    Local $aSecondArray = StringSplit($SecondString, $sMatchBy, 2)
    _ArraySort($aSecondArray) ; Sort Array to Ascending Order
;~ _ArrayDisplay($aSecondArray,"Show Second Array")

    Local $inSL = 0 ;Second Level matching starting Point
    Local $iMatchCount = 0
    Local $sMatchArg = 0
    ; Now Matching Every First Array Element to Second Array Element
    For $iFL = 0 To UBound($aFirstArray) - 1 ; $iPL = Represent First Array Position
        For $iSL = $inSL To UBound($aSecondArray) - 1 ; $iSL = Second Array Position $inSL = Second Array Checking Start from
            If StringCompare($aFirstArray[$iFL], $aSecondArray[$iSL], $inCaseSense) = 0 Then ; If Both Element Matched
                $iMatchCount += 1 ; Match Count get +1
                $inSL = $iSL + 1 ; Set New Starting Position for Second Array
                ExitLoop ; Exit Loop for checking next element
            EndIf
        Next
    Next
    Local $iMax = _Max(UBound($aFirstArray), UBound($aSecondArray)) ; Finding Large Array for Calculation
    Return Round((($iMatchCount <> 0) ? (($iMatchCount / $iMax) * 100) : 0), 2)
EndFunc

 

Link to comment
Share on other sites

  • Developers
Posted (edited)
1 hour ago, mepalival said:

If any suggestion then please write here

Yes: Do not cross post and resurrect old threads to point here! 

All extra posts zapped.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Searching the forum, you could have found this as well:

 

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

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