Exemple #1
0
    def enter(self):
        if inventory.items.holding('key'):
            print "all thats left is to escape via the escape pod! You head to that door and find it locked by a keypad."
            return 'EscapePod'
        else:
            print "As you re-enter the hallway the alien spots you and attacks! You have no choice but to try to defend yourself."
            if inventory.items.holding('pistol'):
                print """You quickly draw your pistol and shoot the alien, after searching his body you find what looks like a car key. Where do you want to go now? The escape pod or the office?"""
                inventory.items.pickup('key')
                action = raw_input("> ")
                if sentence.parse_sentence(lexicon.scan(
                        action)).object == 'Escape' or sentence.parse_sentence(
                            lexicon.scan(action)).object == 'escape':
                    print "You head to the door labeled 'escape pod' and find it locked with a keypad..."
                    return 'EscapePod'
                elif sentence.parse_sentence(lexicon.scan(
                        action)).object == 'Office' or sentence.parse_sentence(
                            lexicon.scan(action)).object == 'office':
                    print "You decide to snoop around a bit more before leaving and enter the office."
                    return 'Office'
                else:
                    print "The ceiling collapses and you die!"
                    return 'death'

            elif inventory.items.holding('grenade'):
                print """You throw the grenade at the alien in a panic, the explosion blows open the walls of the ship sucking you into space"""
                return 'death'
            else:
                print "The alien has no problem dealing with an unarmed human such as yourself"
                return 'death'
Exemple #2
0
def get_input(text):
    display_text(text)

    choice = raw_input(">.. ")
    if choice == "end":
        exit(0)
    else:
        scan = lexicon.scan(choice)
        sentence = parser.parse_sentence(scan)

    while not sentence:
        text = """
        You make no sense. \\n
        You can enter a sentence like 'Shoot the Gothon' or 
        'Hide in the doorway' \\n
        Try again
        """
        display_text(text)

        choice = raw_input(">..")
        if choice == "end":
            exit(0)
        else:
            scan = lexicon.scan(choice)
            sentence = parser.parse_sentence(scan)

    return sentence
Exemple #3
0
def test_errors():
    assert_equal(lexicon.scan("ASDFADFASDF"),[('error','ASDFADFASDF')])
    result=lexicon.scan("bear IAS princess")
    assert_equal(result,[('noun','bear'),
                         ('error','IAS'),
                         ('noun','princess')])
                 
Exemple #4
0
def get_input(text):
    display_text(text)
    
    choice = raw_input(">.. ")
    if choice == "end":
        exit(0)
    else:
       scan = lexicon.scan(choice)
       sentence = parser.parse_sentence(scan)  
    
    while not sentence:
        text = """
        You make no sense. \\n
        You can enter a sentence like 'Shoot the Gothon' or 
        'Hide in the doorway' \\n
        Try again
        """
        display_text(text)
        
        choice = raw_input(">..")
        if choice == "end":
            exit(0)
        else:
            scan = lexicon.scan(choice)
            sentence = parser.parse_sentence(scan)  
        
    return sentence
def test_errors():
    assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
    result = lexicon.scan("bear IAS princess west")  # study drill 1 我加上了west
    assert_equal(result, [('noun', 'bear'),
                          ('error', 'IAS'),
                          ('noun', 'princess'),
                          ('direction', 'west')])
Exemple #6
0
def starter(user):
	list = user.split()
	if len(list) > 1:
		sent = parse_sentence(lexicon.scan(user))
		build_response(sent)
	else:
		tin = lexicon.scan(user)
		if tin[0][0] == 'verb':
			print "%s is not sufficient. You must also supply a object" % tin[0][1]
		else:
			print "I dont uderstand that"
Exemple #7
0
def starter(user):
    list = user.split()
    if len(list) > 1:
        sent = parse_sentence(lexicon.scan(user))
        build_response(sent)
    else:
        tin = lexicon.scan(user)
        if tin[0][0] == 'verb':
            print "%s is not sufficient. You must also supply a object" % tin[
                0][1]
        else:
            print "I dont uderstand that"
