def demo(choice=None,
         print_times=True, print_grammar=False,
         print_trees=True, trace=2,
         sent='I saw John with a dog with my cookie', numparses=5):
    import sys, time
    from nltk import nonterminals, Production, CFG

    # The grammar for ChartParser and SteppingChartParser:
    grammar = demo_grammar()
    if print_grammar:
        print("* Grammar")
        print(grammar)

    # Tokenize the sample sentence.
    print("* Sentence:")
    print(sent)
    tokens = sent.split()
    print(tokens)
    print()

    # Ask the user which parser to test,
    # if the parser wasn't provided as an argument
    if choice is None:
        print('  1: Top-down chart parser')
        print('  2: Bottom-up chart parser')
        print('  3: Bottom-up left-corner chart parser')
        print('  4: Left-corner chart parser with bottom-up filter')
        print('  5: Stepping chart parser (alternating top-down & bottom-up)')
        print('  6: All parsers')
        print('\nWhich parser (1-6)? ', end=' ')
        choice = sys.stdin.readline().strip()
        print()

    choice = str(choice)
    if choice not in "123456":
        print('Bad parser number')
        return

    # Keep track of how long each parser takes.
    times = {}

    strategies = {'1': ('Top-down', TD_STRATEGY),
                  '2': ('Bottom-up', BU_STRATEGY),
                  '3': ('Bottom-up left-corner', BU_LC_STRATEGY),
                  '4': ('Filtered left-corner', LC_STRATEGY)}
    choices = []
    if choice in strategies: choices = [choice]
    if choice=='6': choices = "1234"

    # Run the requested chart parser(s), except the stepping parser.
    for strategy in choices:
        print("* Strategy: " + strategies[strategy][0])
        print()
        cp = ChartParser(grammar, strategies[strategy][1], trace=trace)
        t = time.time()
        chart = cp.chart_parse(tokens)
        parses = list(chart.parses(grammar.start()))
        
        times[strategies[strategy][0]] = time.time()-t
        print("Nr edges in chart:", len(chart.edges()))
        if numparses:
            assert len(parses)==numparses, 'Not all parses found'
        if print_trees:
            for tree in parses: print(tree)
        else:
            print("Nr trees:", len(parses))
        print()

    # Run the stepping parser, if requested.
    if choice in "56":
        print("* Strategy: Stepping (top-down vs bottom-up)")
        print()
        t = time.time()
        cp = SteppingChartParser(grammar, trace=trace)
        cp.initialize(tokens)
        for i in range(5):
            print('*** SWITCH TO TOP DOWN')
            cp.set_strategy(TD_STRATEGY)
            for j, e in enumerate(cp.step()):
                if j>20 or e is None: break
            print('*** SWITCH TO BOTTOM UP')
            cp.set_strategy(BU_STRATEGY)
            for j, e in enumerate(cp.step()):
                if j>20 or e is None: break
        times['Stepping'] = time.time()-t
        print("Nr edges in chart:", len(cp.chart().edges()))
        if numparses:
            assert len(list(cp.parses()))==numparses, 'Not all parses found'
        if print_trees:
            for tree in cp.parses(): print(tree)
        else:
            print("Nr trees:", len(list(cp.parses())))
        print()

    # Print the times of all parsers:
    if not (print_times and times): return
    print("* Parsing times")
    print()
    maxlen = max(len(key) for key in times)
    format = '%' + repr(maxlen) + 's parser: %6.3fsec'
    times_items = times.items()
    for (parser, t) in sorted(times_items, key=lambda a:a[1]):
        print(format % (parser, t))
A demonstration of the chart parsers.
dataset/ETHPy150Open nltk/nltk/nltk/parse/chart.py/demo