解析NLTK Chart图解

问题描述 投票:0回答:0

我正在尝试使用返回多个解析树的上下文无关语法进行解析。 我可以使用下面的代码将这些解析树一一可视化:

grammar = nltk.CFG.fromstring("""
  S   -> VP NP PUN | NP VP PUN | NP PUN | VP PUN
  NP  -> ADP NP | NOUN | ADJ NP | ADV ADJ NP |ADJ | CONJ NP | NOUN NP | DET NP |ADV
  VP  -> VERB | ADV VP | ADV | VERB VP | VERB NP
  
  ADP -> 'as' | 'in' | 'along' | 'with' | 'of' | 'a'
  NOUN ->  'three' | 'parts' | 'milk' | 'oak' | 'body' | 'center' | 'stage' | 'notes' | 'date' | 'rice' | 'structure' | 'a' | 'hint' | 'blossom' | 'candy' | 'chocolate' | 'lemon' | 'thyme' | 'espresso' | 'cacao' | 'tart' | 'honeysuckle' | 'citrus' | 'apricot' | 'finish' | 'paste' | 'grapefruit' | 'cherry' | 'mouthfeel' | 'spice' | 'cup' | 'vanilla' | 'narcissus' | 'savory-tart' | 'aroma' | 'leads' | 'zest' | 'herb' | 'florals' | 'tones' | 'peppercorn' | 'intimations' | 'nib'
  PUN -> '.' | ',' | ';' | '(' | ')'
  DET -> 'the' | 'all' | 'a'
  ADV -> 'as' | 'deeply' | 'long' | 'all' | 'gently' | 'crisply' | 'richly' | 'delicately' | 'sweetly' 
  VERB -> 'evaluated' | 'take' | 'notes' | 'follow' | 'roasted' | 'dried' | 'fruit' | 'finish' | 'drying' | 'leads' | 'intensify'
  X -> 'a'
  CONJ -> 'and'
  PRT -> 'in'
  ADJ -> 'rich' | 'dark' | 'black' | 'short' | 'small' | 'sweet' | 'white' | 'floral' | 'silky' | 'pink' | 'thyme-like' | 'syrupy' | 'roasted' | 'dried' | 'floral-toned' | 'chocolaty' | 'sweet-toned' | 'cocoa-toned' | 'plush' | 'floral-driven' | 'herb-toned' | 'delicate' | 'resonant' | 'flavor-saturated'
  """)

statement = nltk.word_tokenize("Crisply sweet cocoa-toned Lemon blossom roasted cacao nib date rice candy white peppercorn in aroma and cup.")
statement = [i.lower().strip() for i in statement]

rd_parser = nltk.RecursiveDescentParser(grammar)
for pos, tree in enumerate(rd_parser.parse(statement)):
    tree.draw() 

但问题是它会一张一张地生成树状图,程序会停止,直到我手动停止所有图表。有没有一种方法可以将所有图表生成为单个图像并同时将其显示在我的 jupyter notebook 中?

python nlp treeview nltk text-parsing
© www.soinside.com 2019 - 2024. All rights reserved.