Exemple #1
0
 def __init__(self, root):
     """ Initialize user interface for the parser. """
     self.root = root
     self.grammar = Grammar()
     self.lexicon = Lexicon()
     self.root.title("Interactive Chart Parser")
     self.mainframe = ttk.Frame(self.root, padding='3 3 12 12')
     self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
     self.mainframe.columnconfigure(0, weight=1)
     self.mainframe.rowconfigure(0, weight=1)
     ttk.Button(self.mainframe,
                text='Show Lexicon',
                width=15,
                command=self.show_lexicon).grid(column=1, row=1, sticky=W)
     ttk.Button(self.mainframe,
                text='Show Grammar',
                width=15,
                command=self.show_grammar).grid(column=1, row=2, sticky=W)
     ttk.Button(self.mainframe,
                text='Add a Word',
                width=15,
                command=self.add_lexical_entry).grid(column=2,
                                                     row=1,
                                                     sticky=W)
     ttk.Button(self.mainframe,
                text='Add a Rule',
                width=15,
                command=self.add_grammar_rule).grid(column=2,
                                                    row=2,
                                                    sticky=W)
     ttk.Button(self.mainframe,
                text='Import Lexicon',
                width=15,
                command=self.import_lexicon).grid(column=3, row=1, sticky=W)
     ttk.Button(self.mainframe,
                text='Import Grammar',
                width=15,
                command=self.import_grammar).grid(column=3, row=2, sticky=W)
     ttk.Button(self.mainframe,
                text='Search for Word',
                width=15,
                command=self.search).grid(column=1, row=8, sticky=W)
     self.sentence = ttk.Entry(self.mainframe, width=15)
     self.sentence.grid(column=2, row=5, columnspan=2, sticky=(W, E))
     ttk.Button(self.mainframe,
                text='Parse',
                width=15,
                command=self.parse_sentence).grid(column=3, row=6, sticky=E)
     ttk.Button(self.mainframe, text='Exit', width=10,
                command=exit).grid(column=3, row=8, sticky=E)
     ttk.Label(self.mainframe, text='Enter a sentence:').grid(column=1,
                                                              row=5,
                                                              sticky=E)
     ttk.Label(self.mainframe, text='').grid(column=1, row=3, sticky=E)
     ttk.Label(self.mainframe, text='').grid(column=1, row=7, sticky=E)
     # add padding around every widget
     for child in self.mainframe.winfo_children():
         child.grid_configure(padx=5, pady=5)
     # ensure user doesn't have to click first
     self.sentence.focus()
Exemple #2
0
 def __init__(self, grammar=Grammar(), lexicon=Lexicon()):
     """ Initializes parser with grammar and lexicon.
     Args:
         grammar (Grammar) : Grammar object for parser
         lexicon (Lexicon) : Lexicon object for parser
     """
     self._grammar = grammar
     self._lexicon = lexicon
def test_grammar_import():
    grammar = Grammar()
    grammar.load(StringIO(test_gram))
    assert grammar['NP'] == {NonTerminal('S', 'NP', 'VP'), }
    assert grammar['V'] == {
        NonTerminal('VP', 'V'), NonTerminal('VP', 'V', 'NP')}
    with pytest.raises(ValueError):
        grammar.load(StringIO('S NP VP'))
def test_grammar_add():
    grammar = Grammar()
    grammar.add(NonTerminal.from_string('np --> n'))
    assert grammar['N'] == {NonTerminal('NP', 'N')}
    grammar.add(NonTerminal('vp', 'v', 'np'))
    assert grammar['V'] == {NonTerminal('VP', 'V', 'NP')}
    grammar.add(NonTerminal('NP', 'N', 'Y'))
    assert grammar['N'] == {
        NonTerminal('NP', 'N'), NonTerminal('NP', 'N', 'Y')}
def test_grammar_str():
    grammar = Grammar()
    grammar.load(StringIO(test_gram))
    answer = [
        "NP --> DT N",
        "NP --> PN",
        "S --> NP VP",
        "VP --> V",
        "VP --> V NP"]
    answer = '\n'.join(answer)
    result = str(grammar)
    print(result)
    assert result == answer
def test_grammar_len():
    grammar = Grammar()
    assert not grammar
    grammar.load(StringIO(test_gram))
    assert grammar
    assert len(grammar) == 5
def test_grammar_str():
    grammar = Grammar()
    grammar.load(StringIO(test_gram))
    answer = [
        "NP --> DT N",
        "NP --> PN",
        "S --> NP VP",
        "VP --> V",
        "VP --> V NP"]
    answer = '\n'.join(answer)
    result = str(grammar)
    print(result)
    assert result == answer


simple_grammar = Grammar()
simple_grammar.load(StringIO("""
    S --> NP VP
    NP --> PN
    VP --> V
    """))
simple_lexicon = Lexicon()
simple_lexicon.load(StringIO("""
    I : PN
    sleep : V
    """))
simple_parser = Parser(simple_grammar, simple_lexicon)
simple_sentence = ' i   sleep '
simple_tokens = ['I', 'SLEEP']
simple_parse = '[.S [.NP [.PN I]][.VP [.V SLEEP]]]'
simple_chart = [