This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Compare text and output match in percentage ?

Help for those learning Tcl or writing their own scripts.
Post Reply
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Compare text and output match in percentage ?

Post by Elfriede »

Good morning everyone :)

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
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Re: Compare text and output match in percentage ?

Post by willyw »

There could be more than one way. :)

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. :)
User avatar
Get_A_Fix
Master
Posts: 206
Joined: Sat May 07, 2005 6:11 pm
Location: New Zealand

Post by Get_A_Fix »

:thumbsup: willyw :)

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.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Explain what you are talking about Get_A_Fix...
Last edited by SpiKe^^ on Thu Mar 12, 2015 5:00 pm, edited 1 time in total.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

Thanks for your support :)
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

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: Select all

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: Select all

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: Select all

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

Code: Select all

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: Select all

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: Select all

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.
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

wow thanks, ill give it a try
Post Reply