def get_grammar_for_service(short_name): file_name = '{0}_grammar.txt'.format(short_name) file_path = path.abspath(path.join(path.dirname(__file__), 'services', file_name)) grammar = parse_grammar_from_file(file_path) make_chomsky_normal_form(grammar) grammar_features = generate_grammar_features(grammar) return grammar_features
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')
#!/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]