def extract_pizza_features(parse_tree): non_tops = set(search(parse_tree, 'negation_phrase topping_item')) all_tops = set(search(parse_tree, 'topping_item')) yes_tops = all_tops.difference(non_tops) crust_size = extract(parse_tree, 'crust_size') or 'medium' crust_type = extract(parse_tree, 'crust_type') or 'regular' crust = '{} {}'.format(crust_size, crust_type) num_pizzas = text_to_int(extract(parse_tree, 'number') or 'one') toppings = [] for t in yes_tops: item = extract(t, 'topping_item') amount = 'normal' parent_ = parent(parse_tree, t) if parent and parent_[0] == 'ingredient': amount = extract(parent_, 'topping_amount') or amount toppings.append((item, amount)) for t in non_tops: item = extract(t, 'topping_item') amount = 'none' toppings.append((item, amount)) return { 'crust': crust, 'quantity': num_pizzas, 'options': toppings, }
def extract_role(parse_tree): if extract(parse_tree, 'actor_word') \ or extract(parse_tree, 'movie_act_on_person'): return 'actor' if extract(parse_tree, 'direction_word') \ or extract(parse_tree, 'direct'): return 'director' if extract(parse_tree, 'producer_word') \ or extract(parse_tree, 'produce'): return 'producer' if extract(parse_tree, 'writer_word') \ or extract(parse_tree, 'write'): return 'writer' return ''
def extract_intent(parse_tree): if extract(parse_tree, 'person_to_movie_yes_no'): intent = ('CREDITS', 'BOOLEAN_OF') elif extract(parse_tree, 'wh_count'): intent = ('CREDITS', 'COUNT_OF') elif extract(parse_tree, 'movie_to_person_question') \ or extract(parse_tree, 'movie_to_person_description'): intent = ('CREDITS', 'MOVIES_OF') elif extract(parse_tree, 'person_to_movie_question'): intent = ('CREDITS', 'PEOPLE_OF') elif extract(parse_tree, 'year_of_movie'): intent = ('MOVIES', 'YEAR_OF') return intent
def go(self, params): query = params['query'] if self.cached_parse and self.cached_parse[0] == query: parse_tree = self.cached_parse[1] else: parse_tree = parse(query, self.grammar) pizza = extract_pizza_features(parse_tree) # Case 1: Pizza pricing query if extract(parse_tree, 'price_query'): price = price_pizzas([pizza]) if price: return "It costs $" + "{:20,.2f}.".format(price).strip() else: return ('ERROR', "I think it's more than zero dollars...") # Case 2: Pizza order query ud = params.get('user-data', {}) needs = [ req for req in PIZZA_ORDER_REQUIRED_FIELDS if req[0] not in ud ] if needs: return ('NEEDS DATA - USER', needs) card = { 'num': ud['cc-number'].strip(), 'type': ud['cc-type'].strip(), 'expire': ud['cc-expiration'].strip(), 'cvv': ud['cvv'].strip(), 'zip': ud['cc-zip'].strip(), } address = [ud['line1'], ud['line2']] res = order_pizzas(ud['phone'], ud['name'], address, card, [pizza]) if res.get('msg', '') == 'Pizza complete.': return "I just finished ordering! Your pizza is on its way!" else: msg, dts = res.get('msg', ''), res.get('details', '') return ('ERROR', "Sorry, I couldn't finish the order.<br><br>{}" "<br>{}<br><br>But you could try calling " "Dominos.".format(msg, dts))
def go(self, params): query = params['query'] if self.cached_parse and self.cached_parse[0] == query: parse_tree = self.cached_parse[1] else: parse_tree = parse(query, self.grammar) pizza = extract_pizza_features(parse_tree) # Case 1: Pizza pricing query if extract(parse_tree, 'price_query'): price = price_pizzas([pizza]) if price: return "It costs $" + "{:20,.2f}.".format(price).strip() else: return ('ERROR', "I think it's more than zero dollars...") # Case 2: Pizza order query ud = params.get('user-data', {}) needs = [req for req in PIZZA_ORDER_REQUIRED_FIELDS if req[0] not in ud] if needs: return ('NEEDS DATA - USER', needs) card = { 'num': ud['cc-number'].strip(), 'type': ud['cc-type'].strip(), 'expire': ud['cc-expiration'].strip(), 'cvv': ud['cvv'].strip(), 'zip': ud['cc-zip'].strip(), } address = [ud['line1'], ud['line2']] res = order_pizzas(ud['phone'], ud['name'], address, card, [pizza]) if res.get('msg', '') == 'Pizza complete.': return "I just finished ordering! Your pizza is on its way!" else: msg, dts = res.get('msg', ''), res.get('details', '') return ('ERROR', "Sorry, I couldn't finish the order.<br><br>{}" "<br>{}<br><br>But you could try calling " "Dominos.".format(msg, dts))
def extract_object(parse_tree): name = extract(parse_tree, 'name') title = extract(parse_tree, 'title') year = extract(parse_tree, 'year') role = MovieService.extract_role(parse_tree) return {'name': name, 'title': title, 'role': role, 'year': year}
#!/usr/bin/env python # coding: utf-8 # # Copyright (c) 2015, PAL Team. # All rights reserved. See LICENSE for details. import pprint from pal.grammars.grammars import make_chomsky_normal_form from pal.grammars.grammars import parse_grammar_from_file from pal.grammars.parser import parse, generate_grammar_features, extract, search, parent string = 'how much for two medium thin pizza with extra pineapple but no cheese sauce or mushrooms' grammar = parse_grammar_from_file('pal/grammars/services/dominos_grammar.txt') make_chomsky_normal_form(grammar) grammar_features = generate_grammar_features(grammar) parse_tree = parse(string, grammar_features) pprint.pprint(parse_tree) print 'NOT' for x in search(parse_tree, 'negation_phrase topping_item'): print extract(x, x[0]) # flatten print parent(parse_tree, x)[0] print '\nWITH' for x in set(search(parse_tree, 'topping_item')).difference( set(search(parse_tree, 'negation_phrase topping_item'))): print extract(x, x[0]) # flatten print parent(parse_tree, x)[0]
#!/usr/bin/env python # coding: utf-8 # # Copyright (c) 2015, PAL Team. # All rights reserved. See LICENSE for details. import pprint from pal.grammars.grammars import make_chomsky_normal_form from pal.grammars.grammars import parse_grammar_from_file from pal.grammars.parser import parse, generate_grammar_features, extract, search, parent string = 'how much for two medium thin pizza with extra pineapple but no cheese sauce or mushrooms' grammar = parse_grammar_from_file('pal/grammars/services/dominos_grammar.txt') make_chomsky_normal_form(grammar) grammar_features = generate_grammar_features(grammar) parse_tree = parse(string, grammar_features) pprint.pprint(parse_tree) print 'NOT' for x in search(parse_tree, 'negation_phrase topping_item'): print extract(x, x[0]) # flatten print parent(parse_tree, x)[0] print '\nWITH' for x in set(search(parse_tree, 'topping_item')).difference(set(search(parse_tree, 'negation_phrase topping_item'))): print extract(x, x[0]) # flatten print parent(parse_tree, x)[0]