Exemple #8
0
def valid_sent(input):
    sent = lexicon.scan(input)
    # Sets the starting values of these to be 1000, since we dont want it to be 0 initially
    verb_index = 1000
    object_index = 1000
    stop_count = 0
    verb_count = 0
    object_count = 0
    for i, item in enumerate(sent):
        if item[0] == "verb":
            verb_index = i
            verb_count += 1
        if item[0] == "noun" or item[0] == "direction":
            object_index = i
            object_count += 1
        if item[0] == "stop" and object_index > i:
            stop_count += 1
    total_count = stop_count + verb_count
    # If the object in the sentence is presented right after the verb
    # adding the stopcount.
    # the total_count let us return false, if the user types "go north north"( want the user to type "go north")
    if object_index == verb_index + 1 + stop_count and total_count < 3:
        return True
    # If the verb in the sentence is presented right after the object
    # and the total_count is greater than 2 ("i play them")
    # adding the stopcount.
    elif verb_index == object_index + 1 + stop_count and total_count > 2:
        return True
    else:
        return False
Exemple #9
0
def test_noun_verb_noun():
    phrase = "The bear kill the princess"
    sentence = parse_sentence(lexicon.scan(phrase))

    assert_equal(sentence.subject, 'bear')
    assert_equal(sentence.verb, 'kill')
    assert_equal(sentence.object, 'princess')
Exemple #10
0
def test_verb_direction():
    phrase = "Go north"
    sentence = parse_sentence(lexicon.scan(phrase))

    assert_equal(sentence.subject, 'player')
    assert_equal(sentence.verb, 'go')
    assert_equal(sentence.object, 'north')
Exemple #11
0
def check_stop(input):
    sent = lexicon.scan(input)
    temp_list = []
    for word in sent:
        if word[0] != "stop":
            temp_list.append(word[1])
    return temp_list
Exemple #12
0
def check_stop(input):
	sent = lexicon.scan(input)
	temp_list = []
	for word in sent:
		if word[0] != "stop":
			temp_list.append(word[1])
	return temp_list
Exemple #13
0
def convert_word(words):
	chan=scan(words)
	result=parse_sentence(chan)
	hh=result.subject+' '+result.verb+' '+result.obj
	return hh

		
Exemple #14
0
def valid_sent(input):
	sent = lexicon.scan(input)
	# Sets the starting values of these to be 1000, since we dont want it to be 0 initially
	verb_index = 1000
	object_index = 1000
	stop_count = 0
	verb_count = 0
	object_count = 0
	for i, item in enumerate(sent):
		if item[0] == "verb":
			verb_index =  i
			verb_count += 1
		if item[0] == "noun" or item[0] == "direction":
			object_index =  i
			object_count += 1
		if item[0] == "stop" and object_index > i:
			stop_count += 1
	total_count = stop_count + verb_count
	# If the object in the sentence is presented right after the verb
	# adding the stopcount.
	# the total_count let us return false, if the user types "go north north"( want the user to type "go north")
	if object_index == verb_index + 1 + stop_count and total_count < 3:
		return True
	# If the verb in the sentence is presented right after the object 
	# and the total_count is greater than 2 ("i play them")
	# adding the stopcount.
	elif verb_index == object_index + 1 + stop_count and total_count > 2:
		return True
	else:
		return False
Exemple #15
0
    def POST(self):
        print 'enter game post'
        form = game_form()

        if form.validates():
            print 'form valid'

            # show help if user types 'help'
            if form['answer'].value == 'help':
                session.help_user = 1

            word_list = lexicon.scan(form['answer'].value)
            sentence = my_parser.parse_sentence(word_list)

            # there is a bug here, can you fix it?
            if session.room and sentence:
                sentence_str = sentence.build_string()
                session.room = session.room.go(sentence_str)

        else:
            return render.show_room(room=session.room,
                                    form=form,
                                    help_user=False)

        seeother("/game")
