def test_wrong_subject2(): test_util.start('Testing WrongSubject2...') ep = WrongSubject2() test_util.assertion(ep.match(NLP('She is a cook')).has_error, True, None) test_util.assertion(ep.match(NLP('He is a cook')).has_error, False, None) test_util.assertion(ep.match(NLP('It is a cook.')).has_error, True, None) test_util.result()
def test_he_is_a_nurse(): test_util.start('Testing HeIsANurse match...') pattern = HeIsANurse() test_util.assertion(pattern.match(NLP('He is a nurse')), True, None) test_util.assertion(pattern.match(NLP('Nurse')), False, None) test_util.assertion(pattern.match(NLP("A nurse.")), False, None) test_util.result()
def test_where_are_you_from(): test_util.start('Testing errors1.where_are_you_from...') test_util.assertion( where_are_you_from(NLP('I am Taiwan.')).has_error, True, 'I am Taiwan') test_util.assertion( where_are_you_from(NLP('I am from Taiwan.')).has_error, False, 'I am from Taiwan') test_util.result()
def test_he_is_a_job(): test_util.start('Testing HeIsAJob match...') pattern = HeIsAJob() test_util.assertion(pattern.match(NLP('He is a cook')), True, None) test_util.assertion(pattern.match(NLP('He is a taxi driver.')), True, None) test_util.assertion(pattern.match(NLP('She is a policeman')), False, None) test_util.assertion(pattern.match(NLP('He is a Cook')), False, None) test_util.result()
def test_no_he_is_not(): test_util.start('Testing NoHeIsNot match...') pattern = NoHeIsNot() test_util.assertion(pattern.match(NLP('No')), True, None) test_util.assertion(pattern.match(NLP('No, he is not.')), True, None) test_util.assertion(pattern.match(NLP("No, he isn't")), True, None) test_util.assertion(pattern.match(NLP('Yes he is.')), False, None) test_util.result()
def test_capitalized_cook(): test_util.start('Testing CapitalizedCook...') ep = CapitalizedCook() test_util.assertion( ep.match(NLP('He is a cook')).has_error, False, 'He is a cook') test_util.assertion( ep.match(NLP('He is a Cook')).has_error, True, 'He is a Cook') test_util.result()
def test_capitalized_doctor(): test_util.start('Testing CapitalizedDoctor...') ep = CapitalizedDoctor() test_util.assertion( ep.match(NLP('She is a doctor')).has_error, False, 'She is a doctor') test_util.assertion( ep.match(NLP('She is a Doctor')).has_error, True, 'She is a Doctor') test_util.result()
def test_he_is_a_cook(): test_util.start('Testing SheIsADoctor match...') pattern = HeIsACook() test_util.assertion(pattern.match(NLP('He is a cook')), True, None) test_util.assertion(pattern.match(NLP('He is a cook.')), True, None) test_util.assertion(pattern.match(NLP('She is a cook')), False, None) test_util.assertion(pattern.match(NLP('He is a Cook')), False, None) test_util.result()
def test_she_is_a_doctor_match(): test_util.start('Testing SheIsADoctor match...') pattern = SheIsADoctor() test_util.assertion(pattern.match(NLP('She is a doctor')), True, None) test_util.assertion(pattern.match(NLP('She is a doctor.')), True, None) test_util.assertion(pattern.match(NLP('He is a doctor')), False, None) test_util.assertion(pattern.match(NLP('She is a Doctor')), False, None) test_util.result()
def test_polarity_mismatch(): test_util.start('Testing PolarityMismatch...') ep = PolarityMismatch() test_util.assertion(ep.match(NLP('Yes, he is')).has_error, False, None) test_util.assertion(ep.match(NLP('No, he is not')).has_error, False, None) test_util.assertion(ep.match(NLP("Yes, he isn't")).has_error, True, None) test_util.assertion(ep.match(NLP('No, he is.')).has_error, True, None) test_util.result()
def test_wrong_determiner(): test_util.start('Testing WrongDeterminer...') ep = WrongDeterminer() test_util.assertion(ep.match(NLP('He is an doctor')).has_error, True, None) test_util.assertion(ep.match(NLP('He is a apple')).has_error, True, None) test_util.assertion(ep.match(NLP('He is an apple')).has_error, False, None) test_util.assertion(ep.match(NLP('He is a doctor')).has_error, False, None) test_util.result()
def test_sentence_starts_lower(): test_util.start('Testing SentenceStartsLower...') ep = SentenceStartsLower() test_util.assertion(ep.match(NLP('hi')).has_error, True, 'hi') test_util.assertion(ep.match(NLP('Hi')).has_error, False, 'Hi') test_util.assertion(ep.match(NLP('i am Tim')).has_error, True, 'i am Tim') test_util.assertion( ep.match(NLP('She is a cook')).has_error, False, 'She is a cook') test_util.result()
def test_missing_determiner(): test_util.start('Testing MissingDeterminer...') ep = MissingDeterminer() test_util.assertion(ep.match(NLP('He is doctor')).has_error, True, None) test_util.assertion( ep.match(NLP('I have cold')).has_error, True, 'I have cold') test_util.assertion(ep.match(NLP('He is a doctor')).has_error, False, None) test_util.assertion(ep.match(NLP('Yes, he is')).has_error, False, None) test_util.result()
def test_match_what_grade_are_you_in_response(): matches = [ (NLP('I am in the 7th grade.'), '7th'), (NLP("I'm in the eighth grade."), 'eighth'), (NLP('The Ninth grade.'), 'Ninth'), ] non_matches = [NLP('I am in the eight grade.')] test_util.test_matches(matches, non_matches, match_what_grade_are_you_in_response)
def test_wrong_job_2(): test_util.start('Testing WrongJob2...') ep = WrongJob('cook') test_util.assertion( ep.match(NLP('He is a driver')).has_error, True, 'driver') test_util.assertion(ep.match(NLP('He is a cook')).has_error, False, 'cook') test_util.assertion( ep.match(NLP('He is a teacher')).has_error, True, 'teacher') test_util.result()
def test_missing_verb(): test_util.start('Testing MissingVerb...') ep = MissingVerb() test_util.assertion(ep.match(NLP('He a doctor')).has_error, True, None) test_util.assertion(ep.match(NLP('Happy')).has_error, False, None) test_util.assertion(ep.match(NLP('I happy')).has_error, True, None) test_util.assertion( ep.match(NLP('She is a doctor')).has_error, False, None) test_util.assertion(ep.match(NLP('She a doctor.')).has_error, True, None) test_util.result()
def test_capitalized_father(): test_util.start('Testing CapitalizedFather...') ep = CapitalizedFather() test_util.assertion( ep.match(NLP('My father is a cook')).has_error, False, 'My father is a cook') test_util.assertion( ep.match(NLP('My Father is a cook')).has_error, True, 'My Father is a cook') test_util.result()
def test_wrong_job_1(): test_util.start('Testing WrongJob1...') ep = WrongJob('doctor') test_util.assertion( ep.match(NLP('She is a driver')).has_error, True, 'driver') test_util.assertion( ep.match(NLP('She is a doctor')).has_error, False, 'doctor') test_util.assertion( ep.match(NLP('She is a teacher')).has_error, True, 'teacher') test_util.result()
def test_my_mother_is_a_job(): test_util.start('Testing MyMotherIsAJob match...') pattern = MyMotherIsAJob() test_util.assertion(pattern.match(NLP('My mother is a cook')), True, 'My mother is a cook') test_util.assertion(pattern.match(NLP('My mother is a taxi driver.')), True, 'My mother is a taxi driver.') test_util.assertion(pattern.match(NLP('My Mother is a policeman')), False, 'My Mother is a policeman') test_util.assertion(pattern.match(NLP('My mother is a Cook')), False, 'My mother is a Cook') test_util.result()
def test_wrong_subject_is_3(): test_util.start('Testing WrongSubjectIs3...') ep = WrongSubjectIs('She') test_util.assertion( ep.match(NLP('He is a nurse')).has_error, True, 'He is a nurse') test_util.assertion( ep.match(NLP('It is a nurse')).has_error, True, 'It is a nurse') test_util.assertion( ep.match(NLP('This is a nurse')).has_error, True, 'This is a nurse') test_util.assertion( ep.match(NLP('That is a nurse')).has_error, True, 'That is a nurse') test_util.assertion( ep.match(NLP('She is a nurse')).has_error, False, 'She is a nurse') test_util.result()
def match_how_are_you_response(user_input): """Match user input pattern for task 1.3. PATTERN MATCHED: BOT: How are you today? USR: [options] I am {state}(, thank you|thanks) I'm {state}(, thank you|thanks) {State}(, thank you|thanks) I have a cold Not bad Not too bad I feel happy [/options] INFO RETURNED: The user's state. ISSUES: - There is a lot of potential variation in the state. Therefore make this a regex group, extract it, and process it separately. Args: user_input: the user input (SpaCy doc). Returns: models.Match object. """ match = False info = None r1 = r"^(I'm |I am )?" \ r"(very |not )?" \ r"(?P<state>([A-Z]{1}[a-z]*)|[a-z-]*)" \ r"(, thank you|, thanks)?(.)?$" r2 = r'^I have (?P<state>(a cold|the flu))(.)?$' cold_flu = ['a cold', 'the flu'] pattern_match1 = re.match(r1, user_input.text) pattern_match2 = re.match(r2, user_input.text) if pattern_match1: modifier = pattern_match1.group(2) state = pattern_match1.group(3) # state should be an adjective info_tok = NLP(state)[0] if info_tok.tag_ in pos.ADJECTIVES: match = True info = state if modifier: info = modifier + state elif pattern_match2: state = pattern_match2.group(1) if state in cold_flu: match = True info = state # if we don't have a match thus far... try some more complicated methods. if not match: match, info = how_are_you_dep_match(user_input) # NOTE: these nested if blocks suck. Wrap these as subfunctions and # return for failing conditions. Cleaner that way... return models.Match(user_input, match, info)
def test_what_is_your_name(): test_util.start('Testing errors1.what_is_your_name...') assert not what_is_your_name(NLP('I am Tim')).has_error assert what_is_your_name(NLP('I am tim')).has_error assert what_is_your_name(NLP('He is Tim')).has_error test_util.assertion(what_is_your_name(NLP('Hi')).has_error, True, 'Hi') test_util.assertion(what_is_your_name(NLP('hi')).has_error, True, 'hi') test_util.assertion(what_is_your_name(NLP('Tim')).has_error, False, 'Tim') test_util.assertion(what_is_your_name(NLP('tim')).has_error, True, 'tim') test_util.result()
def test_match_how_old_are_you_response(): matches = [ (NLP('I am eight years old'), 'eight'), (NLP("I'm 9 years old"), '9'), (NLP('I am thirteen'), 'thirteen'), (NLP("I'm twenty"), 'twenty'), (NLP('Seven years old'), 'Seven'), (NLP('11'), '11') ] non_matches = [] test_util.test_matches(matches, non_matches, match_how_old_are_you_response)
def test_match_where_are_your_from_response(): matches = [(NLP('I am from New Zealand'), 'New Zealand'), (NLP("I'm from Taiwan."), 'Taiwan'), (NLP('Iran.'), 'Iran')] non_matches = [ NLP('I am an elephant'), NLP('I am Taiwan.'), NLP('I am from taiwan') ] test_util.test_matches(matches, non_matches, match_where_are_you_from_response)
def what_is_your_name(user_input): """ Error set: 1: He is Sina. [wrong subject] 2: invalid name 3: I am tim. [Non-capital name] """ # 1 incorrect_subjects = ['You', 'He', 'She', 'This'] head = next(t for t in user_input if t.head == t) if user_input[0].text in incorrect_subjects: return models.ErrorResult( True, 'You must give me YOUR name. Start with "I"...') # 2 if user_input.text in common.INVALID_NAMES: return models.ErrorResult(True, 'That is not a name.') # 3 if len(user_input) == 1 and user_input[0].is_lower: return models.ErrorResult( True, 'Your name must start with a big letter: ' '"Steve" and not "steve".') if head.lemma_ == 'be': right_children = list(head.rights) if len(right_children) >= 1: name = right_children[0] # first is all we want # 2 if name.text in common.INVALID_NAMES: return models.ErrorResult(True, 'That is not a name.') # 3 if name.is_lower: if name.pos_ == 'NOUN': return models.ErrorResult( True, 'Your name must start with a big letter: ' '"Steve" and not "steve".') if name.pos_ == 'ADJ': temp = NLP(name.text) if temp[0].pos_ == 'NOUN': return models.ErrorResult( True, 'Your name must start with a big letter: ' '"Steve" and not "steve".') return models.ErrorResult(False)
def match_how_old_are_you_response(user_input): """Match user input pattern for task 1.5. PATTERN MATCHES: BOT: How old are you? USR: [options] I am {age} years old I'm {age} years old I am {age} I'm {age} {age} years old {age} [/options] INFO RETURNED: The user's age. ISSUES: - The age could be given as text or a number. Args: user_input: the user input (SpaCy doc). Returns: models.Match object. """ r = r'^' \ r"(I am |I'm )?" \ r"%s" % common_regex.NUMBER + \ r"( years old)?" \ r'(.)?' \ r'$' match = False info = None pattern_match = re.match(r, user_input.text) if pattern_match: info = pattern_match.group('number') age_match = common_regex.match_number(NLP(info)) match = age_match.match return models.Match(user_input, match, info)