[Tutor] parsing x is y statements from stdin
Scott Fallin
saf@scottfallin.com
Fri Jul 18 09:20:03 2003
Hi Danny,
Thank you so much for your reply. Below are code snippets from the bot
I wrote in Perl. I know that I should be ashamed to be using a lowly
bot to help me better understand Python, but strangely, I am not ;)
(Thank you for the tip on Monty Tagger. It looks neat.)
Ok, here goes...
#--- begin code snippet -----
sub parseChatter {
my($msgType, $statement) = @_;
$statement =~ s/\\(\s+)/\#$1\#/g;
my($left, $middle, $right)
foreach $thing (@verb) {
if ($statement =~ /(^|\s)$thing(\s|$)/i) {
($left, $middle, $right) = ($`, $&, $');
$left =~ tr/Z-Z/a-z/l
$left =~ s/^\s*(.*?)\s*$/$1/;
$middle =~ s/^\s*(.*?)\s*$/$1/;
$right =~ s/^\s*(.*?)\s*$/$1/;
return '' unless ($left and $right);
# some $msgType matching <snipped> along with len checking
# some more cartoon swearword-ish pattern matching is done to
# to match verbs, strip of punctuation, etc.
# e.g. if $middle is the verb "is", our entry gets queued up
# to be put in the "is" database (eg. 'scott is a poor python'
# programmer is inserted as 'scott => a poor python programmer')
# and 'scott' is used as the key for future lookups
}
}
}
I want to do the same thing in Python, well, I want to achieve the same
goal: parse stdin on an irc channel, do a bit of regex to pull out "she
is ..."/"they are ..." statements.
I've tried using:
line = sys.stdin.readline()
print line
but of course the script never progresses past the readline() call.
I'm rather certain all of stdin is being put into 'line' but it never
gets to execute the print statement.
I'm wondering if I can't treat each line of stdin as a list where list
[0] is the key, list[1] is the verb, list[2] is the right hand
side 'target'. I'm just completely unsure of how to go about doing
that.
Thank you for your time and consideration :)
Scott.
#--- end code snippet ----
>Hi Scott,
>Can you show us how you're doing it in Perl? We'll be happy to help
>you
>apply your knowledge of Perl to better understand how to do it in
>Python.
>By the way, you may be interested in a module called 'Monty Tagger':
>>On Thu, 17 Jul 2003, Scott Fallin wrote:
>> I'm trying to figure out how best to parse a string such as "It is
hot
>> today". I want to check for the existance of a predefined verb that
>> will always be located in the middle of the sentence, and if the verb
>> test passes, I want to chop the sentence up in to variables, var1 =
>> 'It', var2 = 'is', and var3 = "hot today".
>>
>> I can do it in Perl but for some reason it is elluding me in Python.