-
Notifications
You must be signed in to change notification settings - Fork 1
/
TagYourText.py
346 lines (284 loc) · 11.2 KB
/
TagYourText.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import time
import wx.stc as stc
import os
import os.path
import matplotlib.pyplot as plt
import jieba.posseg as pseg
import jieba
import jieba.analyse
import matplotlib
import sys
from pylab import *
import numpy as np
import utils
from codecs import open
from subprocess import Popen, PIPE
import subprocess
from utils import VAR_DICT_FILE
from utils import VAR_NEW_BOW_FILE
from utils import VAR_TOPNWORDS_FILE
from utils import VAR_SYS_ENCODING
from utils import VAR_STOPWORD_FILE
from utils import VAR_THETA_FILE_RESULT
from utils import VAR_MODEL_FILE
from utils import VAR_NUM_WORD_TOPIC_FILE
from utils import VAR_NUM_DOC_TOPIC_FILE
from utils import VAR_TOTAL_WORDS_PER_TOPIC_FILE
from utils import VAR_NEW_PATH_FILE
from preprocess import Preprocessor
class MyPanel(wx.Panel):
def __init__(self, parent, image_file='Pie.png'):
wx.Panel.__init__(self, parent, id=-1)
try:
imagefile=image_file
image = wx.Image(imagefile)
temp = image.ConvertToBitmap()
size = temp.GetWidth(),temp.GetHeight()
wx.StaticBitmap(self, -1, temp)
except IOError:
print 'Image file %s not found' % image_file
raise SystemExi
class MyFrame(wx.Frame):
title = "Show more detial"
phasesList = [u"文学", u"体育", u"金融","IT"]
def __init__(self,filenames="",label="",count=0):
wx.Frame.__init__(self, wx.GetApp().TopWindow, title=self.title,size=(720,400))
self.filename=filenames
self.panel1=wx.Panel(self)
self.label1 = wx.StaticText(self.panel1, -1, label=u"所属类别为: "+label)
# self.label2=wx.StaticText(self.panel1,-1,label=u"结果不对我要更正:")
# self.label1.SetForegroundColour("blue")
# self.label2.SetForegroundColour("blue")
self.content2 = wx.TextCtrl(self.panel1, style=wx.TE_MULTILINE)
with open(filenames, encoding='utf-8') as f:
doc =f.read()
self.content2.SetValue(doc)
self.panel2=MyPanel(self.panel1,"Pie.png")
# self.content3=wx.ComboBox(self.panel1, choices=self.phasesList)
# self.content3.Bind(wx.EVT_COMBOBOX, self.onCombo)
# self.button2=wx.Button(self.panel1,label=u"更改")
#
self.hbox1=wx.BoxSizer()
self.hbox1.Add(self.label1,proportion=0,flag=wx.EXPAND|wx.LEFT,border=10)
self.hbox2=wx.BoxSizer()
self.hbox2.Add(self.content2,proportion=1,flag=wx.EXPAND|wx.LEFT,border=10)
self.hbox2.Add(self.panel2,proportion=0,flag=wx.EXPAND|wx.RIGHT,border=10)
# self.hbox3=wx.BoxSizer()
# self.hbox3.Add(self.label2,proportion=0,flag=wx.LEFT,border=10)
# self.hbox3.Add(self.content3,proportion=0,flag=wx.LEFT|wx.RIGHT,border=5)
# self.hbox3.Add(self.button2,proportion=0,flag=wx.RIGHT,border=10)
self.vbox1=wx.BoxSizer(wx.VERTICAL)
self.vbox1.Add(self.hbox1,proportion=0,flag=wx.EXPAND|wx.TOP|wx.BOTTOM,border=5 )
self.vbox1.Add(self.hbox2,proportion=1,flag=wx.EXPAND|wx.TOP|wx.BOTTOM,border=5 )
# self.vbox1.Add(self.hbox3,proportion=0,flag=wx.EXPAND|wx.TOP|wx.BOTTOM,border=5 )
self.panel1.SetSizer(self.vbox1)
def onCombo(self, event):
"""
"""
phaseSelection = self.content3.GetValue()
# print phaseSelection
class Frame(wx.Frame):
def __init__(self): #3
"""
the Constructor wx.Frame(parent, id=-1, title="", pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE,
name="frame")
"""
title="Tag Your text"
wx.Frame.__init__(self, None, title=title,
size=(800,600))
self.panel =wx.Panel(self)
self.panel.SetBackgroundColour((0,205,213))
self.CreateListctr()
self.CreateTextBox()
self.CreateButton()
self.createLayout()
self.BindEvent()
self.InitStatusBar()
self.CreateMenuBar()
self.statusBasPrint(u"加载成功")
self.dirname = None
#init the Frame
def CreateListctr(self):
self.list = wx.ListCtrl(self.panel, -1, style=wx.LC_REPORT)
self.list.InsertColumn(0, u"文件名", width=100)
self.list.InsertColumn(1, u"主题分布", width=400)
self.list.InsertColumn(2,u"类别",width=100)
self.list.InsertColumn(3, u"分布注释", wx.LIST_FORMAT_RIGHT, 80)
def CreateTextBox(self):
'''
create TextBox
the contents2 is the ColorTextBox class which is the Subclass of the wx.stc.StyledTextCtrl
we use its write function to tag the highlight words
'''
self.contents1=wx.TextCtrl(self.panel) #add the texttctl componment
# self.contents2=ColorTextBox(self.panel)
# self.contents2.SetWrapMode(stc.STC_WRAP_WORD)
def BindEvent(self):
'''
bind the button
'''
self.button1.Bind(wx.EVT_BUTTON, self.category)
self.list.Bind(wx.EVT_LIST_ITEM_ACTIVATED,self.Onclick)
def CreateButton(self):
'''
create the button
'''
self.button1=wx.Button(self.panel,label=u'分类')
def createLayout(self):
'''
BoxSizer is the Layout component
vbox is the Vertical
hbox is the Horizontal
'''
self.hbox=wx.BoxSizer()
self.hbox.Add(self.contents1,proportion=1,flag=wx.EXPAND|wx.LEFT,border=30)
self.hbox.Add(self.button1,proportion=0,flag=wx.RIGHT,border=30)
self.vbox=wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.hbox,proportion=0,flag=wx.EXPAND|wx.TOP|wx.BOTTOM,border=30 )
self.vbox.Add(self.list,proportion=1,flag=wx.EXPAND|wx.TOP|wx.BOTTOM|wx.RIGHT|wx.LEFT,border=30)
self.panel.SetSizer(self.vbox)
def builtdata(self,datalist):
i=0
newlist=[]
for data1 in datalist:
su=0
a=data1[:3]
# print a
for k in a:
su+=k[1]
a1=[(j[0],j[1]/su*100) for j in a]
sts=''
for d in a1:
fds = '{0:.1f}%\t'.format(d[1])
sts+=d[0] + ": " + fds + " "
newlist.append([str(i),sts,a1[0][0],u"双击查看详情"])
i+=1
return newlist
def category(self,event):
if self.dirname is None:
return
start = time.time()
self.statusBasPrint(u"系统正在疯狂运算中,请稍候...")
Preprocessor.preprocess(self.dirname)
self.id2path = utils.parse_path_file(VAR_NEW_PATH_FILE)
self.statusBasPrint('C++ classifier is running...')
# p = subprocess.call('Utils.exe', stdout=PIPE, stdin=PIPE, stderr=PIPE)
p = subprocess.call('Utils.exe')
self.statusBasPrint('C++ classifier has finished')
theta = np.loadtxt(VAR_THETA_FILE_RESULT)
# return as a list [ [(name, probability), (name, probability), ...], ...]
self.result = utils.get_topic_dist(theta)
self.packages=self.builtdata(self.result)
for i in self.packages:
index = self.list.InsertStringItem(sys.maxint, i[0])
self.list.SetStringItem(index, 1, i[1])
self.list.SetStringItem(index, 2, i[2])
self.list.SetStringItem(index,3,i[3])
#max(dict.iterkeys(),key=lambda k:dict[k])
self.statusBasPrint(u"文档分类完成!")
elapsed = (time.time() - start)
self.Printtime(str(elapsed)+'s')
def Onclick(self, event):
data=self.result[int(event.GetText())][:3]
su=0
for da in data:
su+=da[1]
fracs=[i[1]/su*100 for i in data]
# print fracs
mylabel=[i[0] for i in data]
# print mylabel
self.id = int(event.GetText())
if os.path.isfile('Pie.png'):
os.remove('Pie.png')
self.createPie(mylabel,fracs)
MyFrame(self.id2path[self.id],mylabel[0]).Show()
def createPie(self,mylabels=['it', u'你妹', 'EQ'],fracs=[40, 30, 30]):
matplotlib.rcParams['font.sans-serif'] = ['Microsoft YaHei'] #设置缺省字体
matplotlib.rcParams['toolbar'] = 'None'
plt.ioff()
fig = figure(3, figsize=(3,3))
mycolors=['red', 'yellow', 'green']
pie(fracs,labels=mylabels,colors=mycolors)
savefig('Pie.png',transparent=False, bbox_inches='tight', pad_inches=0)
plt.close(fig)
#Ok
def InitStatusBar(self):
self.statusbar = self.CreateStatusBar()
self.statusbar.SetFieldsCount(2)
self.statusbar.SetStatusWidths([-4,-1])
def statusBasPrint(self,strs):
self.statusbar.SetStatusText(u''+strs, 0)
def Printtime(self,strs):
self.statusbar.SetStatusText(u'用时 '+strs,1)
def MenuData(self):
'''
menu data
'''
return [("&Menu", (
("Open", u"打开", self.OnOpen),
("&About", u"关于本软件", self.Onabout), #
("&Quit", u"退出", self.OnCloseWindow)))
]
def CreateMenuBar(self):
'''
createMunBar
'''
menuBar = wx.MenuBar()
for eachMenuData in self.MenuData():
menuLabel = eachMenuData[0]
menuItems = eachMenuData[1]
menuBar.Append(self.CreateMenu(menuItems), menuLabel)
self.SetMenuBar(menuBar)
def CreateMenu(self, menuData):
'''
create the munu
'''
menu = wx.Menu()
for eachItem in menuData:
if len(eachItem) == 2:
label = eachItem[0]
subMenu = self.CreateMenu(eachItem[1])
menu.AppendMenu(wx.NewId(), label, subMenu) #
else:
self.CreateMenuItem(menu, *eachItem)
return menu
def CreateMenuItem(self, menu, label, status, handler, kind = wx.ITEM_NORMAL):
'''topic
create the item in the menu
'''
if not label:
menu.AppendSeparator()
return
menuItem = menu.Append(-1, label, status, kind)
self.Bind(wx.EVT_MENU, handler,menuItem)
#event handler of the menu
def OnOpen(self, event):
dlg = wx.DirDialog(self, "Please choose your directory which contains txts:", \
style=1)
if dlg.ShowModal() == wx.ID_OK:
self.dirname = dlg.GetPath()
self.contents1.SetValue(self.dirname)
def Onabout(self,event):
mess='Tag Your Text Version 1.0 \nAuthor: \
GuoWeiCheng, WangJunSheng, HuangHongJie, LinJingPei, GuoBaoRong and GuoMengLu!'
dlg = wx.MessageDialog(None,mess,
'What about !', wx.OK | wx.ICON_INFORMATION)
result = dlg.ShowModal()
dlg.Destroy()
def OnCloseWindow(self, event):
self.Destroy()
class App(wx.App):
def OnInit(self):
self.frame=Frame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main(): # main
app = App(redirect=False)
app.MainLoop()
if __name__ == '__main__':
main()