Jump to content

Floating Compare String - simple way


BorisPihtin
 Share

Recommended Posts

Example:

You want to compare such strings as "Hello how are you fine ?" , and "Are you fine Hello" ?

After this comparing you will have a coefficient of identical ...

---------------------------------------------------------------------------------

#include <String.au3>

#include <Array.au3>

func _compareequ($x1,$x2)

dim $y1[100]

dim $y2[100]

$x1=StringLower($x1)

$x2=StringLower($x2)

$x1=StringStripWS($x1,8)

$x2=StringStripWS($x2,8)

$len1=StringLen($x1)

$len2=StringLen($x2)

$y1 = StringToASCIIArray($x1)

$y2 = StringToASCIIArray($x2)

for $i=0 to Ubound($y1)-1

for $j=0 to Ubound($y2)-1

if $y1[$i]=$y2[$j] Then

$y1[$i]=0

$y2[$j]=0

EndIf

Next

Next

$mus1=0

$mus2=0

for $i=0 to Ubound($y1)-1

if $y1[$i]>0 then $mus1=$mus1+1

Next

for $j=0 to Ubound($y2)-1

if $y2[$j]>0 then $mus2=$mus2+1

Next

return (($len1-$mus1)/$len1)*(($len2-$mus2)/$len2)

EndFunc

Link to comment
Share on other sites

is there a way to convert it back to stringform ?

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Sorry I misunderstood?

My mistake :D

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • 2 months later...

Example:

You want to compare such strings as "Hello how are you fine ?" , and "Are you fine Hello" ?

After this comparing you will have a coefficient of identical ...

Working on a Search Engines, I was interested by your function for filter results.

But after some tries, comparing strings by characters can distort the result...

So, i have found another approach, by matching words.

may be it was your first intention ? :)

#Include <Math.au3>

ConsoleWrite ( '- Equivalence : ' & _WordsStringCompare ( 'Hello how are you fine ?', 'Are you fine Hello ?' ) &  '%' & @Crlf )

Func _WordsStringCompare ( $_Str1, $_Str2 )
    $_Str1 = StringStripWS ( StringLower ( StringRegExpReplace ( $_Str1, '(?i)[^a-z0-9]', ' ' ) ), 7 )
    $_Str2 = StringStripWS ( StringLower ( StringRegExpReplace ( $_Str2, '(?i)[^a-z0-9]', ' ' ) ), 7 )
    Local $_Ar1 = StringSplit ( $_Str1, ' ', 2 ), $_Ar2 = StringSplit ( $_Str2, ' ', 2 ), $_Match
    For $_I = 0 To Ubound ( $_Ar1 ) -1
        For $_J = 0 To Ubound ( $_Ar2 ) -1
            If $_Ar1[$_I] And $_Ar1[$_I] = $_Ar2[$_J] Then
                $_Ar1[$_I] = ''
                $_Ar2[$_J] = ''
                $_Match += 1
            EndIf
        Next
    Next
    Return Round ( $_Match / _Max ( Ubound ( $_Ar1 ), Ubound ( $_Ar2 ) ), 4 ) * 100
EndFunc ;==> _WordsStringCompare ( )
Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

@wakillon

In my opinion is perfect, ideal even for searches inside files

JS

Thanks

here is one with a partial match you can adjust .

That can avoid to miss a match for a plurial or a conjugated verb... :D

#Include <Math.au3>

$_Result1 = _WordsStringCompare ( 'Hello how are you fine ?', "Are you fine, Hello ?" )
ConsoleWrite ( '+ @extended : ' & @extended & @Crlf )
ConsoleWrite ( '- Equivalence : ' & $_Result1 & '%' & @Crlf )

$_Result2 = _WordsStringCompare ( 'Thanks for the original idea!', 'In my opinion is perfect, ideal even for searches inside files', 1 )
ConsoleWrite ( '+ @extended : ' & @extended & @Crlf )
ConsoleWrite ( '- Equivalence : ' & $_Result2 & '%' & @Crlf )

$_Result3 = _WordsStringCompare ( 'Keep up the good work', 'These tools are very good, glad you have worked on them.', 1, 2 )
ConsoleWrite ( '+ @extended : ' & @extended & @Crlf )
ConsoleWrite ( '- Equivalence : ' & $_Result3 & '%' & @Crlf )

Func _WordsStringCompare ( $_Str1, $_Str2, $_Partial=0, $_StringLenDiffMax=1 )
    $_Str1 = StringStripWS ( StringLower ( StringRegExpReplace ( $_Str1, '(?i)[^a-z0-9]', ' ' ) ), 7 )
    $_Str2 = StringStripWS ( StringLower ( StringRegExpReplace ( $_Str2, '(?i)[^a-z0-9]', ' ' ) ), 7 )
    Local $_Ar1 = StringSplit ( $_Str1, ' ', 2 ), $_Ar2 = StringSplit ( $_Str2, ' ', 2 ), $_Match, $_Slen1, $_Slen2
    For $_I = 0 To Ubound ( $_Ar1 ) -1
        $_Slen1 = StringLen ( $_Ar1[$_I] )
        For $_J = 0 To Ubound ( $_Ar2 ) -1
            $_Slen2 = StringLen ( $_Ar2[$_J] )
            If $_Ar1[$_I] Then
                If $_Ar1[$_I] = $_Ar2[$_J] Or _
                    ( $_Partial And ( StringInStr ( $_Ar2[$_J], $_Ar1[$_I] ) Or StringInStr ( $_Ar1[$_I], $_Ar2[$_J] ) And ( $_Slen1 > 1 And $_Slen2 > 1 ) And ( Abs ( $_Slen1 - $_Slen2 ) <= $_StringLenDiffMax ) ) ) Then
                    ConsoleWrite ( '! $_Ar1[$_I] : ' & $_Ar1[$_I] & ', $_Ar2[$_J] : ' & $_Ar2[$_J] & @Crlf )
                    $_Ar1[$_I] = ''
                    $_Ar2[$_J] = ''
                    $_Match += 1
                EndIf
            EndIf
        Next
    Next
    Return SetExtended ( $_Match, Round ( $_Match / _Max ( Ubound ( $_Ar1 ), Ubound ( $_Ar2 ) ), 4 ) * 100 )
EndFunc ;==> _WordsStringCompare ( )
Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...