1 # -*- coding: utf-8 -*-
3 #----------------------------------------------------------------------------
4 # Author: Pierre Ratinaud
7 #comes from ListCtrl.py from the demo tool of wxPython:
8 # Author: Robin Dunn & Gary Dumer
11 # Copyright: (c) 1998 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------------
18 import wx.lib.mixins.listctrl as listmix
20 from functions import exec_rcode, doconcorde
21 from chemins import ffr
22 from PrintRScript import barplot
23 from dialog import SearchDial, message, BarGraphDialog, BarFrame
24 from operator import itemgetter
25 #---------------------------------------------------------------------------
27 class ListForSpec(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
28 def __init__(self, parent,gparent, dlist = {}, first = [], usefirst = False, menu = True):
29 #def __init__(self, parent) :
30 wx.ListCtrl.__init__( self, parent, -1, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
37 if 'etoiles' in dir(self.gparent) and not usefirst :
38 self.etoiles = self.gparent.etoiles
41 for val in self.first :
42 if val.startswith(u'X.') :
43 val = val.replace(u'X.', u'*')
44 self.etoiles.append(val)
48 search_id = wx.NewId()
49 self.parent.Bind(wx.EVT_MENU, self.onsearch, id = search_id)
50 self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('F'), search_id)])
51 self.SetAcceleratorTable(self.accel_tbl)
53 self.il = wx.ImageList(16, 16)
54 a={"sm_up":"GO_UP","sm_dn":"GO_DOWN","w_idx":"WARNING","e_idx":"ERROR","i_idx":"QUESTION"}
56 s="self.%s= self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_%s,wx.ART_TOOLBAR,(16,16)))" % (k,v)
58 self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
62 self.attr1 = wx.ListItemAttr()
63 self.attr1.SetBackgroundColour((230, 230, 230))
64 self.attr2 = wx.ListItemAttr()
65 self.attr2.SetBackgroundColour("light blue")
66 self.attrselected = wx.ListItemAttr()
67 self.attrselected.SetBackgroundColour("red")
71 for name in [u'formes'] + self.etoiles :
72 self.InsertColumn(i,name,wx.LIST_FORMAT_LEFT)
75 self.SetColumnWidth(0, 180)
77 for i in range(0,len(self.etoiles)):
78 self.SetColumnWidth(i + 1, self.checkcolumnwidth(len(self.etoiles[i]) * 10))
80 self.itemDataMap = self.dlist
81 self.itemIndexMap = self.dlist.keys()
82 self.SetItemCount(len(self.dlist))
84 #listmix.ListCtrlAutoWidthMixin.__init__(self)
85 listmix.ColumnSorterMixin.__init__(self, len(self.first) + 1)
86 self.SortListItems(1, 0)
88 #-----------------------------------------------------------------------------------------
89 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self)
91 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED , self.OnPopupTwo, self)
93 self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
96 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
98 #-----------------------------------------------------------------------------------------
99 def RefreshData(self, data):
100 self.itemDataMap = data
101 self.itemIndexMap = data.keys()
102 self.SetItemCount(len(data))
105 def checkcolumnwidth(self, width) :
111 def OnGetItemText(self, item, col):
112 index=self.itemIndexMap[item]
113 s = self.itemDataMap[index][col]
116 def OnGetItemAttr(self, item):
117 if self.getColumnText(item, 0) not in self.selected :
123 return self.attrselected
125 def GetItemByWord(self, word):
126 return [ val for val in self.dlist if self.dlist[val][0] == word ][0]
128 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
129 def GetListCtrl(self):
132 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
133 def GetSortImages(self):
134 return (self.sm_dn, self.sm_up)
136 def OnRightDown(self, event):
139 item, flags = self.HitTest((x, y))
141 if flags & wx.LIST_HITTEST_ONITEM:
146 def GetString(self, evt):
147 return self.getselectedwords()[0]
149 def GetSelections(self):
150 return self.getselectedwords()
152 def getColumnText(self, index, col):
153 item = self.GetItem(index, col)
154 return item.GetText()
156 def GetItemData(self, item) :
157 index=self.itemIndexMap[item]
158 s = self.itemDataMap[index]
161 def SortItems(self,sorter=cmp):
162 items = list(self.itemDataMap.keys())
164 self.itemIndexMap = items
168 def OnItemSelected(self, event):
169 self.currentItem = event.m_itemIndex
172 def onsearch(self, evt) :
173 self.dial = SearchDial(self, self, 0, True)
174 self.dial.CenterOnParent()
178 def OnRightClick(self, event):
180 # only do this part the first time so the events are only bound once
181 if not hasattr(self, "popupID1"):
182 self.popupID1 = wx.NewId()
183 self.popupID2 = wx.NewId()
184 self.popupID3 = wx.NewId()
185 self.popup_Tgen_glob = wx.NewId()
186 self.onmaketgen = wx.NewId()
187 self.ID_stcaract = wx.NewId()
188 self.id_tgendetails = wx.NewId()
190 self.Bind(wx.EVT_MENU, self.OnPopupOne, id = self.popupID1)
191 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id = self.popupID2)
192 self.Bind(wx.EVT_MENU, self.OnPopupThree, id = self.popupID3)
193 self.Bind(wx.EVT_MENU, self.OnTgen_glob, id = self.popup_Tgen_glob)
194 self.Bind(wx.EVT_MENU, self.OnMakeTgen, id = self.onmaketgen)
195 self.Bind(wx.EVT_MENU, self.OnTgenDetails, id = self.id_tgendetails)
196 #self.Bind(wx.EVT_MENU, self.onstcaract, id = self.ID_stcaract)
200 menu.Append(self.popupID1, _(u"Associated forms").decode('utf8'))
201 menu.Append(self.popupID2, _(u"Concordance").decode('utf8'))
202 menu.Append(self.popupID3, _(u"Graphic").decode('utf8'))
203 menu_stcaract = wx.Menu()
206 for i, et in enumerate(self.etoiles) :
209 menu_stcaract.Append(nid, et)
210 self.Bind(wx.EVT_MENU, self.onstcaract, id = nid)
211 menu.AppendMenu(-1, _(u"Typical text segments").decode('utf8'), menu_stcaract)
212 menu.Append(self.onmaketgen, _(u"Make Tgen").decode('utf8'))
214 menu.Append(self.id_tgendetails, _(u'Tgen details').decode('utf8'))
218 def getselectedwords(self) :
219 words = [self.getColumnText(self.GetFirstSelected(), 0)]
220 last = self.GetFirstSelected()
221 while self.GetNextSelected(last) != -1:
222 last = self.GetNextSelected(last)
223 words.append(self.getColumnText(last, 0))
226 def OnPopupOne(self, event):
227 activenotebook = self.parent.nb.GetSelection()
228 page = self.parent.nb.GetPage(activenotebook)
230 word = self.getselectedwords()[0]
231 lems = corpus.getlems()
233 for forme in lems[word].formes :
234 rep.append([corpus.getforme(forme).forme, corpus.getforme(forme).freq])
235 rep.sort(key = itemgetter(1), reverse = True)
236 items = dict([[i, '<font face="courier">' + '\t:\t'.join([str(val) for val in forme]) + '</font>'] for i, forme in enumerate(rep)])
237 win = message(self, items, _(u"Associated forms").decode('utf8'), (300, 200))
238 #win = message(self, u"Formes associƩes", (300, 200))
239 #win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
240 #win.HtmlPage.SetPage(win.html)
243 def onstcaract(self, evt) :
244 ind = self.menuid[evt.Id]
247 activenotebook = self.parent.nb.GetSelection()
248 page = self.parent.nb.GetPage(activenotebook)
249 item=self.getColumnText(self.GetFirstSelected(), 0)
251 parametres = page.parametres
252 paneff = self.gparent.ListPanEff
253 panchi = self.gparent.ListPan
254 et = self.etoiles[ind]
255 uces = corpus.getucesfrometoile(et)
256 self.la = [panchi.dlist[i][0] for i in range(0, len(panchi.dlist)) if panchi.dlist[i][ind+1] >= minind ]
257 self.lchi = [panchi.dlist[i][ind+1] for i in range(0, len(panchi.dlist)) if panchi.dlist[i][ind+1] >= minind ]
258 if max(self.lchi) == float('inf') :
260 for val in self.lchi :
261 if val == float('inf') :
266 nchi = [val if val != float('inf') else nmax + 2 for val in self.lchi]
268 tab = corpus.make_pondtable_with_classe(uces, self.la)
270 ntab = [round(sum([(self.lchi[i] * word) for i, word in enumerate(line) if word != 0]),2) for line in tab]
271 ntab2 = [[ntab[i], uces[i]] for i, val in enumerate(ntab) if ntab[i] != 0]
273 ntab2.sort(reverse = True)
274 ntab2 = ntab2[:limite]
275 nuces = [val[1] for val in ntab2]
276 ucis_txt, ucestxt = doconcorde(corpus, nuces, self.la)
277 items = dict([[i, '<br>'.join([ucis_txt[i], '<table bgcolor = #1BF0F7 border=0><tr><td><b>score : %.2f</b></td></tr></table><br>' % ntab2[i][0], ucestxt[i]])] for i in range(0,len(ucestxt))])
278 win = message(self, items, ' - '.join([_(u"Typical text segments").decode('utf8'), "%s" % self.first[ind]]), (900, 600))
281 def OnPopupTwo(self, event):
282 if 'nb' in dir(self.parent) :
283 activenotebook = self.parent.nb.GetSelection()
284 page = self.parent.nb.GetPage(activenotebook)
287 corpus = self.parent.parent.parent.corpus
288 ira = wx.GetApp().GetTopWindow()
289 item=self.getColumnText(self.GetFirstSelected(), 0)
291 uce_ok = corpus.getlemuces(item)
294 uce_ok = corpus.gettgenst(self.tgens[item])
295 wordlist = [val for val in self.tgens[item] if val in corpus.lems]
296 ucis_txt, ucestxt = doconcorde(corpus, uce_ok, wordlist)
297 items = dict([[i, '<br><br>'.join([ucis_txt[i], ucestxt[i]])] for i in range(0,len(ucestxt))])
298 win = message(ira, items, ' - '.join([_(u"Concordance").decode('utf8'), "%s" % item]), (800, 500), uceids = uce_ok)
301 def getinf(self, txt) :
302 if txt == float('Inf') :
304 elif txt == float('-Inf') :
309 def OnPopupThree(self, event) :
310 datas = [self.GetItemData(self.GetFirstSelected())]
311 last = self.GetFirstSelected()
312 while self.GetNextSelected(last) != -1:
313 last = self.GetNextSelected(last)
314 data = self.GetItemData(last)
316 colnames = self.first
317 table = [[self.getinf(val) for val in line[1:]] for line in datas]
318 rownames = [val[0] for val in datas]
319 BarFrame(self.parent, table, colnames, rownames)
321 def OnTgen_glob(self, evt) :
322 activenotebook = self.parent.nb.GetSelection()
323 page = self.parent.nb.GetPage(activenotebook)
326 tmpgraph = tempfile.mktemp(dir=self.parent.TEMPDIR)
329 """ % corpus.dictpathout['RData']
333 for (i in 1:ncol(dmf)) {
334 Tgen.glob <- rbind(Tgen.glob,colSums(dmf[which(specf[,i] > 3),]))
335 tovire <- append(tovire, which(specf[,i] > 3))
337 rownames(Tgen.glob) <- colnames(dmf)
338 Tgen.table <- dmf[-unique(tovire),]
339 Tgen.table<- rbind(Tgen.table, Tgen.glob)
340 spec.Tgen.glob <- AsLexico2(Tgen.table)
341 spec.Tgen.glob <- spec.Tgen.glob[[1]][((nrow(Tgen.table)-ncol(Tgen.table))+1):nrow(Tgen.table),]
344 txt = barplot('', '', '', self.parent.RscriptsPath['Rgraph'], tmpgraph, intxt = intxt)
345 tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
346 with open(tmpscript, 'w') as f :
348 exec_rcode(self.parent.RPath, tmpscript, wait = True)
349 win = MessageImage(self, -1, _(u"Graphic").decode('utf8'), size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
350 win.addsaveimage(tmpgraph)
351 txt = "<img src='%s'>" % tmpgraph
352 win.HtmlPage.SetPage(txt)
355 def OnTgenDetails(self, evt):
356 if 'nb' in dir(self.parent) :
357 activenotebook = self.parent.nb.GetSelection()
358 page = self.parent.nb.GetPage(activenotebook)
361 corpus = self.parent.parent.parent.corpus
362 ira = wx.GetApp().GetTopWindow()
363 item=self.getColumnText(self.GetFirstSelected(), 0)
364 wordlist = [val for val in self.tgens[item] if val in corpus.lems]
365 wordlist = dict(zip(wordlist,wordlist))
366 res = dict([[val, self.tgenlem[val]] for val in self.tgenlem if self.tgenlem[val][0] in wordlist])
367 win = ListLexFrame(self, ira, corpus, res, self.etoiles)
371 def OnMakeTgen(self, evt):
372 self.parent.tree.OnTgenEditor(self.getselectedwords())
374 class ListLexFrame ( wx.Frame ):
376 def __init__( self, parent, ira, corpus, data, columns ):
377 wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
379 self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
381 bSizer1 = wx.BoxSizer( wx.VERTICAL )
384 self.listlex = ListForSpec(self, ira, data, columns)
385 bSizer1.Add( self.listlex, 5, wx.ALL|wx.EXPAND, 5 )
387 m_sdbSizer1 = wx.StdDialogButtonSizer()
388 self.m_sdbSizer1OK = wx.Button( self, wx.ID_OK )
389 m_sdbSizer1.AddButton( self.m_sdbSizer1OK )
390 self.m_sdbSizer1Cancel = wx.Button( self, wx.ID_CANCEL )
391 m_sdbSizer1.AddButton( self.m_sdbSizer1Cancel )
392 m_sdbSizer1.Realize();
394 bSizer1.Add( m_sdbSizer1, 0, wx.EXPAND, 5 )
397 self.SetSizer( bSizer1 )
400 self.Centre( wx.BOTH )