#NEWLINE# def demo( ) : #NEWLINE# #INDENT# import sys #NEWLINE# from optparse import OptionParser #NEWLINE# description = '\n    Parse and evaluate some sentences.\n    ' #NEWLINE# opts = OptionParser ( description = description ) #NEWLINE# opts . set_defaults ( evaluate = True , beta = True , syntrace = 0 , semtrace = 0 , demo = 'default' , grammar = '' , sentences = '' ) #NEWLINE# opts . add_option ( '-d' , '--demo' , dest = 'demo' , help = "choose demo D; omit this for the default demo, or specify 'chat80'" , metavar = 'D' ) #NEWLINE# opts . add_option ( '-g' , '--gram' , dest = 'grammar' , help = 'read in grammar G' , metavar = 'G' ) #NEWLINE# opts . add_option ( '-m' , '--model' , dest = 'model' , help = "import model M (omit '.py' suffix)" , metavar = 'M' ) #NEWLINE# opts . add_option ( '-s' , '--sentences' , dest = 'sentences' , help = 'read in a file of test sentences S' , metavar = 'S' ) #NEWLINE# opts . add_option ( '-e' , '--no-eval' , action = 'store_false' , dest = 'evaluate' , help = 'just do a syntactic analysis' ) #NEWLINE# opts . add_option ( '-b' , '--no-beta-reduction' , action = 'store_false' , dest = 'beta' , help = "don't carry out beta-reduction" ) #NEWLINE# opts . add_option ( '-t' , '--syntrace' , action = 'count' , dest = 'syntrace' , help = "set syntactic tracing on; requires '-e' option" ) #NEWLINE# opts . add_option ( '-T' , '--semtrace' , action = 'count' , dest = 'semtrace' , help = 'set semantic tracing on' ) #NEWLINE# ( options , args ) = opts . parse_args ( ) #NEWLINE# SPACER = ( '-' * 30 ) #NEWLINE# demo_model0 ( ) #NEWLINE# sents = [ 'Fido sees a boy with Mary' , 'John sees Mary' , 'every girl chases a dog' , 'every boy chases a girl' , 'John walks with a girl in Noosa' , 'who walks' ] #NEWLINE# gramfile = 'grammars/sample_grammars/sem2.fcfg' #NEWLINE# if options . sentences : #NEWLINE# #INDENT# sentsfile = options . sentences #NEWLINE# #UNINDENT# if options . grammar : #NEWLINE# #INDENT# gramfile = options . grammar #NEWLINE# #UNINDENT# if options . model : #NEWLINE# #INDENT# exec ( ( 'import %s as model' % options . model ) ) #NEWLINE# #UNINDENT# if ( description is None ) : #NEWLINE# #INDENT# sents = read_sents ( sentsfile ) #NEWLINE# #UNINDENT# model = m0 #NEWLINE# g = g0 #NEWLINE# if options . evaluate : #NEWLINE# #INDENT# evaluations = evaluate_sents ( sents , gramfile , model , g , trace = options . semtrace ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# semreps = interpret_sents ( sents , gramfile , trace = options . syntrace ) #NEWLINE# #UNINDENT# for ( i , sent ) in enumerate ( sents ) : #NEWLINE# #INDENT# n = 1 #NEWLINE# print ( ( '\nSentence: %s' % sent ) ) #NEWLINE# print ( SPACER ) #NEWLINE# if options . evaluate : #NEWLINE# #INDENT# for ( syntree , semrep , value ) in evaluations [ i ] : #NEWLINE# #INDENT# if isinstance ( value , dict ) : #NEWLINE# #INDENT# value = set ( value . keys ( ) ) #NEWLINE# #UNINDENT# print ( ( '%d:  %s' % ( n , semrep ) ) ) #NEWLINE# print ( value ) #NEWLINE# n += 1 #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for ( syntree , semrep ) in semreps [ i ] : #NEWLINE# #INDENT# print ( ( '%d:  %s' % ( n , semrep ) ) ) #NEWLINE# n += 1