Exemple #16
0
 def enter(self):
     print """You see a large selection of guns. You decide it would be prudent to take one before continuing on your adventure. There is a pistol, a machine gun and a grenade."""
     action = raw_input("> ")
     if sentence.parse_sentence(lexicon.scan(action)).object == 'pistol':
         print """You grab the pistol and head back into the Hallway"""
         inventory.items.pickup('pistol')
         return 'Hallway2'
     elif sentence.parse_sentence(lexicon.scan(action)).object == 'machine':
         print """You pick up the machine gun, but this actions triggers and alarm, alien guards come rushing in and kill you before you can even figure out how to shoot the thing."""
         return 'death'
     elif sentence.parse_sentence(lexicon.scan(action)).object == 'grenade':
         print """You grab the grenade and head back into the Hallway"""
         inventory.items.pickup('grenade')
         return 'Hallway2'
     else:
         print "The alien hears you fumbling around and shoots, you die"
         return 'death'
Exemple #17
0
def make_valid_sent(input):
	sent = lexicon.scan(input)
	verb = ""
	object =""
	for word in sent:
		if word[0] == "verb":
			verb = word
		elif word[0] == "noun" or word[0] == "direction":
			object = word
Exemple #18
0
def parse_answer(answer):
    print 'enter parse_answer'
    print answer
    try:
        word_list = scan(answer)
        sentence = parse_sentence(word_list)
        return True
    except ParserError as e:
        return False
Exemple #19
0
def make_valid_sent(input):
    sent = lexicon.scan(input)
    verb = ""
    object = ""
    for word in sent:
        if word[0] == "verb":
            verb = word
        elif word[0] == "noun" or word[0] == "direction":
            object = word
Exemple #20
0
def process_input(input, lista):
    ignore_case = input.lower()
    if len(ignore_case.split()) == 1:
        return inputAlgo.simple_resp(ignore_case, inputAlgo.levels)
    else:
        if valid_sent(ignore_case):
            sent = parse_sentence(lexicon.scan(ignore_case))
            return inputAlgo.respond_dir(sent, lista, lexicon, 0)
        else:
            return inputAlgo.get_error_message()
Exemple #21
0
def process_input(input, lista):
	ignore_case = input.lower()
	if len(ignore_case.split()) == 1:
		return inputAlgo.simple_resp(ignore_case, inputAlgo.levels)
	else:
		if valid_sent(ignore_case):
			sent = parse_sentence(lexicon.scan(ignore_case))
			return inputAlgo.respond_dir(sent,lista,lexicon,0)
		else:
			return inputAlgo.get_error_message()
Exemple #22
0
    def enter(self):
        print """You find yourself in a long hallway with an alien at the end of the hall
luckily he is looking the other way. There are three doors that you think you can 
sneak to without being detected. One is labeled 'Escape Pod', one is 'Office' and the final is 'Weaponry'
what do you do?"""
        action = raw_input("> ")
        if sentence.parse_sentence(lexicon.scan(action)).object == 'Escape':
            print """The door is locked but there is a keypad"""
            return 'EscapePod'
        elif sentence.parse_sentence(lexicon.scan(action)).object == 'Office':
            print """You open the door and slip inside without being detected"""
            return 'Office'
        elif sentence.parse_sentence(lexicon.scan(
                action)).object == 'Weaponry' or sentence.parse_sentence(
                    lexicon.scan(action)).object == 'weaponry':
            print """You open the door and slip in undetected."""
            return 'Weaponry'
        else:
            print "The alien hears you fumbling around and shoots, you die"
            return 'death'
