(base) ling388-24$ more g2.txt S -> NP VP NP -> DT NN NP -> NP PP_possession VP -> VBD NP PP_instrument VP -> VBD NP PP_possession -> IN_possession NP PP_instrument -> IN_instrument NP NP -> 'John' NP -> 'Mary' DT -> 'a' DT -> 'the' NN -> 'boy' NN -> 'man' NN -> 'telescope' IN_possession -> 'with' IN_instrument -> 'with' VBD -> 'saw' (base) ling388-24$ python (base) ling388-24$ python Python 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:38:11) [Clang 14.0.6 ] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import nltk >>> parser2 = nltk.ChartParser(nltk.CFG.fromstring(open('g2.txt').read())) >>> for parse in parser2.parse(nltk.word_tokenize("a man with a telescope saw the boy")): ... parse.pretty_print() ... S _____________|__________________________ NP | ____________|_____________ | | PP_possession VP | _____________|________ ___|___ NP | NP | NP ___|___ | ________|______ | ___|___ DT NN IN_possession DT NN VBD DT NN | | | | | | | | a man with a telescope saw the boy >>> for parse in parser2.parse(nltk.word_tokenize("a man with a telescope saw the boy")): ... parse.draw() ... >>> >>> for parse in parser2.parse(nltk.word_tokenize("a man with a telescope saw the boy with a telescope")): ... parse.pretty_print() ... S ____________________________________|___________ | VP | ___________|________ NP | NP ____________|_____________ | ____________|_____________ | PP_possession | | PP_possession | _____________|________ | | _____________|________ NP | NP | NP | NP ___|___ | ________|______ | ___|___ | ________|______ DT NN IN_possession DT NN VBD DT NN IN_possession DT NN | | | | | | | | | | | a man with a telescope saw the boy with a telescope S ____________________________________|___________ NP VP ____________|_____________ ___________|______________________ | PP_possession | | PP_instrument | _____________|________ | | _____________|________ NP | NP | NP | NP ___|___ | ________|______ | ___|___ | ________|______ DT NN IN_possession DT NN VBD DT NN IN_instrument DT NN | | | | | | | | | | | a man with a telescope saw the boy with a telescope >>> parser2 = nltk.ChartParser(nltk.CFG.fromstring(open('g2.txt').read())) >>> parser3 = nltk.ChartParser(nltk.CFG.fromstring(open('g3.txt').read())) >>> parser3 >>> cfg = nltk.CFG.fromstring(open('g3.txt').read()) >>> cfg >>> for parse in parser3.parse(nltk.word_tokenize("the chicken is ready to eat")): ... parse.draw() ... Traceback (most recent call last): File "", line 1, in File "/Users/sandiway/opt/anaconda3/lib/python3.9/site-packages/nltk/parse/chart.py", line 1474, in parse chart = self.chart_parse(tokens) File "/Users/sandiway/opt/anaconda3/lib/python3.9/site-packages/nltk/parse/chart.py", line 1432, in chart_parse self._grammar.check_coverage(tokens) File "/Users/sandiway/opt/anaconda3/lib/python3.9/site-packages/nltk/grammar.py", line 665, in check_coverage raise ValueError( ValueError: Grammar does not cover some of the input words: "'chicken'". >>> parser3 = nltk.ChartParser(nltk.CFG.fromstring(open('g3.txt').read())) >>> for parse in parser3.parse(nltk.word_tokenize("the chicken is ready to eat")): ... parse.draw() ... >>> for parse in parser3.parse(nltk.word_tokenize("the chicken is ready to eat")): ... parse.pretty_print() ... S ________________|____ | VP | _________|___ | | ADJP | | ________|____ | | | S | | | ________|___ | | | | VP | | | | ________|___ NP | | | | VP ___|_____ | | | | ___|___ DT NN VBZ JJ NP TO VB NP | | | | | | | | the chicken is ready ... to eat ... >>> Segmentation fault: 11 (base) ling388-24$