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 main(): """ Example of generating the language of a grammar. """ grammar = parse_grammar_from_file( 'pal/grammars/services/movie_grammar.txt') make_chomsky_normal_form(grammar) try: language = generate_language_cnf(grammar) language.sort() for phrase in language: print phrase print 'Size of language:', len(language) except KeyError as e: print 'Error: No rule for symbol \'{0}\''.format(e.args[0])
def main(): grammar = parse_grammar_from_file(sys.argv[1]) make_chomsky_normal_form(grammar) grammar_features = generate_grammar_features(grammar) pprint.pprint(grammar) while True: string = raw_input().strip().lower() if not string: continue if string[-1] in ['.', '?', '!']: string = string[:-1] parse_tree = parse(string, grammar_features) pprint.pprint(parse_tree) if parse_tree: print 'Extract key:', symbol = raw_input().strip().lower() print extract(parse_tree, symbol)
def main(): ''' Reads a grammar from a file. Checks that it generates the language. Converts the grammar to CNF. Checks that the grammar is in CNF, and that it still generates the language. ''' print('Loading grammar from file "{0}"...'.format(_GRAMMAR_FILE)) grammar = parse_grammar_from_file(_GRAMMAR_FILE) with open(_LANGUAGE_FILE) as f: language = filter(lambda x: not not x, map(lambda x: x.strip(), f.read().split('\n'))) check_grammar(grammar, language, cnf=False) print('Converting grammar to CNF...') make_chomsky_normal_form(grammar) check_grammar(grammar, language, cnf=True) check_cnf(grammar) print('Testing the CYK parser...') test_parser(language, [], grammar)
#!/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]