Untitled
# Adjust the grammar to properly handle the direct address and parse the sentence cfg = CFG.fromstring(""" S -> NP VP NP -> 'We' | 'you' | Det ADJP | Det N | N | 'Dr.' Noun VP -> V NP | V NP NP V -> 'wish' Det -> 'a' ADJP -> Adj N Adj -> 'happy' N -> 'year' | 'Eman' Noun -> 'Eman' ADJP -> 'new' N """) # Sentence to parse sentence = ['We', 'wish', 'you', 'a', 'happy', 'new', 'year', 'Dr.', 'Eman'] parser = nltk.ChartParser(cfg) # Try parsing the sentence try: tree = next(parser.parse(sentence)) tree.show() except Exception as e: print(f"Parsing failed: {e}")
Leave a Comment