mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jul 2014
Posts: 5
S
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
S
Joined: Jul 2014
Posts: 5
I don't know if the subject makes a lot of sense, but I don't quite know how to explain what I'm trying to do in a title, and I've searched a lot for answers.

So, for example, if someone sends a line of text saying:

"This is an example"

and I have a .txt file called keywords.txt which has a list of words, such as
Code:
test
example
word

How would I write a script that would go through the text file (by line) searching for a match in the message?
I imagined it would be something along the line of the following, but with some sort of loop for each line.
Code:
on *:text:*:* {
  if (read(keywords.txt, nw, ???) isin $1-)
  msg # You said a keyword.
}

Joined: Jul 2006
Posts: 4,144
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,144
You must define how you are going to match, otherwise what you said is non-sense, basically.

Do you want to check each word in the sentence from the on text event against all words on each line in the text file?
How should it match? Is it the words in the file that should be matched against the words in the sentence or the other way around, also, should it match on whole word or partial expression (test matching testing for example)


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jul 2014
Posts: 5
S
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
S
Joined: Jul 2014
Posts: 5
Sorry for not explaining myself properly, I'm still new to all of this and my knowledge is very limited, so if what I say is non-sense I apologize.

I want a match of a full line in the text file to any word in message.
So the script will go through the text file by line and try to match it to the message text event.
So an example, if the word "and" is on a line text file and the message "I am in a band" is sent, it will come back true and continue the script.

Thanks for any and all help and sorry if my explanation is poor.

Joined: Jul 2006
Posts: 4,144
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,144
So, you need a full match and your line in the file are one word only, this simplify things a bit because it then doesn't matter if you match the lines in the file against the words in the sentence or if you match the words in the sentence against the lines in the file, this also means you'll indeed use $read and the 'w' switch, which provides a way to do the latter for one word, so a loop and you're good.
Note that $read with 'w' already performs a loop for you on each line, and it will return that whole line, you only need to check that $read is different from $null to make sure you have a match in this case.
Code:
on *:text:*:#chan:{
  var %a 1
  ;as long as we have a word in the sentence
  while ($gettok($1-,%a,32) != $null) {
    var %v $v1
    ;if the current word matches a line
    if ($read(badwords.txt,tnw,%v) != $null) { ;a badword is found }
    inc %a
  }
}
Remember to always use the 'n' switch with $read and $readini to prevent code injection


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jul 2014
Posts: 5
S
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
S
Joined: Jul 2014
Posts: 5
Thank you so much!
This is exactly what I was looking for.

Joined: Jul 2014
Posts: 5
S
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
S
Joined: Jul 2014
Posts: 5
I've noticed this only only works on exact matches, eg. "the" and "the" in both the message and the text file, I was wondering if there was any way to make the script detect a word that isn't separated by spaces in the message.

So, if I were to have "the" in the text file, and someone says "their" or "then" or something containing the word "the" if that could detected.

I would imagine the script would have to read the text file first, but I am unsure how to code that.

Joined: Jul 2006
Posts: 4,144
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,144
Yeah, I was afraid you wanted that and actually started writting a lot about it, but that would have been confusing so I just hoped...
To do that, you need to be able to do a wildcard match based on the line in the file, and $read doesn't allow you to do that.
We can loop on all lines in the file ourselves but it could be slow depending on the number of lines.., they are better way to accomplish this.
A very nice way to do that is to use hash tables, $hfind is a powerful tool which can do what you want.
Just like in the text file, you'll use the hash table as an unordered list of string like "the", but this time you add ** around it: *the*.
After that you use $hfind(table,<word from sentence>,1,W).item which will return *the* if <word from sentence> is "their" for example.

Code:
on *:start:{
  hmake matchinglist
  if ($exists(matchinglist)) hload matchinglist matchinglist 
}
alias addtolist {
  hadd matchinglist * $+ $1* 
  hsave matchinglist matchinglist
}
alias delfromlist {
  if ($hget(matchinglist,* $+ $1*) != $null) { 
    hdel matchinglist * $+ $1*
    hsave matchinglist matchinglist 
 }
}
Put that in your remote and use "/addtolist the" and "/delfromlist the" to add/remove stuff, then:

Code:
on *:text:*:#chan:{
  var %a 1,%v
  while ($gettok($1-,%a,32) != $null) {
    %v = $v1
    if ($hfind(matchinglist,%v,1,W).item != $null) {
      echo $chan The word %v matches the entry $v1
    }
    inc %a
  }
}



#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jul 2014
Posts: 5
S
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
S
Joined: Jul 2014
Posts: 5
Ahh, thanks for clearing that up. I assumed I was doing things inefficiently to begin with, but text files were the only way I knew how.

Thank you for the help, that makes a lot of sense.


Link Copied to Clipboard