Análisis Sintáctico (Parsing) - Inteligencia Artificial - Año 2012

http://www.cs.famaf.unc.edu.ar/~francolq/docencia/ia

NLTK

Dependencias:

  • python-nltk
  • python-tk
$ python
>>> import nltk
>>> nltk.download(info_or_id='treebank')
>>> from nltk.corpus import treebank
>>> t = treebank.parsed_sents()[0]
>>> print t.pprint()
>>> t.draw()
>>> grammar = nltk.parse_pcfg("""
  S    -> NP VP              [1.0]
  VP   -> TV NP              [0.4]
  VP   -> IV                 [0.3]
  VP   -> DatV NP NP         [0.3]
  TV   -> 'saw'              [1.0]
  IV   -> 'ate'              [1.0]
  DatV -> 'gave'             [1.0]
  NP   -> 'telescopes'       [0.8]
  NP   -> 'Jack'             [0.2]
  """)
>>> viterbi_parser = nltk.ViterbiParser(grammar)
>>> t = viterbi_parser.parse(['Jack', 'saw', 'telescopes'])
>>> print t.pprint_latex_qtree()
>>> prods = []
>>> for t in treebank.parsed_sents():
...     prods += t.productions()
>>> len(prods)
179360
>>> start = nltk.grammar.Nonterminal('S')
>>> grammar = nltk.induce_pcfg(start, prods)