chartparser_app.py 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575
  1. # Natural Language Toolkit: Chart Parser Application
  2. #
  3. # Copyright (C) 2001-2020 NLTK Project
  4. # Author: Edward Loper <edloper@gmail.com>
  5. # Jean Mark Gawron <gawron@mail.sdsu.edu>
  6. # Steven Bird <stevenbird1@gmail.com>
  7. # URL: <http://nltk.org/>
  8. # For license information, see LICENSE.TXT
  9. """
  10. A graphical tool for exploring chart parsing.
  11. Chart parsing is a flexible parsing algorithm that uses a data
  12. structure called a "chart" to record hypotheses about syntactic
  13. constituents. Each hypothesis is represented by a single "edge" on
  14. the chart. A set of "chart rules" determine when new edges can be
  15. added to the chart. This set of rules controls the overall behavior
  16. of the parser (e.g. whether it parses top-down or bottom-up).
  17. The chart parsing tool demonstrates the process of parsing a single
  18. sentence, with a given grammar and lexicon. Its display is divided
  19. into three sections: the bottom section displays the chart; the middle
  20. section displays the sentence; and the top section displays the
  21. partial syntax tree corresponding to the selected edge. Buttons along
  22. the bottom of the window are used to control the execution of the
  23. algorithm.
  24. The chart parsing tool allows for flexible control of the parsing
  25. algorithm. At each step of the algorithm, you can select which rule
  26. or strategy you wish to apply. This allows you to experiment with
  27. mixing different strategies (e.g. top-down and bottom-up). You can
  28. exercise fine-grained control over the algorithm by selecting which
  29. edge you wish to apply a rule to.
  30. """
  31. # At some point, we should rewrite this tool to use the new canvas
  32. # widget system.
  33. import pickle
  34. import os.path
  35. from tkinter import (
  36. Button,
  37. Canvas,
  38. Checkbutton,
  39. Frame,
  40. IntVar,
  41. Label,
  42. Menu,
  43. Scrollbar,
  44. Tk,
  45. Toplevel,
  46. )
  47. from tkinter.font import Font
  48. from tkinter.messagebox import showerror, showinfo
  49. from tkinter.filedialog import asksaveasfilename, askopenfilename
  50. from nltk.parse.chart import (
  51. BottomUpPredictCombineRule,
  52. BottomUpPredictRule,
  53. Chart,
  54. LeafEdge,
  55. LeafInitRule,
  56. SingleEdgeFundamentalRule,
  57. SteppingChartParser,
  58. TopDownInitRule,
  59. TopDownPredictRule,
  60. TreeEdge,
  61. )
  62. from nltk.tree import Tree
  63. from nltk.grammar import Nonterminal, CFG
  64. from nltk.util import in_idle
  65. from nltk.draw.util import (
  66. CanvasFrame,
  67. ColorizedList,
  68. EntryDialog,
  69. MutableOptionMenu,
  70. ShowText,
  71. SymbolWidget,
  72. )
  73. from nltk.draw import CFGEditor, tree_to_treesegment, TreeSegmentWidget
  74. # Known bug: ChartView doesn't handle edges generated by epsilon
  75. # productions (e.g., [Production: PP -> ]) very well.
  76. #######################################################################
  77. # Edge List
  78. #######################################################################
  79. class EdgeList(ColorizedList):
  80. ARROW = SymbolWidget.SYMBOLS["rightarrow"]
  81. def _init_colortags(self, textwidget, options):
  82. textwidget.tag_config("terminal", foreground="#006000")
  83. textwidget.tag_config("arrow", font="symbol", underline="0")
  84. textwidget.tag_config("dot", foreground="#000000")
  85. textwidget.tag_config(
  86. "nonterminal", foreground="blue", font=("helvetica", -12, "bold")
  87. )
  88. def _item_repr(self, item):
  89. contents = []
  90. contents.append(("%s\t" % item.lhs(), "nonterminal"))
  91. contents.append((self.ARROW, "arrow"))
  92. for i, elt in enumerate(item.rhs()):
  93. if i == item.dot():
  94. contents.append((" *", "dot"))
  95. if isinstance(elt, Nonterminal):
  96. contents.append((" %s" % elt.symbol(), "nonterminal"))
  97. else:
  98. contents.append((" %r" % elt, "terminal"))
  99. if item.is_complete():
  100. contents.append((" *", "dot"))
  101. return contents
  102. #######################################################################
  103. # Chart Matrix View
  104. #######################################################################
  105. class ChartMatrixView(object):
  106. """
  107. A view of a chart that displays the contents of the corresponding matrix.
  108. """
  109. def __init__(
  110. self, parent, chart, toplevel=True, title="Chart Matrix", show_numedges=False
  111. ):
  112. self._chart = chart
  113. self._cells = []
  114. self._marks = []
  115. self._selected_cell = None
  116. if toplevel:
  117. self._root = Toplevel(parent)
  118. self._root.title(title)
  119. self._root.bind("<Control-q>", self.destroy)
  120. self._init_quit(self._root)
  121. else:
  122. self._root = Frame(parent)
  123. self._init_matrix(self._root)
  124. self._init_list(self._root)
  125. if show_numedges:
  126. self._init_numedges(self._root)
  127. else:
  128. self._numedges_label = None
  129. self._callbacks = {}
  130. self._num_edges = 0
  131. self.draw()
  132. def _init_quit(self, root):
  133. quit = Button(root, text="Quit", command=self.destroy)
  134. quit.pack(side="bottom", expand=0, fill="none")
  135. def _init_matrix(self, root):
  136. cframe = Frame(root, border=2, relief="sunken")
  137. cframe.pack(expand=0, fill="none", padx=1, pady=3, side="top")
  138. self._canvas = Canvas(cframe, width=200, height=200, background="white")
  139. self._canvas.pack(expand=0, fill="none")
  140. def _init_numedges(self, root):
  141. self._numedges_label = Label(root, text="0 edges")
  142. self._numedges_label.pack(expand=0, fill="none", side="top")
  143. def _init_list(self, root):
  144. self._list = EdgeList(root, [], width=20, height=5)
  145. self._list.pack(side="top", expand=1, fill="both", pady=3)
  146. def cb(edge, self=self):
  147. self._fire_callbacks("select", edge)
  148. self._list.add_callback("select", cb)
  149. self._list.focus()
  150. def destroy(self, *e):
  151. if self._root is None:
  152. return
  153. try:
  154. self._root.destroy()
  155. except:
  156. pass
  157. self._root = None
  158. def set_chart(self, chart):
  159. if chart is not self._chart:
  160. self._chart = chart
  161. self._num_edges = 0
  162. self.draw()
  163. def update(self):
  164. if self._root is None:
  165. return
  166. # Count the edges in each cell
  167. N = len(self._cells)
  168. cell_edges = [[0 for i in range(N)] for j in range(N)]
  169. for edge in self._chart:
  170. cell_edges[edge.start()][edge.end()] += 1
  171. # Color the cells correspondingly.
  172. for i in range(N):
  173. for j in range(i, N):
  174. if cell_edges[i][j] == 0:
  175. color = "gray20"
  176. else:
  177. color = "#00%02x%02x" % (
  178. min(255, 50 + 128 * cell_edges[i][j] / 10),
  179. max(0, 128 - 128 * cell_edges[i][j] / 10),
  180. )
  181. cell_tag = self._cells[i][j]
  182. self._canvas.itemconfig(cell_tag, fill=color)
  183. if (i, j) == self._selected_cell:
  184. self._canvas.itemconfig(cell_tag, outline="#00ffff", width=3)
  185. self._canvas.tag_raise(cell_tag)
  186. else:
  187. self._canvas.itemconfig(cell_tag, outline="black", width=1)
  188. # Update the edge list.
  189. edges = list(self._chart.select(span=self._selected_cell))
  190. self._list.set(edges)
  191. # Update our edge count.
  192. self._num_edges = self._chart.num_edges()
  193. if self._numedges_label is not None:
  194. self._numedges_label["text"] = "%d edges" % self._num_edges
  195. def activate(self):
  196. self._canvas.itemconfig("inactivebox", state="hidden")
  197. self.update()
  198. def inactivate(self):
  199. self._canvas.itemconfig("inactivebox", state="normal")
  200. self.update()
  201. def add_callback(self, event, func):
  202. self._callbacks.setdefault(event, {})[func] = 1
  203. def remove_callback(self, event, func=None):
  204. if func is None:
  205. del self._callbacks[event]
  206. else:
  207. try:
  208. del self._callbacks[event][func]
  209. except:
  210. pass
  211. def _fire_callbacks(self, event, *args):
  212. if event not in self._callbacks:
  213. return
  214. for cb_func in list(self._callbacks[event].keys()):
  215. cb_func(*args)
  216. def select_cell(self, i, j):
  217. if self._root is None:
  218. return
  219. # If the cell is already selected (and the chart contents
  220. # haven't changed), then do nothing.
  221. if (i, j) == self._selected_cell and self._chart.num_edges() == self._num_edges:
  222. return
  223. self._selected_cell = (i, j)
  224. self.update()
  225. # Fire the callback.
  226. self._fire_callbacks("select_cell", i, j)
  227. def deselect_cell(self):
  228. if self._root is None:
  229. return
  230. self._selected_cell = None
  231. self._list.set([])
  232. self.update()
  233. def _click_cell(self, i, j):
  234. if self._selected_cell == (i, j):
  235. self.deselect_cell()
  236. else:
  237. self.select_cell(i, j)
  238. def view_edge(self, edge):
  239. self.select_cell(*edge.span())
  240. self._list.view(edge)
  241. def mark_edge(self, edge):
  242. if self._root is None:
  243. return
  244. self.select_cell(*edge.span())
  245. self._list.mark(edge)
  246. def unmark_edge(self, edge=None):
  247. if self._root is None:
  248. return
  249. self._list.unmark(edge)
  250. def markonly_edge(self, edge):
  251. if self._root is None:
  252. return
  253. self.select_cell(*edge.span())
  254. self._list.markonly(edge)
  255. def draw(self):
  256. if self._root is None:
  257. return
  258. LEFT_MARGIN = BOT_MARGIN = 15
  259. TOP_MARGIN = 5
  260. c = self._canvas
  261. c.delete("all")
  262. N = self._chart.num_leaves() + 1
  263. dx = (int(c["width"]) - LEFT_MARGIN) / N
  264. dy = (int(c["height"]) - TOP_MARGIN - BOT_MARGIN) / N
  265. c.delete("all")
  266. # Labels and dotted lines
  267. for i in range(N):
  268. c.create_text(
  269. LEFT_MARGIN - 2, i * dy + dy / 2 + TOP_MARGIN, text=repr(i), anchor="e"
  270. )
  271. c.create_text(
  272. i * dx + dx / 2 + LEFT_MARGIN,
  273. N * dy + TOP_MARGIN + 1,
  274. text=repr(i),
  275. anchor="n",
  276. )
  277. c.create_line(
  278. LEFT_MARGIN,
  279. dy * (i + 1) + TOP_MARGIN,
  280. dx * N + LEFT_MARGIN,
  281. dy * (i + 1) + TOP_MARGIN,
  282. dash=".",
  283. )
  284. c.create_line(
  285. dx * i + LEFT_MARGIN,
  286. TOP_MARGIN,
  287. dx * i + LEFT_MARGIN,
  288. dy * N + TOP_MARGIN,
  289. dash=".",
  290. )
  291. # A box around the whole thing
  292. c.create_rectangle(
  293. LEFT_MARGIN, TOP_MARGIN, LEFT_MARGIN + dx * N, dy * N + TOP_MARGIN, width=2
  294. )
  295. # Cells
  296. self._cells = [[None for i in range(N)] for j in range(N)]
  297. for i in range(N):
  298. for j in range(i, N):
  299. t = c.create_rectangle(
  300. j * dx + LEFT_MARGIN,
  301. i * dy + TOP_MARGIN,
  302. (j + 1) * dx + LEFT_MARGIN,
  303. (i + 1) * dy + TOP_MARGIN,
  304. fill="gray20",
  305. )
  306. self._cells[i][j] = t
  307. def cb(event, self=self, i=i, j=j):
  308. self._click_cell(i, j)
  309. c.tag_bind(t, "<Button-1>", cb)
  310. # Inactive box
  311. xmax, ymax = int(c["width"]), int(c["height"])
  312. t = c.create_rectangle(
  313. -100,
  314. -100,
  315. xmax + 100,
  316. ymax + 100,
  317. fill="gray50",
  318. state="hidden",
  319. tag="inactivebox",
  320. )
  321. c.tag_lower(t)
  322. # Update the cells.
  323. self.update()
  324. def pack(self, *args, **kwargs):
  325. self._root.pack(*args, **kwargs)
  326. #######################################################################
  327. # Chart Results View
  328. #######################################################################
  329. class ChartResultsView(object):
  330. def __init__(self, parent, chart, grammar, toplevel=True):
  331. self._chart = chart
  332. self._grammar = grammar
  333. self._trees = []
  334. self._y = 10
  335. self._treewidgets = []
  336. self._selection = None
  337. self._selectbox = None
  338. if toplevel:
  339. self._root = Toplevel(parent)
  340. self._root.title("Chart Parser Application: Results")
  341. self._root.bind("<Control-q>", self.destroy)
  342. else:
  343. self._root = Frame(parent)
  344. # Buttons
  345. if toplevel:
  346. buttons = Frame(self._root)
  347. buttons.pack(side="bottom", expand=0, fill="x")
  348. Button(buttons, text="Quit", command=self.destroy).pack(side="right")
  349. Button(buttons, text="Print All", command=self.print_all).pack(side="left")
  350. Button(buttons, text="Print Selection", command=self.print_selection).pack(
  351. side="left"
  352. )
  353. # Canvas frame.
  354. self._cframe = CanvasFrame(self._root, closeenough=20)
  355. self._cframe.pack(side="top", expand=1, fill="both")
  356. # Initial update
  357. self.update()
  358. def update(self, edge=None):
  359. if self._root is None:
  360. return
  361. # If the edge isn't a parse edge, do nothing.
  362. if edge is not None:
  363. if edge.lhs() != self._grammar.start():
  364. return
  365. if edge.span() != (0, self._chart.num_leaves()):
  366. return
  367. for parse in self._chart.parses(self._grammar.start()):
  368. if parse not in self._trees:
  369. self._add(parse)
  370. def _add(self, parse):
  371. # Add it to self._trees.
  372. self._trees.append(parse)
  373. # Create a widget for it.
  374. c = self._cframe.canvas()
  375. treewidget = tree_to_treesegment(c, parse)
  376. # Add it to the canvas frame.
  377. self._treewidgets.append(treewidget)
  378. self._cframe.add_widget(treewidget, 10, self._y)
  379. # Register callbacks.
  380. treewidget.bind_click(self._click)
  381. # Update y.
  382. self._y = treewidget.bbox()[3] + 10
  383. def _click(self, widget):
  384. c = self._cframe.canvas()
  385. if self._selection is not None:
  386. c.delete(self._selectbox)
  387. self._selection = widget
  388. (x1, y1, x2, y2) = widget.bbox()
  389. self._selectbox = c.create_rectangle(x1, y1, x2, y2, width=2, outline="#088")
  390. def _color(self, treewidget, color):
  391. treewidget.label()["color"] = color
  392. for child in treewidget.subtrees():
  393. if isinstance(child, TreeSegmentWidget):
  394. self._color(child, color)
  395. else:
  396. child["color"] = color
  397. def print_all(self, *e):
  398. if self._root is None:
  399. return
  400. self._cframe.print_to_file()
  401. def print_selection(self, *e):
  402. if self._root is None:
  403. return
  404. if self._selection is None:
  405. showerror("Print Error", "No tree selected")
  406. else:
  407. c = self._cframe.canvas()
  408. for widget in self._treewidgets:
  409. if widget is not self._selection:
  410. self._cframe.destroy_widget(widget)
  411. c.delete(self._selectbox)
  412. (x1, y1, x2, y2) = self._selection.bbox()
  413. self._selection.move(10 - x1, 10 - y1)
  414. c["scrollregion"] = "0 0 %s %s" % (x2 - x1 + 20, y2 - y1 + 20)
  415. self._cframe.print_to_file()
  416. # Restore our state.
  417. self._treewidgets = [self._selection]
  418. self.clear()
  419. self.update()
  420. def clear(self):
  421. if self._root is None:
  422. return
  423. for treewidget in self._treewidgets:
  424. self._cframe.destroy_widget(treewidget)
  425. self._trees = []
  426. self._treewidgets = []
  427. if self._selection is not None:
  428. self._cframe.canvas().delete(self._selectbox)
  429. self._selection = None
  430. self._y = 10
  431. def set_chart(self, chart):
  432. self.clear()
  433. self._chart = chart
  434. self.update()
  435. def set_grammar(self, grammar):
  436. self.clear()
  437. self._grammar = grammar
  438. self.update()
  439. def destroy(self, *e):
  440. if self._root is None:
  441. return
  442. try:
  443. self._root.destroy()
  444. except:
  445. pass
  446. self._root = None
  447. def pack(self, *args, **kwargs):
  448. self._root.pack(*args, **kwargs)
  449. #######################################################################
  450. # Chart Comparer
  451. #######################################################################
  452. class ChartComparer(object):
  453. """
  454. :ivar _root: The root window
  455. :ivar _charts: A dictionary mapping names to charts. When
  456. charts are loaded, they are added to this dictionary.
  457. :ivar _left_chart: The left ``Chart``.
  458. :ivar _left_name: The name ``_left_chart`` (derived from filename)
  459. :ivar _left_matrix: The ``ChartMatrixView`` for ``_left_chart``
  460. :ivar _left_selector: The drop-down ``MutableOptionsMenu`` used
  461. to select ``_left_chart``.
  462. :ivar _right_chart: The right ``Chart``.
  463. :ivar _right_name: The name ``_right_chart`` (derived from filename)
  464. :ivar _right_matrix: The ``ChartMatrixView`` for ``_right_chart``
  465. :ivar _right_selector: The drop-down ``MutableOptionsMenu`` used
  466. to select ``_right_chart``.
  467. :ivar _out_chart: The out ``Chart``.
  468. :ivar _out_name: The name ``_out_chart`` (derived from filename)
  469. :ivar _out_matrix: The ``ChartMatrixView`` for ``_out_chart``
  470. :ivar _out_label: The label for ``_out_chart``.
  471. :ivar _op_label: A Label containing the most recent operation.
  472. """
  473. _OPSYMBOL = {
  474. "-": "-",
  475. "and": SymbolWidget.SYMBOLS["intersection"],
  476. "or": SymbolWidget.SYMBOLS["union"],
  477. }
  478. def __init__(self, *chart_filenames):
  479. # This chart is displayed when we don't have a value (eg
  480. # before any chart is loaded).
  481. faketok = [""] * 8
  482. self._emptychart = Chart(faketok)
  483. # The left & right charts start out empty.
  484. self._left_name = "None"
  485. self._right_name = "None"
  486. self._left_chart = self._emptychart
  487. self._right_chart = self._emptychart
  488. # The charts that have been loaded.
  489. self._charts = {"None": self._emptychart}
  490. # The output chart.
  491. self._out_chart = self._emptychart
  492. # The most recent operation
  493. self._operator = None
  494. # Set up the root window.
  495. self._root = Tk()
  496. self._root.title("Chart Comparison")
  497. self._root.bind("<Control-q>", self.destroy)
  498. self._root.bind("<Control-x>", self.destroy)
  499. # Initialize all widgets, etc.
  500. self._init_menubar(self._root)
  501. self._init_chartviews(self._root)
  502. self._init_divider(self._root)
  503. self._init_buttons(self._root)
  504. self._init_bindings(self._root)
  505. # Load any specified charts.
  506. for filename in chart_filenames:
  507. self.load_chart(filename)
  508. def destroy(self, *e):
  509. if self._root is None:
  510. return
  511. try:
  512. self._root.destroy()
  513. except:
  514. pass
  515. self._root = None
  516. def mainloop(self, *args, **kwargs):
  517. return
  518. self._root.mainloop(*args, **kwargs)
  519. # ////////////////////////////////////////////////////////////
  520. # Initialization
  521. # ////////////////////////////////////////////////////////////
  522. def _init_menubar(self, root):
  523. menubar = Menu(root)
  524. # File menu
  525. filemenu = Menu(menubar, tearoff=0)
  526. filemenu.add_command(
  527. label="Load Chart",
  528. accelerator="Ctrl-o",
  529. underline=0,
  530. command=self.load_chart_dialog,
  531. )
  532. filemenu.add_command(
  533. label="Save Output",
  534. accelerator="Ctrl-s",
  535. underline=0,
  536. command=self.save_chart_dialog,
  537. )
  538. filemenu.add_separator()
  539. filemenu.add_command(
  540. label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
  541. )
  542. menubar.add_cascade(label="File", underline=0, menu=filemenu)
  543. # Compare menu
  544. opmenu = Menu(menubar, tearoff=0)
  545. opmenu.add_command(
  546. label="Intersection", command=self._intersection, accelerator="+"
  547. )
  548. opmenu.add_command(label="Union", command=self._union, accelerator="*")
  549. opmenu.add_command(
  550. label="Difference", command=self._difference, accelerator="-"
  551. )
  552. opmenu.add_separator()
  553. opmenu.add_command(label="Swap Charts", command=self._swapcharts)
  554. menubar.add_cascade(label="Compare", underline=0, menu=opmenu)
  555. # Add the menu
  556. self._root.config(menu=menubar)
  557. def _init_divider(self, root):
  558. divider = Frame(root, border=2, relief="sunken")
  559. divider.pack(side="top", fill="x", ipady=2)
  560. def _init_chartviews(self, root):
  561. opfont = ("symbol", -36) # Font for operator.
  562. eqfont = ("helvetica", -36) # Font for equals sign.
  563. frame = Frame(root, background="#c0c0c0")
  564. frame.pack(side="top", expand=1, fill="both")
  565. # The left matrix.
  566. cv1_frame = Frame(frame, border=3, relief="groove")
  567. cv1_frame.pack(side="left", padx=8, pady=7, expand=1, fill="both")
  568. self._left_selector = MutableOptionMenu(
  569. cv1_frame, list(self._charts.keys()), command=self._select_left
  570. )
  571. self._left_selector.pack(side="top", pady=5, fill="x")
  572. self._left_matrix = ChartMatrixView(
  573. cv1_frame, self._emptychart, toplevel=False, show_numedges=True
  574. )
  575. self._left_matrix.pack(side="bottom", padx=5, pady=5, expand=1, fill="both")
  576. self._left_matrix.add_callback("select", self.select_edge)
  577. self._left_matrix.add_callback("select_cell", self.select_cell)
  578. self._left_matrix.inactivate()
  579. # The operator.
  580. self._op_label = Label(
  581. frame, text=" ", width=3, background="#c0c0c0", font=opfont
  582. )
  583. self._op_label.pack(side="left", padx=5, pady=5)
  584. # The right matrix.
  585. cv2_frame = Frame(frame, border=3, relief="groove")
  586. cv2_frame.pack(side="left", padx=8, pady=7, expand=1, fill="both")
  587. self._right_selector = MutableOptionMenu(
  588. cv2_frame, list(self._charts.keys()), command=self._select_right
  589. )
  590. self._right_selector.pack(side="top", pady=5, fill="x")
  591. self._right_matrix = ChartMatrixView(
  592. cv2_frame, self._emptychart, toplevel=False, show_numedges=True
  593. )
  594. self._right_matrix.pack(side="bottom", padx=5, pady=5, expand=1, fill="both")
  595. self._right_matrix.add_callback("select", self.select_edge)
  596. self._right_matrix.add_callback("select_cell", self.select_cell)
  597. self._right_matrix.inactivate()
  598. # The equals sign
  599. Label(frame, text="=", width=3, background="#c0c0c0", font=eqfont).pack(
  600. side="left", padx=5, pady=5
  601. )
  602. # The output matrix.
  603. out_frame = Frame(frame, border=3, relief="groove")
  604. out_frame.pack(side="left", padx=8, pady=7, expand=1, fill="both")
  605. self._out_label = Label(out_frame, text="Output")
  606. self._out_label.pack(side="top", pady=9)
  607. self._out_matrix = ChartMatrixView(
  608. out_frame, self._emptychart, toplevel=False, show_numedges=True
  609. )
  610. self._out_matrix.pack(side="bottom", padx=5, pady=5, expand=1, fill="both")
  611. self._out_matrix.add_callback("select", self.select_edge)
  612. self._out_matrix.add_callback("select_cell", self.select_cell)
  613. self._out_matrix.inactivate()
  614. def _init_buttons(self, root):
  615. buttons = Frame(root)
  616. buttons.pack(side="bottom", pady=5, fill="x", expand=0)
  617. Button(buttons, text="Intersection", command=self._intersection).pack(
  618. side="left"
  619. )
  620. Button(buttons, text="Union", command=self._union).pack(side="left")
  621. Button(buttons, text="Difference", command=self._difference).pack(side="left")
  622. Frame(buttons, width=20).pack(side="left")
  623. Button(buttons, text="Swap Charts", command=self._swapcharts).pack(side="left")
  624. Button(buttons, text="Detatch Output", command=self._detatch_out).pack(
  625. side="right"
  626. )
  627. def _init_bindings(self, root):
  628. # root.bind('<Control-s>', self.save_chart)
  629. root.bind("<Control-o>", self.load_chart_dialog)
  630. # root.bind('<Control-r>', self.reset)
  631. # ////////////////////////////////////////////////////////////
  632. # Input Handling
  633. # ////////////////////////////////////////////////////////////
  634. def _select_left(self, name):
  635. self._left_name = name
  636. self._left_chart = self._charts[name]
  637. self._left_matrix.set_chart(self._left_chart)
  638. if name == "None":
  639. self._left_matrix.inactivate()
  640. self._apply_op()
  641. def _select_right(self, name):
  642. self._right_name = name
  643. self._right_chart = self._charts[name]
  644. self._right_matrix.set_chart(self._right_chart)
  645. if name == "None":
  646. self._right_matrix.inactivate()
  647. self._apply_op()
  648. def _apply_op(self):
  649. if self._operator == "-":
  650. self._difference()
  651. elif self._operator == "or":
  652. self._union()
  653. elif self._operator == "and":
  654. self._intersection()
  655. # ////////////////////////////////////////////////////////////
  656. # File
  657. # ////////////////////////////////////////////////////////////
  658. CHART_FILE_TYPES = [("Pickle file", ".pickle"), ("All files", "*")]
  659. def save_chart_dialog(self, *args):
  660. filename = asksaveasfilename(
  661. filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
  662. )
  663. if not filename:
  664. return
  665. try:
  666. with open(filename, "wb") as outfile:
  667. pickle.dump(self._out_chart, outfile)
  668. except Exception as e:
  669. showerror(
  670. "Error Saving Chart", "Unable to open file: %r\n%s" % (filename, e)
  671. )
  672. def load_chart_dialog(self, *args):
  673. filename = askopenfilename(
  674. filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
  675. )
  676. if not filename:
  677. return
  678. try:
  679. self.load_chart(filename)
  680. except Exception as e:
  681. showerror(
  682. "Error Loading Chart", "Unable to open file: %r\n%s" % (filename, e)
  683. )
  684. def load_chart(self, filename):
  685. with open(filename, "rb") as infile:
  686. chart = pickle.load(infile)
  687. name = os.path.basename(filename)
  688. if name.endswith(".pickle"):
  689. name = name[:-7]
  690. if name.endswith(".chart"):
  691. name = name[:-6]
  692. self._charts[name] = chart
  693. self._left_selector.add(name)
  694. self._right_selector.add(name)
  695. # If either left_matrix or right_matrix is empty, then
  696. # display the new chart.
  697. if self._left_chart is self._emptychart:
  698. self._left_selector.set(name)
  699. elif self._right_chart is self._emptychart:
  700. self._right_selector.set(name)
  701. def _update_chartviews(self):
  702. self._left_matrix.update()
  703. self._right_matrix.update()
  704. self._out_matrix.update()
  705. # ////////////////////////////////////////////////////////////
  706. # Selection
  707. # ////////////////////////////////////////////////////////////
  708. def select_edge(self, edge):
  709. if edge in self._left_chart:
  710. self._left_matrix.markonly_edge(edge)
  711. else:
  712. self._left_matrix.unmark_edge()
  713. if edge in self._right_chart:
  714. self._right_matrix.markonly_edge(edge)
  715. else:
  716. self._right_matrix.unmark_edge()
  717. if edge in self._out_chart:
  718. self._out_matrix.markonly_edge(edge)
  719. else:
  720. self._out_matrix.unmark_edge()
  721. def select_cell(self, i, j):
  722. self._left_matrix.select_cell(i, j)
  723. self._right_matrix.select_cell(i, j)
  724. self._out_matrix.select_cell(i, j)
  725. # ////////////////////////////////////////////////////////////
  726. # Operations
  727. # ////////////////////////////////////////////////////////////
  728. def _difference(self):
  729. if not self._checkcompat():
  730. return
  731. out_chart = Chart(self._left_chart.tokens())
  732. for edge in self._left_chart:
  733. if edge not in self._right_chart:
  734. out_chart.insert(edge, [])
  735. self._update("-", out_chart)
  736. def _intersection(self):
  737. if not self._checkcompat():
  738. return
  739. out_chart = Chart(self._left_chart.tokens())
  740. for edge in self._left_chart:
  741. if edge in self._right_chart:
  742. out_chart.insert(edge, [])
  743. self._update("and", out_chart)
  744. def _union(self):
  745. if not self._checkcompat():
  746. return
  747. out_chart = Chart(self._left_chart.tokens())
  748. for edge in self._left_chart:
  749. out_chart.insert(edge, [])
  750. for edge in self._right_chart:
  751. out_chart.insert(edge, [])
  752. self._update("or", out_chart)
  753. def _swapcharts(self):
  754. left, right = self._left_name, self._right_name
  755. self._left_selector.set(right)
  756. self._right_selector.set(left)
  757. def _checkcompat(self):
  758. if (
  759. self._left_chart.tokens() != self._right_chart.tokens()
  760. or self._left_chart.property_names() != self._right_chart.property_names()
  761. or self._left_chart == self._emptychart
  762. or self._right_chart == self._emptychart
  763. ):
  764. # Clear & inactivate the output chart.
  765. self._out_chart = self._emptychart
  766. self._out_matrix.set_chart(self._out_chart)
  767. self._out_matrix.inactivate()
  768. self._out_label["text"] = "Output"
  769. # Issue some other warning?
  770. return False
  771. else:
  772. return True
  773. def _update(self, operator, out_chart):
  774. self._operator = operator
  775. self._op_label["text"] = self._OPSYMBOL[operator]
  776. self._out_chart = out_chart
  777. self._out_matrix.set_chart(out_chart)
  778. self._out_label["text"] = "%s %s %s" % (
  779. self._left_name,
  780. self._operator,
  781. self._right_name,
  782. )
  783. def _clear_out_chart(self):
  784. self._out_chart = self._emptychart
  785. self._out_matrix.set_chart(self._out_chart)
  786. self._op_label["text"] = " "
  787. self._out_matrix.inactivate()
  788. def _detatch_out(self):
  789. ChartMatrixView(self._root, self._out_chart, title=self._out_label["text"])
  790. #######################################################################
  791. # Chart View
  792. #######################################################################
  793. class ChartView(object):
  794. """
  795. A component for viewing charts. This is used by ``ChartParserApp`` to
  796. allow students to interactively experiment with various chart
  797. parsing techniques. It is also used by ``Chart.draw()``.
  798. :ivar _chart: The chart that we are giving a view of. This chart
  799. may be modified; after it is modified, you should call
  800. ``update``.
  801. :ivar _sentence: The list of tokens that the chart spans.
  802. :ivar _root: The root window.
  803. :ivar _chart_canvas: The canvas we're using to display the chart
  804. itself.
  805. :ivar _tree_canvas: The canvas we're using to display the tree
  806. that each edge spans. May be None, if we're not displaying
  807. trees.
  808. :ivar _sentence_canvas: The canvas we're using to display the sentence
  809. text. May be None, if we're not displaying the sentence text.
  810. :ivar _edgetags: A dictionary mapping from edges to the tags of
  811. the canvas elements (lines, etc) used to display that edge.
  812. The values of this dictionary have the form
  813. ``(linetag, rhstag1, dottag, rhstag2, lhstag)``.
  814. :ivar _treetags: A list of all the tags that make up the tree;
  815. used to erase the tree (without erasing the loclines).
  816. :ivar _chart_height: The height of the chart canvas.
  817. :ivar _sentence_height: The height of the sentence canvas.
  818. :ivar _tree_height: The height of the tree
  819. :ivar _text_height: The height of a text string (in the normal
  820. font).
  821. :ivar _edgelevels: A list of edges at each level of the chart (the
  822. top level is the 0th element). This list is used to remember
  823. where edges should be drawn; and to make sure that no edges
  824. are overlapping on the chart view.
  825. :ivar _unitsize: Pixel size of one unit (from the location). This
  826. is determined by the span of the chart's location, and the
  827. width of the chart display canvas.
  828. :ivar _fontsize: The current font size
  829. :ivar _marks: A dictionary from edges to marks. Marks are
  830. strings, specifying colors (e.g. 'green').
  831. """
  832. _LEAF_SPACING = 10
  833. _MARGIN = 10
  834. _TREE_LEVEL_SIZE = 12
  835. _CHART_LEVEL_SIZE = 40
  836. def __init__(self, chart, root=None, **kw):
  837. """
  838. Construct a new ``Chart`` display.
  839. """
  840. # Process keyword args.
  841. draw_tree = kw.get("draw_tree", 0)
  842. draw_sentence = kw.get("draw_sentence", 1)
  843. self._fontsize = kw.get("fontsize", -12)
  844. # The chart!
  845. self._chart = chart
  846. # Callback functions
  847. self._callbacks = {}
  848. # Keep track of drawn edges
  849. self._edgelevels = []
  850. self._edgetags = {}
  851. # Keep track of which edges are marked.
  852. self._marks = {}
  853. # These are used to keep track of the set of tree tokens
  854. # currently displayed in the tree canvas.
  855. self._treetoks = []
  856. self._treetoks_edge = None
  857. self._treetoks_index = 0
  858. # Keep track of the tags used to draw the tree
  859. self._tree_tags = []
  860. # Put multiple edges on each level?
  861. self._compact = 0
  862. # If they didn't provide a main window, then set one up.
  863. if root is None:
  864. top = Tk()
  865. top.title("Chart View")
  866. def destroy1(e, top=top):
  867. top.destroy()
  868. def destroy2(top=top):
  869. top.destroy()
  870. top.bind("q", destroy1)
  871. b = Button(top, text="Done", command=destroy2)
  872. b.pack(side="bottom")
  873. self._root = top
  874. else:
  875. self._root = root
  876. # Create some fonts.
  877. self._init_fonts(root)
  878. # Create the chart canvas.
  879. (self._chart_sb, self._chart_canvas) = self._sb_canvas(self._root)
  880. self._chart_canvas["height"] = 300
  881. self._chart_canvas["closeenough"] = 15
  882. # Create the sentence canvas.
  883. if draw_sentence:
  884. cframe = Frame(self._root, relief="sunk", border=2)
  885. cframe.pack(fill="both", side="bottom")
  886. self._sentence_canvas = Canvas(cframe, height=50)
  887. self._sentence_canvas["background"] = "#e0e0e0"
  888. self._sentence_canvas.pack(fill="both")
  889. # self._sentence_canvas['height'] = self._sentence_height
  890. else:
  891. self._sentence_canvas = None
  892. # Create the tree canvas.
  893. if draw_tree:
  894. (sb, canvas) = self._sb_canvas(self._root, "n", "x")
  895. (self._tree_sb, self._tree_canvas) = (sb, canvas)
  896. self._tree_canvas["height"] = 200
  897. else:
  898. self._tree_canvas = None
  899. # Do some analysis to figure out how big the window should be
  900. self._analyze()
  901. self.draw()
  902. self._resize()
  903. self._grow()
  904. # Set up the configure callback, which will be called whenever
  905. # the window is resized.
  906. self._chart_canvas.bind("<Configure>", self._configure)
  907. def _init_fonts(self, root):
  908. self._boldfont = Font(family="helvetica", weight="bold", size=self._fontsize)
  909. self._font = Font(family="helvetica", size=self._fontsize)
  910. # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
  911. self._sysfont = Font(font=Button()["font"])
  912. root.option_add("*Font", self._sysfont)
  913. def _sb_canvas(self, root, expand="y", fill="both", side="bottom"):
  914. """
  915. Helper for __init__: construct a canvas with a scrollbar.
  916. """
  917. cframe = Frame(root, relief="sunk", border=2)
  918. cframe.pack(fill=fill, expand=expand, side=side)
  919. canvas = Canvas(cframe, background="#e0e0e0")
  920. # Give the canvas a scrollbar.
  921. sb = Scrollbar(cframe, orient="vertical")
  922. sb.pack(side="right", fill="y")
  923. canvas.pack(side="left", fill=fill, expand="yes")
  924. # Connect the scrollbars to the canvas.
  925. sb["command"] = canvas.yview
  926. canvas["yscrollcommand"] = sb.set
  927. return (sb, canvas)
  928. def scroll_up(self, *e):
  929. self._chart_canvas.yview("scroll", -1, "units")
  930. def scroll_down(self, *e):
  931. self._chart_canvas.yview("scroll", 1, "units")
  932. def page_up(self, *e):
  933. self._chart_canvas.yview("scroll", -1, "pages")
  934. def page_down(self, *e):
  935. self._chart_canvas.yview("scroll", 1, "pages")
  936. def _grow(self):
  937. """
  938. Grow the window, if necessary
  939. """
  940. # Grow, if need-be
  941. N = self._chart.num_leaves()
  942. width = max(
  943. int(self._chart_canvas["width"]), N * self._unitsize + ChartView._MARGIN * 2
  944. )
  945. # It won't resize without the second (height) line, but I
  946. # don't understand why not.
  947. self._chart_canvas.configure(width=width)
  948. self._chart_canvas.configure(height=self._chart_canvas["height"])
  949. self._unitsize = (width - 2 * ChartView._MARGIN) / N
  950. # Reset the height for the sentence window.
  951. if self._sentence_canvas is not None:
  952. self._sentence_canvas["height"] = self._sentence_height
  953. def set_font_size(self, size):
  954. self._font.configure(size=-abs(size))
  955. self._boldfont.configure(size=-abs(size))
  956. self._sysfont.configure(size=-abs(size))
  957. self._analyze()
  958. self._grow()
  959. self.draw()
  960. def get_font_size(self):
  961. return abs(self._fontsize)
  962. def _configure(self, e):
  963. """
  964. The configure callback. This is called whenever the window is
  965. resized. It is also called when the window is first mapped.
  966. It figures out the unit size, and redraws the contents of each
  967. canvas.
  968. """
  969. N = self._chart.num_leaves()
  970. self._unitsize = (e.width - 2 * ChartView._MARGIN) / N
  971. self.draw()
  972. def update(self, chart=None):
  973. """
  974. Draw any edges that have not been drawn. This is typically
  975. called when a after modifies the canvas that a CanvasView is
  976. displaying. ``update`` will cause any edges that have been
  977. added to the chart to be drawn.
  978. If update is given a ``chart`` argument, then it will replace
  979. the current chart with the given chart.
  980. """
  981. if chart is not None:
  982. self._chart = chart
  983. self._edgelevels = []
  984. self._marks = {}
  985. self._analyze()
  986. self._grow()
  987. self.draw()
  988. self.erase_tree()
  989. self._resize()
  990. else:
  991. for edge in self._chart:
  992. if edge not in self._edgetags:
  993. self._add_edge(edge)
  994. self._resize()
  995. def _edge_conflict(self, edge, lvl):
  996. """
  997. Return True if the given edge overlaps with any edge on the given
  998. level. This is used by _add_edge to figure out what level a
  999. new edge should be added to.
  1000. """
  1001. (s1, e1) = edge.span()
  1002. for otheredge in self._edgelevels[lvl]:
  1003. (s2, e2) = otheredge.span()
  1004. if (s1 <= s2 < e1) or (s2 <= s1 < e2) or (s1 == s2 == e1 == e2):
  1005. return True
  1006. return False
  1007. def _analyze_edge(self, edge):
  1008. """
  1009. Given a new edge, recalculate:
  1010. - _text_height
  1011. - _unitsize (if the edge text is too big for the current
  1012. _unitsize, then increase _unitsize)
  1013. """
  1014. c = self._chart_canvas
  1015. if isinstance(edge, TreeEdge):
  1016. lhs = edge.lhs()
  1017. rhselts = []
  1018. for elt in edge.rhs():
  1019. if isinstance(elt, Nonterminal):
  1020. rhselts.append(str(elt.symbol()))
  1021. else:
  1022. rhselts.append(repr(elt))
  1023. rhs = " ".join(rhselts)
  1024. else:
  1025. lhs = edge.lhs()
  1026. rhs = ""
  1027. for s in (lhs, rhs):
  1028. tag = c.create_text(
  1029. 0, 0, text=s, font=self._boldfont, anchor="nw", justify="left"
  1030. )
  1031. bbox = c.bbox(tag)
  1032. c.delete(tag)
  1033. width = bbox[2] # + ChartView._LEAF_SPACING
  1034. edgelen = max(edge.length(), 1)
  1035. self._unitsize = max(self._unitsize, width / edgelen)
  1036. self._text_height = max(self._text_height, bbox[3] - bbox[1])
  1037. def _add_edge(self, edge, minlvl=0):
  1038. """
  1039. Add a single edge to the ChartView:
  1040. - Call analyze_edge to recalculate display parameters
  1041. - Find an available level
  1042. - Call _draw_edge
  1043. """
  1044. # Do NOT show leaf edges in the chart.
  1045. if isinstance(edge, LeafEdge):
  1046. return
  1047. if edge in self._edgetags:
  1048. return
  1049. self._analyze_edge(edge)
  1050. self._grow()
  1051. if not self._compact:
  1052. self._edgelevels.append([edge])
  1053. lvl = len(self._edgelevels) - 1
  1054. self._draw_edge(edge, lvl)
  1055. self._resize()
  1056. return
  1057. # Figure out what level to draw the edge on.
  1058. lvl = 0
  1059. while True:
  1060. # If this level doesn't exist yet, create it.
  1061. while lvl >= len(self._edgelevels):
  1062. self._edgelevels.append([])
  1063. self._resize()
  1064. # Check if we can fit the edge in this level.
  1065. if lvl >= minlvl and not self._edge_conflict(edge, lvl):
  1066. # Go ahead and draw it.
  1067. self._edgelevels[lvl].append(edge)
  1068. break
  1069. # Try the next level.
  1070. lvl += 1
  1071. self._draw_edge(edge, lvl)
  1072. def view_edge(self, edge):
  1073. level = None
  1074. for i in range(len(self._edgelevels)):
  1075. if edge in self._edgelevels[i]:
  1076. level = i
  1077. break
  1078. if level is None:
  1079. return
  1080. # Try to view the new edge..
  1081. y = (level + 1) * self._chart_level_size
  1082. dy = self._text_height + 10
  1083. self._chart_canvas.yview("moveto", 1.0)
  1084. if self._chart_height != 0:
  1085. self._chart_canvas.yview("moveto", (y - dy) / self._chart_height)
  1086. def _draw_edge(self, edge, lvl):
  1087. """
  1088. Draw a single edge on the ChartView.
  1089. """
  1090. c = self._chart_canvas
  1091. # Draw the arrow.
  1092. x1 = edge.start() * self._unitsize + ChartView._MARGIN
  1093. x2 = edge.end() * self._unitsize + ChartView._MARGIN
  1094. if x2 == x1:
  1095. x2 += max(4, self._unitsize / 5)
  1096. y = (lvl + 1) * self._chart_level_size
  1097. linetag = c.create_line(x1, y, x2, y, arrow="last", width=3)
  1098. # Draw a label for the edge.
  1099. if isinstance(edge, TreeEdge):
  1100. rhs = []
  1101. for elt in edge.rhs():
  1102. if isinstance(elt, Nonterminal):
  1103. rhs.append(str(elt.symbol()))
  1104. else:
  1105. rhs.append(repr(elt))
  1106. pos = edge.dot()
  1107. else:
  1108. rhs = []
  1109. pos = 0
  1110. rhs1 = " ".join(rhs[:pos])
  1111. rhs2 = " ".join(rhs[pos:])
  1112. rhstag1 = c.create_text(x1 + 3, y, text=rhs1, font=self._font, anchor="nw")
  1113. dotx = c.bbox(rhstag1)[2] + 6
  1114. doty = (c.bbox(rhstag1)[1] + c.bbox(rhstag1)[3]) / 2
  1115. dottag = c.create_oval(dotx - 2, doty - 2, dotx + 2, doty + 2)
  1116. rhstag2 = c.create_text(dotx + 6, y, text=rhs2, font=self._font, anchor="nw")
  1117. lhstag = c.create_text(
  1118. (x1 + x2) / 2, y, text=str(edge.lhs()), anchor="s", font=self._boldfont
  1119. )
  1120. # Keep track of the edge's tags.
  1121. self._edgetags[edge] = (linetag, rhstag1, dottag, rhstag2, lhstag)
  1122. # Register a callback for clicking on the edge.
  1123. def cb(event, self=self, edge=edge):
  1124. self._fire_callbacks("select", edge)
  1125. c.tag_bind(rhstag1, "<Button-1>", cb)
  1126. c.tag_bind(rhstag2, "<Button-1>", cb)
  1127. c.tag_bind(linetag, "<Button-1>", cb)
  1128. c.tag_bind(dottag, "<Button-1>", cb)
  1129. c.tag_bind(lhstag, "<Button-1>", cb)
  1130. self._color_edge(edge)
  1131. def _color_edge(self, edge, linecolor=None, textcolor=None):
  1132. """
  1133. Color in an edge with the given colors.
  1134. If no colors are specified, use intelligent defaults
  1135. (dependent on selection, etc.)
  1136. """
  1137. if edge not in self._edgetags:
  1138. return
  1139. c = self._chart_canvas
  1140. if linecolor is not None and textcolor is not None:
  1141. if edge in self._marks:
  1142. linecolor = self._marks[edge]
  1143. tags = self._edgetags[edge]
  1144. c.itemconfig(tags[0], fill=linecolor)
  1145. c.itemconfig(tags[1], fill=textcolor)
  1146. c.itemconfig(tags[2], fill=textcolor, outline=textcolor)
  1147. c.itemconfig(tags[3], fill=textcolor)
  1148. c.itemconfig(tags[4], fill=textcolor)
  1149. return
  1150. else:
  1151. N = self._chart.num_leaves()
  1152. if edge in self._marks:
  1153. self._color_edge(self._marks[edge])
  1154. if edge.is_complete() and edge.span() == (0, N):
  1155. self._color_edge(edge, "#084", "#042")
  1156. elif isinstance(edge, LeafEdge):
  1157. self._color_edge(edge, "#48c", "#246")
  1158. else:
  1159. self._color_edge(edge, "#00f", "#008")
  1160. def mark_edge(self, edge, mark="#0df"):
  1161. """
  1162. Mark an edge
  1163. """
  1164. self._marks[edge] = mark
  1165. self._color_edge(edge)
  1166. def unmark_edge(self, edge=None):
  1167. """
  1168. Unmark an edge (or all edges)
  1169. """
  1170. if edge is None:
  1171. old_marked_edges = list(self._marks.keys())
  1172. self._marks = {}
  1173. for edge in old_marked_edges:
  1174. self._color_edge(edge)
  1175. else:
  1176. del self._marks[edge]
  1177. self._color_edge(edge)
  1178. def markonly_edge(self, edge, mark="#0df"):
  1179. self.unmark_edge()
  1180. self.mark_edge(edge, mark)
  1181. def _analyze(self):
  1182. """
  1183. Analyze the sentence string, to figure out how big a unit needs
  1184. to be, How big the tree should be, etc.
  1185. """
  1186. # Figure out the text height and the unit size.
  1187. unitsize = 70 # min unitsize
  1188. text_height = 0
  1189. c = self._chart_canvas
  1190. # Check against all tokens
  1191. for leaf in self._chart.leaves():
  1192. tag = c.create_text(
  1193. 0, 0, text=repr(leaf), font=self._font, anchor="nw", justify="left"
  1194. )
  1195. bbox = c.bbox(tag)
  1196. c.delete(tag)
  1197. width = bbox[2] + ChartView._LEAF_SPACING
  1198. unitsize = max(width, unitsize)
  1199. text_height = max(text_height, bbox[3] - bbox[1])
  1200. self._unitsize = unitsize
  1201. self._text_height = text_height
  1202. self._sentence_height = self._text_height + 2 * ChartView._MARGIN
  1203. # Check against edges.
  1204. for edge in self._chart.edges():
  1205. self._analyze_edge(edge)
  1206. # Size of chart levels
  1207. self._chart_level_size = self._text_height * 2
  1208. # Default tree size..
  1209. self._tree_height = 3 * (ChartView._TREE_LEVEL_SIZE + self._text_height)
  1210. # Resize the scrollregions.
  1211. self._resize()
  1212. def _resize(self):
  1213. """
  1214. Update the scroll-regions for each canvas. This ensures that
  1215. everything is within a scroll-region, so the user can use the
  1216. scrollbars to view the entire display. This does *not*
  1217. resize the window.
  1218. """
  1219. c = self._chart_canvas
  1220. # Reset the chart scroll region
  1221. width = self._chart.num_leaves() * self._unitsize + ChartView._MARGIN * 2
  1222. levels = len(self._edgelevels)
  1223. self._chart_height = (levels + 2) * self._chart_level_size
  1224. c["scrollregion"] = (0, 0, width, self._chart_height)
  1225. # Reset the tree scroll region
  1226. if self._tree_canvas:
  1227. self._tree_canvas["scrollregion"] = (0, 0, width, self._tree_height)
  1228. def _draw_loclines(self):
  1229. """
  1230. Draw location lines. These are vertical gridlines used to
  1231. show where each location unit is.
  1232. """
  1233. BOTTOM = 50000
  1234. c1 = self._tree_canvas
  1235. c2 = self._sentence_canvas
  1236. c3 = self._chart_canvas
  1237. margin = ChartView._MARGIN
  1238. self._loclines = []
  1239. for i in range(0, self._chart.num_leaves() + 1):
  1240. x = i * self._unitsize + margin
  1241. if c1:
  1242. t1 = c1.create_line(x, 0, x, BOTTOM)
  1243. c1.tag_lower(t1)
  1244. if c2:
  1245. t2 = c2.create_line(x, 0, x, self._sentence_height)
  1246. c2.tag_lower(t2)
  1247. t3 = c3.create_line(x, 0, x, BOTTOM)
  1248. c3.tag_lower(t3)
  1249. t4 = c3.create_text(x + 2, 0, text=repr(i), anchor="nw", font=self._font)
  1250. c3.tag_lower(t4)
  1251. # if i % 4 == 0:
  1252. # if c1: c1.itemconfig(t1, width=2, fill='gray60')
  1253. # if c2: c2.itemconfig(t2, width=2, fill='gray60')
  1254. # c3.itemconfig(t3, width=2, fill='gray60')
  1255. if i % 2 == 0:
  1256. if c1:
  1257. c1.itemconfig(t1, fill="gray60")
  1258. if c2:
  1259. c2.itemconfig(t2, fill="gray60")
  1260. c3.itemconfig(t3, fill="gray60")
  1261. else:
  1262. if c1:
  1263. c1.itemconfig(t1, fill="gray80")
  1264. if c2:
  1265. c2.itemconfig(t2, fill="gray80")
  1266. c3.itemconfig(t3, fill="gray80")
  1267. def _draw_sentence(self):
  1268. """Draw the sentence string."""
  1269. if self._chart.num_leaves() == 0:
  1270. return
  1271. c = self._sentence_canvas
  1272. margin = ChartView._MARGIN
  1273. y = ChartView._MARGIN
  1274. for i, leaf in enumerate(self._chart.leaves()):
  1275. x1 = i * self._unitsize + margin
  1276. x2 = x1 + self._unitsize
  1277. x = (x1 + x2) / 2
  1278. tag = c.create_text(
  1279. x, y, text=repr(leaf), font=self._font, anchor="n", justify="left"
  1280. )
  1281. bbox = c.bbox(tag)
  1282. rt = c.create_rectangle(
  1283. x1 + 2,
  1284. bbox[1] - (ChartView._LEAF_SPACING / 2),
  1285. x2 - 2,
  1286. bbox[3] + (ChartView._LEAF_SPACING / 2),
  1287. fill="#f0f0f0",
  1288. outline="#f0f0f0",
  1289. )
  1290. c.tag_lower(rt)
  1291. def erase_tree(self):
  1292. for tag in self._tree_tags:
  1293. self._tree_canvas.delete(tag)
  1294. self._treetoks = []
  1295. self._treetoks_edge = None
  1296. self._treetoks_index = 0
  1297. def draw_tree(self, edge=None):
  1298. if edge is None and self._treetoks_edge is None:
  1299. return
  1300. if edge is None:
  1301. edge = self._treetoks_edge
  1302. # If it's a new edge, then get a new list of treetoks.
  1303. if self._treetoks_edge != edge:
  1304. self._treetoks = [t for t in self._chart.trees(edge) if isinstance(t, Tree)]
  1305. self._treetoks_edge = edge
  1306. self._treetoks_index = 0
  1307. # Make sure there's something to draw.
  1308. if len(self._treetoks) == 0:
  1309. return
  1310. # Erase the old tree.
  1311. for tag in self._tree_tags:
  1312. self._tree_canvas.delete(tag)
  1313. # Draw the new tree.
  1314. tree = self._treetoks[self._treetoks_index]
  1315. self._draw_treetok(tree, edge.start())
  1316. # Show how many trees are available for the edge.
  1317. self._draw_treecycle()
  1318. # Update the scroll region.
  1319. w = self._chart.num_leaves() * self._unitsize + 2 * ChartView._MARGIN
  1320. h = tree.height() * (ChartView._TREE_LEVEL_SIZE + self._text_height)
  1321. self._tree_canvas["scrollregion"] = (0, 0, w, h)
  1322. def cycle_tree(self):
  1323. self._treetoks_index = (self._treetoks_index + 1) % len(self._treetoks)
  1324. self.draw_tree(self._treetoks_edge)
  1325. def _draw_treecycle(self):
  1326. if len(self._treetoks) <= 1:
  1327. return
  1328. # Draw the label.
  1329. label = "%d Trees" % len(self._treetoks)
  1330. c = self._tree_canvas
  1331. margin = ChartView._MARGIN
  1332. right = self._chart.num_leaves() * self._unitsize + margin - 2
  1333. tag = c.create_text(right, 2, anchor="ne", text=label, font=self._boldfont)
  1334. self._tree_tags.append(tag)
  1335. _, _, _, y = c.bbox(tag)
  1336. # Draw the triangles.
  1337. for i in range(len(self._treetoks)):
  1338. x = right - 20 * (len(self._treetoks) - i - 1)
  1339. if i == self._treetoks_index:
  1340. fill = "#084"
  1341. else:
  1342. fill = "#fff"
  1343. tag = c.create_polygon(
  1344. x, y + 10, x - 5, y, x - 10, y + 10, fill=fill, outline="black"
  1345. )
  1346. self._tree_tags.append(tag)
  1347. # Set up a callback: show the tree if they click on its
  1348. # triangle.
  1349. def cb(event, self=self, i=i):
  1350. self._treetoks_index = i
  1351. self.draw_tree()
  1352. c.tag_bind(tag, "<Button-1>", cb)
  1353. def _draw_treetok(self, treetok, index, depth=0):
  1354. """
  1355. :param index: The index of the first leaf in the tree.
  1356. :return: The index of the first leaf after the tree.
  1357. """
  1358. c = self._tree_canvas
  1359. margin = ChartView._MARGIN
  1360. # Draw the children
  1361. child_xs = []
  1362. for child in treetok:
  1363. if isinstance(child, Tree):
  1364. child_x, index = self._draw_treetok(child, index, depth + 1)
  1365. child_xs.append(child_x)
  1366. else:
  1367. child_xs.append((2 * index + 1) * self._unitsize / 2 + margin)
  1368. index += 1
  1369. # If we have children, then get the node's x by averaging their
  1370. # node x's. Otherwise, make room for ourselves.
  1371. if child_xs:
  1372. nodex = sum(child_xs) / len(child_xs)
  1373. else:
  1374. # [XX] breaks for null productions.
  1375. nodex = (2 * index + 1) * self._unitsize / 2 + margin
  1376. index += 1
  1377. # Draw the node
  1378. nodey = depth * (ChartView._TREE_LEVEL_SIZE + self._text_height)
  1379. tag = c.create_text(
  1380. nodex,
  1381. nodey,
  1382. anchor="n",
  1383. justify="center",
  1384. text=str(treetok.label()),
  1385. fill="#042",
  1386. font=self._boldfont,
  1387. )
  1388. self._tree_tags.append(tag)
  1389. # Draw lines to the children.
  1390. childy = nodey + ChartView._TREE_LEVEL_SIZE + self._text_height
  1391. for childx, child in zip(child_xs, treetok):
  1392. if isinstance(child, Tree) and child:
  1393. # A "real" tree token:
  1394. tag = c.create_line(
  1395. nodex,
  1396. nodey + self._text_height,
  1397. childx,
  1398. childy,
  1399. width=2,
  1400. fill="#084",
  1401. )
  1402. self._tree_tags.append(tag)
  1403. if isinstance(child, Tree) and not child:
  1404. # An unexpanded tree token:
  1405. tag = c.create_line(
  1406. nodex,
  1407. nodey + self._text_height,
  1408. childx,
  1409. childy,
  1410. width=2,
  1411. fill="#048",
  1412. dash="2 3",
  1413. )
  1414. self._tree_tags.append(tag)
  1415. if not isinstance(child, Tree):
  1416. # A leaf:
  1417. tag = c.create_line(
  1418. nodex,
  1419. nodey + self._text_height,
  1420. childx,
  1421. 10000,
  1422. width=2,
  1423. fill="#084",
  1424. )
  1425. self._tree_tags.append(tag)
  1426. return nodex, index
  1427. def draw(self):
  1428. """
  1429. Draw everything (from scratch).
  1430. """
  1431. if self._tree_canvas:
  1432. self._tree_canvas.delete("all")
  1433. self.draw_tree()
  1434. if self._sentence_canvas:
  1435. self._sentence_canvas.delete("all")
  1436. self._draw_sentence()
  1437. self._chart_canvas.delete("all")
  1438. self._edgetags = {}
  1439. # Redraw any edges we erased.
  1440. for lvl in range(len(self._edgelevels)):
  1441. for edge in self._edgelevels[lvl]:
  1442. self._draw_edge(edge, lvl)
  1443. for edge in self._chart:
  1444. self._add_edge(edge)
  1445. self._draw_loclines()
  1446. def add_callback(self, event, func):
  1447. self._callbacks.setdefault(event, {})[func] = 1
  1448. def remove_callback(self, event, func=None):
  1449. if func is None:
  1450. del self._callbacks[event]
  1451. else:
  1452. try:
  1453. del self._callbacks[event][func]
  1454. except:
  1455. pass
  1456. def _fire_callbacks(self, event, *args):
  1457. if event not in self._callbacks:
  1458. return
  1459. for cb_func in list(self._callbacks[event].keys()):
  1460. cb_func(*args)
  1461. #######################################################################
  1462. # Edge Rules
  1463. #######################################################################
  1464. # These version of the chart rules only apply to a specific edge.
  1465. # This lets the user select an edge, and then apply a rule.
  1466. class EdgeRule(object):
  1467. """
  1468. To create an edge rule, make an empty base class that uses
  1469. EdgeRule as the first base class, and the basic rule as the
  1470. second base class. (Order matters!)
  1471. """
  1472. def __init__(self, edge):
  1473. super = self.__class__.__bases__[1]
  1474. self._edge = edge
  1475. self.NUM_EDGES = super.NUM_EDGES - 1
  1476. def apply(self, chart, grammar, *edges):
  1477. super = self.__class__.__bases__[1]
  1478. edges += (self._edge,)
  1479. for e in super.apply(self, chart, grammar, *edges):
  1480. yield e
  1481. def __str__(self):
  1482. super = self.__class__.__bases__[1]
  1483. return super.__str__(self)
  1484. class TopDownPredictEdgeRule(EdgeRule, TopDownPredictRule):
  1485. pass
  1486. class BottomUpEdgeRule(EdgeRule, BottomUpPredictRule):
  1487. pass
  1488. class BottomUpLeftCornerEdgeRule(EdgeRule, BottomUpPredictCombineRule):
  1489. pass
  1490. class FundamentalEdgeRule(EdgeRule, SingleEdgeFundamentalRule):
  1491. pass
  1492. #######################################################################
  1493. # Chart Parser Application
  1494. #######################################################################
  1495. class ChartParserApp(object):
  1496. def __init__(self, grammar, tokens, title="Chart Parser Application"):
  1497. # Initialize the parser
  1498. self._init_parser(grammar, tokens)
  1499. self._root = None
  1500. try:
  1501. # Create the root window.
  1502. self._root = Tk()
  1503. self._root.title(title)
  1504. self._root.bind("<Control-q>", self.destroy)
  1505. # Set up some frames.
  1506. frame3 = Frame(self._root)
  1507. frame2 = Frame(self._root)
  1508. frame1 = Frame(self._root)
  1509. frame3.pack(side="bottom", fill="none")
  1510. frame2.pack(side="bottom", fill="x")
  1511. frame1.pack(side="bottom", fill="both", expand=1)
  1512. self._init_fonts(self._root)
  1513. self._init_animation()
  1514. self._init_chartview(frame1)
  1515. self._init_rulelabel(frame2)
  1516. self._init_buttons(frame3)
  1517. self._init_menubar()
  1518. self._matrix = None
  1519. self._results = None
  1520. # Set up keyboard bindings.
  1521. self._init_bindings()
  1522. except:
  1523. print("Error creating Tree View")
  1524. self.destroy()
  1525. raise
  1526. def destroy(self, *args):
  1527. if self._root is None:
  1528. return
  1529. self._root.destroy()
  1530. self._root = None
  1531. def mainloop(self, *args, **kwargs):
  1532. """
  1533. Enter the Tkinter mainloop. This function must be called if
  1534. this demo is created from a non-interactive program (e.g.
  1535. from a secript); otherwise, the demo will close as soon as
  1536. the script completes.
  1537. """
  1538. if in_idle():
  1539. return
  1540. self._root.mainloop(*args, **kwargs)
  1541. # ////////////////////////////////////////////////////////////
  1542. # Initialization Helpers
  1543. # ////////////////////////////////////////////////////////////
  1544. def _init_parser(self, grammar, tokens):
  1545. self._grammar = grammar
  1546. self._tokens = tokens
  1547. self._reset_parser()
  1548. def _reset_parser(self):
  1549. self._cp = SteppingChartParser(self._grammar)
  1550. self._cp.initialize(self._tokens)
  1551. self._chart = self._cp.chart()
  1552. # Insert LeafEdges before the parsing starts.
  1553. for _new_edge in LeafInitRule().apply(self._chart, self._grammar):
  1554. pass
  1555. # The step iterator -- use this to generate new edges
  1556. self._cpstep = self._cp.step()
  1557. # The currently selected edge
  1558. self._selection = None
  1559. def _init_fonts(self, root):
  1560. # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
  1561. self._sysfont = Font(font=Button()["font"])
  1562. root.option_add("*Font", self._sysfont)
  1563. # TWhat's our font size (default=same as sysfont)
  1564. self._size = IntVar(root)
  1565. self._size.set(self._sysfont.cget("size"))
  1566. self._boldfont = Font(family="helvetica", weight="bold", size=self._size.get())
  1567. self._font = Font(family="helvetica", size=self._size.get())
  1568. def _init_animation(self):
  1569. # Are we stepping? (default=yes)
  1570. self._step = IntVar(self._root)
  1571. self._step.set(1)
  1572. # What's our animation speed (default=fast)
  1573. self._animate = IntVar(self._root)
  1574. self._animate.set(3) # Default speed = fast
  1575. # Are we currently animating?
  1576. self._animating = 0
  1577. def _init_chartview(self, parent):
  1578. self._cv = ChartView(self._chart, parent, draw_tree=1, draw_sentence=1)
  1579. self._cv.add_callback("select", self._click_cv_edge)
  1580. def _init_rulelabel(self, parent):
  1581. ruletxt = "Last edge generated by:"
  1582. self._rulelabel1 = Label(parent, text=ruletxt, font=self._boldfont)
  1583. self._rulelabel2 = Label(
  1584. parent, width=40, relief="groove", anchor="w", font=self._boldfont
  1585. )
  1586. self._rulelabel1.pack(side="left")
  1587. self._rulelabel2.pack(side="left")
  1588. step = Checkbutton(parent, variable=self._step, text="Step")
  1589. step.pack(side="right")
  1590. def _init_buttons(self, parent):
  1591. frame1 = Frame(parent)
  1592. frame2 = Frame(parent)
  1593. frame1.pack(side="bottom", fill="x")
  1594. frame2.pack(side="top", fill="none")
  1595. Button(
  1596. frame1,
  1597. text="Reset\nParser",
  1598. background="#90c0d0",
  1599. foreground="black",
  1600. command=self.reset,
  1601. ).pack(side="right")
  1602. # Button(frame1, text='Pause',
  1603. # background='#90c0d0', foreground='black',
  1604. # command=self.pause).pack(side='left')
  1605. Button(
  1606. frame1,
  1607. text="Top Down\nStrategy",
  1608. background="#90c0d0",
  1609. foreground="black",
  1610. command=self.top_down_strategy,
  1611. ).pack(side="left")
  1612. Button(
  1613. frame1,
  1614. text="Bottom Up\nStrategy",
  1615. background="#90c0d0",
  1616. foreground="black",
  1617. command=self.bottom_up_strategy,
  1618. ).pack(side="left")
  1619. Button(
  1620. frame1,
  1621. text="Bottom Up\nLeft-Corner Strategy",
  1622. background="#90c0d0",
  1623. foreground="black",
  1624. command=self.bottom_up_leftcorner_strategy,
  1625. ).pack(side="left")
  1626. Button(
  1627. frame2,
  1628. text="Top Down Init\nRule",
  1629. background="#90f090",
  1630. foreground="black",
  1631. command=self.top_down_init,
  1632. ).pack(side="left")
  1633. Button(
  1634. frame2,
  1635. text="Top Down Predict\nRule",
  1636. background="#90f090",
  1637. foreground="black",
  1638. command=self.top_down_predict,
  1639. ).pack(side="left")
  1640. Frame(frame2, width=20).pack(side="left")
  1641. Button(
  1642. frame2,
  1643. text="Bottom Up Predict\nRule",
  1644. background="#90f090",
  1645. foreground="black",
  1646. command=self.bottom_up,
  1647. ).pack(side="left")
  1648. Frame(frame2, width=20).pack(side="left")
  1649. Button(
  1650. frame2,
  1651. text="Bottom Up Left-Corner\nPredict Rule",
  1652. background="#90f090",
  1653. foreground="black",
  1654. command=self.bottom_up_leftcorner,
  1655. ).pack(side="left")
  1656. Frame(frame2, width=20).pack(side="left")
  1657. Button(
  1658. frame2,
  1659. text="Fundamental\nRule",
  1660. background="#90f090",
  1661. foreground="black",
  1662. command=self.fundamental,
  1663. ).pack(side="left")
  1664. def _init_bindings(self):
  1665. self._root.bind("<Up>", self._cv.scroll_up)
  1666. self._root.bind("<Down>", self._cv.scroll_down)
  1667. self._root.bind("<Prior>", self._cv.page_up)
  1668. self._root.bind("<Next>", self._cv.page_down)
  1669. self._root.bind("<Control-q>", self.destroy)
  1670. self._root.bind("<Control-x>", self.destroy)
  1671. self._root.bind("<F1>", self.help)
  1672. self._root.bind("<Control-s>", self.save_chart)
  1673. self._root.bind("<Control-o>", self.load_chart)
  1674. self._root.bind("<Control-r>", self.reset)
  1675. self._root.bind("t", self.top_down_strategy)
  1676. self._root.bind("b", self.bottom_up_strategy)
  1677. self._root.bind("c", self.bottom_up_leftcorner_strategy)
  1678. self._root.bind("<space>", self._stop_animation)
  1679. self._root.bind("<Control-g>", self.edit_grammar)
  1680. self._root.bind("<Control-t>", self.edit_sentence)
  1681. # Animation speed control
  1682. self._root.bind("-", lambda e, a=self._animate: a.set(1))
  1683. self._root.bind("=", lambda e, a=self._animate: a.set(2))
  1684. self._root.bind("+", lambda e, a=self._animate: a.set(3))
  1685. # Step control
  1686. self._root.bind("s", lambda e, s=self._step: s.set(not s.get()))
  1687. def _init_menubar(self):
  1688. menubar = Menu(self._root)
  1689. filemenu = Menu(menubar, tearoff=0)
  1690. filemenu.add_command(
  1691. label="Save Chart",
  1692. underline=0,
  1693. command=self.save_chart,
  1694. accelerator="Ctrl-s",
  1695. )
  1696. filemenu.add_command(
  1697. label="Load Chart",
  1698. underline=0,
  1699. command=self.load_chart,
  1700. accelerator="Ctrl-o",
  1701. )
  1702. filemenu.add_command(
  1703. label="Reset Chart", underline=0, command=self.reset, accelerator="Ctrl-r"
  1704. )
  1705. filemenu.add_separator()
  1706. filemenu.add_command(label="Save Grammar", command=self.save_grammar)
  1707. filemenu.add_command(label="Load Grammar", command=self.load_grammar)
  1708. filemenu.add_separator()
  1709. filemenu.add_command(
  1710. label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
  1711. )
  1712. menubar.add_cascade(label="File", underline=0, menu=filemenu)
  1713. editmenu = Menu(menubar, tearoff=0)
  1714. editmenu.add_command(
  1715. label="Edit Grammar",
  1716. underline=5,
  1717. command=self.edit_grammar,
  1718. accelerator="Ctrl-g",
  1719. )
  1720. editmenu.add_command(
  1721. label="Edit Text",
  1722. underline=5,
  1723. command=self.edit_sentence,
  1724. accelerator="Ctrl-t",
  1725. )
  1726. menubar.add_cascade(label="Edit", underline=0, menu=editmenu)
  1727. viewmenu = Menu(menubar, tearoff=0)
  1728. viewmenu.add_command(
  1729. label="Chart Matrix", underline=6, command=self.view_matrix
  1730. )
  1731. viewmenu.add_command(label="Results", underline=0, command=self.view_results)
  1732. menubar.add_cascade(label="View", underline=0, menu=viewmenu)
  1733. rulemenu = Menu(menubar, tearoff=0)
  1734. rulemenu.add_command(
  1735. label="Top Down Strategy",
  1736. underline=0,
  1737. command=self.top_down_strategy,
  1738. accelerator="t",
  1739. )
  1740. rulemenu.add_command(
  1741. label="Bottom Up Strategy",
  1742. underline=0,
  1743. command=self.bottom_up_strategy,
  1744. accelerator="b",
  1745. )
  1746. rulemenu.add_command(
  1747. label="Bottom Up Left-Corner Strategy",
  1748. underline=0,
  1749. command=self.bottom_up_leftcorner_strategy,
  1750. accelerator="c",
  1751. )
  1752. rulemenu.add_separator()
  1753. rulemenu.add_command(label="Bottom Up Rule", command=self.bottom_up)
  1754. rulemenu.add_command(
  1755. label="Bottom Up Left-Corner Rule", command=self.bottom_up_leftcorner
  1756. )
  1757. rulemenu.add_command(label="Top Down Init Rule", command=self.top_down_init)
  1758. rulemenu.add_command(
  1759. label="Top Down Predict Rule", command=self.top_down_predict
  1760. )
  1761. rulemenu.add_command(label="Fundamental Rule", command=self.fundamental)
  1762. menubar.add_cascade(label="Apply", underline=0, menu=rulemenu)
  1763. animatemenu = Menu(menubar, tearoff=0)
  1764. animatemenu.add_checkbutton(
  1765. label="Step", underline=0, variable=self._step, accelerator="s"
  1766. )
  1767. animatemenu.add_separator()
  1768. animatemenu.add_radiobutton(
  1769. label="No Animation", underline=0, variable=self._animate, value=0
  1770. )
  1771. animatemenu.add_radiobutton(
  1772. label="Slow Animation",
  1773. underline=0,
  1774. variable=self._animate,
  1775. value=1,
  1776. accelerator="-",
  1777. )
  1778. animatemenu.add_radiobutton(
  1779. label="Normal Animation",
  1780. underline=0,
  1781. variable=self._animate,
  1782. value=2,
  1783. accelerator="=",
  1784. )
  1785. animatemenu.add_radiobutton(
  1786. label="Fast Animation",
  1787. underline=0,
  1788. variable=self._animate,
  1789. value=3,
  1790. accelerator="+",
  1791. )
  1792. menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)
  1793. zoommenu = Menu(menubar, tearoff=0)
  1794. zoommenu.add_radiobutton(
  1795. label="Tiny",
  1796. variable=self._size,
  1797. underline=0,
  1798. value=10,
  1799. command=self.resize,
  1800. )
  1801. zoommenu.add_radiobutton(
  1802. label="Small",
  1803. variable=self._size,
  1804. underline=0,
  1805. value=12,
  1806. command=self.resize,
  1807. )
  1808. zoommenu.add_radiobutton(
  1809. label="Medium",
  1810. variable=self._size,
  1811. underline=0,
  1812. value=14,
  1813. command=self.resize,
  1814. )
  1815. zoommenu.add_radiobutton(
  1816. label="Large",
  1817. variable=self._size,
  1818. underline=0,
  1819. value=18,
  1820. command=self.resize,
  1821. )
  1822. zoommenu.add_radiobutton(
  1823. label="Huge",
  1824. variable=self._size,
  1825. underline=0,
  1826. value=24,
  1827. command=self.resize,
  1828. )
  1829. menubar.add_cascade(label="Zoom", underline=0, menu=zoommenu)
  1830. helpmenu = Menu(menubar, tearoff=0)
  1831. helpmenu.add_command(label="About", underline=0, command=self.about)
  1832. helpmenu.add_command(
  1833. label="Instructions", underline=0, command=self.help, accelerator="F1"
  1834. )
  1835. menubar.add_cascade(label="Help", underline=0, menu=helpmenu)
  1836. self._root.config(menu=menubar)
  1837. # ////////////////////////////////////////////////////////////
  1838. # Selection Handling
  1839. # ////////////////////////////////////////////////////////////
  1840. def _click_cv_edge(self, edge):
  1841. if edge != self._selection:
  1842. # Clicking on a new edge selects it.
  1843. self._select_edge(edge)
  1844. else:
  1845. # Repeated clicks on one edge cycle its trees.
  1846. self._cv.cycle_tree()
  1847. # [XX] this can get confused if animation is running
  1848. # faster than the callbacks...
  1849. def _select_matrix_edge(self, edge):
  1850. self._select_edge(edge)
  1851. self._cv.view_edge(edge)
  1852. def _select_edge(self, edge):
  1853. self._selection = edge
  1854. # Update the chart view.
  1855. self._cv.markonly_edge(edge, "#f00")
  1856. self._cv.draw_tree(edge)
  1857. # Update the matrix view.
  1858. if self._matrix:
  1859. self._matrix.markonly_edge(edge)
  1860. if self._matrix:
  1861. self._matrix.view_edge(edge)
  1862. def _deselect_edge(self):
  1863. self._selection = None
  1864. # Update the chart view.
  1865. self._cv.unmark_edge()
  1866. self._cv.erase_tree()
  1867. # Update the matrix view
  1868. if self._matrix:
  1869. self._matrix.unmark_edge()
  1870. def _show_new_edge(self, edge):
  1871. self._display_rule(self._cp.current_chartrule())
  1872. # Update the chart view.
  1873. self._cv.update()
  1874. self._cv.draw_tree(edge)
  1875. self._cv.markonly_edge(edge, "#0df")
  1876. self._cv.view_edge(edge)
  1877. # Update the matrix view.
  1878. if self._matrix:
  1879. self._matrix.update()
  1880. if self._matrix:
  1881. self._matrix.markonly_edge(edge)
  1882. if self._matrix:
  1883. self._matrix.view_edge(edge)
  1884. # Update the results view.
  1885. if self._results:
  1886. self._results.update(edge)
  1887. # ////////////////////////////////////////////////////////////
  1888. # Help/usage
  1889. # ////////////////////////////////////////////////////////////
  1890. def help(self, *e):
  1891. self._animating = 0
  1892. # The default font's not very legible; try using 'fixed' instead.
  1893. try:
  1894. ShowText(
  1895. self._root,
  1896. "Help: Chart Parser Application",
  1897. (__doc__ or "").strip(),
  1898. width=75,
  1899. font="fixed",
  1900. )
  1901. except:
  1902. ShowText(
  1903. self._root,
  1904. "Help: Chart Parser Application",
  1905. (__doc__ or "").strip(),
  1906. width=75,
  1907. )
  1908. def about(self, *e):
  1909. ABOUT = "NLTK Chart Parser Application\n" + "Written by Edward Loper"
  1910. showinfo("About: Chart Parser Application", ABOUT)
  1911. # ////////////////////////////////////////////////////////////
  1912. # File Menu
  1913. # ////////////////////////////////////////////////////////////
  1914. CHART_FILE_TYPES = [("Pickle file", ".pickle"), ("All files", "*")]
  1915. GRAMMAR_FILE_TYPES = [
  1916. ("Plaintext grammar file", ".cfg"),
  1917. ("Pickle file", ".pickle"),
  1918. ("All files", "*"),
  1919. ]
  1920. def load_chart(self, *args):
  1921. "Load a chart from a pickle file"
  1922. filename = askopenfilename(
  1923. filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
  1924. )
  1925. if not filename:
  1926. return
  1927. try:
  1928. with open(filename, "rb") as infile:
  1929. chart = pickle.load(infile)
  1930. self._chart = chart
  1931. self._cv.update(chart)
  1932. if self._matrix:
  1933. self._matrix.set_chart(chart)
  1934. if self._matrix:
  1935. self._matrix.deselect_cell()
  1936. if self._results:
  1937. self._results.set_chart(chart)
  1938. self._cp.set_chart(chart)
  1939. except Exception as e:
  1940. raise
  1941. showerror("Error Loading Chart", "Unable to open file: %r" % filename)
  1942. def save_chart(self, *args):
  1943. "Save a chart to a pickle file"
  1944. filename = asksaveasfilename(
  1945. filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
  1946. )
  1947. if not filename:
  1948. return
  1949. try:
  1950. with open(filename, "wb") as outfile:
  1951. pickle.dump(self._chart, outfile)
  1952. except Exception as e:
  1953. raise
  1954. showerror("Error Saving Chart", "Unable to open file: %r" % filename)
  1955. def load_grammar(self, *args):
  1956. "Load a grammar from a pickle file"
  1957. filename = askopenfilename(
  1958. filetypes=self.GRAMMAR_FILE_TYPES, defaultextension=".cfg"
  1959. )
  1960. if not filename:
  1961. return
  1962. try:
  1963. if filename.endswith(".pickle"):
  1964. with open(filename, "rb") as infile:
  1965. grammar = pickle.load(infile)
  1966. else:
  1967. with open(filename, "r") as infile:
  1968. grammar = CFG.fromstring(infile.read())
  1969. self.set_grammar(grammar)
  1970. except Exception as e:
  1971. showerror("Error Loading Grammar", "Unable to open file: %r" % filename)
  1972. def save_grammar(self, *args):
  1973. filename = asksaveasfilename(
  1974. filetypes=self.GRAMMAR_FILE_TYPES, defaultextension=".cfg"
  1975. )
  1976. if not filename:
  1977. return
  1978. try:
  1979. if filename.endswith(".pickle"):
  1980. with open(filename, "wb") as outfile:
  1981. pickle.dump((self._chart, self._tokens), outfile)
  1982. else:
  1983. with open(filename, "w") as outfile:
  1984. prods = self._grammar.productions()
  1985. start = [p for p in prods if p.lhs() == self._grammar.start()]
  1986. rest = [p for p in prods if p.lhs() != self._grammar.start()]
  1987. for prod in start:
  1988. outfile.write("%s\n" % prod)
  1989. for prod in rest:
  1990. outfile.write("%s\n" % prod)
  1991. except Exception as e:
  1992. showerror("Error Saving Grammar", "Unable to open file: %r" % filename)
  1993. def reset(self, *args):
  1994. self._animating = 0
  1995. self._reset_parser()
  1996. self._cv.update(self._chart)
  1997. if self._matrix:
  1998. self._matrix.set_chart(self._chart)
  1999. if self._matrix:
  2000. self._matrix.deselect_cell()
  2001. if self._results:
  2002. self._results.set_chart(self._chart)
  2003. # ////////////////////////////////////////////////////////////
  2004. # Edit
  2005. # ////////////////////////////////////////////////////////////
  2006. def edit_grammar(self, *e):
  2007. CFGEditor(self._root, self._grammar, self.set_grammar)
  2008. def set_grammar(self, grammar):
  2009. self._grammar = grammar
  2010. self._cp.set_grammar(grammar)
  2011. if self._results:
  2012. self._results.set_grammar(grammar)
  2013. def edit_sentence(self, *e):
  2014. sentence = " ".join(self._tokens)
  2015. title = "Edit Text"
  2016. instr = "Enter a new sentence to parse."
  2017. EntryDialog(self._root, sentence, instr, self.set_sentence, title)
  2018. def set_sentence(self, sentence):
  2019. self._tokens = list(sentence.split())
  2020. self.reset()
  2021. # ////////////////////////////////////////////////////////////
  2022. # View Menu
  2023. # ////////////////////////////////////////////////////////////
  2024. def view_matrix(self, *e):
  2025. if self._matrix is not None:
  2026. self._matrix.destroy()
  2027. self._matrix = ChartMatrixView(self._root, self._chart)
  2028. self._matrix.add_callback("select", self._select_matrix_edge)
  2029. def view_results(self, *e):
  2030. if self._results is not None:
  2031. self._results.destroy()
  2032. self._results = ChartResultsView(self._root, self._chart, self._grammar)
  2033. # ////////////////////////////////////////////////////////////
  2034. # Zoom Menu
  2035. # ////////////////////////////////////////////////////////////
  2036. def resize(self):
  2037. self._animating = 0
  2038. self.set_font_size(self._size.get())
  2039. def set_font_size(self, size):
  2040. self._cv.set_font_size(size)
  2041. self._font.configure(size=-abs(size))
  2042. self._boldfont.configure(size=-abs(size))
  2043. self._sysfont.configure(size=-abs(size))
  2044. def get_font_size(self):
  2045. return abs(self._size.get())
  2046. # ////////////////////////////////////////////////////////////
  2047. # Parsing
  2048. # ////////////////////////////////////////////////////////////
  2049. def apply_strategy(self, strategy, edge_strategy=None):
  2050. # If we're animating, then stop.
  2051. if self._animating:
  2052. self._animating = 0
  2053. return
  2054. # Clear the rule display & mark.
  2055. self._display_rule(None)
  2056. # self._cv.unmark_edge()
  2057. if self._step.get():
  2058. selection = self._selection
  2059. if (selection is not None) and (edge_strategy is not None):
  2060. # Apply the given strategy to the selected edge.
  2061. self._cp.set_strategy([edge_strategy(selection)])
  2062. newedge = self._apply_strategy()
  2063. # If it failed, then clear the selection.
  2064. if newedge is None:
  2065. self._cv.unmark_edge()
  2066. self._selection = None
  2067. else:
  2068. self._cp.set_strategy(strategy)
  2069. self._apply_strategy()
  2070. else:
  2071. self._cp.set_strategy(strategy)
  2072. if self._animate.get():
  2073. self._animating = 1
  2074. self._animate_strategy()
  2075. else:
  2076. for edge in self._cpstep:
  2077. if edge is None:
  2078. break
  2079. self._cv.update()
  2080. if self._matrix:
  2081. self._matrix.update()
  2082. if self._results:
  2083. self._results.update()
  2084. def _stop_animation(self, *e):
  2085. self._animating = 0
  2086. def _animate_strategy(self, speed=1):
  2087. if self._animating == 0:
  2088. return
  2089. if self._apply_strategy() is not None:
  2090. if self._animate.get() == 0 or self._step.get() == 1:
  2091. return
  2092. if self._animate.get() == 1:
  2093. self._root.after(3000, self._animate_strategy)
  2094. elif self._animate.get() == 2:
  2095. self._root.after(1000, self._animate_strategy)
  2096. else:
  2097. self._root.after(20, self._animate_strategy)
  2098. def _apply_strategy(self):
  2099. new_edge = next(self._cpstep)
  2100. if new_edge is not None:
  2101. self._show_new_edge(new_edge)
  2102. return new_edge
  2103. def _display_rule(self, rule):
  2104. if rule is None:
  2105. self._rulelabel2["text"] = ""
  2106. else:
  2107. name = str(rule)
  2108. self._rulelabel2["text"] = name
  2109. size = self._cv.get_font_size()
  2110. # ////////////////////////////////////////////////////////////
  2111. # Parsing Strategies
  2112. # ////////////////////////////////////////////////////////////
  2113. # Basic rules:
  2114. _TD_INIT = [TopDownInitRule()]
  2115. _TD_PREDICT = [TopDownPredictRule()]
  2116. _BU_RULE = [BottomUpPredictRule()]
  2117. _BU_LC_RULE = [BottomUpPredictCombineRule()]
  2118. _FUNDAMENTAL = [SingleEdgeFundamentalRule()]
  2119. # Complete strategies:
  2120. _TD_STRATEGY = _TD_INIT + _TD_PREDICT + _FUNDAMENTAL
  2121. _BU_STRATEGY = _BU_RULE + _FUNDAMENTAL
  2122. _BU_LC_STRATEGY = _BU_LC_RULE + _FUNDAMENTAL
  2123. # Button callback functions:
  2124. def top_down_init(self, *e):
  2125. self.apply_strategy(self._TD_INIT, None)
  2126. def top_down_predict(self, *e):
  2127. self.apply_strategy(self._TD_PREDICT, TopDownPredictEdgeRule)
  2128. def bottom_up(self, *e):
  2129. self.apply_strategy(self._BU_RULE, BottomUpEdgeRule)
  2130. def bottom_up_leftcorner(self, *e):
  2131. self.apply_strategy(self._BU_LC_RULE, BottomUpLeftCornerEdgeRule)
  2132. def fundamental(self, *e):
  2133. self.apply_strategy(self._FUNDAMENTAL, FundamentalEdgeRule)
  2134. def bottom_up_strategy(self, *e):
  2135. self.apply_strategy(self._BU_STRATEGY, BottomUpEdgeRule)
  2136. def bottom_up_leftcorner_strategy(self, *e):
  2137. self.apply_strategy(self._BU_LC_STRATEGY, BottomUpLeftCornerEdgeRule)
  2138. def top_down_strategy(self, *e):
  2139. self.apply_strategy(self._TD_STRATEGY, TopDownPredictEdgeRule)
  2140. def app():
  2141. grammar = CFG.fromstring(
  2142. """
  2143. # Grammatical productions.
  2144. S -> NP VP
  2145. VP -> VP PP | V NP | V
  2146. NP -> Det N | NP PP
  2147. PP -> P NP
  2148. # Lexical productions.
  2149. NP -> 'John' | 'I'
  2150. Det -> 'the' | 'my' | 'a'
  2151. N -> 'dog' | 'cookie' | 'table' | 'cake' | 'fork'
  2152. V -> 'ate' | 'saw'
  2153. P -> 'on' | 'under' | 'with'
  2154. """
  2155. )
  2156. sent = "John ate the cake on the table with a fork"
  2157. sent = "John ate the cake on the table"
  2158. tokens = list(sent.split())
  2159. print("grammar= (")
  2160. for rule in grammar.productions():
  2161. print((" ", repr(rule) + ","))
  2162. print(")")
  2163. print(("tokens = %r" % tokens))
  2164. print('Calling "ChartParserApp(grammar, tokens)"...')
  2165. ChartParserApp(grammar, tokens).mainloop()
  2166. if __name__ == "__main__":
  2167. app()
  2168. # Chart comparer:
  2169. # charts = ['/tmp/earley.pickle',
  2170. # '/tmp/topdown.pickle',
  2171. # '/tmp/bottomup.pickle']
  2172. # ChartComparer(*charts).mainloop()
  2173. # import profile
  2174. # profile.run('demo2()', '/tmp/profile.out')
  2175. # import pstats
  2176. # p = pstats.Stats('/tmp/profile.out')
  2177. # p.strip_dirs().sort_stats('time', 'cum').print_stats(60)
  2178. # p.strip_dirs().sort_stats('cum', 'time').print_stats(60)
  2179. __all__ = ["app"]