|
Joined: Apr 2004
Posts: 17
Pikka bird
|
OP
Pikka bird
Joined: Apr 2004
Posts: 17 |
Hello, Im trying to make a code to count how many words somebody says that matchs a list of words.
Somenthing like: My list has the words (one,two,three,four,five) Somebody says: The one I have was five hundred. Then it will return me 2
Thanks
|
|
|
|
Joined: Feb 2004
Posts: 714
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 714 |
on *:TEXT:*:#: {
var %i = $numtok(%wordlist,44)
while (%i) {
if ($istok($strip($1-),$gettok(%wordlist,%i,44),32)) { inc %word.count. [ $+ [ $wildsite ] ] }
dec %i
}
[color:green] other commands that use the count here[/color]
} This should work It loops through the words in the %wordlist variable (that must be separated by commas (,)) and it tries to match with the sentece said. The variable that holds the work count is: %word.count.<the person's wildsite> - where wildsite is their whole IP. Eg: *!*@ John.comHope it helps! Zyzzyx
"All we are saying is give peace a chance" -- John Lennon
|
|
|
|
Joined: Aug 2003
Posts: 314
Fjord artisan
|
Fjord artisan
Joined: Aug 2003
Posts: 314 |
Incidentally you don't necessarily need a loop if you use alternation with regular expressions:
alias countwords { var %str = one,two,three,four,five return $regex($1-,$+(/,$replace(%str,$chr(44),|),/ig)) }
$countwords(The one I have was five hundred.) would be used for your example, where %str is your string of words in this example separated by a comma. All you need to know is that if you adjust the string in such a way that you get one|two|three|four|five then you can pass it into $regex and it will count the number of matches that exist in the string
|
|
|
|
Joined: Apr 2004
Posts: 17
Pikka bird
|
OP
Pikka bird
Joined: Apr 2004
Posts: 17 |
This stuff helped a lot Thanks
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
Hi,
you might want to add some word boundaries, so that "one" wouldn't match on "alone" etc. Also, see what happens if you want to check how many question marks were in a string, like if you put ? as your string to be counted. This sort of thing will happen with some characters that have a special meaning inside a regex, like ^, $, ., etc.
Greets
Gone.
|
|
|
|
Joined: Apr 2004
Posts: 17
Pikka bird
|
OP
Pikka bird
Joined: Apr 2004
Posts: 17 |
you're right, I'm having this problem. Do you have any ideia?
Thanks
Last edited by kaotikoo; 21/08/04 10:13 PM.
|
|
|
|
Joined: Feb 2004
Posts: 714
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 714 |
The code I gave you earlier wouldn't pick up repeated words in the same sentece. This one does (and words as 'alone' wont be picked up) on *:TEXT:*:#: {
unset %word.count. [ $+ [ $wildsite ] ]
[color:green]; The line above must be removed if you want to keep a record of that person.
; If not, leave it there and the counter will reset everytime the person says something[/color]
var %i = $numtok(%wordlist,44)
while (%i) {
if ($wildtok($strip($1-),$gettok(%wordlist,%i,44),0,32)) { inc %word.count. [ $+ [ $wildsite ] ] $ifmatch }
dec %i
}
if (%word.count. [ $+ [ $wildsite ] ]) { [color:red]Other commands here[/color] }
}
I hope this helps I know nothing about $regex, so I can only work something out with basic mIRC scripting Zyzzyx.
"All we are saying is give peace a chance" -- John Lennon
|
|
|
|
Joined: Apr 2004
Posts: 17
Pikka bird
|
OP
Pikka bird
Joined: Apr 2004
Posts: 17 |
it works when I use regular words when I have (brb) for example, it doesnt work
Im about to bang my head in the wall LOL
|
|
|
|
Joined: Feb 2004
Posts: 714
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 714 |
Yes, that is true. That script will only detect the exact words, such as 'one' and 'two'. The same way 'alone' doesn't get picked, '(one)' won't get either. I bet a regex can fit your needs better, because that code could get big if you don't use them :P Good luck!
"All we are saying is give peace a chance" -- John Lennon
|
|
|
|
Joined: Apr 2004
Posts: 17
Pikka bird
|
OP
Pikka bird
Joined: Apr 2004
Posts: 17 |
Thanks for helping Zyzzyx
|
|
|
|
Joined: Feb 2004
Posts: 714
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 714 |
yw Sorry I couldn't do much more
"All we are saying is give peace a chance" -- John Lennon
|
|
|
|
Joined: Apr 2003
Posts: 701
Hoopy frood
|
Hoopy frood
Joined: Apr 2003
Posts: 701 |
alias wordcount { var %words = one|two|\(three\)|four\?|\Qspecials"?+*-/[]\E return $regex($1-,/(?:^|[\s\w])(?: $+ %words $+ )(?:[\s\w]|$)/[color:purple]igS) }[/color] ; usage: $wordcount(sentence) ; probably $1- in an on text event i guess The variable %words contains the words you want to count. Separate different words with |. Make sure that every special is either escaped by a backslash \ or put every word with special characters between \Q and \E. For the purple switches: i means ignore case, if you want it to make a difference between A and a, just remove the i. The S means strip control codes (color,bold,reverse,underline,normal) before matching and the g is needed to maker it count everything instead of stopping at the first match. Fun reading for regular expressions: http://www.pcre.org/pcre.txt or you can find some tutorials all over the internet. Try mirc scripting sites though, or you might get confused by perl syntax.
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
Hi, sorry I was offline for a couple of hours. The solution that I would come up with is similar to Kelder's solution. However I think you don't have to escape those special chars yourself, after all, code needs to be user friendly Usage: $mwords(string)
alias mwords {
var %a = one two
.echo -q $regsub(%a,/\s/g,\\E|\\Q,%a)
return $regex($$1,/(?<=^|\s)(?:\Q $+ %a $+ \E)(?=\s|$)/gSi)
}
So just put whatever words or characters there in the %a variable, and make sure they're seperated by a space if you want seperate words. Special characters don't need to be escaped, I'm doing it for you. The alias is made to match only stand alone words, which means it'll catch "one" but not "alone". I'm assuming that's the way you want it. Btw, when you copy the code from the msg board, make sure you retain the newlines, cuz as you'll notice when pasting in mirc, the code will be one 1 line, while it needs to be on 5 lines, as you see in this post. The explanation of Kelder still stands, regarding the S, i and g parameters. Alternatively, if you don't like the regex mambo jambo:
alias mwords {
var %a = 1, %b
while $gettok(one two,%a,32) != $null { inc %b $findtok($$1,$v1,0,32) | inc %a }
return %b
}
@Kelder: your alias doesn't work, try $wordcount(one one one one) Greets
Last edited by FiberOPtics; 22/08/04 02:53 AM.
Gone.
|
|
|
|
Joined: Apr 2004
Posts: 17
Pikka bird
|
OP
Pikka bird
Joined: Apr 2004
Posts: 17 |
I finally got it, the \Q and \E was the answer for my problem. Thanks everyone.
|
|
|
|
Joined: Apr 2003
Posts: 701
Hoopy frood
|
Hoopy frood
Joined: Apr 2003
Posts: 701 |
You're right, and the solution you gave is indeed assertions. If you don't like those there's also the \G assertion: replace ^ with \G and it works ok My code might have been less user friendly, but it allowed the TS to use the full regex powers in the match words (ok that doesn't matter since almost no one knows regex )
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
Thanks for the \G tip. Yeah, regex looks kinda scary at first, much like Chinese. Personally, I found the basics very easy to learn, and for mIRC, with basic regex knowledge you can already come up with many good solutions. The complex pattern matches on the other hand, are something else Cya
Gone.
|
|
|
|
|