-
-
Save 1328/0e56ee0f622744bb487d to your computer and use it in GitHub Desktop.
x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import namedtuple | |
| Action = namedtuple('Action', ['verb', 'noun', 'adjective', 'indirect']) | |
| nouns = ['sword', 'dog', 'hat'] | |
| verbs = ['get', 'go', 'eat', 'north', 'east'] | |
| prepositions = [ | |
| 'below', 'off', 'toward', 'above', 'beneath', 'on', 'under', | |
| 'across', 'beside', 'from', 'onto', 'underneath', 'between', | |
| 'in', 'out', 'until', 'against', 'beyond', | |
| 'outside', 'along', 'inside', 'over', 'upon', 'among', | |
| 'by', 'past', 'around', 'with', 'at ', 'into', | |
| 'within', 'down', 'through', 'without', 'before', | |
| 'near', 'throughout', 'behind', 'except', 'of', 'to' | |
| ] | |
| cinnamons = {'walk':'go', 'consume':'eat', 'n': 'north'} | |
| elide = ['the', 'a'] | |
| def parse(command): | |
| words = [cinnamons.get(w, w) for w in command.split() if w not in elide] | |
| return create_action(words) | |
| def parse_preposition(words, **kws): | |
| last = None | |
| for idx, word in enumerate(words): | |
| if word in prepositions: | |
| last = idx | |
| remaining = [w for idx, w in enumerate(words) if idx < last] | |
| if last is None or last == len(words)-1: | |
| return remaining, None | |
| preposition = words[last+1] | |
| return remaining, preposition | |
| def parse_adjective(words): | |
| last = None | |
| remaining = [] | |
| for idx, word in enumerate(words): | |
| if word in nouns: | |
| last = idx | |
| break | |
| if last is None or idx > len(words): | |
| return words, None, None | |
| used = (last, last -1) | |
| remaining = [w for idx, w in enumerate(words) if idx not in used] | |
| return remaining, words[last], words[last-1] | |
| def create_action(words, **kws): | |
| #print('create_action called on {}, kws = {}'.format(words, [(k,v) for k,v in kws.items()])) | |
| verb = kws.get('verb') | |
| noun = kws.get('noun') | |
| adjective = kws.get('adjective') | |
| indirect = kws.get('indirect') | |
| if len(words) in (1, 2) and verb is None: | |
| verb = words[0] | |
| if len(words) == 2 and noun is None: | |
| noun = words[1] | |
| if len(words) > 2 : | |
| if noun is None and any(w in nouns for w in words): | |
| words, noun, adjective = parse_adjective(words) | |
| kws['noun'] = noun | |
| kws['adjective'] = adjective | |
| return create_action(words, **kws) | |
| if any(w in prepositions for w in words): | |
| words, indirect = parse_preposition(words) | |
| kws['indirect'] = indirect | |
| return create_action(words, **kws) | |
| return Action(verb, noun, adjective, indirect) | |
| check = [ | |
| 'north', | |
| 'n', | |
| 'eat hat', | |
| 'consume hat', | |
| 'consume green hat', | |
| 'get dog from table', | |
| 'get orange dog from table', | |
| 'get orange dog from under', | |
| 'get orange dog from under table', | |
| 'get the really, really big sword', | |
| ] | |
| for phrase in check: | |
| print('"{}" => {}'.format(phrase, parse(phrase))) | |
| print('-'*10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment