egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Compare text and output match in percentage ?

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Sun Mar 08, 2015 5:51 am    Post subject: Compare text and output match in percentage ? Reply with quote

Good morning everyone Smile

Im just asking myself, if theres a way to compare text like:

texta: I.am.just.here
textb: I.was.just.here

Bot should now compare those two textes and output the percentage of matching. Is there a way to do so ?

greets & thanks
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sun Mar 08, 2015 9:15 am    Post subject: Re: Compare text and output match in percentage ? Reply with quote

There could be more than one way. Smile

Go here :
http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm
and find:
string
and see the various ways to use that TCL command to manipulate strings.

I think you will be using:
string length
(along with a foreach loop, and you can find the command:
foreach
there too. Use it to iterate through the string, char by char )
You'll want:
string equal
too.

Be sure to look up
expr
as that is how you do math.
Tip:
14/15
will return 0
While:
14/15.0
will return
0.9333333333333333

I hope this gets you started. Actually, I hope you find a better way. Smile
Back to top
View user's profile Send private message
Get_A_Fix
Master


Joined: 07 May 2005
Posts: 206
Location: New Zealand

PostPosted: Mon Mar 09, 2015 7:17 am    Post subject: Reply with quote

:thumbsup: willyw Smile

We need a rep button or something, I wonder if slennox can be bothered, or someone else possibly with access to template editing.
_________________
We explore.. and you call us criminals. We seek after knowledge.. and you call us criminals. We exist without skin color, without nationality, without religious bias.. and you call us criminals.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Mon Mar 09, 2015 8:01 am    Post subject: Reply with quote

Explain what you are talking about Get_A_Fix...
_________________
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.


Last edited by SpiKe^^ on Thu Mar 12, 2015 5:00 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Tue Mar 10, 2015 7:02 am    Post subject: Reply with quote

Thanks for your support Smile
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Tue Mar 10, 2015 8:15 am    Post subject: Reply with quote

There's no straight forward functions that you can use to get such a percentage, but different functions to help you achieve that.

If the two strings have words separated by a dot, comma or just space you would need to turn the string into a list by splitting them by that element, in your case that dot. We achieve this by using the split function.
Code:

set texta [split $texta .]
set textb [split $textb .]

Next to to see how many elements of the first list (texta) are in the second (textb) you have to loop tough all the elements of the list and check if they are present in the second.

We achieve this by using foreach to loop, lsearch to see if a element is in a list and by using incr will be incrementing the count variable when a match was found.
Code:

set count 0
foreach ele $texta {
   if {[lsearch -nocase $textb $ele] != -1} {
      incr count
   }
}

The -nocase option tells the lsearch function to ignore the lower, upper or mixed case of the characters of the "element" (or simply put word) when doing the match search.

After the foreach loop has finished you will have in $count the total number of matches it found. Notice the lack of $ in front of the count at the incr line. There's no need for it there, but when you want to see it's value you would need that dollar sign in front of it.

If you want to see how long is the second list and how many of the words it matched then you could use llength to get it's length and expr to do the math calculation of the exact percentage.
Code:

set size [llength $textb]
set percentage [expr $count*100/$size]

So now in $percentage you got exactly how much the first string matches the second, in your case it's a 75% match.

Something worth checking: lists and strings

Edit: After a bit of thinking I've come with a different approach that's a bit more straight forward than the above method by using lmap, but would still need to split those two strings. Smile
Code:

set texta [split $texta .]
set textb [split $textb .]
set matches [lmap x $texta {expr {[lsearch -nocase $textb $x] !=1 ? $x : [continue]}}]

Now $matches would contain the elements that are common to the two strings and from here can just get the size of the second list (after splitting the second string) and the size of the matches list then do the math to get percentage.
Code:

set size [llength $textb]
set count [llength $matches]
set percentage [expr $count*100/$size]


If you want to stick with the first approach and get the matches inside a variable then we add the lappend to the foreach loop list this.
Code:

set matches [list]
foreach ele $texta {
   if {[lsearch -nocase $textb $ele] != -1} {
      incr count
      lappend matches $ele
   }
}

Notice that we first need to define the matches variable prior to using it, hence the line on top of the start of the foreach loop.

If you got any questions don't hesitate to ask. Wink
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Fri Mar 13, 2015 11:59 am    Post subject: Reply with quote

wow thanks, ill give it a try
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber