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
21 from functions import exec_rcode, MessageImage, doconcorde
22 from chemins import ffr
23 from PrintRScript import barplot
24 from dialog import SearchDial, message
25 from operator import itemgetter
26 #---------------------------------------------------------------------------
28 class ListForSpec(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
29 def __init__(self, parent,gparent, dlist = {}, first = [], menu = True):
30 #def __init__(self, parent) :
31 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) :
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)
49 search_id = wx.NewId()
50 self.parent.Bind(wx.EVT_MENU, self.onsearch, id = search_id)
51 self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('F'), search_id)])
52 self.SetAcceleratorTable(self.accel_tbl)
54 self.il = wx.ImageList(16, 16)
55 a={"sm_up":"GO_UP","sm_dn":"GO_DOWN","w_idx":"WARNING","e_idx":"ERROR","i_idx":"QUESTION"}
57 s="self.%s= self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_%s,wx.ART_TOOLBAR,(16,16)))" % (k,v)
59 self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
63 self.attr1 = wx.ListItemAttr()
64 self.attr1.SetBackgroundColour((230, 230, 230))
65 self.attr2 = wx.ListItemAttr()
66 self.attr2.SetBackgroundColour("light blue")
67 self.attrselected = wx.ListItemAttr()
68 self.attrselected.SetBackgroundColour("red")
72 for name in [u'formes'] + self.first :
73 self.InsertColumn(i,name,wx.LIST_FORMAT_LEFT)
76 self.SetColumnWidth(0, 180)
78 for i in range(0,len(self.first)):
79 self.SetColumnWidth(i + 1, self.checkcolumnwidth(len(self.first[i]) * 10))
81 self.itemDataMap = self.dlist
82 self.itemIndexMap = self.dlist.keys()
83 self.SetItemCount(len(self.dlist))
85 #listmix.ListCtrlAutoWidthMixin.__init__(self)
86 listmix.ColumnSorterMixin.__init__(self, len(self.first) + 1)
87 self.SortListItems(1, 0)
89 #-----------------------------------------------------------------------------------------
90 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self)
92 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED , self.OnPopupTwo, self)
94 self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
97 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
99 #-----------------------------------------------------------------------------------------
100 def RefreshData(self, data):
101 self.itemDataMap = data
102 self.itemIndexMap = data.keys()
103 self.SetItemCount(len(data))
106 def checkcolumnwidth(self, width) :
112 def OnGetItemText(self, item, col):
113 index=self.itemIndexMap[item]
114 s = self.itemDataMap[index][col]
117 def OnGetItemAttr(self, item):
118 if self.getColumnText(item, 0) not in self.selected :
124 return self.attrselected
126 def GetItemByWord(self, word):
127 return [ val for val in self.dlist if self.dlist[val][0] == word ][0]
129 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
130 def GetListCtrl(self):
133 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
134 def GetSortImages(self):
135 return (self.sm_dn, self.sm_up)
137 def OnRightDown(self, event):
140 item, flags = self.HitTest((x, y))
142 if flags & wx.LIST_HITTEST_ONITEM:
147 def GetString(self, evt):
148 return self.getselectedwords()[0]
150 def GetSelections(self):
151 return self.getselectedwords()
153 def getColumnText(self, index, col):
154 item = self.GetItem(index, col)
155 return item.GetText()
157 def GetItemData(self, item) :
158 index=self.itemIndexMap[item]
159 s = self.itemDataMap[index]
162 def SortItems(self,sorter=cmp):
163 items = list(self.itemDataMap.keys())
165 self.itemIndexMap = items
169 def OnItemSelected(self, event):
170 self.currentItem = event.m_itemIndex
173 def onsearch(self, evt) :
174 self.dial = SearchDial(self, self, 0, True)
175 self.dial.CenterOnParent()
179 def OnRightClick(self, event):
181 # only do this part the first time so the events are only bound once
182 if not hasattr(self, "popupID1"):
183 self.popupID1 = wx.NewId()
184 self.popupID2 = wx.NewId()
185 self.popupID3 = wx.NewId()
186 self.popup_Tgen_glob = wx.NewId()
187 self.onmaketgen = wx.NewId()
188 self.ID_stcaract = 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.onstcaract, id = self.ID_stcaract)
199 menu.Append(self.popupID1, u"Formes associées")
200 menu.Append(self.popupID2, u"Concordancier")
201 menu.Append(self.popupID3, u"Graphique")
202 menu_stcaract = wx.Menu()
204 for i, et in enumerate(self.etoiles) :
207 menu_stcaract.Append(nid, et)
208 self.Bind(wx.EVT_MENU, self.onstcaract, id = nid)
209 menu.AppendMenu(-1, u"Segments de texte caractéristiques", menu_stcaract)
210 #menu.Append(self.popup_Tgen_glob, "Tgen global")
211 menu.Append(self.onmaketgen, "Make Tgen")
215 def getselectedwords(self) :
216 words = [self.getColumnText(self.GetFirstSelected(), 0)]
217 last = self.GetFirstSelected()
218 while self.GetNextSelected(last) != -1:
219 last = self.GetNextSelected(last)
220 words.append(self.getColumnText(last, 0))
223 def OnPopupOne(self, event):
224 activenotebook = self.parent.nb.GetSelection()
225 page = self.parent.nb.GetPage(activenotebook)
227 word = self.getselectedwords()[0]
228 lems = corpus.getlems()
230 for forme in lems[word].formes :
231 rep.append([corpus.getforme(forme).forme, corpus.getforme(forme).freq])
232 rep.sort(key = itemgetter(1), reverse = True)
233 win = message(self, u"Formes associées", (300, 200))
234 win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
235 win.HtmlPage.SetPage(win.html)
238 def onstcaract(self, evt) :
239 ind = self.menuid[evt.Id]
242 activenotebook = self.parent.nb.GetSelection()
243 page = self.parent.nb.GetPage(activenotebook)
244 item=self.getColumnText(self.GetFirstSelected(), 0)
246 parametres = page.parametres
247 paneff = self.gparent.ListPanEff
248 panchi = self.gparent.ListPan
249 #etoiles = self.gparent.etoiles
250 et = self.etoiles[ind]
252 #if et.startswith(u'X.') :
253 # et = et.replace(u'X.', u'*')
254 uces = corpus.getucesfrometoile(et)
255 self.la = [panchi.dlist[i][0] for i in range(0, len(panchi.dlist)) if panchi.dlist[i][ind+1] >= minind ]
256 self.lchi = [panchi.dlist[i][ind+1] for i in range(0, len(panchi.dlist)) if panchi.dlist[i][ind+1] >= minind ]
257 #lfreq = [paneff.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, u"Segments de texte caractéristiques - %s" % self.first[ind], (900, 600))
279 #win.html = '<html>\n' + '<br>'.join(['<br>'.join([ucis_txt[i], '<table bgcolor = #1BF0F7 border=0><tr><td><b>score : %.2f</b></td></tr></table>' % ntab2[i][0], ucestxt[i]]) for i in range(0,len(ucestxt))]) + '\n</html>'
280 #win.HtmlPage.SetPage(win.html)
283 def OnPopupTwo(self, event):
284 if 'nb' in dir(self.parent) :
285 activenotebook = self.parent.nb.GetSelection()
286 page = self.parent.nb.GetPage(activenotebook)
289 corpus = self.parent.parent.parent.corpus
290 item=self.getColumnText(self.GetFirstSelected(), 0)
291 uce_ok = corpus.getlemuces(item)
292 ira = wx.GetApp().GetTopWindow()
293 ucis_txt, ucestxt = doconcorde(corpus, uce_ok, [item])
294 items = dict([[i, '<br><br>'.join([ucis_txt[i], ucestxt[i]])] for i in range(0,len(ucestxt))])
295 win = message(ira, items, u"Concordancier - %s" % item, (800, 500))
296 #win = message(ira, u"Concordancier", (800, 500))
297 #win.html = ('<html>\n<h1>%s</h1>' % item) + '<br>'.join(['<br>'.join([ucis_txt[i], ucestxt[i]]) for i in range(0,len(ucestxt))]) + '\n</html>'
298 #win.HtmlPage.SetPage(win.html)
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.etoiles
317 table = [[self.getinf(val) for val in line[1:]] for line in datas]
318 rownames = [val[0] for val in datas]
319 tmpgraph = tempfile.mktemp(dir=self.parent.TEMPDIR)
320 txt = barplot(table, rownames, colnames, self.parent.RscriptsPath['Rgraph'], tmpgraph)
321 tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
322 with open(tmpscript,'w') as f :
324 exec_rcode(self.parent.RPath, tmpscript, wait = True)
325 win = MessageImage(self, u"Graphique", size=(700, 500))
326 win.addsaveimage(tmpgraph)
327 txt = "<img src='%s'>" % tmpgraph
328 win.HtmlPage.SetPage(txt)
331 def OnTgen_glob(self, evt) :
332 activenotebook = self.parent.nb.GetSelection()
333 page = self.parent.nb.GetPage(activenotebook)
336 tmpgraph = tempfile.mktemp(dir=self.parent.TEMPDIR)
339 """ % corpus.dictpathout['RData']
343 for (i in 1:ncol(dmf)) {
344 Tgen.glob <- rbind(Tgen.glob,colSums(dmf[which(specf[,i] > 3),]))
345 tovire <- append(tovire, which(specf[,i] > 3))
347 rownames(Tgen.glob) <- colnames(dmf)
348 Tgen.table <- dmf[-unique(tovire),]
349 Tgen.table<- rbind(Tgen.table, Tgen.glob)
350 spec.Tgen.glob <- AsLexico2(Tgen.table)
351 spec.Tgen.glob <- spec.Tgen.glob[[1]][((nrow(Tgen.table)-ncol(Tgen.table))+1):nrow(Tgen.table),]
354 txt = barplot('', '', '', self.parent.RscriptsPath['Rgraph'], tmpgraph, intxt = intxt)
355 tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
356 with open(tmpscript, 'w') as f :
358 exec_rcode(self.parent.RPath, tmpscript, wait = True)
359 win = MessageImage(self, -1, u"Graphique", size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
360 win.addsaveimage(tmpgraph)
361 txt = "<img src='%s'>" % tmpgraph
362 win.HtmlPage.SetPage(txt)
365 def OnMakeTgen(self, evt):
366 self.parent.tree.OnTgenEditor(self.getselectedwords())