Exemple #23
0
    def enter(self):
        print """Welcome to my game, please type in "full" sentences at all times I.E. "Enter the Room" when giving commands\n------------------------\n
        You awake in a spaceship on what appears to be an operating table
look down and see you are wearing what looks like a hospital gown. 
Suddenly you hear the sound of someone approaching from the main door way.
You frantically look around and see that there is another doorway and a closet.
You also see a scalpel on the table, you are running out of time, what do you do?"""
        action = raw_input("> ")
        if sentence.parse_sentence(lexicon.scan(action)).object == 'closet':
            print """You hide in the closet and see as an alien enters the room,
The alien looks paniced that you are not on the operating table and presses
a large emergency button on the wall before leaving the room.
red lights start flashing and a gas enters the room,
You breath in the gas and pass out."""
            return 'WakeUp'
        elif sentence.parse_sentence(lexicon.scan(
                action)).object == 'doorway' or sentence.parse_sentence(
                    lexicon.scan(action)).object == 'door':
            print """you run out the back door"""
            return 'Hallway'
        elif sentence.parse_sentence(lexicon.scan(
                action)).verb == 'leave' or sentence.parse_sentence(
                    lexicon.scan(action)).verb == 'exit':
            print """you run out the back door"""
            return 'Hallway'
        elif sentence.parse_sentence(lexicon.scan(action)).object == 'scalpel':
            print """An alien enters the room and sees you holding a scalpel
he quickly grabs his laser gun off his belt and shoots you"""
            return 'death'
        else:
            print "Your eyes are tired and you fall asleep"
            return 'WakeUp'
def scan_sentence(ans):
    scanned_phrase = lexicon.scan(ans)
    
    if len(scanned_phrase) <= 1:
       print "Not enough words! Try again"
       get_user_input()
    
    parsed_phrase = Parser()
    sentence = parsed_phrase.parse_sentence(scanned_phrase)
    
    print sentence.subject
    print sentence.verb
    print sentence.object
    exit(1)
Exemple #25
0
    def enter(self):
        print """You look down at the keypad and realize you dont have time to go to another door, you realize you have to guess What code do you enter?"""
        action = raw_input("> ")
        if sentence.parse_sentence(lexicon.scan(action)).object == 8956:
            print """The door opens and you find yourself in the cockpit of an escape pod"""
            if inventory.items.holding('key'):
                print """You launch the ship into space and set the course for earth, you are free!"""
                return 'Win'
            else:
                print """You dont have a key the launch the space craft! The alien guards captures you before you can make another move"""
            return 'death'

        else:
            print "The keypad loudly beeps to inform you that the code was incorrect, this alerts nearby aliens who shoots you dead."
            return 'death'
Exemple #26
0
 def go(self, direction):
     Room.score += 1
     # direction with only one word does not need parsing
     if len(direction.split()) == 1:
         return self.paths.get(direction, self.paths.get("*"))
     # Larger sentences do. Here it searches using key value if key contains verb and object
     else:
         try:
             # ParserError needs to be handled, otherwise crashes site with bad input
             sentence = lexicon_parser.parse_sentence(
                 lexicon.scan(direction))
         except ParserError:
             return self.paths.get("*", None)
         for i in self.paths.keys():
             if sentence.verb in i and sentence.object in i:
                 return self.paths.get(i, self.paths.get("*"))
Exemple #27
0
def process_input(input, lista, objects):
    if check_utf8_alpha(input):
        if input in lexicon.dir:
            input = "go " + input
        ignore_case = input.lower()
        temp = check_stop(ignore_case)
        if len(temp) == 1:
            return inputAlgo.simple_resp(temp[0], lista)
        else:
            if valid_sent(ignore_case):
                sent = parse_sentence(lexicon.scan(ignore_case))
                return inputAlgo.respond_dir(sent, lista, lexicon, objects)
            else:
                return inputAlgo.get_error_message()
    else:
        return "Invalid input. Letters and numbers only please."
Exemple #28
0
def process_input(input, lista, objects):
	if check_utf8_alpha(input):
		if input in lexicon.dir:
			input = "go " + input 
		ignore_case = input.lower()
		temp = check_stop(ignore_case)
		if len(temp) == 1:
			return inputAlgo.simple_resp(temp[0], lista)
		else:
			if valid_sent(ignore_case):
				sent = parse_sentence(lexicon.scan(ignore_case))
				return inputAlgo.respond_dir(sent,lista,lexicon,objects)
			else:
				return inputAlgo.get_error_message()
	else:
		return "Invalid input. Letters and numbers only please."
Exemple #29
0
def valid_sent(input):
    sent = lexicon.scan(input)
    verb_index = 1000
    object_index = 1000
    stop_count = 0
    verb_count = 0
    object_count = 0
    for i, item in enumerate(sent):
        if item[0] == "verb":
            verb_index = i
            verb_count += 1
        if item[0] == "noun" or item[0] == "direction":
            object_index = i
            object_count += 1
        if item[0] == "stop" and object_index > i:
            stop_count += 1
    total_count = stop_count + verb_count
    if object_index == verb_index + 1 + stop_count and total_count < 3:
        return True
    else:
        return False
