Skip to content

Instantly share code, notes, and snippets.

@tjguk
Created November 14, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjguk/fabd53b025dc8f9fe5b76ee900db62f9 to your computer and use it in GitHub Desktop.
Save tjguk/fabd53b025dc8f9fe5b76ee900db62f9 to your computer and use it in GitHub Desktop.
Parsing text for adventure
import os, sys
import re
text = 'utilise the big red ball.'
#~ phone_text = "My phone number is 07749 298683 and my phone number is 012342837 89 and this is another number 13245767802"
verbs = {"go", "get", "drop", "jump", "eat", "use"}
synonyms = {
"use" : {"wave", "utilise", "brandish"}
}
def get_verb(word):
if word in verbs:
return word
else:
for verb, synonym_list in synonyms.items():
if word in synonym_list:
return verb
raise RuntimeError("No hablo espa�ol")
#if word not in verbs:
# raise RuntimeError("I don't know how to %s" % verb)
class Room(object):
def __init__(self):
self.items = set()
class Person(object):
def __init__(self):
self.inventory = set()
self.location = Room()
# person.location.items.add(ball)
# room.items.remove(ball)
tokens_re = re.compile(r"\w+")
tokens = tokens_re.findall(text)
#~ print(re.findall(r"\d{5}\s*\d{3}\s*\d{3}", phone_text))
# ['Get', 'a', 'red', 'ball']
articles = {"a", "an", "the"}
print(tokens)
tokens = [token for token in tokens if token not in articles]
print(tokens)
verb = get_verb(tokens[0])
print(verb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment