(base) ling388-24$ ls -l g.txt -rw-r--r-- 1 sandiway staff 141 Apr 18 12:47 g.txt (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 >>> g = open('g.txt').read() >>> g "S -> NP VP\nNP -> DT NN\nDT -> 'a'\nNN -> 'man'\nVP -> VBD NP PP\nDT -> 'the'\nNN -> 'boy'\nPP -> IN NP\nIN -> 'with'\nNN -> 'telescope'\nVBD -> 'saw'\n" >>> cfg = nltk.CFG.fromstring(g) >>> cfg >>> parser = nltk.ChartParser(cfg) >>> parser >>> for parse in parser.parse(nltk.word_tokenize("a boy saw the man with a telescope")): ... parse.draw() ... >>> for parse in parser.parse(nltk.word_tokenize("a boy saw the man")): ... parse.draw() ... >>> cfg = nltk.CFG.fromstring(open('g.txt').read()) >>> cfg >>> cfg >>> parser = nltk.ChartParser(cfg) >>> for parse in parser.parse(nltk.word_tokenize("a boy saw the man")): ... parse.draw() ... >>> parser = nltk.ChartParser(nltk.CFG.fromstring(open('g.txt').read())) >>> parser >>> for parse in parser.parse(nltk.word_tokenize("a boy saw the man with a telescope")): ... parse.draw() ... >>> parser2 = nltk.ChartParser(nltk.CFG.fromstring(open('g2.txt').read())) >>> for parse in parser2.parse(nltk.word_tokenize("a boy saw the man with a telescope")): ... parse.draw() ... >>> for parse in parser2.parse(nltk.word_tokenize("a telescope saw the boy")): ... parse.draw() ... >>> parser2 = nltk.ChartParser(nltk.CFG.fromstring(open('g2.txt').read())) >>> for parse in parser2.parse(nltk.word_tokenize("John saw the boy")): ... parse.draw() ... >>>