Exemple #30
0
def valid_sent(input):
	sent = lexicon.scan(input)
	verb_index = 1000
	object_index = 1000
	stop_count = 0
	verb_count = 0
	object_count = 0
	for i, item in enumerate(sent):
		if item[0] == "verb":
			verb_index =  i
			verb_count += 1
		if item[0] == "noun" or item[0] == "direction":
			object_index =  i
			object_count += 1
		if item[0] == "stop" and object_index > i:
			stop_count += 1
	total_count = stop_count + verb_count
	if object_index == verb_index + 1 + stop_count and total_count < 3:
		return True
	else:
		return False
Exemple #31
0
    def user_input(self, room_nr):
        while True:
            try:
                action = raw_input("> ")

                if action == "quit" or action == "exit":
                    self.quit()

                p = parsers.Parser(lexicon.scan(action))
                s = p.parse_sentence()

                if s.verb == "show" and s.object == "inventory":
                    player.print_inventory()
                elif s.verb == "read" and s.object == "map" or s.verb == "show" and s.object == "map":
                    self.print_map(self.room_nr)
                elif s.verb == "take" and i.check_item(self.room_nr,
                                                       s.object) == True:
                    self.add_item(s.object)
                else:
                    break

            except parsers.ParserError:
                self.invalid_input()
        return s
Exemple #32
0
 def enter(self):
     print """It looks just like a human office. You see a computer a file cabinet and a closet what do you do? (you can also always leave)"""
     action = raw_input("> ")
     if sentence.parse_sentence(lexicon.scan(action)).object == 'computer':
         print """The computer is locked and the attempt to login causes an alarm to go off, aliens flood in to the room and detain you knocking you out."""
         return 'WakeUp'
     elif sentence.parse_sentence(lexicon.scan(action)).object == 'closet':
         print """You open the closet and are terrified to find that it is full of human skins on coat hangers as though they are clothes, They look alarmingly like many of the politicians you sometimes see on TV. you look around the office again."""
         return 'Office'
     elif sentence.parse_sentence(lexicon.scan(
             action)).object == 'file' or sentence.parse_sentence(
                 lexicon.scan(action)).object == 'cabinet':
         print """You open filecabinet and start looking through the files, its all written in nonsense alien languages The only thing you can make out are the numbers 8956, you look around the office again"""
         return 'Office'
     elif sentence.parse_sentence(lexicon.scan(
             action)).verb == 'leave' or sentence.parse_sentence(
                 lexicon.scan(action)).verb == 'exit':
         print "Satified with your investigation you head back into the hallway"
         return 'Hallway2'
     else:
         print "The door to the office opens and an alien enters, he is quick to shoot you on sight, you die"
         return 'death'
Exemple #33
0
def test_directions():
	assert_equal(lexicon.scan("north"), [('direction', 'north')])
	result = lexicon.scan("north south east")
	assert_equal(result, [('direction', 'north'),
							('direction', 'south'),
							('direction', 'east')])
Exemple #34
0
def test_capitals():
	assert_equal(lexicon.scan("AllWeNeedIsWove"), [('error', 'AllWeNeedIsWove')])
Exemple #35
0
def test_errors():
    assert_equal(lexicon.scan("ASJEUF"), [('error', 'ASJEUF')])
    result = lexicon.scan("bear ISA the princess")
    assert_equal(result, [('noun', 'bear'), ('error', 'ISA'), ('stop', 'the'),
                          ('noun', 'princess')])
Exemple #36
0
def test_nouns():
    assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
    result = lexicon.scan("bear princess")
    assert_equal(result, [('noun', 'bear'), ('noun', 'princess')])
Exemple #37
0
def test_verbs():
    assert_equal(lexicon.scan("go"), [('verb', 'go')])
    result = lexicon.scan("go kill eat")
    assert_equal(result, [('verb', 'go'), ('verb', 'kill'), ('verb', 'eat')])
