rdparser_app.py 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. # Natural Language Toolkit: Recursive Descent Parser Application
  2. #
  3. # Copyright (C) 2001-2020 NLTK Project
  4. # Author: Edward Loper <edloper@gmail.com>
  5. # URL: <http://nltk.org/>
  6. # For license information, see LICENSE.TXT
  7. """
  8. A graphical tool for exploring the recursive descent parser.
  9. The recursive descent parser maintains a tree, which records the
  10. structure of the portion of the text that has been parsed. It uses
  11. CFG productions to expand the fringe of the tree, and matches its
  12. leaves against the text. Initially, the tree contains the start
  13. symbol ("S"). It is shown in the main canvas, to the right of the
  14. list of available expansions.
  15. The parser builds up a tree structure for the text using three
  16. operations:
  17. - "expand" uses a CFG production to add children to a node on the
  18. fringe of the tree.
  19. - "match" compares a leaf in the tree to a text token.
  20. - "backtrack" returns the tree to its state before the most recent
  21. expand or match operation.
  22. The parser maintains a list of tree locations called a "frontier" to
  23. remember which nodes have not yet been expanded and which leaves have
  24. not yet been matched against the text. The leftmost frontier node is
  25. shown in green, and the other frontier nodes are shown in blue. The
  26. parser always performs expand and match operations on the leftmost
  27. element of the frontier.
  28. You can control the parser's operation by using the "expand," "match,"
  29. and "backtrack" buttons; or you can use the "step" button to let the
  30. parser automatically decide which operation to apply. The parser uses
  31. the following rules to decide which operation to apply:
  32. - If the leftmost frontier element is a token, try matching it.
  33. - If the leftmost frontier element is a node, try expanding it with
  34. the first untried expansion.
  35. - Otherwise, backtrack.
  36. The "expand" button applies the untried expansion whose CFG production
  37. is listed earliest in the grammar. To manually choose which expansion
  38. to apply, click on a CFG production from the list of available
  39. expansions, on the left side of the main window.
  40. The "autostep" button will let the parser continue applying
  41. applications to the tree until it reaches a complete parse. You can
  42. cancel an autostep in progress at any time by clicking on the
  43. "autostep" button again.
  44. Keyboard Shortcuts::
  45. [Space]\t Perform the next expand, match, or backtrack operation
  46. [a]\t Step through operations until the next complete parse
  47. [e]\t Perform an expand operation
  48. [m]\t Perform a match operation
  49. [b]\t Perform a backtrack operation
  50. [Delete]\t Reset the parser
  51. [g]\t Show/hide available expansions list
  52. [h]\t Help
  53. [Ctrl-p]\t Print
  54. [q]\t Quit
  55. """
  56. from tkinter.font import Font
  57. from tkinter import Listbox, IntVar, Button, Frame, Label, Menu, Scrollbar, Tk
  58. from nltk.tree import Tree
  59. from nltk.util import in_idle
  60. from nltk.parse import SteppingRecursiveDescentParser
  61. from nltk.draw.util import TextWidget, ShowText, CanvasFrame, EntryDialog
  62. from nltk.draw import CFGEditor, TreeSegmentWidget, tree_to_treesegment
  63. class RecursiveDescentApp(object):
  64. """
  65. A graphical tool for exploring the recursive descent parser. The tool
  66. displays the parser's tree and the remaining text, and allows the
  67. user to control the parser's operation. In particular, the user
  68. can expand subtrees on the frontier, match tokens on the frontier
  69. against the text, and backtrack. A "step" button simply steps
  70. through the parsing process, performing the operations that
  71. ``RecursiveDescentParser`` would use.
  72. """
  73. def __init__(self, grammar, sent, trace=0):
  74. self._sent = sent
  75. self._parser = SteppingRecursiveDescentParser(grammar, trace)
  76. # Set up the main window.
  77. self._top = Tk()
  78. self._top.title("Recursive Descent Parser Application")
  79. # Set up key bindings.
  80. self._init_bindings()
  81. # Initialize the fonts.
  82. self._init_fonts(self._top)
  83. # Animations. animating_lock is a lock to prevent the demo
  84. # from performing new operations while it's animating.
  85. self._animation_frames = IntVar(self._top)
  86. self._animation_frames.set(5)
  87. self._animating_lock = 0
  88. self._autostep = 0
  89. # The user can hide the grammar.
  90. self._show_grammar = IntVar(self._top)
  91. self._show_grammar.set(1)
  92. # Create the basic frames.
  93. self._init_menubar(self._top)
  94. self._init_buttons(self._top)
  95. self._init_feedback(self._top)
  96. self._init_grammar(self._top)
  97. self._init_canvas(self._top)
  98. # Initialize the parser.
  99. self._parser.initialize(self._sent)
  100. # Resize callback
  101. self._canvas.bind("<Configure>", self._configure)
  102. #########################################
  103. ## Initialization Helpers
  104. #########################################
  105. def _init_fonts(self, root):
  106. # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
  107. self._sysfont = Font(font=Button()["font"])
  108. root.option_add("*Font", self._sysfont)
  109. # TWhat's our font size (default=same as sysfont)
  110. self._size = IntVar(root)
  111. self._size.set(self._sysfont.cget("size"))
  112. self._boldfont = Font(family="helvetica", weight="bold", size=self._size.get())
  113. self._font = Font(family="helvetica", size=self._size.get())
  114. if self._size.get() < 0:
  115. big = self._size.get() - 2
  116. else:
  117. big = self._size.get() + 2
  118. self._bigfont = Font(family="helvetica", weight="bold", size=big)
  119. def _init_grammar(self, parent):
  120. # Grammar view.
  121. self._prodframe = listframe = Frame(parent)
  122. self._prodframe.pack(fill="both", side="left", padx=2)
  123. self._prodlist_label = Label(
  124. self._prodframe, font=self._boldfont, text="Available Expansions"
  125. )
  126. self._prodlist_label.pack()
  127. self._prodlist = Listbox(
  128. self._prodframe,
  129. selectmode="single",
  130. relief="groove",
  131. background="white",
  132. foreground="#909090",
  133. font=self._font,
  134. selectforeground="#004040",
  135. selectbackground="#c0f0c0",
  136. )
  137. self._prodlist.pack(side="right", fill="both", expand=1)
  138. self._productions = list(self._parser.grammar().productions())
  139. for production in self._productions:
  140. self._prodlist.insert("end", (" %s" % production))
  141. self._prodlist.config(height=min(len(self._productions), 25))
  142. # Add a scrollbar if there are more than 25 productions.
  143. if len(self._productions) > 25:
  144. listscroll = Scrollbar(self._prodframe, orient="vertical")
  145. self._prodlist.config(yscrollcommand=listscroll.set)
  146. listscroll.config(command=self._prodlist.yview)
  147. listscroll.pack(side="left", fill="y")
  148. # If they select a production, apply it.
  149. self._prodlist.bind("<<ListboxSelect>>", self._prodlist_select)
  150. def _init_bindings(self):
  151. # Key bindings are a good thing.
  152. self._top.bind("<Control-q>", self.destroy)
  153. self._top.bind("<Control-x>", self.destroy)
  154. self._top.bind("<Escape>", self.destroy)
  155. self._top.bind("e", self.expand)
  156. # self._top.bind('<Alt-e>', self.expand)
  157. # self._top.bind('<Control-e>', self.expand)
  158. self._top.bind("m", self.match)
  159. self._top.bind("<Alt-m>", self.match)
  160. self._top.bind("<Control-m>", self.match)
  161. self._top.bind("b", self.backtrack)
  162. self._top.bind("<Alt-b>", self.backtrack)
  163. self._top.bind("<Control-b>", self.backtrack)
  164. self._top.bind("<Control-z>", self.backtrack)
  165. self._top.bind("<BackSpace>", self.backtrack)
  166. self._top.bind("a", self.autostep)
  167. # self._top.bind('<Control-a>', self.autostep)
  168. self._top.bind("<Control-space>", self.autostep)
  169. self._top.bind("<Control-c>", self.cancel_autostep)
  170. self._top.bind("<space>", self.step)
  171. self._top.bind("<Delete>", self.reset)
  172. self._top.bind("<Control-p>", self.postscript)
  173. # self._top.bind('<h>', self.help)
  174. # self._top.bind('<Alt-h>', self.help)
  175. self._top.bind("<Control-h>", self.help)
  176. self._top.bind("<F1>", self.help)
  177. # self._top.bind('<g>', self.toggle_grammar)
  178. # self._top.bind('<Alt-g>', self.toggle_grammar)
  179. # self._top.bind('<Control-g>', self.toggle_grammar)
  180. self._top.bind("<Control-g>", self.edit_grammar)
  181. self._top.bind("<Control-t>", self.edit_sentence)
  182. def _init_buttons(self, parent):
  183. # Set up the frames.
  184. self._buttonframe = buttonframe = Frame(parent)
  185. buttonframe.pack(fill="none", side="bottom", padx=3, pady=2)
  186. Button(
  187. buttonframe,
  188. text="Step",
  189. background="#90c0d0",
  190. foreground="black",
  191. command=self.step,
  192. ).pack(side="left")
  193. Button(
  194. buttonframe,
  195. text="Autostep",
  196. background="#90c0d0",
  197. foreground="black",
  198. command=self.autostep,
  199. ).pack(side="left")
  200. Button(
  201. buttonframe,
  202. text="Expand",
  203. underline=0,
  204. background="#90f090",
  205. foreground="black",
  206. command=self.expand,
  207. ).pack(side="left")
  208. Button(
  209. buttonframe,
  210. text="Match",
  211. underline=0,
  212. background="#90f090",
  213. foreground="black",
  214. command=self.match,
  215. ).pack(side="left")
  216. Button(
  217. buttonframe,
  218. text="Backtrack",
  219. underline=0,
  220. background="#f0a0a0",
  221. foreground="black",
  222. command=self.backtrack,
  223. ).pack(side="left")
  224. # Replace autostep...
  225. # self._autostep_button = Button(buttonframe, text='Autostep',
  226. # underline=0, command=self.autostep)
  227. # self._autostep_button.pack(side='left')
  228. def _configure(self, event):
  229. self._autostep = 0
  230. (x1, y1, x2, y2) = self._cframe.scrollregion()
  231. y2 = event.height - 6
  232. self._canvas["scrollregion"] = "%d %d %d %d" % (x1, y1, x2, y2)
  233. self._redraw()
  234. def _init_feedback(self, parent):
  235. self._feedbackframe = feedbackframe = Frame(parent)
  236. feedbackframe.pack(fill="x", side="bottom", padx=3, pady=3)
  237. self._lastoper_label = Label(
  238. feedbackframe, text="Last Operation:", font=self._font
  239. )
  240. self._lastoper_label.pack(side="left")
  241. lastoperframe = Frame(feedbackframe, relief="sunken", border=1)
  242. lastoperframe.pack(fill="x", side="right", expand=1, padx=5)
  243. self._lastoper1 = Label(
  244. lastoperframe, foreground="#007070", background="#f0f0f0", font=self._font
  245. )
  246. self._lastoper2 = Label(
  247. lastoperframe,
  248. anchor="w",
  249. width=30,
  250. foreground="#004040",
  251. background="#f0f0f0",
  252. font=self._font,
  253. )
  254. self._lastoper1.pack(side="left")
  255. self._lastoper2.pack(side="left", fill="x", expand=1)
  256. def _init_canvas(self, parent):
  257. self._cframe = CanvasFrame(
  258. parent,
  259. background="white",
  260. # width=525, height=250,
  261. closeenough=10,
  262. border=2,
  263. relief="sunken",
  264. )
  265. self._cframe.pack(expand=1, fill="both", side="top", pady=2)
  266. canvas = self._canvas = self._cframe.canvas()
  267. # Initially, there's no tree or text
  268. self._tree = None
  269. self._textwidgets = []
  270. self._textline = None
  271. def _init_menubar(self, parent):
  272. menubar = Menu(parent)
  273. filemenu = Menu(menubar, tearoff=0)
  274. filemenu.add_command(
  275. label="Reset Parser", underline=0, command=self.reset, accelerator="Del"
  276. )
  277. filemenu.add_command(
  278. label="Print to Postscript",
  279. underline=0,
  280. command=self.postscript,
  281. accelerator="Ctrl-p",
  282. )
  283. filemenu.add_command(
  284. label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
  285. )
  286. menubar.add_cascade(label="File", underline=0, menu=filemenu)
  287. editmenu = Menu(menubar, tearoff=0)
  288. editmenu.add_command(
  289. label="Edit Grammar",
  290. underline=5,
  291. command=self.edit_grammar,
  292. accelerator="Ctrl-g",
  293. )
  294. editmenu.add_command(
  295. label="Edit Text",
  296. underline=5,
  297. command=self.edit_sentence,
  298. accelerator="Ctrl-t",
  299. )
  300. menubar.add_cascade(label="Edit", underline=0, menu=editmenu)
  301. rulemenu = Menu(menubar, tearoff=0)
  302. rulemenu.add_command(
  303. label="Step", underline=1, command=self.step, accelerator="Space"
  304. )
  305. rulemenu.add_separator()
  306. rulemenu.add_command(
  307. label="Match", underline=0, command=self.match, accelerator="Ctrl-m"
  308. )
  309. rulemenu.add_command(
  310. label="Expand", underline=0, command=self.expand, accelerator="Ctrl-e"
  311. )
  312. rulemenu.add_separator()
  313. rulemenu.add_command(
  314. label="Backtrack", underline=0, command=self.backtrack, accelerator="Ctrl-b"
  315. )
  316. menubar.add_cascade(label="Apply", underline=0, menu=rulemenu)
  317. viewmenu = Menu(menubar, tearoff=0)
  318. viewmenu.add_checkbutton(
  319. label="Show Grammar",
  320. underline=0,
  321. variable=self._show_grammar,
  322. command=self._toggle_grammar,
  323. )
  324. viewmenu.add_separator()
  325. viewmenu.add_radiobutton(
  326. label="Tiny",
  327. variable=self._size,
  328. underline=0,
  329. value=10,
  330. command=self.resize,
  331. )
  332. viewmenu.add_radiobutton(
  333. label="Small",
  334. variable=self._size,
  335. underline=0,
  336. value=12,
  337. command=self.resize,
  338. )
  339. viewmenu.add_radiobutton(
  340. label="Medium",
  341. variable=self._size,
  342. underline=0,
  343. value=14,
  344. command=self.resize,
  345. )
  346. viewmenu.add_radiobutton(
  347. label="Large",
  348. variable=self._size,
  349. underline=0,
  350. value=18,
  351. command=self.resize,
  352. )
  353. viewmenu.add_radiobutton(
  354. label="Huge",
  355. variable=self._size,
  356. underline=0,
  357. value=24,
  358. command=self.resize,
  359. )
  360. menubar.add_cascade(label="View", underline=0, menu=viewmenu)
  361. animatemenu = Menu(menubar, tearoff=0)
  362. animatemenu.add_radiobutton(
  363. label="No Animation", underline=0, variable=self._animation_frames, value=0
  364. )
  365. animatemenu.add_radiobutton(
  366. label="Slow Animation",
  367. underline=0,
  368. variable=self._animation_frames,
  369. value=10,
  370. accelerator="-",
  371. )
  372. animatemenu.add_radiobutton(
  373. label="Normal Animation",
  374. underline=0,
  375. variable=self._animation_frames,
  376. value=5,
  377. accelerator="=",
  378. )
  379. animatemenu.add_radiobutton(
  380. label="Fast Animation",
  381. underline=0,
  382. variable=self._animation_frames,
  383. value=2,
  384. accelerator="+",
  385. )
  386. menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)
  387. helpmenu = Menu(menubar, tearoff=0)
  388. helpmenu.add_command(label="About", underline=0, command=self.about)
  389. helpmenu.add_command(
  390. label="Instructions", underline=0, command=self.help, accelerator="F1"
  391. )
  392. menubar.add_cascade(label="Help", underline=0, menu=helpmenu)
  393. parent.config(menu=menubar)
  394. #########################################
  395. ## Helper
  396. #########################################
  397. def _get(self, widget, treeloc):
  398. for i in treeloc:
  399. widget = widget.subtrees()[i]
  400. if isinstance(widget, TreeSegmentWidget):
  401. widget = widget.label()
  402. return widget
  403. #########################################
  404. ## Main draw procedure
  405. #########################################
  406. def _redraw(self):
  407. canvas = self._canvas
  408. # Delete the old tree, widgets, etc.
  409. if self._tree is not None:
  410. self._cframe.destroy_widget(self._tree)
  411. for twidget in self._textwidgets:
  412. self._cframe.destroy_widget(twidget)
  413. if self._textline is not None:
  414. self._canvas.delete(self._textline)
  415. # Draw the tree.
  416. helv = ("helvetica", -self._size.get())
  417. bold = ("helvetica", -self._size.get(), "bold")
  418. attribs = {
  419. "tree_color": "#000000",
  420. "tree_width": 2,
  421. "node_font": bold,
  422. "leaf_font": helv,
  423. }
  424. tree = self._parser.tree()
  425. self._tree = tree_to_treesegment(canvas, tree, **attribs)
  426. self._cframe.add_widget(self._tree, 30, 5)
  427. # Draw the text.
  428. helv = ("helvetica", -self._size.get())
  429. bottom = y = self._cframe.scrollregion()[3]
  430. self._textwidgets = [
  431. TextWidget(canvas, word, font=self._font) for word in self._sent
  432. ]
  433. for twidget in self._textwidgets:
  434. self._cframe.add_widget(twidget, 0, 0)
  435. twidget.move(0, bottom - twidget.bbox()[3] - 5)
  436. y = min(y, twidget.bbox()[1])
  437. # Draw a line over the text, to separate it from the tree.
  438. self._textline = canvas.create_line(-5000, y - 5, 5000, y - 5, dash=".")
  439. # Highlight appropriate nodes.
  440. self._highlight_nodes()
  441. self._highlight_prodlist()
  442. # Make sure the text lines up.
  443. self._position_text()
  444. def _redraw_quick(self):
  445. # This should be more-or-less sufficient after an animation.
  446. self._highlight_nodes()
  447. self._highlight_prodlist()
  448. self._position_text()
  449. def _highlight_nodes(self):
  450. # Highlight the list of nodes to be checked.
  451. bold = ("helvetica", -self._size.get(), "bold")
  452. for treeloc in self._parser.frontier()[:1]:
  453. self._get(self._tree, treeloc)["color"] = "#20a050"
  454. self._get(self._tree, treeloc)["font"] = bold
  455. for treeloc in self._parser.frontier()[1:]:
  456. self._get(self._tree, treeloc)["color"] = "#008080"
  457. def _highlight_prodlist(self):
  458. # Highlight the productions that can be expanded.
  459. # Boy, too bad tkinter doesn't implement Listbox.itemconfig;
  460. # that would be pretty useful here.
  461. self._prodlist.delete(0, "end")
  462. expandable = self._parser.expandable_productions()
  463. untried = self._parser.untried_expandable_productions()
  464. productions = self._productions
  465. for index in range(len(productions)):
  466. if productions[index] in expandable:
  467. if productions[index] in untried:
  468. self._prodlist.insert(index, " %s" % productions[index])
  469. else:
  470. self._prodlist.insert(index, " %s (TRIED)" % productions[index])
  471. self._prodlist.selection_set(index)
  472. else:
  473. self._prodlist.insert(index, " %s" % productions[index])
  474. def _position_text(self):
  475. # Line up the text widgets that are matched against the tree
  476. numwords = len(self._sent)
  477. num_matched = numwords - len(self._parser.remaining_text())
  478. leaves = self._tree_leaves()[:num_matched]
  479. xmax = self._tree.bbox()[0]
  480. for i in range(0, len(leaves)):
  481. widget = self._textwidgets[i]
  482. leaf = leaves[i]
  483. widget["color"] = "#006040"
  484. leaf["color"] = "#006040"
  485. widget.move(leaf.bbox()[0] - widget.bbox()[0], 0)
  486. xmax = widget.bbox()[2] + 10
  487. # Line up the text widgets that are not matched against the tree.
  488. for i in range(len(leaves), numwords):
  489. widget = self._textwidgets[i]
  490. widget["color"] = "#a0a0a0"
  491. widget.move(xmax - widget.bbox()[0], 0)
  492. xmax = widget.bbox()[2] + 10
  493. # If we have a complete parse, make everything green :)
  494. if self._parser.currently_complete():
  495. for twidget in self._textwidgets:
  496. twidget["color"] = "#00a000"
  497. # Move the matched leaves down to the text.
  498. for i in range(0, len(leaves)):
  499. widget = self._textwidgets[i]
  500. leaf = leaves[i]
  501. dy = widget.bbox()[1] - leaf.bbox()[3] - 10.0
  502. dy = max(dy, leaf.parent().label().bbox()[3] - leaf.bbox()[3] + 10)
  503. leaf.move(0, dy)
  504. def _tree_leaves(self, tree=None):
  505. if tree is None:
  506. tree = self._tree
  507. if isinstance(tree, TreeSegmentWidget):
  508. leaves = []
  509. for child in tree.subtrees():
  510. leaves += self._tree_leaves(child)
  511. return leaves
  512. else:
  513. return [tree]
  514. #########################################
  515. ## Button Callbacks
  516. #########################################
  517. def destroy(self, *e):
  518. self._autostep = 0
  519. if self._top is None:
  520. return
  521. self._top.destroy()
  522. self._top = None
  523. def reset(self, *e):
  524. self._autostep = 0
  525. self._parser.initialize(self._sent)
  526. self._lastoper1["text"] = "Reset Application"
  527. self._lastoper2["text"] = ""
  528. self._redraw()
  529. def autostep(self, *e):
  530. if self._animation_frames.get() == 0:
  531. self._animation_frames.set(2)
  532. if self._autostep:
  533. self._autostep = 0
  534. else:
  535. self._autostep = 1
  536. self._step()
  537. def cancel_autostep(self, *e):
  538. # self._autostep_button['text'] = 'Autostep'
  539. self._autostep = 0
  540. # Make sure to stop auto-stepping if we get any user input.
  541. def step(self, *e):
  542. self._autostep = 0
  543. self._step()
  544. def match(self, *e):
  545. self._autostep = 0
  546. self._match()
  547. def expand(self, *e):
  548. self._autostep = 0
  549. self._expand()
  550. def backtrack(self, *e):
  551. self._autostep = 0
  552. self._backtrack()
  553. def _step(self):
  554. if self._animating_lock:
  555. return
  556. # Try expanding, matching, and backtracking (in that order)
  557. if self._expand():
  558. pass
  559. elif self._parser.untried_match() and self._match():
  560. pass
  561. elif self._backtrack():
  562. pass
  563. else:
  564. self._lastoper1["text"] = "Finished"
  565. self._lastoper2["text"] = ""
  566. self._autostep = 0
  567. # Check if we just completed a parse.
  568. if self._parser.currently_complete():
  569. self._autostep = 0
  570. self._lastoper2["text"] += " [COMPLETE PARSE]"
  571. def _expand(self, *e):
  572. if self._animating_lock:
  573. return
  574. old_frontier = self._parser.frontier()
  575. rv = self._parser.expand()
  576. if rv is not None:
  577. self._lastoper1["text"] = "Expand:"
  578. self._lastoper2["text"] = rv
  579. self._prodlist.selection_clear(0, "end")
  580. index = self._productions.index(rv)
  581. self._prodlist.selection_set(index)
  582. self._animate_expand(old_frontier[0])
  583. return True
  584. else:
  585. self._lastoper1["text"] = "Expand:"
  586. self._lastoper2["text"] = "(all expansions tried)"
  587. return False
  588. def _match(self, *e):
  589. if self._animating_lock:
  590. return
  591. old_frontier = self._parser.frontier()
  592. rv = self._parser.match()
  593. if rv is not None:
  594. self._lastoper1["text"] = "Match:"
  595. self._lastoper2["text"] = rv
  596. self._animate_match(old_frontier[0])
  597. return True
  598. else:
  599. self._lastoper1["text"] = "Match:"
  600. self._lastoper2["text"] = "(failed)"
  601. return False
  602. def _backtrack(self, *e):
  603. if self._animating_lock:
  604. return
  605. if self._parser.backtrack():
  606. elt = self._parser.tree()
  607. for i in self._parser.frontier()[0]:
  608. elt = elt[i]
  609. self._lastoper1["text"] = "Backtrack"
  610. self._lastoper2["text"] = ""
  611. if isinstance(elt, Tree):
  612. self._animate_backtrack(self._parser.frontier()[0])
  613. else:
  614. self._animate_match_backtrack(self._parser.frontier()[0])
  615. return True
  616. else:
  617. self._autostep = 0
  618. self._lastoper1["text"] = "Finished"
  619. self._lastoper2["text"] = ""
  620. return False
  621. def about(self, *e):
  622. ABOUT = (
  623. "NLTK Recursive Descent Parser Application\n" + "Written by Edward Loper"
  624. )
  625. TITLE = "About: Recursive Descent Parser Application"
  626. try:
  627. from tkinter.messagebox import Message
  628. Message(message=ABOUT, title=TITLE).show()
  629. except:
  630. ShowText(self._top, TITLE, ABOUT)
  631. def help(self, *e):
  632. self._autostep = 0
  633. # The default font's not very legible; try using 'fixed' instead.
  634. try:
  635. ShowText(
  636. self._top,
  637. "Help: Recursive Descent Parser Application",
  638. (__doc__ or "").strip(),
  639. width=75,
  640. font="fixed",
  641. )
  642. except:
  643. ShowText(
  644. self._top,
  645. "Help: Recursive Descent Parser Application",
  646. (__doc__ or "").strip(),
  647. width=75,
  648. )
  649. def postscript(self, *e):
  650. self._autostep = 0
  651. self._cframe.print_to_file()
  652. def mainloop(self, *args, **kwargs):
  653. """
  654. Enter the Tkinter mainloop. This function must be called if
  655. this demo is created from a non-interactive program (e.g.
  656. from a secript); otherwise, the demo will close as soon as
  657. the script completes.
  658. """
  659. if in_idle():
  660. return
  661. self._top.mainloop(*args, **kwargs)
  662. def resize(self, size=None):
  663. if size is not None:
  664. self._size.set(size)
  665. size = self._size.get()
  666. self._font.configure(size=-(abs(size)))
  667. self._boldfont.configure(size=-(abs(size)))
  668. self._sysfont.configure(size=-(abs(size)))
  669. self._bigfont.configure(size=-(abs(size + 2)))
  670. self._redraw()
  671. #########################################
  672. ## Expand Production Selection
  673. #########################################
  674. def _toggle_grammar(self, *e):
  675. if self._show_grammar.get():
  676. self._prodframe.pack(
  677. fill="both", side="left", padx=2, after=self._feedbackframe
  678. )
  679. self._lastoper1["text"] = "Show Grammar"
  680. else:
  681. self._prodframe.pack_forget()
  682. self._lastoper1["text"] = "Hide Grammar"
  683. self._lastoper2["text"] = ""
  684. # def toggle_grammar(self, *e):
  685. # self._show_grammar = not self._show_grammar
  686. # if self._show_grammar:
  687. # self._prodframe.pack(fill='both', expand='y', side='left',
  688. # after=self._feedbackframe)
  689. # self._lastoper1['text'] = 'Show Grammar'
  690. # else:
  691. # self._prodframe.pack_forget()
  692. # self._lastoper1['text'] = 'Hide Grammar'
  693. # self._lastoper2['text'] = ''
  694. def _prodlist_select(self, event):
  695. selection = self._prodlist.curselection()
  696. if len(selection) != 1:
  697. return
  698. index = int(selection[0])
  699. old_frontier = self._parser.frontier()
  700. production = self._parser.expand(self._productions[index])
  701. if production:
  702. self._lastoper1["text"] = "Expand:"
  703. self._lastoper2["text"] = production
  704. self._prodlist.selection_clear(0, "end")
  705. self._prodlist.selection_set(index)
  706. self._animate_expand(old_frontier[0])
  707. else:
  708. # Reset the production selections.
  709. self._prodlist.selection_clear(0, "end")
  710. for prod in self._parser.expandable_productions():
  711. index = self._productions.index(prod)
  712. self._prodlist.selection_set(index)
  713. #########################################
  714. ## Animation
  715. #########################################
  716. def _animate_expand(self, treeloc):
  717. oldwidget = self._get(self._tree, treeloc)
  718. oldtree = oldwidget.parent()
  719. top = not isinstance(oldtree.parent(), TreeSegmentWidget)
  720. tree = self._parser.tree()
  721. for i in treeloc:
  722. tree = tree[i]
  723. widget = tree_to_treesegment(
  724. self._canvas,
  725. tree,
  726. node_font=self._boldfont,
  727. leaf_color="white",
  728. tree_width=2,
  729. tree_color="white",
  730. node_color="white",
  731. leaf_font=self._font,
  732. )
  733. widget.label()["color"] = "#20a050"
  734. (oldx, oldy) = oldtree.label().bbox()[:2]
  735. (newx, newy) = widget.label().bbox()[:2]
  736. widget.move(oldx - newx, oldy - newy)
  737. if top:
  738. self._cframe.add_widget(widget, 0, 5)
  739. widget.move(30 - widget.label().bbox()[0], 0)
  740. self._tree = widget
  741. else:
  742. oldtree.parent().replace_child(oldtree, widget)
  743. # Move the children over so they don't overlap.
  744. # Line the children up in a strange way.
  745. if widget.subtrees():
  746. dx = (
  747. oldx
  748. + widget.label().width() / 2
  749. - widget.subtrees()[0].bbox()[0] / 2
  750. - widget.subtrees()[0].bbox()[2] / 2
  751. )
  752. for subtree in widget.subtrees():
  753. subtree.move(dx, 0)
  754. self._makeroom(widget)
  755. if top:
  756. self._cframe.destroy_widget(oldtree)
  757. else:
  758. oldtree.destroy()
  759. colors = [
  760. "gray%d" % (10 * int(10 * x / self._animation_frames.get()))
  761. for x in range(self._animation_frames.get(), 0, -1)
  762. ]
  763. # Move the text string down, if necessary.
  764. dy = widget.bbox()[3] + 30 - self._canvas.coords(self._textline)[1]
  765. if dy > 0:
  766. for twidget in self._textwidgets:
  767. twidget.move(0, dy)
  768. self._canvas.move(self._textline, 0, dy)
  769. self._animate_expand_frame(widget, colors)
  770. def _makeroom(self, treeseg):
  771. """
  772. Make sure that no sibling tree bbox's overlap.
  773. """
  774. parent = treeseg.parent()
  775. if not isinstance(parent, TreeSegmentWidget):
  776. return
  777. index = parent.subtrees().index(treeseg)
  778. # Handle siblings to the right
  779. rsiblings = parent.subtrees()[index + 1 :]
  780. if rsiblings:
  781. dx = treeseg.bbox()[2] - rsiblings[0].bbox()[0] + 10
  782. for sibling in rsiblings:
  783. sibling.move(dx, 0)
  784. # Handle siblings to the left
  785. if index > 0:
  786. lsibling = parent.subtrees()[index - 1]
  787. dx = max(0, lsibling.bbox()[2] - treeseg.bbox()[0] + 10)
  788. treeseg.move(dx, 0)
  789. # Keep working up the tree.
  790. self._makeroom(parent)
  791. def _animate_expand_frame(self, widget, colors):
  792. if len(colors) > 0:
  793. self._animating_lock = 1
  794. widget["color"] = colors[0]
  795. for subtree in widget.subtrees():
  796. if isinstance(subtree, TreeSegmentWidget):
  797. subtree.label()["color"] = colors[0]
  798. else:
  799. subtree["color"] = colors[0]
  800. self._top.after(50, self._animate_expand_frame, widget, colors[1:])
  801. else:
  802. widget["color"] = "black"
  803. for subtree in widget.subtrees():
  804. if isinstance(subtree, TreeSegmentWidget):
  805. subtree.label()["color"] = "black"
  806. else:
  807. subtree["color"] = "black"
  808. self._redraw_quick()
  809. widget.label()["color"] = "black"
  810. self._animating_lock = 0
  811. if self._autostep:
  812. self._step()
  813. def _animate_backtrack(self, treeloc):
  814. # Flash red first, if we're animating.
  815. if self._animation_frames.get() == 0:
  816. colors = []
  817. else:
  818. colors = ["#a00000", "#000000", "#a00000"]
  819. colors += [
  820. "gray%d" % (10 * int(10 * x / (self._animation_frames.get())))
  821. for x in range(1, self._animation_frames.get() + 1)
  822. ]
  823. widgets = [self._get(self._tree, treeloc).parent()]
  824. for subtree in widgets[0].subtrees():
  825. if isinstance(subtree, TreeSegmentWidget):
  826. widgets.append(subtree.label())
  827. else:
  828. widgets.append(subtree)
  829. self._animate_backtrack_frame(widgets, colors)
  830. def _animate_backtrack_frame(self, widgets, colors):
  831. if len(colors) > 0:
  832. self._animating_lock = 1
  833. for widget in widgets:
  834. widget["color"] = colors[0]
  835. self._top.after(50, self._animate_backtrack_frame, widgets, colors[1:])
  836. else:
  837. for widget in widgets[0].subtrees():
  838. widgets[0].remove_child(widget)
  839. widget.destroy()
  840. self._redraw_quick()
  841. self._animating_lock = 0
  842. if self._autostep:
  843. self._step()
  844. def _animate_match_backtrack(self, treeloc):
  845. widget = self._get(self._tree, treeloc)
  846. node = widget.parent().label()
  847. dy = (node.bbox()[3] - widget.bbox()[1] + 14) / max(
  848. 1, self._animation_frames.get()
  849. )
  850. self._animate_match_backtrack_frame(self._animation_frames.get(), widget, dy)
  851. def _animate_match(self, treeloc):
  852. widget = self._get(self._tree, treeloc)
  853. dy = (self._textwidgets[0].bbox()[1] - widget.bbox()[3] - 10.0) / max(
  854. 1, self._animation_frames.get()
  855. )
  856. self._animate_match_frame(self._animation_frames.get(), widget, dy)
  857. def _animate_match_frame(self, frame, widget, dy):
  858. if frame > 0:
  859. self._animating_lock = 1
  860. widget.move(0, dy)
  861. self._top.after(10, self._animate_match_frame, frame - 1, widget, dy)
  862. else:
  863. widget["color"] = "#006040"
  864. self._redraw_quick()
  865. self._animating_lock = 0
  866. if self._autostep:
  867. self._step()
  868. def _animate_match_backtrack_frame(self, frame, widget, dy):
  869. if frame > 0:
  870. self._animating_lock = 1
  871. widget.move(0, dy)
  872. self._top.after(
  873. 10, self._animate_match_backtrack_frame, frame - 1, widget, dy
  874. )
  875. else:
  876. widget.parent().remove_child(widget)
  877. widget.destroy()
  878. self._animating_lock = 0
  879. if self._autostep:
  880. self._step()
  881. def edit_grammar(self, *e):
  882. CFGEditor(self._top, self._parser.grammar(), self.set_grammar)
  883. def set_grammar(self, grammar):
  884. self._parser.set_grammar(grammar)
  885. self._productions = list(grammar.productions())
  886. self._prodlist.delete(0, "end")
  887. for production in self._productions:
  888. self._prodlist.insert("end", (" %s" % production))
  889. def edit_sentence(self, *e):
  890. sentence = " ".join(self._sent)
  891. title = "Edit Text"
  892. instr = "Enter a new sentence to parse."
  893. EntryDialog(self._top, sentence, instr, self.set_sentence, title)
  894. def set_sentence(self, sentence):
  895. self._sent = sentence.split() # [XX] use tagged?
  896. self.reset()
  897. def app():
  898. """
  899. Create a recursive descent parser demo, using a simple grammar and
  900. text.
  901. """
  902. from nltk.grammar import CFG
  903. grammar = CFG.fromstring(
  904. """
  905. # Grammatical productions.
  906. S -> NP VP
  907. NP -> Det N PP | Det N
  908. VP -> V NP PP | V NP | V
  909. PP -> P NP
  910. # Lexical productions.
  911. NP -> 'I'
  912. Det -> 'the' | 'a'
  913. N -> 'man' | 'park' | 'dog' | 'telescope'
  914. V -> 'ate' | 'saw'
  915. P -> 'in' | 'under' | 'with'
  916. """
  917. )
  918. sent = "the dog saw a man in the park".split()
  919. RecursiveDescentApp(grammar, sent).mainloop()
  920. if __name__ == "__main__":
  921. app()
  922. __all__ = ["app"]