def test_diections(): lexic = lexicon() #interesante lo de aca abajo assert_equal(lexicon().scan("north"), [('direction', 'north')]) result = lexic.scan("nortH south east") assert_equal(result, [('direction', 'north'), ('direction', 'south'), ('direction', 'east')])
def test_errors(): lexic = lexicon() assert_equal(lexic.scan("ASDFADFASDF"), [('error', 'asdfadfasdf')]) result = lexic.scan("bear IAS princess") assert_equal(result, [('noun', 'bear'), ('error', 'ias'), ('noun', 'princess')])
def test_stops(): lexic = lexicon() assert_equal(lexic.scan("the"), [('stop', 'the')]) result = lexic.scan("the in of") assert_equal(result, [('stop', 'the'), ('stop', 'in'), ('stop', 'of')])
def test_verbs(): lexic = lexicon() assert_equal(lexic.scan("go"), [('verb', 'go')]) result = lexic.scan("go kill eat") assert_equal(result, [('verb', 'go'), ('verb', 'kill'), ('verb', 'eat')])
else: raise ParserError("Expected a noun or direction next.") def parse_subject(word_list, subj): verb = parse_verb(word_list) obj = parse_object(word_list) return Sentence(subj, verb, obj) def parse_sentence(word_list): skip(word_list, 'stop') start = peek(word_list) print start if start == 'noun': print "Start == noun -> CHECK" subj = match(word_list, 'noun') return parse_subject(word_list, subj) elif start == 'verb': #assume the subject is the player then return parse_subject(word_list, ('noun', 'player')) else: raise ParserError("Must start with subject, object, or verb, not: %r" % start) a = lexicon() while True: scanned = a.scan(raw_input("> ")) print scanned print parse_sentence(scanned)
def test_numbers(): lexic = lexicon() assert_equal(lexic.scan("1234"), [('number', 1234)]) result = lexic.scan("3 91234") assert_equal(result, [('number', 3), ('number', 91234)])
def test_nouns(): lexic = lexicon() assert_equal(lexic.scan("bear"), [('noun', 'bear')]) result = lexic.scan("bear princess") assert_equal(result, [('noun', 'bear'), ('noun', 'princess')])