|
golgepapaz Beginner
Joined: 09 Sep 2010 Posts: 13
|
Posted: Sun Dec 19, 2010 5:05 am
script to replace words in a string |
well I need a script to replace some words in a string with other one,more like a dictionary look-up
For example
"You are lucky, human"
with
"You are lucky, rivvil"
It seems to me I have to parse the original string word by word and look for the replacement
string in a hashtable(hashtables in cmud!?) then replace it but I have no idea how to do it...
Any pointers would be greatly appreciated |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Sun Dec 19, 2010 8:00 pm |
You should explain exactly what you are trying to do. You haven't really given enough information to decide the best way to do it.
What I _think_ you might be doing is: whenever you see the word "human" in text from the mud, replace it with "rivvil". And so on, with a long list of word/replacement pairs. If this is what you want, then this is what to do.
Make a database variable, pairing your word/replacements, like so:
Code: |
#var replacements {human=rivvil|elf=bargle|dwarf=fuuflux} |
Make a trigger:
Code: |
#trigger {%q({@replacements})%q} {#sub {%db(@replacements,%1)} |
Now, to explain what this is doing. When you use a database variable in a trigger pattern, it treats it as if it were a stringlist of the database keys. So this trigger pattern will search for any string from the mud which matches {human|elf|dwarf}. %q matches any end-of-word mark (space or punctuation), so putting %q on either side ensures that you are matching a whole word, and not "humanity" or "shelf". %db() is a function that returns the value whose key is %1, the word captured by the trigger. So, if the word matched in the trigger is "human", %db(@replacements,"human") will return "rivvil". The #sub then replaces that word with the value in that line. If you want to replace multiple words within the line if they occur, you will need to turn on the Repeat Within Line option on the trigger.
If this is not what you are trying to do, please explain in detail what you want. |
|
|
|
golgepapaz Beginner
Joined: 09 Sep 2010 Posts: 13
|
Posted: Sun Dec 19, 2010 9:36 pm |
well thank you , this is exactly what I am trying to accomplish except that I don't
receive the text from the mud, but entering it myself.
morelike
drowsay You are lucky, Human (drowsay is the name of alias)
and it will send the text
say You are lucky, rivvil
to the mud.
Thanks again. |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Mon Dec 20, 2010 1:30 am |
Ah. In that case, you don't want #sub, or a trigger. It requires a different method...Maybe using %subregex(). Rather more tricky.
|
|
|
|
golgepapaz Beginner
Joined: 09 Sep 2010 Posts: 13
|
Posted: Mon Dec 20, 2010 2:32 am |
ok now , you've begun to concern me, is it that hard?
|
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Mon Dec 20, 2010 3:06 am |
Well, it's probably not _that_ hard, but my brain isn't up to figuring out the method just now.
|
|
|
|
golgepapaz Beginner
Joined: 09 Sep 2010 Posts: 13
|
Posted: Tue Dec 21, 2010 1:39 am |
ok I've worked out something using the %subregex;
<alias name="dsay" autoappend="true" id="322">
<value>#forall %dbkeys(@replacements)
{
$text= %subregex($text,\b%i\b,%db(@replacements,%i))
}
say $text</value>
<arglist>$text</arglist>
</alias>
I'd be at home if I only I could use ruby with this
my only problem is I need to double quote the string
I am sending, otherwise it takes only the first word
autoappend didn't help. |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Tue Dec 21, 2010 1:50 am |
Yes, if you specify an argument like that it will only take the first word as the argument, or the first quoted string (and delete the quotes) as an argument. Try this version:
Code: |
<alias name="dsay" autoappend="true" id="322">
<value>#local $text
$text = %params
#forall %dbkeys(@replacements)
{
$text= %subregex($text,\b%i\b,%db(@replacements,%i))
}
say $text</value>
</alias>
|
|
|
|
|
|
|