To demonstrate the formal grammar and language framework I developed, I created a formal grammar that implements a weird version of Barsalou's Percetpual Symbol Systems. Each sentence is a "simulation", with each simulation having exactly one "simulator", which in turn could have many categories and instances that make it up. Not that that's exactly what he was saying, but it's a fun attempt to formalize his system from his 1999 BBS paper.
In [66]:
from IPython.display import display, Latex
from grammar import Grammar, ProductionRule
# build a hierarchy of production rules
production_rules = [
ProductionRule('simulation', ['simulator', 'category', 'instance'], n_outputs='all'),
ProductionRule('simulator', ['driving', 'flying', 'seeing red', 'seeing blue', 'seeing green'], n_outputs='one'),
ProductionRule('category', ['car', 'airplane', 'bird', 'grass', 'bed'], n_outputs='many'),
ProductionRule('instance', ['2004 pontiac grand am', '2016 subaru crosstrack', '2010 chevy silverado',
'Boeing 747', 'queen bed with yellow sheets', 'cardinal', 'sky',
'cheatgrass', 'kentucky bluegrass'], n_outputs='many')
]
# instantiate the grammar based on the rules
perceptual_symbol_grammar = Grammar(production_rules)
# use the print_latex() method of the Grammar class to print the formal grammar
display(Latex(perceptual_symbol_grammar.print_latex()))
In [67]:
# now let's create a language that will produce sentences based on the grammar
from grammar import Language
l = Language(perceptual_symbol_grammar)
prods = l.productions()
In [68]:
# You can run this as many times as you like and get a new production
for i in range(10):
p = next(prods)
print(next(prods) + '\n')