PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lexiconator-client/src/pywn/pywnshow.py

https://github.com/balajeerc/lexiconator
Python | 174 lines | 150 code | 23 blank | 1 comment | 17 complexity | a3f755b485b2844b8b570e5a5f50dcd5 MD5 | raw file
  1. #! /usr/bin/env python
  2. from Tkinter import *
  3. from string import atoi
  4. class SearchFrame(Frame):
  5. def event(self, event):
  6. self.formSelected()
  7. def formSelected(self):
  8. form = self.formEntry.get()
  9. if not len(form):
  10. return
  11. syns = impl.lookupSynsetsByFormAndPOS(form, self.pos)
  12. self.display.type = None
  13. self.display.setSynsets(syns)
  14. pos = None
  15. def setPOSAny(self):
  16. self.pos = None;
  17. self.posMenu.config(text='Any')
  18. def setPOSNoun(self):
  19. self.pos = 'n';
  20. self.posMenu.config(text='Noun')
  21. def setPOSVerb(self):
  22. self.pos = 'v';
  23. self.posMenu.config(text='Verb')
  24. def setPOSAdj(self):
  25. self.pos = 'a';
  26. self.posMenu.config(text='Adj')
  27. def setPOSAdv(self):
  28. self.pos = 'r';
  29. self.posMenu.config(text='Adv')
  30. def createPOSMenu(self):
  31. posMenuButton = Menubutton(self, text='POS', underline=0)
  32. posMenuButton.menu = Menu(posMenuButton)
  33. menu = posMenuButton.menu
  34. menu.add_command(label='Any', command=self.setPOSAny)
  35. menu.add_command(label='Noun', command=self.setPOSNoun)
  36. menu.add_command(label='Verb', command=self.setPOSVerb)
  37. menu.add_command(label='Adj', command=self.setPOSAdj)
  38. menu.add_command(label='Adv', command=self.setPOSAdv)
  39. posMenuButton['menu'] = menu
  40. return posMenuButton
  41. def createWidgets(self):
  42. Label(self, text='Form:').pack(side=LEFT)
  43. self.formEntry = Entry(self)
  44. self.formEntry.bind('<Key-Return>', self.event)
  45. self.formEntry.pack(side=LEFT)
  46. self.posMenu = self.createPOSMenu()
  47. self.posMenu.pack(side=LEFT)
  48. self.formButton = Button(self, text='Search',
  49. command=self.formSelected)
  50. self.formButton.pack(side=RIGHT)
  51. def __init__(self, master):
  52. Frame.__init__(self, master)
  53. Pack.config(self)
  54. self.createWidgets()
  55. class DisplayFrame(Frame):
  56. def setSynsets(self, syns):
  57. self.syns = syns
  58. self.ssIndex = -1
  59. self.next()
  60. def prev(self):
  61. if len(self.syns):
  62. self.ssIndex -= 1
  63. self.ssLabel.config(text=self.syns[self.ssIndex])
  64. self.displayRelations(self.syns[self.ssIndex])
  65. if self.ssIndex >= len(self.syns) - 1:
  66. self.nextButton.config(state=DISABLED)
  67. else:
  68. self.nextButton.config(state=ACTIVE)
  69. if self.ssIndex == 0:
  70. self.prevButton.config(state=DISABLED)
  71. else:
  72. self.prevButton.config(state=ACTIVE)
  73. else:
  74. self.nextButton.config(state=DISABLED)
  75. self.prevButton.config(state=DISABLED)
  76. def next(self):
  77. if len(self.syns):
  78. self.ssIndex += 1
  79. self.ssLabel.config(text=self.syns[self.ssIndex])
  80. self.displayRelations(self.syns[self.ssIndex])
  81. if self.ssIndex >= len(self.syns) - 1:
  82. self.nextButton.config(state=DISABLED)
  83. else:
  84. self.nextButton.config(state=ACTIVE)
  85. if self.ssIndex == 0:
  86. self.prevButton.config(state=DISABLED)
  87. else:
  88. self.prevButton.config(state=ACTIVE)
  89. else:
  90. self.nextButton.config(state=DISABLED)
  91. self.prevButton.config(state=DISABLED)
  92. def displayRelations(self, ss):
  93. self.relList.delete(0, END)
  94. relations = ss.relations()
  95. for i in range(len(relations)):
  96. rel = relations[i]
  97. type = self.type
  98. if not type:
  99. type = ss.rels[i][0]
  100. self.relList.insert(END, type+' '+repr(rel))
  101. def relSelected(self, event):
  102. items = self.relList.curselection()
  103. if len(items):
  104. index = atoi(items[0])
  105. relations = self.syns[self.ssIndex].relations(self.type)
  106. self.setSynsets([relations[index]])
  107. def createWidgets(self):
  108. showframe = Frame(self)
  109. Pack.config(showframe)
  110. showframe.pack(side=TOP, anchor="w")
  111. self.prevButton = Button(showframe, text='Prev', state=DISABLED,
  112. anchor="n", command=self.prev)
  113. self.prevButton.pack(side=LEFT, anchor="w")
  114. self.nextButton = Button(showframe, text='Next', state=DISABLED,
  115. anchor="n", command=self.next)
  116. self.nextButton.pack(side=LEFT, anchor="w")
  117. self.ssLabel = Label(showframe, anchor="nw", justify=LEFT, width=60,
  118. wraplength=420, height=4)
  119. self.ssLabel.pack(side=LEFT)
  120. scroll = Scrollbar(self)
  121. scroll.pack(side=RIGHT, fill=Y)
  122. self.relList = Listbox(self, width = 80, height = 15,
  123. yscrollcommand=scroll.set)
  124. self.relList.bind('<Key-Return>', self.relSelected)
  125. self.relList.bind('<Double-Button-1>', self.relSelected)
  126. self.relList.pack(side=BOTTOM)
  127. scroll.config(command=self.relList.yview)
  128. def __init__(self, master):
  129. self.type = None
  130. Frame.__init__(self, master)
  131. Pack.config(self)
  132. self.createWidgets()
  133. class PywnShowFrame(Frame):
  134. def __init__(self, master=None):
  135. Frame.__init__(self, master)
  136. Pack.config(self)
  137. self.search = SearchFrame(self)
  138. self.search.pack(side=TOP, anchor="w")
  139. self.display = DisplayFrame(self)
  140. self.display.pack(side=BOTTOM, fill=BOTH)
  141. self.search.display = self.display
  142. from stdwn import impl
  143. from stdwn import close
  144. if __name__ == '__main__':
  145. import sys
  146. impl.open()#sys.argv[1],int(sys.argv[2]))
  147. root = Tk()
  148. pywnframe = PywnShowFrame(root)
  149. root.title('PywnShow - WordNet')
  150. root.mainloop()
  151. close()