-
Notifications
You must be signed in to change notification settings - Fork 5
/
wordgroupz.py
executable file
·1979 lines (1815 loc) · 77 KB
/
wordgroupz.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Copyright (C) 2010 Ratnadeep Debnath <rtnpro@fedoraproject.org>
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import cgi
import pygtk
import gtk
import sqlite3
import os
import sys
import socket
import string
import get_wordnet_def as wordnet
import webkit
import urllib2
from BeautifulSoup import BeautifulSoup
import urllib
import pygst
import gst
import re
from html2text import *
import get_fields
import thread
import threading
import games
import espeak
#from gettext import gettext as _
import locale
import gettext
APP = 'worgroupz'
DIR = 'locale'
VERSION = '0.3.1'
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain(APP, DIR)
gettext.textdomain(APP)
_ = gettext.gettext
usr_home = os.environ['HOME']
wordgroupz_dir = usr_home+'/.wordgroupz'
audio_file_path = wordgroupz_dir + '/audio'
db_file_path = wordgroupz_dir+'/wordgroupz.db'
class wordGroupzSql:
def db_init(self):
if not os.path.exists(wordgroupz_dir):
os.mkdir(wordgroupz_dir, 0755)
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
tables = []
for x in c.execute('''select name from sqlite_master'''):
tables.append(x[0])
if not 'word_groups' in tables:
c.execute('''create table word_groups
(word text, grp text, details text, wordnet text, webster text, wiktionary text, accuracy text)''')
conn.commit()
if not 'groups' in tables:
c.execute('''create table groups
(grp text, details text, wordnet text, webster text, wiktionary text)''')
conn.commit()
#if not 'wiktionary' in tables:
# c.execute('''create table wiktionary
# ()''')
#alter table to port to new db format
c.execute("""select * from groups""")
group_cols = [i[0] for i in c.description]
for i in ['grp', 'details', 'wordnet','webster', 'wiktionary']:
if i not in group_cols:
c.execute("""alter table groups add column %s text"""%(i))
conn.commit()
for j in self.list_groups():
t = (j,)
c.execute("""update groups set wordnet="%s" where grp=?"""%(wordnet.get_definition(j)),t)
conn.commit()
c.execute("""select * from word_groups""")
word_groups_cols = [i[0] for i in c.description]
#print word_groups_cols
for i in ['wordnet', 'webster', 'wiktionary']:
if not i in word_groups_cols:
c.execute("""alter table word_groups add column %s text"""%(i))
for j in self.list_words():
t = (j,)
c.execute("""update word_groups set wordnet="%s" where word=?"""%(wordnet.get_definition(j)), t)
conn.commit()
if not 'accuracy' in word_groups_cols:
c.execute("""alter table word_groups add column accuracy text""")
c.execute("""update word_groups set accuracy='0:0'""")
conn.commit()
c.close()
conn.close()
def save_accuracy_for_word(self, w, val):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (w,)
c.execute("""update word_groups set accuracy='%s' where word=?"""%(val),t)
conn.commit()
c.close()
def list_groups(self):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
groups = []
for row in c.execute("""select grp from groups order by grp"""):
if row[0] is not u'':
groups.append(row[0])
c.close()
return groups
def delete_group(self, tree_value):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (tree_value,)
c.execute("""delete from groups where grp=?""",t)
conn.commit()
c.close()
def list_words_per_group(self,grp):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
words = []
t = (grp,)
for row in c.execute("""select word from word_groups where grp=?""",t):
if row[0] != '':
words.append(row[0])
c.close()
return words
def list_words(self):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
words = []
for row in c.execute("""select word from word_groups"""):
if row[0] != '':
words.append(row[0])
c.close()
return words
def add_to_db(self,word, grp, detail):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
conn.text_factory = str
if grp not in self.list_groups() and grp is not '':
if word is '':
wn = wordnet.get_definition(grp)
t = (grp,detail,wn, '', '')
else:
wn = wordnet.get_definition(grp)
t = (grp,'', wn, '', '')
c.execute("""insert into groups values (?,?,?,?,?)""",t)
conn.commit()
#allow words with no groups to be added
elif 'no-category' not in self.list_groups() and grp is '':
c.execute("""insert into groups values ('no-category','Uncategorized words', '', '', '')""")
if word is not '' and word not in self.list_words_per_group(grp):
if grp == '':
grp = 'no-category'
wn = wordnet.get_definition(word)
#print wn
t = (word, grp, detail, wn, '', '', '0:0')
c.execute('''insert into word_groups
values(?,?,?,?,?,?,?)''', t)
conn.commit()
c.close()
def get_details(self,selection):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (selection, )
if selection in self.list_groups():
t = (selection,)
c.execute("""select details from groups where grp=?""",t)
tmp = c.fetchone()[0]
if tmp is None:
return ''
else:
return tmp.strip("'")
elif selection is None:
return "Nothing selected"
else:
result = c.execute("""select word,grp,details from word_groups where word=?""",t)
tmp = result.fetchone()
return tmp[2]
def update_details(self,tree_value, details):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (tree_value,)
details = details.replace('"', "\'")
#details = details.replace("'", "\'")
if tree_value in self.list_groups():
c.execute("""update groups set details="%s" where grp=?"""%(details),t)
else:
c.execute("""update word_groups set details="%s" where word=?"""% (details),t)
conn.commit()
c.close()
def delete_word(self, tree_value):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (tree_value,)
c.execute("""delete from word_groups where word=?""",t)
conn.commit()
c.close()
def get_details_for_flashcard(self):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
c.execute("""select word, wordnet, accuracy from word_groups order by word""")
data = c.fetchall()
c.close()
conn.close()
return data
def save_wiktionary(self, word, data):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
conn.text_factory = str
#data = data.replace("'", "\'")
data = data.replace('"', "'")
#print [data]
t = (word,)
if word in self.list_groups():
c.execute('''update groups set wiktionary="%s" where grp=?'''%(data), t)
else:
c.execute('''update word_groups set wiktionary= "%s" where word=?'''%(data), t)
conn.commit()
c.close()
conn.close()
def save_wordnet(self, word, data):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
conn.text_factory = str
t = (word,)
data = data.replace("'", "\'")
data = data.replace('"', '\"')
if word in self.list_groups():
c.execute("""update groups set wordnet="%s" where grp=?"""%(data), t)
else:
c.execute("""update word_groups set wordnet="%s" where word=?"""%(data), t)
conn.commit()
c.close()
conn.close()
def save_webster(self, word, data):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
conn.text_factory = str
data = data.replace('"', "\'")
#data = data.replace("'", "\'")
t = (word,)
if word in self.list_groups():
c.execute('''update groups set webster="%s" where grp=?'''%(data), t)
else:
c.execute('''update word_groups set webster="%s" where word=?'''%(data), t)
conn.commit()
c.close()
conn.close()
def get_dict_data(self, dict, word):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (word,)
if word in self.list_groups():
c.execute("""select %s from groups where grp=?"""%(dict), t)
else:
c.execute("""select %s from word_groups where word=?"""%(dict), t)
data = c.fetchall()
#print 'in db'
#print data
#print data
c.close()
conn.close()
return data[0]
def check_ws(self, word):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (word,)
if word in self.list_groups():
c.execute("""select webster from groups where grp=?""",t)
data = c.fetchall()[0][0]
if data is None or data is u'':
status = False
else:
status = True
else:
c.execute("""select webster from word_groups where word=?""", t)
data = c.fetchall()[0][0]
if data is None or data is u'':
status = False
else:
status = True
c.close()
conn.close()
#print 'webster'
#print [data]
return status
def check_wn(self, word):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (word,)
if word in self.list_groups():
c.execute("""select wordnet from groups where grp=?""",t)
data = c.fetchall()[0][0]
if data is None or data is u'':
status = False
else:
status = True
else:
c.execute("""select wordnet from word_groups where word=?""", t)
data = c.fetchall()[0][0]
if data is None or data is u'':
status = False
else:
status = True
c.close()
conn.close()
return status
def check_wik(self, word):
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
t = (word,)
if word in self.list_groups():
c.execute("""select wiktionary from groups where grp=?""",t)
data = c.fetchall()[0][0]
if data is None or data is u'':
status = False
else:
status = True
else:
c.execute("""select wiktionary from word_groups where word=?""", t)
data = c.fetchall()[0][0]
if data is None or data is u'':
status = False
else:
status = True
c.close()
conn.close()
return status
class online_dict:
def __init__(self, addr = 'tcp!dict.org!2628'):
self.sock = self.dial(addr)
self.f = self.sock.makefile("r")
welcome = self.f.readline()
if welcome[0:4] != '220 ':
raise Exception("server doesn't want you (%s)" % welcome[0:4])
r, _ = self._cmd('CLIENT python client, versionless')
if r != '2((40))':
raise Exception('sending client string failed')
def dial(self, dialstr):
proto, host, port = string.split(dialstr, '!', 2)
if proto !='tcp':
raise Exception('Protocols other than tcp not implemented.')
try:
port = int(port)
except:
pass
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
return sock
def definition(self, word, db='!'):
for key, value in [('word', word), ('database', db)]:
if not self.validword(value):
raise Exception('invalid %s: "%s"' % (key, value))
r, line = self._cmd('DEFINE %s %s' % (self.quote(db), self.quote(word)))
if r == '552':
return []
if r[0] in ['4', '5']:
raise Exception('response to define: %s' % line)
defs = []
while 1:
line = self._read()
if line[0:4] == '151 ':
_, _, db, dbdescr = self.split(line, ' ', 3)
defs.append((db, dbdescr, '\n'.join(self._readlist())))
else:
break
return defs
def quote(self, word):
if ' ' in word or "'" in word or '"' in word:
return "'%s'" % string.replace(word, "'", "''")
return word
def split(self, line, delim, num):
def unquote(l):
if l[0] in ['"', "'"]:
q = l[0]
offset = 1
while 1:
offset = string.find(l[offset:], q)
if offset == -1:
raise Exception('Invalidly quoted line from server')
if l[offset-1:offset+1] == (r'\%s' % q):
offset += 1
else:
word = string.replace(l[1:offset+1], r'\%s' % q, q)
l = string.lstrip(l[offset+2:])
break
else:
word, l = string.split(l, delim, 1)
return word, l
r = []
l = line
while num != 0:
word, l = unquote(l)
r.append(word)
num -= 1
word, rest = unquote(l)
r.append(word)
return r
def validword(self, s):
bad = [chr(i) for i in range(20)]
if s == '':
return 0
for c in s:
if c in bad:
return 0
return 1
def _cmd(self, cmd):
self.sock.sendall(cmd + '\r\n')
self.f.flush()
line = self._read()
code = line[0:3]
return code, line
def _read(self):
line = self.f.readline()
if line[-1] == '\n':
line = line[0:-1]
if line[-1] == '\r':
line == line[0:-1]
return line
def _readlist(self):
lines = []
while 1:
line = self._read()
if line.startswith('.'):
break
if line[0:2] == '..':
line = line[1:]
lines.append(line)
return lines
def match(self, word, db='!', strat='.'):
for key, value in [('word', word), ('database', db), ('strategy', strat)]:
if not self.validword(value):
raise Exception('invalid %s: "%s"' % (key, value))
r, line = self._cmd('MATCH %s %s %s' % (self.quote(db), self.quote(strat), self.quote(word)))
if r == '552':
return []
if r[0] in ['4', '5']:
raise Exception('response to match: %s' % line)
lines = [tuple(self.split(l, ' ', 1)) for l in self._readlist()]
line = self._read()
if line[0:4] != '2((40)) ':
raise Exception('expected code 2((40)) after match (%s)' % line)
return lines
def get_def(self, word):
l = self.match(word, db='!', strat='exact')
for db, word in l:
defs = self.definition(word, db=db)
if defs == []:
#print >> sys.stderr, 'no-match'
return 2
db, dbdescr, defstr = defs[0]
s = '\n\n\n'.join([defstr for _, _, defstr in defs])
#print s
return s
class WebView(webkit.WebView):
def get_html(self):
self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
html = self.get_main_frame().get_title()
self.execute_script('document.title=oldtitle')
return html
class get_wiki_data:
def __init__(self, filename):
self.f = open(filename, 'r')
self.s = self.f.read()
self.sections = self.s.split('\n## ')
self.pos = ['Noun', 'Verb', 'Pronoun', 'Adjective', 'Adverb', 'Preposition', 'Conjunction', 'Interjection', 'Antonyms', 'Synonyms']
def get_contents(self):
for i in self.sections:
if i.split('\n')[0].endswith('Contents'):
self.contents = i
#print self.contents
def get_eng_fields(self):
self.fields = []
self.subfields = []
l = self.contents.split('\n *')
for i in l:
i = i.split('\n')
#print i[0]
#print i[0].find('English')
if i[0].find('English') > 0:
for j in i:
'''
if j.startswith(' *'):
j = j.strip(' * ')
j = j[5: j.find(']')]
self.fields.append([j])
'''
j = j.strip()
j = j.lstrip('* [')
j = j.split('][')[0]
j = j.split(' ', 1)
index = j[0].split('.')
if index[0] == '1':
self.fields.append(j)
#print self.fields
def cleanup(self):
#for i in self.fields:
for key in self.dict.keys():
#print self.dict[key]
for i in self.dict[key]:
links = re.findall('\[.*?\]', i)
for link in links:
index = self.dict[key].index(i)
if link.strip('[').strip(']').isdigit():
i = i.replace(link, '')
else:
i = i.replace(link, link.strip('[').strip(']'))
i = i.strip('\n ')
self.dict[key][index] = i
#print self.dict[key]
def show(self, key):
if key in self.dict.keys():
#print key+':'
count = 1
for i in self.dict[key]:
#print '\t'+ str(count)+'. '+i
count = count + 1
def get_field_details(self):
self.dict = {}
for i in self.sections:
if i.split('\n')[0].endswith('English'):
self.eng_sec = i
#print self.eng_sec
self.eng_secs = self.eng_sec.split('\n###')
count = 0
for i in self.eng_secs:
#count = 0
#self.dict[' '.join(self.fields[count])] = i
#count = count + 19
'''
i = i.split('[')
#print i[3]+'^s'
for j in i:
if j.find(']')>=0:
t = j.find(']')
sub = j[0:t+1]
if sub.rstrip(']').isdigit():
new = j[t+1:-1]
else:
new = j[0:t] + j[t+1:-1]
index = i.index(j)
#i.remove(j)
#i.insert(index, new)
i[index]=new
else:
index=i.index(j)
i[index]=j
i = ''.join(i)'''
#print i
'''print i
links = re.findall('\[.*?\]', i)
#print links
for link in links:
if link.strip('[').strip(']').isdigit:
i = i.replace(link, '')
else:
i = i.replace(link, link.strip('[').strip(']'))
#print i'''
t = i.split('\n')
if t[0].find('Etymology')>= 0:
#print '^i\n'+i
if 'Etymology' not in self.dict.keys():
self.dict['Etymology'] = ['\n'.join(t[1:-1])]
else:
self.dict['Etymology'].append('\n'.join(t[1:-1]))
elif t[0].find('Pronunciation')>=0:
if 'Pronunciation' not in self.dict.keys():
self.dict['Pronunciation'] = ['\n'.join(t[1:-1])]
else:
self.dict['Pronunciation'].append('\n'.join(t[1:-1]))
elif t[0].split(']')[-1] in self.pos:
title = t[0].split(']')[-1]
if title not in self.dict.keys():
self.dict[title] = ['\n'.join(t[1:-1])]
else:
self.dict[title].append('\n'.join(t[1:-1]))
self.cleanup()
self.show('Etymology')
#print self.dict.keys()
class wordzGui:
wordz_db=wordGroupzSql()
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file("wordgroupz.glade")
self.window = self.builder.get_object("MainWindow")
self.window.set_icon_from_file("/usr/share/pixmaps/wordgroupz.png")
self.window.set_title(_("wordGroupz"))
self.builder.connect_signals(self)
self.get_word = self.builder.get_object("get_word")
self.get_group = gtk.combo_box_entry_new_text()
self.get_group.set_tooltip_text("Enter a group for your word")
self.details = self.builder.get_object("textview1")
self.eventbox1 = self.builder.get_object('eventbox1')
#self.eventbox2 = self.builder.get_object('eventbox2')
#self.eventbox2.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#444444'))
self.eventbox1.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse('white'))
self.get_group_new = gtk.combo_box_entry_new_text()
self.get_group_new.set_tooltip_text("Enter a group for your word")
self.eventbox1.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#444444'))
self.get_group.child.connect('key-press-event',self.item_list_changed)
self.frame2 = self.builder.get_object('frame2')
self.frame2.hide()
self.preview_button = self.builder.get_object("preview_button")
#self.preview_entry = self.builder.get_object("preview_entry")
self.preview_button.connect("clicked",self.on_preview_button_clicked)
#self.preview_word = self.preview_entry.get_text()
#self.vpan = self.builder.get_object("vpaned1")
self.output_txtview = self.builder.get_object("textview2")
for x in wordz_db.list_groups():
self.get_group.append_text(x)
#self.get_group_new.append_text(x)
self.table1 = self.builder.get_object("table1")
#self.table2 = self.builder.get_object("table2")
self.get_group.show()
#self.get_group_new.show()
self.table1.attach(self.get_group, 1,2,1,2)
#self.table2.attach(self.get_group_new,0,1,0,1)
self.hbox5 = self.builder.get_object("hbox5")
self.hbox5.hide()
self.treestore = gtk.TreeStore(str, int)
self.new_word = []
conn = sqlite3.connect(db_file_path)
c = conn.cursor()
c.execute("""select word, accuracy from word_groups""")
self.ls = []
self.ls = c.fetchall()
#print 'l', self.ls
c.close()
self.acc_dict = {}
for i in self.ls:
if i[1] == u'0:0':
self.acc_dict[i[0]] = 0
else:
t = i[1].split(':')
acc = float(t[0])/float(t[1])*100
acc = int(acc)
self.acc_dict[i[0]] = acc
#print self.acc_dict.keys()
for group in wordz_db.list_groups():
l = wordz_db.list_words_per_group(group)
t = 0
count = 0
for i in l:
if i not in self.new_word:
t = t + self.acc_dict[i]
count = count + 1
if count != 0:
t = t/count
else:
t = 0
piter = self.treestore.append(None, [group,t])
#self.cellpb.hide()
for word in wordz_db.list_words_per_group(group):
self.treestore.append(piter, [word,self.acc_dict[word]])
#self.cellpb.set_property('text', None)
if self.acc_dict[word] == 0:
#self.cellpb.set_property('text', 'New')
pass
"""
try:
self.treestore.append(piter, [word,self.acc_dict[word]])
except:
self.treestore.append(piter, [word,'n/a'])"""
self.treeview = gtk.TreeView()
self.tvcolumn = gtk.TreeViewColumn(_('Word Groups'))
self.tvcolumn1 = gtk.TreeViewColumn(_('Accuracy'))
self.treeview.append_column(self.tvcolumn)
self.treeview.append_column(self.tvcolumn1)
#self.treeview.append_column(self.tvcolumn1)
self.cell = gtk.CellRendererText()
self.cellpb = gtk.CellRendererProgress()
#self.cell1 = gtk.CellRendererText()
self.tvcolumn.pack_start(self.cell, True)
self.tvcolumn.add_attribute(self.cell, 'text', 0)
self.tvcolumn1.pack_start(self.cellpb, True)
#self.tvcolumn.pack_start(self.cell1, True)
self.tvcolumn1.add_attribute(self.cellpb, 'value', 1)
self.cellpb.set_property('text', None)
self.tvcolumn1.set_cell_data_func(self.cellpb, self.custom_tree_col_view, None)
self.treeview.set_search_column(0)
self.tvcolumn.set_sort_column_id(0)
self.treeview.set_reorderable(False)
#self.tvcolumn1.pack_start(self.cell1, True)
#self.tvcolumn1.add_attribute(self.cell1, 'active', 1 )
self.selection = self.treeview.get_selection()
self.selection.connect('changed', self.tree_select_changed)
self.treeview.set_tooltip_text("Shows words classified in groups")
self.treeview.set_model(self.treestore)
self.treeview.show()
self.treeview.expand_all()
#self.scrolledwindow2 = self.builder.get_object("scrolledwindow2")
#self.scrolledwindow2.add_with_viewport(self.treeview)
self.scrolledwindow2 = gtk.ScrolledWindow()
self.scrolledwindow2.add(self.treeview)
self.scrolledwindow2.show()
#print self.scrolledwindow2.get_policy()
self.scrolledwindow2.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.vbox6 = self.builder.get_object('vbox6')
self.vbox6.pack_start(self.scrolledwindow2)
self.search=self.builder.get_object("search")
#self.search.connect('changed',self.on_search_changed)
self.chose_dict_hbox = self.builder.get_object('hbox7')
vseparator = gtk.VSeparator()
vseparator.show()
"""
self.chose_dict = gtk.combo_box_new_text()
for dict in ['webster', 'wordnet', 'wiktionary']:
self.chose_dict.append_text(dict)
self.chose_dict.set_active(1)
self.chose_dict.show()
self.hbox5.pack_start(self.chose_dict, False, True, padding = 5)"""
#webkit in scrolledwindow4
self.web_vbox = self.builder.get_object('vbox14')
#self.scroller = gtk.ScrolledWindow()
#self.scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.browser = WebView()
#self.browser.settings.enable_universal_access_from_file_uris(True)
self.settings = self.browser.get_settings()
#self.settings.set_property("enable-file-access-from-file-uris", True)
self.settings.set_property('enable-page-cache', True)
self.settings.set_property('user-stylesheet-uri', 'http://rtnpro.fedorapeople.org/main.css')
self.settings.set_property('enable-universal-access-from-file-uris', True)
self.browser.set_settings(self.settings)
#self.browser.show()
#self.scroller.add(self.browser)
#self.web_vbox.pack_start(self.scroller)
#self.scroller.show()
self.progress = gtk.ProgressBar()
self.progress.hide()
self.browser.connect("load-progress-changed", self.load_progress_changed)
self.browser.connect("load_started", self.load_started)
self.browser.connect("load-finished", self.load_finished)
#self.browser.connect('navigation-requested', self._navigation_requested_cb)
self.web_vbox.pack_start(self.progress, False)
#self.vbox9 = self.builder.get_object('vbox9')
#self.vbox9.pack_start(self.web_vbox)
#self.web_vbox.show()
#self.vbox9.show()
#self.status_label = self.builder.get_object('label9')
#self.status_label.hide()
#self.save_audio = self.builder.get_object('save_audio')
#self.save_audio.set_label('Download pronunciation')
self.vbox7 = self.builder.get_object('vbox7')
self.hbox2 = self.builder.get_object('hbox2')
self.welcome = self.builder.get_object('frame3')
self.frame4 = self.builder.get_object('frame4')
self.frame2.hide()
self.frame4.hide()
#self.hbox2.pack_start(self.welcome)
self.welcome.show()
self.note = self.builder.get_object('label16')
self.note.set_markup('<span foreground="white"><big><big><big><b>Welcome to wordGroupz</b></big></big></big></span>')
self.note.set_alignment(0.5, 0.2)
'''
self.toolbar = self.builder.get_object('toolbar1')
self.speak_icon = gtk.Image()
self.speak_icon.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_SMALL_TOOLBAR)
self.speak_button = self.toolbar.append_item(
'',
'Speak',
'Private',
self.speak_icon,
self.on_speak_clicked
)
self.toolbar.append_space()
#tool_item = self.toolbar.get_nth_item(1)
#tool_item.set_expand(False)'''
self.player = gst.element_factory_make("playbin2", "player")
fakesink = gst.element_factory_make("fakesink", "fakesink")
self.player.set_property("video-sink", fakesink)
bus = self.player.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_message)
#wiktionary
self.wiki_word = ''
self.browser_load_status = ''
#Menubar
'''self.file_item = gtk.MenuItem('_File')
self.dict_item = gtk.MenuItem('_Dictionary')
self.help_item = gtk.MenuItem('_Help')'''
self.menubar = self.builder.get_object('menubar1')
self.game_item = gtk.MenuItem('_Games')
self.game_item.show()
self.game_item_sub = gtk.Menu()
self.flash_card = gtk.MenuItem('_Flash Card')
self.mcq = gtk.MenuItem('_MCQ')
self.game_item_sub.append(self.flash_card)
self.game_item_sub.append(self.mcq)
self.game_item_sub.show_all()
self.menubar.insert(self.game_item, 1)
self.file_item = self.builder.get_object('file_item')
self.help_item = self.builder.get_object('help_item')
self.file_item_sub = gtk.Menu()
self.quit = gtk.MenuItem('_Quit')
self.file_item_sub.append(self.quit)
self.quit.show()
self.help_item_sub = gtk.Menu()
self.about = gtk.MenuItem('_About')
self.about.show()
self.help_item_sub.append(self.about)
self.game_item.set_submenu(self.game_item_sub)
self.file_item.set_submenu(self.file_item_sub)
self.help_item.set_submenu(self.help_item_sub)
self.flash_card.connect('activate', self.on_flash_card_clicked)
self.mcq.connect('activate', self.on_mcq_clicked)
self.about.connect('activate', self.on_about_clicked)
self.quit.connect('activate', self.on_MainWindow_destroy)
self.selected_word = self.builder.get_object('word_sel')
self.selected_word.hide()
self.audio_found = False
#details treeview
self.details_treestore = gtk.TreeStore(str)
self.details_treeview = gtk.TreeView(self.details_treestore)
self.details_cell = gtk.CellRendererText()
self.details_tvcolumn = gtk.TreeViewColumn(_('Details'),self.details_cell,markup=0)
self.details_treeview.append_column(self.details_tvcolumn)
#self.details_tvcolumn.pack_start(self.details_cell, True)
self.details_tvcolumn.add_attribute(self.details_cell, 'text', 0)
self.details_treeview.set_search_column(0)
self.details_tvcolumn.set_sort_column_id(0)
self.details_treeview.set_reorderable(False)
self.details_treeview.show_all()
self.details_treeview.expand_all()
self.scroller3 = self.builder.get_object('scrolledwindow3')
#self.scroller3.add(self.details_treeview)
#self.scroller3.show()
self.vbox12 = self.builder.get_object('vbox12')
self.vbox8 = self.builder.get_object('vbox8')
self.vbox13 = self.builder.get_object('vbox13')
#self.vbox12.set_spacing(10)
self.delete_b = self.builder.get_object('delete')
self.play_b = self.builder.get_object('speak')
def custom_tree_col_view(self, column, renderer, model, iter, data):
word = model.get_value(iter, 0)
for i in self.ls:
if i[0] == word and i[1] == '0:0' and word not in self.new_word:
#print i
self.new_word.append(word)
#print self.new_word
elif i[0]==word and i[1]!='0:0':
try:
self.new_word.remove(word)
except:
pass
#print self.new_word
if word in self.new_word:
renderer.set_property('text', 'New')
else:
renderer.set_property('text', None)
def on_preview_button_clicked(self, widget=None, event=None):
self.show_word = self.get_word.get_text()
#self.clear_preview_word()
self.show_meaning()
#def clear_preview_word(self, widget=None, event=None):
#label = gtk.Label()
#print 'test'
##label.set_text(' ')
#table = self.builder.get_object('table3')
#table.attach(label,0,1,0,1)
#txt = ' '
#label.set_markup('<span foreground="white"><b>%s</b></span>'%txt)
#label.show()
def show_meaning(self,widget=None, event=None):
self.frame2.hide()
self.welcome.hide()
#self.frame4.show()
table = self.builder.get_object('table3')
#preview_view_word = gtk.Label()
preview_view_word = self.builder.get_object('label13')
#event = gtk.EventBox()
#preview_view_word.set_has_tooltip(True)
preview_view_word.set_text(self.show_word)
#table.attach(preview_view_word,0,1,0,1)
#preview_view_word.set_markup('<span foreground="white"><b>%s</b></span>'%self.get_word)
#event.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#AED6EF'))
preview_view_word.show()
#preview_view_word.set_text(' ')
#event.show()
scrolledwindow5 = self.builder.get_object('scrolledwindow5')
preview_label = self.builder.get_object('preview_label')
preview_event = self.builder.get_object('eventbox3')
temp = wordnet.get_definition(self.show_word)
#scrolledwindow5.add(preview_event)
preview_event.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#AED6EF'))
preview_label.set_line_wrap(True)
preview_label.set_selectable(True)
preview_label.set_text(temp)
self.frame4.show()
def on_flash_card_clicked(self, widget=None, event=None):
self.window.hide()
game = games.flash()
#game.g.builder.get_object('window2').show()
self.window.show()
def on_preview_add_clicked(self, widget, data=None):
word = self.preview_entry.get_text()
self.new_word = []
if word not in self.new_word and word not in wordz_db.list_words():
self.new_word.append(word)
get_group_ch = self.get_group_new.child
group = get_group_ch.get_text()
conts = self.details.get_buffer()
start = conts.get_iter_at_offset(0)
end = conts.get_iter_at_offset(-1)
detail = conts.get_text(start, end)
wordz_db.add_to_db(word, group, detail)