def match_where_are_you_from_response(user_input): """Match user input pattern for task 1.4. PATTERN MATCHED: BOT: Where are you from? USR: [options] I am from {Place} I'm from {Place} {Place} [/options] INFO RETURNED: Where the user is from. ISSUES: - What if the respond with an ethnicity: e.g. I'm Taiwanese? - To really validate this input we need to make sure they say a place, e.g. "Taiwan", and not something silly like "Earth". Args: user_input: the user input (SpaCy doc). Returns: models.Match object. """ r = r"^(I'm from |I am from )?%s(.)?$" % common_regex.NAME # same as place match = False info = None pattern_match = re.match(r, user_input.text) if pattern_match: match = True info = pattern_match.group('name') return models.Match(user_input, match, info)
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 match_nice_to_meet_you(user_input): """Match user input pattern for task 1.2. PATTERN MATCHED: BOT: Nice to meet you. USR: Nice to meet you, too. You, too. Happy|Glad to meet you, too. INFO RETURNED: None. Args: user_input: the user input (SpaCy doc). Returns: models.Match object. """ r = r'^Nice to meet you, too(.)?' match = False info = None if re.match(r, user_input.text): match = True return models.Match(user_input, match, info)
def match_name(user_input): """Match user input pattern for task 1.1. I look for PRP VBP NNP. pattern = db.get(...) PATTERN MATCHED: BOT: What's your name? USR: [option_set] 1: My name's ____. 2: My name is ____. 3: It's ____. 4: It is ____. 5: I'm ____. 6: I am ____. 7: ____. [/option_set] INFO RETURNED: The user's name. ISSUES: - Deal with punctuation - i.e. full stop? - What if they forget to capitalize their name? Args: user_input: the user input (SpaCy doc) Returns: models.Match object. """ regexs = [ r"^My name's %s(.)?$" % common_regex.NAME, r'^My name is %s(.)?$' % common_regex.NAME, r"^It's %s(.)?$" % common_regex.NAME, r'^It is %s(.)?$' % common_regex.NAME, r"^I'm %s(.)?$" % common_regex.NAME, r'^I am %s(.)?$' % common_regex.NAME, r'^%s(.)?$' % common_regex.NAME ] match = False info = None for r in regexs: pattern_match = re.match(r, user_input.text) if pattern_match: info = pattern_match.group('name') if info not in common.INVALID_NAMES: match = True return models.Match(user_input, match, info)
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)
def match_what_grade_are_you_in_response(user_input): """Match user input pattern for task 1.6. PATTERN MATCHES: BOT: What grade are you in? USR: [options] I am in the {ordinal} grade. I'm in the {ordinal} grade. The {ordinal} grade. [/options] INFO RETURNED: The user's grade. ISSUES: - Args: user_input: the user input (SpaCy doc). Returns: models.Match object. """ r = r'^' \ r"(I am in |I'm in )?" \ r'(The |the )' \ r"%s" % common_regex.ORDINAL + \ r"( grade)" \ r'(.)?' \ r'$' match = False info = None pattern_match = re.match(r, user_input.text) if pattern_match: info = pattern_match.group('ordinal') ordinal_match = common_regex.match_ordinal(NLP(info)) match = ordinal_match.match return models.Match(user_input, match, info)
def no_match(user_input): return models.Match(user_input, False, None)