Exemple #38
0
def test_verbs():
	assert_equal(lexicon.scan("go"), [('verb', 'go')])
	result = lexicon.scan("go kill eat")
	assert_equal(result, [('verb', 'go'),
							('verb', 'kill'),
							('verb', 'eat')])
def test_errors():
    assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
    result = lexicon.scan("bear IAS princess west")  # study drill 1 我加上了west
    assert_equal(result, [('noun', 'bear'), ('error', 'IAS'),
                          ('noun', 'princess'), ('direction', 'west')])
def convert_number(s):
    try:
        return int(s)
    except ValueError:
        return None


"""inputted_word = words[0]
inputted_word = Word(inputted_word)
inputted_word.find_word_type()
print inputted_word.word_type
"""


def scanner():
    sentence = []
    for each_word in words:
        each_word = Word(each_word)
        each_word.find_word_type()
        tuple = each_word.word_type, each_word.name
        sentence.append(tuple)
    return sentence

scanned = lexicon.scan(stuff)
parsed = parser.parse_sentence(scanned)
print parsed.show()


        
        
Exemple #41
0
def parse_action(action):
	scanned_words = scan(action)
	print "Scanned words", scanned_words
	return parse_sentence(scanned_words)
Exemple #42
0
	return Sentence(subj, verb, obj)


def parse_sentence(word_list):
	Skip(word_list, 'stop')

	start = (Peek(word_list)).word

	if start == 'noun':
		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: %s" % start)


input_sentence = raw_input("What sentence shall I parse for you? ")
scanned_sentence = lexicon.scan(input_sentence)
try:
	parsed_sentence = parse_sentence(scanned_sentence)
	print "The scanned_sentence is ", scanned_sentence
	print "When parsed, I get: "
	print "\t* parsed_sentence.subject is", parsed_sentence.subject
	print "\t* parsed_sentence.verb is ", parsed_sentence.verb
	print "\t* parsed_sentence.s_object is ", parsed_sentence.s_object
except ParserError:
	print "I'm sorry, but I don't understand that sentence. :-("

Exemple #43
0
def test_numbers():
    assert_equal(lexicon.scan("1234"),[('number',1234)])
    result = lexicon.scan("3 91234")
Exemple #44
0
 def process_input(self, user_input):
     action = lexicon.scan(user_input)
     print(action)
     return parser.parse_sentence(action)
Exemple #45
0
def test_stops():
	assert_equal(lexicon.scan("the"), [('stop', 'the')])
	result = lexicon.scan("the in of")
	assert_equal(result, [('stop', 'the'),
							('stop', 'in'),
							('stop', 'of')])
Exemple #46
0
def test_stops():
    assert_equal(lexicon.scan("the"), [('stop', 'the')])
    result = lexicon.scan("the in of")
    assert_equal(result, [('stop', 'the'), ('stop', 'in'), ('stop', 'of')])
Exemple #47
0
def test_nouns():
	assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
	result = lexicon.scan("bear princess")
	assert_equal(result, [('noun', 'bear'),
							('noun', 'princess')])
Exemple #48
0
def test_numbers():
    assert_equal(lexicon.scan("1234"), [('number', 1234)])
    result = lexicon.scan("3 45 9123")
    assert_equal(result, [('number', 3), ('number', 45), ('number', 9123)])
Exemple #49
0
def test_numbers():
	assert_equal(lexicon.scan("1234"), [('number', 1234)])
	result = lexicon.scan("3 91234")
	assert_equal(result, [('number', 3),
							('number', 91234)])
Exemple #50
0
def test_directions():
    assert_equal(lexicon.scan("north"), [('direction', 'north')])
    result = lexicon.scan("North SOUTH east")
    assert_equal(result, [('direction', 'north'), ('direction', 'south'),
                          ('direction', 'east')])
Exemple #51
0
def test_errors():
	assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
	result = lexicon.scan("bear IAS princess")
	assert_equal(result, [('noun', 'bear'),
							('error', 'IAS'),
							('noun', 'princess')])