Exemple #1
0
def test_grammar(service_name):
    print('Testing grammar for service \'{0}\'...'.format(service_name))
    try:
        grammar = get_grammar_for_service(service_name)
    except ValueError as e:
        print 'Error: {}'.format(e.args[0])
        return
    all_examples = parse_examples()
    ex_total = 0
    counter_ex_total = 0
    hits = 0
    misses = 0
    for key, examples in all_examples.iteritems():
        if key == service_name:
            ex_total += len(examples)
            for example in examples:
                parse_tree = parse(example, grammar)
                if parse_tree:
                    # pprint(parse_tree)
                    # print example
                    hits += 1
                else:
                    print '>', example
        else:
            counter_ex_total += len(examples)
            for counterexample in examples:
                if parse(counterexample, grammar):
                    misses += 1
                    print 'X', counterexample
    print('Success:\t\t{0}/{1}'.format(hits, ex_total))
    print('False Positives:\t{0}/{1}'.format(misses, counter_ex_total))
Exemple #2
0
def test_parser(in_language, not_in_language, grammar):
    grammar_features = generate_grammar_features(grammar)
    try:
        for string in in_language:
            assert parse(string, grammar_features)
        for string in not_in_language:
            assert not parse(string, grammar_features)
    except AssertionError as e:
        e.args += (string,)
        raise
    print('SUCCESS')
Exemple #3
0
 def get_confidence(self, params):
     query = params['query']
     parse_ = parse(query, self.grammar)
     self.cached_parse = (query, parse_)
     # FIXME: Right now 61 is returned due to conflicts with hill climbed
     # values and the grammar parses.
     return 61 if parse_ else 0
Exemple #4
0
 def get_confidence(self, params):
     query = params['query']
     parse_ = parse(query, self.grammar)
     self.cached_parse = (query, parse_)
     # FIXME: Right now 61 is returned due to conflicts with hill climbed
     # values and the grammar parses.
     return 61 if parse_ else 0
Exemple #5
0
 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)
     if parse_tree:
         intent = self.extract_intent(parse_tree)
         object_ = self.extract_object(parse_tree)
         self.update_kb(intent, object_)
         result = self.query_kb(intent, object_)
         summary = self.generate_summary(intent, object_, result)
         return ('SUCCESS', summary)
     else:
         return ('ERROR', 'I don\'t understand.')
Exemple #6
0
 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)
     if parse_tree:
         intent = self.extract_intent(parse_tree)
         object_ = self.extract_object(parse_tree)
         self.update_kb(intent, object_)
         result = self.query_kb(intent, object_)
         summary = self.generate_summary(intent, object_, result)
         return ('SUCCESS', summary)
     else:
         return ('ERROR', 'I don\'t understand.')
Exemple #7
0
    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))
Exemple #8
0
    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))
Exemple #9
0
 def get_confidence(self, params):
     query = params['query']
     parsed = parse(query, self.grammar)
     self.cached_parse = (query, parsed)
     return 100 if parsed else 0
Exemple #10
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]
Exemple #11
0
 def get_confidence(self, params):
     query = params['query']
     parsed = parse(query, self.grammar)
     self.cached_parse = (query, parsed)
     return 100 if parsed else 0
Exemple #12
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]