[Tutor] Textparsing, a better way?

Magnus Lyckå magnus@thinkware.se
Tue May 6 14:03:01 2003


At 09:41 2003-05-06 -0700, Zak Arntson wrote:
>I'm working on my text adventure text parser (think Zork), and have
>created the following code to turn a sentence into a list of words and
>punctuation. E.g.: "Sailor, throw me the bottle. Get bottle" ->
>['sailor',',','throw','me','the','bottle','.','get','bottle']

Is this what you want? (I threw in support for ?, ! and - as well.)

 >>> t = "Sailor, throw me the bottle. Get bottle"
 >>> b = re.compile(r'\S+?\b|[\.,:;\-\?!]')
 >>> b.findall(t.lower())
['sailor', ',', 'throw', 'me', 'the', 'bottle', '.', 'get', 'bottle']


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program