forked from gabrovski/knowevo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_update_db.py
588 lines (460 loc) · 14.8 KB
/
smart_update_db.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
import re, os, sys, traceback, cPickle
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from gravebook.models import Article, Category, Other
from incunabula.models import Article as IArticle
from incunabula.models import MasterArticle as IMasterArticle
from incunabula.models import Reference as IReference
from django.db import transaction
from django.utils.encoding import smart_str, smart_unicode
import settings
settings.DEBUG = False #important otherwise run out of memory on server
tpat = re.compile('title="(.+?)"')
idpat = re.compile('id="(.+?)"')
imgpat = re.compile('image="(.+?)"')
bpat = re.compile('birth="(.+?)"')
dpat = re.compile('death="(.+?)"')
plpat = re.compile('people_links="(.+?)"')
olpat = re.compile('other_links="(.+?)"')
catpat = re.compile('categories="(.+?)"')
LIMIT = -1
START = -1
def insert_xml(path):
f = open(path)
count = 0
for line in f:
count += 1
if count < START:
continue
print count,
if LIMIT > 0 and count == LIMIT:
break
try:
title = tpat.search(line).group(1)
wid = int(idpat.search(line).group(1))
img = imgpat.search(line).group(1)
birth = int(bpat.search(line).group(1))
death = int(dpat.search(line).group(1))
#cats = catpat.search(line).group(1).split('|')
#others = olpat.search(line).group(1).split('|')
art = Article(name=title, wid=wid, image=img, birth=birth, death=death)
art.save()
print title
except:
#raise
continue
f.close()
def add_cats(path):
f = open(path)
count = 0
for line in f:
count += 1
if count < START:
continue
print count
if LIMIT > 0 and count == LIMIT:
break
try:
title = tpat.search(line).group(1)
cats = catpat.search(line).group(1).split('|')
art = Article.objects.get(name=title)
for cat in cats:
if cat == '':
continue
try:
c = Category.objects.get(name=cat)
except:
c = Category(name=cat)
c.save()
art.categories.add(c)
art.save()
except:
continue
f.close()
def update_category_size():
c = 0
for cat in Category.objects.iterator():
cat.size = cat.article_set.count()
cat.save()
print c
c+=1
def build_people_graph(path):
f = open(path)
count = 0
for line in f:
count += 1
if count < START:
continue
print count
if LIMIT > 0 and count == LIMIT:
break
#if count == 2:
# break
try:
title = tpat.search(line).group(1)
people = plpat.search(line).group(1)
us = Article.objects.get(name=title)
us.people.clear()
us.peers.clear()
for link in people.split('|'):
if link == '' or link == title:
continue
try:
them = Article.objects.get(name=link)
except:
#raise
continue
us.people.add(them)
if (us.birth != -1 and us.death != -1 and us.death-them.birth > 15
and them.death-us.birth > 15):
us.peers.add(them)
us.save()
us.save()
except:
#raise
continue
f.close()
def load(path):
f = open(path)
revw = cPickle.load(f)
f.close()
return revw
def insert_incunabula_masters(revw):
c = 0
for k in revw.keys():
ma = IMasterArticle(name=k)
#print k
ma.save()
print c
c+=1
if LIMIT > 0 and c > LIMIT:
break
def insert_incunabula_articles(revw):
c = 0
sizes = revw['sizes']
for k in revw.keys():
try:
ma = IMasterArticle.objects.get(name=k)
for ed in revw[k]['editions']:
a = revw[k]['editions'][ed][0]
ia = IArticle(name=a['name'], art_id=a['id'], art_ed=ed, text=a['txt'],
prank=0.0, volume_score= (0.0 + len(a['txt'])) / sizes[ed],
match_master=ma,
match_score=-1.0)
#print a['name']
ia.save()
except:
continue
print c
c+=1
if LIMIT > 0 and c > LIMIT:
print 'wtf>'
break
def process_split(gravebook=True):
if not gravebook:
for name in os.listdir('_data/split'):
revw = load('_data/split/'+name)
insert_incunabula_masters(revw)
c = 0
for name in os.listdir('_data/split'):
revw = load('_data/split/'+name)
if gravebook:
gr_insert_incunabula_articles(revw)
else:
insert_incunabula_articles(revw)
print c
c+=1
def update_inc_volume_score():
eds = {3 : [], 9 : [], 11 : [], 15 : []}
for art in IArticle.objects.all():
eds[art.art_ed].append(len(art.text))
print 'prepped text dict'
avg = dict()
for k in eds.keys():
if eds[k] == []:
continue
avg[k] = sum(eds[k]) / len(eds[k])
print 'computed averages'
sdev = {3 : 0, 9 : 0, 11 : 0, 15 : 0}
for k in eds.keys():
for v in eds[k]:
sdev[k] += (avg[k] - v)**2
print 'computed variance'
for k in sdev:
if len(eds[k]) > 0:
sdev[k] = (sdev[k]/len(eds[k]))**0.5
for art in IArticle.objects.all():
art.volume_score = (len(art.text)-avg[art.art_ed]) / sdev[art.art_ed]
art.save()
def extract_people_graph(out):
f = open(out, 'w')
c = 0
for art in Article.objects.iterator():
f.write(str(art.wid)+' ')
for ed in art.people.iterator():
f.write(str(ed.wid)+' ')
f.write('\n')
print c
c+=1
f.close()
def get_top_people(pranked, lim=200):
f = open(pranked)
arts = []
for line in f:
arts.append(line.strip().split(':'))
f.close()
arts.sort(key=lambda x: float(x[1]), reverse=True)
for wid, prank in arts[:lim]:
art = Article.objects.get(wid=wid)
def get_inc_matched_edition():
for art in IArticle.objects.iterator():
print str(art)+'#'+str(art.art_ed)+'#'+str(art.match_master)
def fill_gr_linked_by():
c = 0
for art in Article.objects.iterator():
for art_to in art.people.iterator():
art_to.linked_by.add(art)
print c
c+=1
def fill_gr_peers():
c = 0
OVERLAP = 15
for art in Article.objects.iterator():
art.peers.clear()
for person in art.people.iterator():
if person.birth == -1 or person.death == -1:
continue
if person.death-art.birth > OVERLAP and art.death-person.birth > OVERLAP:
art.peers.add(person)
art.save()
print c, art.name
c+=1
def update_gr_vscores(path):
f = open(path)
c = 0
for line in f:
name, score = line.strip().split('\t')
try:
art = Article.objects.get(name=name.strip(), art_ed=1000)
art.vscore = float(score)
art.save()
print c, 'vscores'
c+=1
except Article.DoesNotExist:
continue
except:
traceback.print_exc(file=sys.stdout)
pass
f.close()
def add_inc_wiki_articles():
for art in IArticle.objects.iterator():
try:
master = art.match_master
wa = Article.objects.get(name=master.name.replace('_', ' '))
if art.name == wa.name:
continue
try:
bla = IArticle.objects.filter(name=wa.name+'waw', art_ed=1000)
if bla.count() > 0:
continue
except:
pass
a = IArticle(name=wa.name, art_id=wa.wid, art_ed=1000, text='', prank=0.0,
volume_score=wa.vscore, match_master=master, match_score=1.0)
a.save()
print a.name, 'incwiki'
except:
pass
def parse_name(name):
c = len(name)
while c > 0:
try:
name = smart_unicode(name[:c])
return name
except:
c-=1
def gr_insert_incunabula_articles(revw):
#clean up first
c = 0
'''
for a in Article.objects.filter(art_ed=15).iterator():
a.delete()
print c, 'del'
c+=1
'''
sizes = revw['sizes']
for k in revw.keys():
name = ' '.join(k.split('_'))
try:
ma = Article.objects.get(name=name)
for ed in revw[k]['editions']:
#make matches unique
if ma.article_set.filter(art_ed=ed).count() != 0:
continue
a = revw[k]['editions'][ed][0]
txt = a['txt']
if ed == 15:
pass
name = str(ed)+'_'+str(a['id'])+'_'+a['name']
name = parse_name(name)
if name == None:
continue
ia = Article(name=name, wid=a['id'], art_ed=ed, text=txt,
vscore = 0.0,
match_master=ma)
ia.save()
print ia
'''
for cat in ma.categories.iterator():
ia.categories.add(cat)
ia.save()
'''
ma.match_count = ma.article_set.count()
ma.save()
except Article.DoesNotExist:
continue
except:
raise
#print k,'not found'
continue
def fix_gr_years():
for art in Article.objects.iterator():
for cat in art.categories.iterator():
if 'births' in cat.name:
yr = re.search('\d+', cat.name)
if yr != None:
yr = yr.group()
art.birth = yr
art.save()
if 'deaths' in cat.name:
yr = re.search('\d+', cat.name)
if yr != None:
yr = yr.group()
art.death = yr
art.save()
print art
def gr_update_inc_volume_score():
eds = {3 : [], 9 : [], 11 : [], 15 : []}
for art in Article.objects.filter(art_ed__in=[3,9,11,15]).iterator():
eds[art.art_ed].append(len(art.text))
print 'prepped text dict'
avg = dict()
for k in eds.keys():
if eds[k] == []:
continue
avg[k] = sum(eds[k]) / len(eds[k])
print 'computed averages'
sdev = {3 : 0, 9 : 0, 11 : 0, 15 : 0}
for k in eds.keys():
for v in eds[k]:
sdev[k] += (avg[k] - v)**2
print 'computed variance'
for k in sdev:
if len(eds[k]) > 0:
sdev[k] = (sdev[k]/len(eds[k]))**0.5
for art in Article.objects.filter(art_ed__in=[3,9,11,15]).iterator():
art.vscore = (len(art.text)-avg[art.art_ed]) / sdev[art.art_ed]
art.save()
def gr_add_self_master():
for art in Article.objects.filter(art_ed=1000).iterator():
art.match_master = art
art.save()
def fix_names():
for art in Article.objects.iterator():
print art.name
art.name = smart_str(art.name)
art.text = smart_str(art.text)
art.save()
'''
for m in art.article_set.iterator():
m.name = str(m.name.encode("utf-8"))
m.save()
str(m.name)
'''
def gr_fix_all():
print os.getpid(), 'fixing'
c = 0
#filter for missing self in match master
for art in Article.objects.iterator():
#fix wrong years
for cat in art.categories.iterator():
if 'births' in cat.name:
yr = re.search('\d+', cat.name)
if yr != None:
yr = yr.group()
art.birth = int(yr)
art.save()
if 'deaths' in cat.name:
yr = re.search('\d+', cat.name)
if yr != None:
yr = int(yr.group())
art.death = yr
art.save()
#fix self reference
art.match_master = art
art.save()
c+=1
if c % 100 == 0:
print c, os.getpid(), 'fix all'
def fix_count():
c = 0
for art in Article.objects.iterator():
art.match_count = art.article_set.count()
art.save()
print c
c+=1
def gr_insert_ed_15(split_dir):
#for a in Article.objects.filter(art_ed=15).iterator():
# a.delete()
w = open('15notmatched', 'w')
MAXC = 5
c = 0
for pkl in os.listdir(split_dir):
pkl = split_dir+pkl
f = open(pkl)
arts = cPickle.load(f)
f.close()
for a in arts:
match = ' '. join(a['matched'][0].split('_'))
ma = None
try:
ma = Article.objects.get(name=match, art_ed=1000)
except:
for candid in a['candids'][:MAXC]:
try:
match = ' '. join(candids.split('_'))
ma = Article.objects.get(name=match, art_ed=1000)
except:
continue
if ma == None:
w.write(a['name']+' not matched\n')
continue
ed = 15
name=str(ed)+'_'+str(a['id'])+'_'+a['name']
name = smart_unicode(name)
ia = Article(name=name, wid=a['id'], art_ed=ed, text=a['txt'],
vscore = 0.0,
match_master=ma)
ia.save()
ma.match_count = ma.article_set.count()
ma.save()
c+=1
print c, 'ed15'
w.close()
def final_db_update():
gr_insert_ed_15('_data/ed_15/')
process_split(gravebook=True)
gr_update_inc_volume_score()
if __name__ == '__main__':
#final_db_update()
#process_split(gravebook=True)
#gr_update_inc_volume_score()
#-revw = load('_data/sample_revw.pkl')
#-gr_insert_incunabula_articles(revw)
#gr_update_inc_volume_score()
#fix_count()
#748587
#gr_fix_all()
#fill_gr_peers()
#update_gr_vscores('_data/wiki_vol_zscores')
#fill_gr_linked_by()
build_people_graph('_data/people_articles_filtered.txt')