1
2
3
4
5
6
7
8
9
10
11 """
12 Wordnet Interface: Graphical Wordnet Browser
13 """
14
15 import os
16 import os.path
17 import sys
18 from time import sleep
19 import pickle
20 import platform
21 from urllib import quote_plus, unquote_plus
22 from itertools import groupby
23
24 import wx
25 import wx.html as html
26 import wx.lib.wxpTag
27
28 import util
29
30 __all__ = ['demo']
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 frame_title = 'NLTK Wordnet Browser'
46 help_about = frame_title + __doc__
47
48
49 options_dict = {}
50
51 try:
52 nltk_prefs_dir = os.path.join(os.environ['HOME'], ".nltk")
53 except KeyError:
54
55 nltk_prefs_dir = os.path.curdir
56 pickle_file_name = os.path.join(nltk_prefs_dir,
57 (frame_title + os.path.extsep + 'pkl'))
58
59
60
61
62 WORDNET_DB_INFO_FILEPATH = 'NLTK Wordnet Browser Database Info.html'
63
64
65
74
101
105
109
113
115 linkinfo = cell.GetLink()
116 if linkinfo is not None:
117 pass
118 else:
119 pass
120 super(MyHtmlWindow, self).OnCellClicked(cell, x, y, evt)
121
123 global options_dict
124 page_to_restore = self.GetParser().GetSource()
125 if change:
126 self.font_size += change
127 if self.font_size <= 0: self.font_size = 1
128 else:
129 self.font_size = self.normal_font_size
130 options_dict['font_size'] = self.font_size
131 self.SetStandardFonts(size=self.font_size)
132 self.SetPage(page_to_restore)
133
135 msg1 = '*'*8 + ' ' + msg + ' ' + '*'*8
136 msg2 = '*'*100
137 for i in range(5):
138 for msg in [msg1, msg2]:
139 self.parent.parent.statusbar.SetStatusText(msg)
140 wx.Yield()
141 sleep(0.2)
142 self.parent.parent.statusbar.SetStatusText(' ')
143 wx.Yield()
144
145 - def show_page(self, page):
147
148
149
150 -class NB(wx.Notebook):
159
160 - def OnPageChanged(self, event):
161 old = event.GetOldSelection()
162 new = event.GetSelection()
163 sel = self.GetSelection()
164 if old != new:
165 self.switch_html_page(new)
166 event.Skip()
167
168
169 - def OnPageChanging(self, event):
170 old = event.GetOldSelection()
171 new = event.GetSelection()
172 sel = self.GetSelection()
173 event.Skip()
174
175 - def switch_html_page(self, new_page):
176 self.current_page = new_page
177 self.ChangeSelection(new_page)
178 self.h_w = self.GetPage(new_page)
179 self.parent.prev_btn.Enable(self.h_w.prev_wp_list != [])
180 self.parent.next_btn.Enable(self.h_w.next_wp_list != [])
181 self.parent.search_word.SetValue(self.h_w.current_word)
182
183 - def add_html_page(self, tab_text='', activate=True):
184 h_w = MyHtmlWindow(self, -1)
185 if 'gtk2' in wx.PlatformInfo:
186 h_w.SetStandardFonts()
187 h_w.SetRelatedFrame(self.parent.frame,
188 self.parent.titleBase + ' -- %s')
189 h_w.SetRelatedStatusBar(0)
190 h_w.current_word = ''
191
192 h_w.prev_wp_list = []
193 h_w.next_wp_list = []
194 self.AddPage(h_w, tab_text, activate)
195 if activate:
196 self.current_page = self.GetSelection()
197 self.h_w = h_w
198 if self.h_w.prev_wp_list == []:
199 self.parent.prev_btn.Enable(False)
200 if self.h_w.next_wp_list == []:
201 self.parent.next_btn.Enable(False)
202 return self.current_page
203
204
207 wx.Panel.__init__(self, frame, -1, style=wx.NO_FULL_REPAINT_ON_RESIZE)
208 self.frame = frame
209
210 self.cwd = os.path.split(sys.argv[0])[0]
211
212 if not self.cwd:
213 self.cwd = os.getcwd()
214 if frame:
215 self.titleBase = frame.GetTitle()
216
217 self.statusbar = self.frame.CreateStatusBar()
218
219 self.printer = html.HtmlEasyPrinting(frame_title)
220
221 self.box = wx.BoxSizer(wx.VERTICAL)
222
223 subbox_1 = wx.BoxSizer(wx.HORIZONTAL)
224
225 self.prev_btn = wx.Button(self, -1, 'Previous Page')
226 self.Bind(wx.EVT_BUTTON, self.on_prev_page, self.prev_btn)
227 subbox_1.Add(self.prev_btn, 5, wx.GROW | wx.ALL, 2)
228
229 self.next_btn = wx.Button(self, -1, 'Next Page')
230 self.Bind(wx.EVT_BUTTON, self.on_next_page, self.next_btn)
231 subbox_1.Add(self.next_btn, 5, wx.GROW | wx.ALL, 2)
232
233 btn = wx.Button(self, -1, 'Help')
234 self.Bind(wx.EVT_BUTTON, self.on_help, btn)
235 subbox_1.Add(btn, 5, wx.GROW | wx.ALL, 2)
236
237 btn = wx.Button(self, -1, 'Search the word(s)')
238 self.Bind(wx.EVT_BUTTON, self.on_word_enter, btn)
239 subbox_1.Add(btn, 5, wx.GROW | wx.ALL, 2)
240
241 lbl = wx.StaticText(self, -1, 'Word(s): ',
242 style=wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM)
243 subbox_1.Add(lbl, 5, wx.GROW | wx.ALL, 2)
244
245 self.search_word = wx.TextCtrl(self, -1, '', style=wx.TE_PROCESS_ENTER)
246 self.Bind(wx.EVT_TEXT_ENTER, self.on_word_enter, self.search_word)
247 self.search_word.Bind(wx.EVT_LEFT_UP, self.on_mouse_up)
248 subbox_1.Add(self.search_word, 20, wx.GROW | wx.ALL, 2)
249
250 self.box.Add(subbox_1, 0, wx.GROW)
251 self.nb = NB(self)
252 self.box.Add(self.nb, 1, wx.GROW)
253
254 self.SetSizer(self.box)
255 self.SetAutoLayout(True)
256
259
262
265
267 self.search_word.SetSelection(-1, -1)
268 event.Skip()
269
270 - def on_prev_page(self, event):
271 if self.nb.h_w.prev_wp_list:
272
273 page = self.nb.h_w.GetParser().GetSource()
274 x,y = self.nb.h_w.GetViewStart()
275 entry = (self.nb.h_w.current_word,page,(x,y))
276 self.nb.h_w.next_wp_list = [entry] + self.nb.h_w.next_wp_list
277 self.next_btn.Enable(True)
278
279 word,page,(x,y) = self.nb.h_w.prev_wp_list[-1]
280 self.nb.h_w.prev_wp_list = self.nb.h_w.prev_wp_list[:-1]
281 if self.nb.h_w.prev_wp_list == []:
282 self.prev_btn.Enable(False)
283 self.nb.h_w.current_word = word
284 self.nb.SetPageText(self.nb.current_page, word)
285 self.search_word.SetValue(word)
286 self.nb.h_w.SetPage(page)
287 self.nb.h_w.Scroll(x, y)
288
289 - def on_next_page(self, event):
290 if self.nb.h_w.next_wp_list:
291
292 page = self.nb.h_w.GetParser().GetSource()
293 x,y = self.nb.h_w.GetViewStart()
294 entry = (self.nb.h_w.current_word,page,(x,y))
295 self.nb.h_w.prev_wp_list.append(entry)
296 self.prev_btn.Enable(True)
297
298 word,page,(x,y) = self.nb.h_w.next_wp_list[0]
299 self.nb.h_w.next_wp_list = self.nb.h_w.next_wp_list[1:]
300 if self.nb.h_w.next_wp_list == []:
301 self.next_btn.Enable(False)
302 self.nb.h_w.current_word = word
303 self.nb.SetPageText(self.nb.current_page, word)
304 self.search_word.SetValue(word)
305 self.nb.h_w.SetPage(page)
306 self.nb.h_w.Scroll(x, y)
307
310
312 word = self.search_word.GetValue()
313 if word.isalnum(): return
314 word_2 = ''.join([x for x in word if
315 x.isalnum() or x == ' ' or x == '-'])
316 self.search_word.SetValue(word_2)
317 event.Skip()
318
333
334 - def show_page_and_word(self, page, word=None):
335 if self.nb.h_w.current_word:
336
337 curr_page = self.nb.h_w.GetParser().GetSource()
338 x,y = self.nb.h_w.GetViewStart()
339 entry = (self.nb.h_w.current_word,curr_page,(x,y))
340 self.nb.h_w.prev_wp_list.append(entry)
341 self.prev_btn.Enable(True)
342
343 self.nb.h_w.next_wp_list = []
344 self.next_btn.Enable(False)
345 if not word: x,y = self.nb.h_w.GetViewStart()
346 self.nb.h_w.SetPage(page)
347 if not word: self.nb.h_w.Scroll(x, y)
348 if word:
349 self.search_word.SetValue(word)
350 self.nb.h_w.current_word = word
351
352
355 wx.Frame.__init__(self, parent, -1, title)
356
357 menu_bar = wx.MenuBar()
358
359 menu_1 = wx.Menu()
360 o_f = menu_1.Append(-1, 'Open File...\tCtrl+O')
361 s_a = menu_1.Append(-1, 'Save Page As...\tCtrl+S')
362 menu_1.AppendSeparator()
363 print_ = menu_1.Append(-1, 'Print...\tCtrl+P')
364 preview = menu_1.Append(-1, 'Preview')
365 menu_1.AppendSeparator()
366 ex = menu_1.Append(-1, 'Exit')
367 menu_bar.Append(menu_1, '&File')
368
369 menu_1_2 = wx.Menu()
370 nt = menu_1_2.Append(-1, 'New tabsheet\tCtrl+T')
371 ct = menu_1_2.Append(-1, 'Close tabsheet\tCtrl+W')
372 menu_1_2.AppendSeparator()
373 ssw_nt = menu_1_2.Append(-1,
374 'Show search word in new tabsheet\tAlt+Enter')
375 menu_bar.Append(menu_1_2, '&Tabsheets')
376
377 menu_2 = wx.Menu()
378 prev_p = menu_2.Append(-1, 'Previous\tCtrl+Left Arrow')
379 next_p = menu_2.Append(-1, 'Next\tCtrl+Right Arrow')
380 menu_bar.Append(menu_2, '&Page History')
381
382 menu_3 = wx.Menu()
383 i_f = menu_3.Append(-1,
384 'Increase Font Size\tCtrl++ or Ctrl+Numpad+ or Ctrl+UpArrow')
385 d_f = menu_3.Append(-1,
386 'Decrease Font Size\tCtrl+- or Ctrl+Numpad- or Ctrl+DownArrow')
387 n_f = menu_3.Append(-1, 'Normal Font Size\tCtrl+0')
388 menu_3.AppendSeparator()
389
390
391
392 s_s = menu_3.Append(-1, 'Show HTML Source\tCtrl+U')
393 menu_bar.Append(menu_3, '&View')
394
395 menu_4 = wx.Menu()
396 h_h = menu_4.Append(-1, 'Help')
397 h_a = menu_4.Append(-1, 'About...')
398 menu_bar.Append(menu_4, '&Help')
399
400 self.SetMenuBar(menu_bar)
401
402 self.Bind(wx.EVT_MENU, self.on_open_file, o_f)
403 self.Bind(wx.EVT_MENU, self.on_save_as, s_a)
404 self.Bind(wx.EVT_MENU, self.on_print, print_)
405 self.Bind(wx.EVT_MENU, self.on_preview, preview)
406 self.Bind(wx.EVT_MENU, self.on_exit, ex)
407 self.Bind(wx.EVT_MENU, self.on_new_tab, nt)
408 self.Bind(wx.EVT_MENU, self.on_close_tab, ct)
409 self.Bind(wx.EVT_MENU, self.on_ssw_nt, ssw_nt)
410 self.Bind(wx.EVT_MENU, self.on_prev_page, prev_p)
411 self.Bind(wx.EVT_MENU, self.on_next_page, next_p)
412 self.Bind(wx.EVT_MENU, self.on_incr_font, i_f)
413 self.Bind(wx.EVT_MENU, self.on_decr_font, d_f)
414 self.Bind(wx.EVT_MENU, self.on_norm_font, n_f)
415
416
417 self.Bind(wx.EVT_MENU, self.on_show_source, s_s)
418 self.Bind(wx.EVT_MENU, self.on_help_help, h_h)
419 self.Bind(wx.EVT_MENU, self.on_help_about, h_a)
420
421 acceltbl = wx.AcceleratorTable([
422 (wx.ACCEL_CTRL,ord('O'),o_f.GetId()),
423 (wx.ACCEL_CTRL,ord('S'),s_a.GetId()),
424 (wx.ACCEL_CTRL,ord('P'),print_.GetId()),
425 (wx.ACCEL_CTRL,wx.WXK_ADD,i_f.GetId()),
426 (wx.ACCEL_CTRL,wx.WXK_NUMPAD_ADD,i_f.GetId()),
427 (wx.ACCEL_CTRL,wx.WXK_UP,i_f.GetId()),
428 (wx.ACCEL_CTRL,wx.WXK_SUBTRACT,d_f.GetId()),
429 (wx.ACCEL_CTRL,wx.WXK_NUMPAD_SUBTRACT,d_f.GetId()),
430 (wx.ACCEL_CTRL,wx.WXK_DOWN,d_f.GetId()),
431 (wx.ACCEL_CTRL,ord('0'),n_f.GetId()),
432 (wx.ACCEL_CTRL,ord('U'),s_s.GetId()),
433 (wx.ACCEL_ALT,wx.WXK_LEFT,prev_p.GetId()),
434 (wx.ACCEL_CTRL,wx.WXK_LEFT,prev_p.GetId()),
435 (wx.ACCEL_ALT,wx.WXK_RIGHT,next_p.GetId()),
436 (wx.ACCEL_CTRL,wx.WXK_RIGHT,next_p.GetId()),
437 (wx.ACCEL_ALT,wx.WXK_RETURN,ssw_nt.GetId()),
438 ])
439
440 self.SetAcceleratorTable(acceltbl)
441
442 self.Bind(wx.EVT_SIZE, self.on_frame_resize)
443 self.Bind(wx.EVT_MOVE, self.on_frame_move)
444 self.Bind(wx.EVT_CLOSE, self.on_frame_close)
445
446 self.panel = HtmlPanel(self)
447
448
451
454
468
471
474
477
480
483
486
489
492
495
497 self.panel.nb.DeletePage(self.panel.nb.current_page)
498
501
504
516
517 - def on_prev_page(self, event):
518 self.panel.on_prev_page(event)
519
520 - def on_next_page(self, event):
521 self.panel.on_next_page(event)
522
525
528
531
534
537
540
543
560
561
563 try:
564 if not path.endswith('.htm') and not path.endswith('.html'):
565 path += '.html'
566 page = util.get_static_page_by_path(path)
567 if path == 'NLTK Wordnet Browser Help.html':
568 word = '* Help *'
569 else:
570 txt = '<title>' + frame_title + ' display of: '
571 ind_0 = page.find(txt)
572 if ind_0 == -1:
573 err_mess = 'This file is not in NLTK Browser format!'
574 self.panel.nb.h_w.show_msg(err_mess)
575 return
576 ind_1 = page.find('of: ') + len('of: ')
577 ind_2 = page.find('</title>')
578 word = page[ind_1:ind_2]
579 page = page[:ind_0] + page[ind_2+len('</title>'):]
580 current_page = self.panel.nb.add_html_page()
581 self.panel.nb.SetPageText(current_page,word)
582 self.panel.show_page_and_word(page, word)
583 return current_page
584 except:
585 excpt = str(sys.exc_info())
586 self.panel.nb.h_w.show_msg('Unexpected error; File: ' + \
587 path + ' ; ' + excpt)
588
590 dlg = wx.FileDialog(self, wildcard = '*.htm*',
591 style=wx.OPEN|wx.CHANGE_DIR)
592 if dlg.ShowModal():
593 path = dlg.GetPath()
594 if path == '': return
595 self.read_file(path)
596 dlg.Destroy()
597
599 dlg = wx.FileDialog(self, wildcard='*.html',
600 style=wx.SAVE|wx.CHANGE_DIR|wx.OVERWRITE_PROMPT)
601 if dlg.ShowModal():
602 path = dlg.GetPath()
603 if path == '':
604 self.panel.nb.h_w.show_msg('Empty Filename!')
605 return
606 source = self.panel.nb.h_w.GetParser().GetSource()
607 try:
608 if not path.endswith('.htm') and not path.endswith('.html'):
609 path += '.html'
610 f = open(path, 'w')
611 f.write(source)
612 f.close()
613 except:
614 excpt = str(sys.exc_info())
615 self.panel.nb.h_w.show_msg('Unexpected error; File: ' + \
616 path + ' ; ' + excpt)
617 dlg.Destroy()
618
620 dlg = wx.TextEntryDialog(self, 'Enter the URL')
621 if dlg.ShowModal():
622 url = dlg.GetValue()
623 self.panel.nb.h_w.LoadPage(url)
624 dlg.Destroy()
625
627 import wx.lib.dialogs
628 source = self.panel.nb.h_w.GetParser().GetSource()
629 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, source,
630 'HTML Source', size=(1000, 800))
631 dlg.ShowModal()
632 dlg.Destroy()
633
635 self.panel.printer.GetPrintData().SetFilename('unnamed')
636 html = self.panel.nb.h_w.GetParser().GetSource()
637 self.panel.printer.PrintText(html)
638
640 html = self.panel.nb.h_w.GetParser().GetSource()
641 self.panel.printer.PreviewText(html)
642
657
659
660
661
662 max_width,max_height = frame.GetSize()
663 x,y = frame.GetPosition()
664
665
666
667
668
669
670 if x < 0:
671 max_width += 2 * x
672 x = 0
673 if y < 0:
674 max_height += 2 * y
675 y = 0
676
677 if options_dict['frame_pos'] == (-1,-1):
678 return
679
680 x,y = options_dict['frame_pos']
681 width,height = options_dict['frame_size']
682 if x < 0:
683 width += x
684 x = 0
685 if y < 0:
686 height += y
687 y = 0
688 width = min(width, max_width)
689 height = min(height, max_height)
690 if x + width > max_width:
691 x -= x + width - max_width
692 if x < 0:
693 width += x
694 width = min(width, max_width)
695 x = 0
696 if y + height > max_height:
697 y -= y + height - max_height
698 if y < 0:
699 height += y
700 height = min(height, max_height)
701 y = 0
702 frame.Maximize(False)
703 frame.SetSize((width,height))
704 frame.SetPosition((x,y))
705 frame.Iconize(False)
706
730
731
732 if __name__ == '__main__':
733 demo()
734