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, doconcorde
22 from chemins import ffr
23 from PrintRScript import barplot
24 from dialog import SearchDial, message, BarGraphDialog, MessageImage, BarFrame
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)
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.first)):
78 self.SetColumnWidth(i + 1, self.checkcolumnwidth(len(self.first[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()
189 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
190 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
191 self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
192 self.Bind(wx.EVT_MENU, self.OnTgen_glob, id=self.popup_Tgen_glob)
193 self.Bind(wx.EVT_MENU, self.OnMakeTgen, id=self.onmaketgen)
194 #self.Bind(wx.EVT_MENU, self.onstcaract, id = self.ID_stcaract)
198 menu.Append(self.popupID1, u"Formes associées")
199 menu.Append(self.popupID2, u"Concordancier")
200 menu.Append(self.popupID3, u"Graphique")
201 menu_stcaract = wx.Menu()
203 for i, et in enumerate(self.etoiles) :
206 menu_stcaract.Append(nid, et)
207 self.Bind(wx.EVT_MENU, self.onstcaract, id = nid)
208 menu.AppendMenu(-1, u"Segments de texte caractéristiques", menu_stcaract)
209 #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 items = dict([[i, '<font face="courier">' + '\t:\t'.join([str(val) for val in forme]) + '</font>'] for i, forme in enumerate(rep)])
234 win = message(self, items, u"Formes associées", (300, 200))
235 #win = message(self, u"Formes associées", (300, 200))
236 #win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
237 #win.HtmlPage.SetPage(win.html)
240 def onstcaract(self, evt) :
241 ind = self.menuid[evt.Id]
244 activenotebook = self.parent.nb.GetSelection()
245 page = self.parent.nb.GetPage(activenotebook)
246 item=self.getColumnText(self.GetFirstSelected(), 0)
248 parametres = page.parametres
249 paneff = self.gparent.ListPanEff
250 panchi = self.gparent.ListPan
251 #etoiles = self.gparent.etoiles
252 et = self.etoiles[ind]
254 #if et.startswith(u'X.') :
255 # et = et.replace(u'X.', u'*')
256 uces = corpus.getucesfrometoile(et)
257 self.la = [panchi.dlist[i][0] for i in range(0, len(panchi.dlist)) if panchi.dlist[i][ind+1] >= minind ]
258 self.lchi = [panchi.dlist[i][ind+1] for i in range(0, len(panchi.dlist)) if panchi.dlist[i][ind+1] >= minind ]
259 #lfreq = [paneff.dlist[i][ind+1] for i in range(0, len(panchi.dlist)) if panchi.dlist[i][ind+1] >= minind ]
260 if max(self.lchi) == float('inf') :
262 for val in self.lchi :
263 if val == float('inf') :
268 nchi = [val if val != float('inf') else nmax + 2 for val in self.lchi]
270 tab = corpus.make_pondtable_with_classe(uces, self.la)
272 ntab = [round(sum([(self.lchi[i] * word) for i, word in enumerate(line) if word != 0]),2) for line in tab]
273 ntab2 = [[ntab[i], uces[i]] for i, val in enumerate(ntab) if ntab[i] != 0]
275 ntab2.sort(reverse = True)
276 ntab2 = ntab2[:limite]
277 nuces = [val[1] for val in ntab2]
278 ucis_txt, ucestxt = doconcorde(corpus, nuces, self.la)
279 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))])
280 win = message(self, items, u"Segments de texte caractéristiques - %s" % self.first[ind], (900, 600))
281 #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>'
282 #win.HtmlPage.SetPage(win.html)
285 def OnPopupTwo(self, event):
286 if 'nb' in dir(self.parent) :
287 activenotebook = self.parent.nb.GetSelection()
288 page = self.parent.nb.GetPage(activenotebook)
291 corpus = self.parent.parent.parent.corpus
292 item=self.getColumnText(self.GetFirstSelected(), 0)
293 uce_ok = corpus.getlemuces(item)
294 ira = wx.GetApp().GetTopWindow()
295 ucis_txt, ucestxt = doconcorde(corpus, uce_ok, [item])
296 items = dict([[i, '<br><br>'.join([ucis_txt[i], ucestxt[i]])] for i in range(0,len(ucestxt))])
297 win = message(ira, items, u"Concordancier - %s" % item, (800, 500))
298 #win = message(ira, u"Concordancier", (800, 500))
299 #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>'
300 #win.HtmlPage.SetPage(win.html)
303 def getinf(self, txt) :
304 if txt == float('Inf') :
306 elif txt == float('-Inf') :
311 def OnPopupThree(self, event) :
312 datas = [self.GetItemData(self.GetFirstSelected())]
313 last = self.GetFirstSelected()
314 while self.GetNextSelected(last) != -1:
315 last = self.GetNextSelected(last)
316 data = self.GetItemData(last)
318 colnames = self.first
319 table = [[self.getinf(val) for val in line[1:]] for line in datas]
320 rownames = [val[0] for val in datas]
321 BarFrame(self.parent, table, colnames, rownames)
323 def OnTgen_glob(self, evt) :
324 activenotebook = self.parent.nb.GetSelection()
325 page = self.parent.nb.GetPage(activenotebook)
328 tmpgraph = tempfile.mktemp(dir=self.parent.TEMPDIR)
331 """ % corpus.dictpathout['RData']
335 for (i in 1:ncol(dmf)) {
336 Tgen.glob <- rbind(Tgen.glob,colSums(dmf[which(specf[,i] > 3),]))
337 tovire <- append(tovire, which(specf[,i] > 3))
339 rownames(Tgen.glob) <- colnames(dmf)
340 Tgen.table <- dmf[-unique(tovire),]
341 Tgen.table<- rbind(Tgen.table, Tgen.glob)
342 spec.Tgen.glob <- AsLexico2(Tgen.table)
343 spec.Tgen.glob <- spec.Tgen.glob[[1]][((nrow(Tgen.table)-ncol(Tgen.table))+1):nrow(Tgen.table),]
346 txt = barplot('', '', '', self.parent.RscriptsPath['Rgraph'], tmpgraph, intxt = intxt)
347 tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
348 with open(tmpscript, 'w') as f :
350 exec_rcode(self.parent.RPath, tmpscript, wait = True)
351 win = MessageImage(self, -1, u"Graphique", size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
352 win.addsaveimage(tmpgraph)
353 txt = "<img src='%s'>" % tmpgraph
354 win.HtmlPage.SetPage(txt)
357 def OnMakeTgen(self, evt):
358 self.parent.tree.OnTgenEditor(self.getselectedwords())