def display_examples(utterances, grammar=None, domain=None): for utterance in utterances: print "="*70 print utterance parses = grammar.parse_input(utterance) for parse in parses: print '\nParse:\n', parse_to_pretty_string(parse, show_sem=True) print 'Denotation:', domain.execute(parse.semantics)
def display_examples(utterances, grammar=None, domain=None): for utterance in utterances: print("=" * 70) print(utterance) parses = grammar.parse_input(utterance) for parse in parses: print('\nParse:\n', parse_to_pretty_string(parse, show_sem=True)) print('Denotation:', domain.execute(parse.semantics))
def print_parses(example, parses, metrics=[NumParsesMetric()], max_parses=10000, show_syntax=False): print('%-34s %s' % ('input', example.input)) if example.semantics != None: print('%-34s %s' % ('target semantics', str(example.semantics))) if example.denotation != None: print('%-34s %s' % ('target denotation', str(example.denotation))) print() for metric in metrics: metric_value = metric.evaluate(example, parses) print('%-34s %.3g' % (metric.name(), metric_value)) print() for idx, parse in enumerate(parses[:max_parses]): def outcome_marker(target, prediction): return ('+' if prediction == target else '-') if target != None else ' ' parse_outcome = outcome_marker(example.parse, parse) semantics_outcome = outcome_marker(example.semantics, parse.semantics) if example.denotation != None: denotation_outcome = outcome_marker(example.denotation, parse.denotation) else: denotation_outcome = ' ' lines = [] if show_syntax: lines.append( '%-15s %s \n%s' % ('parse', parse_outcome, parse_to_pretty_string(parse))) lines.append('%-15s %s %s' % ('semantics', semantics_outcome, parse.semantics)) if example.denotation != None or parse.denotation != None: lines.append('%-15s %s %s' % ('denotation', denotation_outcome, parse.denotation)) for l, line in enumerate(lines): if l == 0: print('%-3s %8.3f %s' % (idx, parse.score, line)) else: print('%-3s %8s %s' % ('', '', line)) if len(parses) > max_parses: print('(additional parses truncated)') print('\n' + '-' * 80)
def print_parses(example, parses, metrics=[NumParsesMetric()], max_parses=10000, show_syntax=False): print('%-34s %s' % ('input', example.input)) if example.semantics != None: print('%-34s %s' % ('target semantics', str(example.semantics))) if example.denotation != None: print('%-34s %s' % ('target denotation', str(example.denotation))) print() for metric in metrics: metric_value = metric.evaluate(example, parses) print('%-34s %.3g' % (metric.name(), metric_value)) print() for idx, parse in enumerate(parses[:max_parses]): def outcome_marker(target, prediction): return ('+' if prediction == target else '-') if target != None else ' ' parse_outcome = outcome_marker(example.parse, parse) semantics_outcome = outcome_marker(example.semantics, parse.semantics) if example.denotation != None: denotation_outcome = outcome_marker(example.denotation, parse.denotation) else: denotation_outcome = ' ' lines = [] if show_syntax: lines.append('%-15s %s \n%s' % ('parse', parse_outcome, parse_to_pretty_string(parse))) lines.append('%-15s %s %s' % ('semantics', semantics_outcome, parse.semantics)) if example.denotation != None or parse.denotation != None: lines.append('%-15s %s %s' % ('denotation', denotation_outcome, parse.denotation)) for l, line in enumerate(lines): if l == 0: print('%-3s %8.3f %s' % (idx, parse.score, line)) else: print('%-3s %8s %s' % ('', '', line)) if len(parses) > max_parses: print('(additional parses truncated)') print('\n' + '-' * 80)
# In[ ]: from arithmetic import ArithmeticDomain from parsing import parse_to_pretty_string # Import the domain and make sure all is well: math_domain = ArithmeticDomain() # Core grammar: math_grammar = math_domain.grammar() # A few examples: parses = math_grammar.parse_input("minus two plus three") for parse in parses: print '\nParse:\n', parse_to_pretty_string(parse, show_sem=True) print 'Denotation:', math_domain.execute(parse.semantics) # This is a convenience function we'll use for seeing what the grammar # is doing: # In[ ]: def display_examples(utterances, grammar=None, domain=None): for utterance in utterances: print "="*70 print utterance parses = grammar.parse_input(utterance) for parse in parses: print '\nParse:\n', parse_to_pretty_string(parse, show_sem=True)
# In[ ]: from arithmetic import ArithmeticDomain from parsing import parse_to_pretty_string # Import the domain and make sure all is well: math_domain = ArithmeticDomain() # Core grammar: math_grammar = math_domain.grammar() # A few examples: parses = math_grammar.parse_input("minus two plus three") for parse in parses: print('\nParse:\n', parse_to_pretty_string(parse, show_sem=True)) print('Denotation:', math_domain.execute(parse.semantics)) # This is a convenience function we'll use for seeing what the grammar is doing: # In[ ]: def display_examples(utterances, grammar=None, domain=None): for utterance in utterances: print("=" * 70) print(utterance) parses = grammar.parse_input(utterance) for parse in parses: print('\nParse:\n', parse_to_pretty_string(parse, show_sem=True)) print('Denotation:', domain.execute(parse.semantics))