So, I’ve seen this type thing on several occasions, but I’ve never been able to find out anything about it. Can someone please explain what these “r’ foo(*)'” things are and link me to the python docs for them? I think they have to do with finding key words in strings. Am I right?
(r'why (.*) i (.*)\?',
( "You%1%2?",
"Perhaps you only think you%1%2")),
(r'why (.*) you(.*)\?',
( "Why%1 you%2?",
"%2 I%1",
"Are you sure I%2?")),
(r'why (.*)\?',
( "I cannot tell you why%1.",
"Why do you think %1?" )),
This particular segment of code is from the nltk.chat.zen module.
Those appear to be regular expressions. Regular expressions let you search for patterns in strings in a fairly powerful and sophisticated way. They can be a bit cryptic though. These appear to be the basis of some kind of primitive chatterbot.
Ah, and perhaps you were specifically wondering what
r'blahblah'means. Well, as others have explained, therjust makes it a raw string — Python doesn’t do certain kinds of processing on it, which makes REs easier to read.Here’s something closer to what I think the original intent was for these strings. They don’t seem to be using standard Python group substitution syntax, so I assume they must be using a custom substitution function for this — perhaps one sophisticated enough to handle converting the verb form! You can see the basic idea here.