-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsefanfic.py
executable file
·755 lines (610 loc) · 22.4 KB
/
parsefanfic.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 17 14:54:14 2021
@author: mdp38
"""
import glob
import re
from bs4 import BeautifulSoup
import json
import codecs
import copy
import collections
import numpy as np
count = 0
wk = re.compile('Witch-King$')
wk2 = re.compile('Witch-King,')
fix_dict = {'Elvenking Thranduil':'Thranduil',
'The Necromancer/Sauron': 'Sauron',
re.compile('Witch-King(,|$)'): r'Witch-King of Angmar\1'}
with open("genres.txt") as inF:
genres = [t.strip() for t in inF.readlines()]
with open("lotr_titles.txt") as inF:
wiki_titles = [t.lower().strip() for t in inF.readlines()]
def parse_files(filename):
langs = []
files = glob.glob(filename)
n = len(files)
print(f"Parsing {n} total files.")
for i,f in enumerate(files):
if (i % 200 == 0):
print(f"Parsing file {i} of {n}. Remaining: {n-i}")
with open(f) as infile:
for line in infile.readlines():
res = parse_blurb(line)
if res:
langs.append(res)
return langs
def parse_blurb(b):
meta = {}
soup = BeautifulSoup(b, "html.parser")
t_block = soup.findAll("a", class_="stitle")
if (len(t_block) != 1 ):
print ("We have an error title not right!")
print(b)
meta["title"] = t_block[0].text
a_block = [a.text for a in soup.findAll("a", class_="") if a.text != '']
if len(a_block) != 1:
print ("We have an error author not right!")
print(b)
meta["author"] = a_block[0]
t_block = [x.text for x in soup.findAll("div", class_="z-padtop2") if x.text != '']
tags = re.split(r"\s-\s", t_block[0])
if tags[0] == 'Crossover':
tags.pop(0)
meta['fandoms'] = [x.strip() for x in tags.pop(0).split('&', 1)]
else:
pass
#print("Check on " + meta["title"])
params = [x for x in tags if x.find(":") < 0]
pairs = [x for x in tags if x.find(":") >= 0]
if 'Complete' in params:
params.remove('Complete')
if 'English' in params:
params.remove('English')
for p in params:
if p in genres:
meta["genre"] = p
else:
meta["characters"] = p
for old, new in fix_dict.items():
meta["characters"] = re.sub(old, new, meta["characters"])
for pair in pairs:
k = pair.split(":")[0].lower()
v = pair.split(":")[1]
if k in ("rated", "words", "reviews", "favs", "follows", "published"):
meta[k]= v
return meta
def save_json(li, filename = "lotr.json"):
with codecs.open(filename, 'w', encoding="utf-8") as fout:
json.dump(li, fout, ensure_ascii=False)
def load_jsons(glob_pattern):
data = []
files = glob.glob(glob_pattern)
n = len(files)
print(f"Parsing {n} total files.")
for i,f in enumerate(files):
if (i % 200 == 0):
print(f"Parsing file {i} of {n}. Remaining: {n-i}")
with open(f) as infile:
d = json.load(infile)
if d is None:
print(f"Error in file {f}")
else:
data = data + d
return data
def ao3_fix_relationships(li):
for blurb in li:
tmp = []
for r in blurb["relationships"]:
if len(r) == 1:
new_r = r[0].split('/')
if new_r not in tmp:
tmp.append(sorted(new_r))
else:
if r not in tmp:
tmp.append(sorted(r))
blurb["relationships"] = sorted(tmp)
def merge_blurbs(*lists, dupes):
md = {}
for li in lists:
for b in li:
k = (b["title"], b["author"])
if k in md:
print(f'Found a dupe for {b["title"]} by {b["author"]}')
for f in b['fandoms']:
if f not in md[k]['fandoms']:
md[k]['fandoms'].append(f)
dupes.append(b)
else:
md[k] = b
return md
def get_relationships(li):
for l in li:
if "characters" not in l:
l["characters"] = "NA"
l["relationships"] = "NA"
continue
if "[" not in l["characters"]:
#now also clean up the characters list
l["characters"] = [x.strip().replace(',','') for x in l["characters"].split(',')]
l["relationships"] = "NA"
continue
#Find the relationships (between [])
res = re.findall(r'\[.*?\]', l['characters'])
#Turn each relationship into a list
relationships =[]
for x in res:
new_r = []
parts = x.split(',')
for y in parts:
y = y.strip().replace("[", '').replace("]", '')
new_r.append(y)
relationships.append(new_r)
#now also clean up the characters list
l["characters"] = l["characters"].replace("[",'').replace("]", ',')
l["characters"] = [x.strip() for x in l["characters"].split(',') if x.strip() != ""]
if len(relationships) > 0:
l["relationships"] = relationships
else:
l["relationsips"] = "NA"
def get_all_relationships(li):
rels = []
for l in li:
if l["relationships"] == "NA":
continue
for r in l["relationships"]:
for s in r:
rels.append(s)
return rels
def has_relationship(li, match):
for i, blurb in enumerate(li):
for r in blurb["relationships"]:
for elem in r:
if match in elem:
print(f"Found {match} in index {i}")
print(r)
def get_all_r_containing(li, match):
blurbs = []
for i, blurb in enumerate(li):
for r in blurb["relationships"]:
for elem in r:
if match.lower() in elem.lower():
blurbs.append(blurb)
return(blurbs)
def get_all_c_containing(li, match):
blurbs = []
idx = []
for i, blurb in enumerate(li):
for r in blurb["characters"]:
if match.lower() in r.lower():
blurbs.append(blurb)
idx.append(i)
return(idx)
def count_relationships(li, names):
rels = {}
for a in names:
for b in names:
key = (a, b)
rels[key] = 0
for l in li:
for groups in l["relationships"]:
for i,r in enumerate(groups):
for j,s in enumerate(groups):
if r==s: continue
key = (r, s)
if key in rels:
rels[key] += 1
#else:
# print(f"Key {key} not found.")
return rels
def make_rel_json(edge_dict, names):
nodes = []
edges = []
for i,name in enumerate(names):
new_dict = {}
new_dict['id'] = i
new_dict['name'] = name
nodes.append(new_dict)
for k,v in edge_dict.items():
if v == 0:
continue
new_dict = {}
new_dict['source'] =k[0]
new_dict['target'] = k[1]
new_dict['weight'] = str(v)
new_dict['source_index'] = names.index(k[0])
new_dict['target_index'] = names.index(k[1])
edges.append(new_dict)
json_out = {}
json_out['nodes'] = nodes
json_out['edges'] = edges
json.dumps(json_out)
save_json(json_out, filename = "relationships.json")
def get_all_characters(li):
chars = []
for l in li:
if l["characters"] == "NA" or len(l["characters"])==0:
continue
for r in l["characters"]:
if r == "":
continue
chars.append(r)
return chars
def remove_tags_matching(li, matches):
return None
def dict_to_matrix(d, names):
keys = []
for a in names:
for b in names:
keys.append((a, b))
matrix = np.array([d[i] for i in keys])
print(matrix)
return(matrix)
filename = "/Users/mdp38/outputS/*txt"
silm = parse_files(filename)
for s in silm:
s['fandoms'] = ['The Silmarillion']
filename = "/Users/mdp38/output2/*txt"
lotr = parse_files(filename)
for s in lotr:
s['fandoms'] = ['The Lord of the Rings']
filename = "/Users/mdp38/outputHobbit/*txt"
hobbit = parse_files(filename)
for s in hobbit:
s['fandoms'] = ['The Hobbit']
lx_filename = 'outpuCrossoversLOTR/*txt'
lx = parse_files(lx_filename)
hx_filename = 'outpuCrossoversHobbit/*txt'
hx = parse_files(hx_filename)
dupes = []
merged = merge_blurbs(lotr, hobbit, silm, lx, hx, dupes = dupes)
raw = copy.deepcopy(merged)
li_merged = list(raw.values())
get_relationships(li_merged)
li_rs = get_all_relationships(li_merged)
rs_occ = collections.Counter(li_rs)
chars = get_all_characters(li_merged)
collections.Counter(chars).most_common(20)
all_rs = get_all_relationships(li_merged)
occurences = collections.Counter(all_rs)
occurences.most_common(20)
glob_pattern = "ao3_with_author/*.json"
ao3 = load_jsons(glob_pattern)
ao3_rs = get_all_relationships(ao3)
collections.Counter(ao3_rs).most_common(20)
def split_by_years(li):
valid_list = [x for x in li if ',' in x["published"]]
years = sorted(set([x["published"].split(',')[1].strip() for x in valid_list]))
by_year = {}
for y in years:
by_year[y] = []
for l in valid_list:
year = l['published'].split(',')[1].strip()
by_year[year].append(l)
return by_year
def make_character_json(d):
output = []
keys = sorted(d.keys())[1:]
for k in keys:
year_dict = {}
year_dict['year'] = k
v = d[k]
year_dict['total_tolkien'] = len(v)
year_dict['characters'] = []
chars = get_all_characters(v)
occ = dict(collections.Counter(chars).most_common())
for k2,v2 in occ.items():
char_dict = {}
char_dict['name'] = k2
char_dict['appearances'] = v2
year_dict['characters'].append(char_dict)
output.append(year_dict)
return output
def fix_characters(li, fix_dict):
for old, new in fix_dict.items():
for l in li:
l["characters"] = [x.replace(old, new) for x in l["characters"]]
for r in l["relationships"]:
l["relationships"] = [x.replace(old, new) for x in r]
newp = []
for p in set(probs):
#print(p)
result = re.search(r'\(([^)]+)\)', p)
if result:
print(result[1])
newp.append(p)
#search wiki titles for characters:
found = []
unfound = []
idx = []
string_vals = []
wiki = [x.lower() for x in wiki_titles]
for i, line in enumerate(alts):
foundMatch = False
chars = [x.strip() for x in line.split("|")]
found_str = ''
for c in chars:
if c.lower() in wiki:
foundMatch = True
found_str += 'T'
else:
found_str += 'F'
if foundMatch:
found.append(line)
idx.append(i)
string_vals.append(found_str)
else:
unfound.append(line)
changes = {}
for line in found:
chars = [x.strip() for x in line.split("|")]
replacements = {}
with open("replacements.txt") as inF:
for line in inF.readlines():
args = line.split(":")
if args[0] in replacements:
print("Problem with " + args[0])
else:
replacements[args[0].strip()] = args[1].strip()
def clean_up_ao3(list_to_copy):
li = copy.deepcopy(list_to_copy)
with open("tags_to_remove.txt") as infile:
tags_to_remove = infile.readlines()
with open("replacements.txt") as inF:
for line in inF.readlines():
args = line.split(":")
if args[0] in replacements:
print("Problem with " + args[0])
else:
replacements[args[0].strip()] = args[1].strip()
#simplify OCs
for l in li:
if l["characters"] == "NA" or len(l["characters"])==0: continue
l["characters"] = [re.sub(r"- ?[Cc]haracter", "", x).strip() for x in l["characters"]]
#remove parentheses from tags
l["characters"] = [re.sub(r"\(([^)]+)\)", "", x).strip() for x in l["characters"]]
for bad_value in tags_to_remove:
l["characters"] = [x for x in l["characters"] if bad_value.lower() not in x.lower()]
for i in range(0, len(l["characters"])):
elem = l["characters"][i].lower()
if re.search(r"\boc", elem):
l["characters"][i] = "OC"
elif re.search(r"\bofc", elem):
#print(f"{elem} is now OFC")
l["characters"][i] = "OFC"
elif re.search(r"\bomc", elem):
#print(f"{elem} is now OMC")
l["characters"][i] = "OMC"
elif re.search(r"original (\s*|.*)character", elem):
if "female" in elem:
l["characters"][i] = "OFC"
elif "male" in elem:
l["characters"][i] = "OMC"
else:
l["characters"][i] = "OC"
for old, new in replacements.items():
if old.lower() in elem or new.lower() in elem:
l["characters"][i] = new
#print(f"Replacing {elem} with {new}")
#print("After:" + str(len(l["characters"])) )
return li
def create_fandom_list(li):
fandoms = []
books = []
for idx, a in enumerate(li):
new_fandoms = []
tolkien= []
for f in li[idx]['fandoms']:
f = re.sub(r'\(([^)]+)\)', '', f)
f = f.split('-')[0].strip()
f = f.replace(' ', ' ')
f = f.replace('RPF', '').strip()
if re.search(r'\s?[Ll]ord [Oo]f', f) or re.search(r'[Ll][Oo][[Tt][[Rr]', f):
tolkien.append('The Lord of the Rings')
elif re.search(r'\s?[Hh]obbit', f):
tolkien.append('The Hobbit')
elif 'silma' in f.lower():
tolkien.append('The Silmarillion')
elif 'middle' in f.lower():
tolkien.append('The Silmarillion')
elif 'tolkien' in f.lower() or 'thranduil' in f.lower():
continue
elif 'harry' in f.lower():
new_fandoms.append('Harry Potter')
elif re.search(r"[Mm]arvel(['\sv]|$)", f):
new_fandoms.append('Marvel Universe')
elif re.search(r"Thor([\s:]|$)", f):
new_fandoms.append("Thor")
new_fandoms.append("Marvel Universe")
elif 'Captain America' in f:
new_fandoms.append("Captain America")
new_fandoms.append("Marvel Universe")
elif 'Iron Man' in f or 'Ironman' in f:
new_fandoms.append("Iron Man")
new_fandoms.append("Marvel Universe")
elif 'sherlock' in f.lower():
new_fandoms.append('Sherlock Holmes')
elif 'buffy' in f.lower():
new_fandoms.append('Buffy the Vampire Slayer')
elif re.search(r'[Ss]tar\s?[Ww]ars', f):
new_fandoms.append('Star Wars Universe')
elif re.search('[Ss]tar\s?[Tt]rek', f):
new_fandoms.append('Star Trek Universe')
elif re.search(r'^[Dd][Cc]', f):
new_fandoms.append("DC Universe")
elif "Batman" in f:
new_fandoms.append("Batman")
new_fandoms.append("DC Universe")
elif "Superman" in f:
new_fandoms.append("Superman")
new_fandoms.append("DC Universe")
elif "Wonder Woman" in f:
new_fandoms.append("Wonder Woman")
new_fandoms.append("DC Universe")
elif "Aquanman" in f:
new_fandoms.append("Aquaman")
new_fandoms.append("DC Universe")
elif re.search(r"^Flash$", f) or re.search(r"^The Flash$", f):
new_fandoms.append("The Flash")
new_fandoms.append("DC Universe")
elif 'ice and fire' in f.lower() or 'throne' in f.lower():
new_fandoms.append("Game of Thrones")
elif 'percy' in f.lower():
new_fandoms.append('Percy Jackson and the Olympians')
elif 'Hunger' in f:
new_fandoms.append('The Hunger Games')
elif f == 'Twilight':
new_fandoms.append('Twilight')
elif 'disney' in f.lower():
new_fandoms.append('Disney Universe')
elif 'shannara' in f.lower():
new_fandoms.append('The Shannara Chronicles')
elif 'pokemon' in f.lower():
new_fandoms.append('Pokemon')
elif 'doctor who' in f.lower():
new_fandoms.append('Doctor Who')
elif 'Actor' in f or 'Real Person' in f or 'thorin' in f or 'Multi' in f:
continue
elif 'vampire diaries' in f.lower():
new_fandoms.append('The Vampire Diaries')
elif 'Highlander' in f:
new_fandoms.append('Highlander')
elif 'Witcher' in f:
new_fandoms.append('The Witcher')
elif 'avengers' in f.lower():
new_fandoms.append('The Avengers')
new_fandoms.append('Marvel Universe')
elif 'walking dead' in f.lower():
new_fandoms.append('The Walking Dead')
elif 'stargate' in f.lower():
new_fandoms.append('Stargate')
else:
new_fandoms.append(f)
li[idx]['fandoms'] = new_fandoms
li[idx]['series'] = tolkien
#fandoms += list(set(new_fandoms))
#books += list(set(tolkien))
fandoms += new_fandoms
books += tolkien
return fandoms, books
#return fandoms + books
def make_fandom_json(d):
output = []
keys = sorted(d.keys())
for k in keys:
year_dict = {}
year_dict['year'] = k
v = d[k]
(fandoms, books) = create_fandom_list(v)
mc_f = dict(collections.Counter(fandoms).most_common(50))
series = dict(collections.Counter(books).most_common())
year_dict['fandoms'] = []
year_dict['series'] = []
for k2,v2 in mc_f.items():
char_dict = {}
char_dict['fandom'] = k2
char_dict['count'] = v2
year_dict['fandoms'].append(char_dict)
for k2, v2 in series.items():
char_dict = {}
char_dict['series'] = k2
char_dict['count'] = v2
year_dict['series'].append(char_dict)
output.append(year_dict)
return output
validated_chars = load_jsons("validated_characters2.json")
final_chars = [x['name'] for x in validated_chars]
for i in range(0, len(validated_chars)):
if validated_chars[i]["gender"] == 'Males' or validated_chars[i]["gender"] == 'male':
validated_chars[i]["gender"] = 'Male'
elif validated_chars[i]["gender"] =='Other':
validated_chars[i]["gender"] = 'NA'
ao3 = load_jsons("ao3_with_author/parsed*.json")
race_corrections = { 'Orc':'Orcs',
'Goblin':'Orcs',
'Man':'Men',
'Men':'Men',
'Half':'Elves',
'Elves':'Elves',
'Elven':'Elves',
'Elf':'Elves',
'Unknown':'Unknown',
'Ents':'Ents',
'Dwarf':'Dwarves',
'Hobbit':'Hobbits',
'Eagle':'Great Eagles',
'Balrog':'Balrog',
'Dwarven':'Dwarves',
'Horse':'Horses',
}
for i in range(0, len(validated_chars)):
race = validated_chars[i]["race"]
if re.search(r'Ent$', race):
validated_chars[i]["race"] = 'Ents'
continue
for old, new in race_corrections.items():
if old in race:
validated_chars[i]["race"] = new
culture_corrections = {
'Shire':'Shire-hobbits',
'Hobbits of Bree':'Bree-hobbits',
'Stoor':'Stoor-hobbits',
'Took':'Shire-hobbits',
'Brandybuck':'Shire-hobbits',
'Ñoldor':'Ñoldor (Deep Elves)',
'Noldor':'Ñoldor (Deep Elves)',
'Tatyar':'Ñoldor (Deep Elves)',
'Sindar':'Sindar (Grey Elves)',
'Silvan':'Silvan (Wood Elves)',
'Vanyar':'Vanyar (Light Elves)',
'Minyar':'Vanyar (Light Elves)',
'Gondolindrim':'Gondolindrim (Hidden Elves)',
'Nandor':'Silvan (Wood Elves)',
'Nelyar':'Teleri (Sea Elves)',
'Teleri':'Teleri (Sea Elves)',
'Falmari':'Teleri (Sea Elves)',
'Elves of Rivendell':'Elves of Rivendell',
'Rohirri':'Rohirrim',
'Rohan':'Rohirrim',
'Gondor':'Men of Gondor',
'Númenórean':'Númenóreans',
'Númenoreans': 'Númenóreans',
'Bëor':'Edain',
'Haleth':'Edain',
'Durin':"Durin's Folk (Dwarves)",
'Firebeards':'Dwarves of Nogrod',
'Broadbeams':'Dwarves of Belegost',
'Uruk':'Uruk-hai of Isengard',
'Moria Orcs':'Orcs of Moria',
'Lake-town':'Men of Dale',
'Istari':'Istari (Wizards)',
'Wizards':'Istari (Wizards)',
'Maia':'Maiar',
'Balrog':'Maiar',
'Valar':'Valar',
'Ringwraith':'Nazgûl',
'Easterling':'Easterlings',
'Eagles':'Great Eagles',
'Galadhrim':'Tree Elves',
'NA':"Unkown",
'Edain':'Men of Edain'
}
for i in range(0, len(validated_chars)):
if 'culture' not in validated_chars[i]: continue
culture = validated_chars[i]["culture"]
if 'Dúnedain' in culture:
if 'Gondor'in culture or 'Dol Amroth' in culture:
validated_chars[i]["culture"] = 'Dúnedain of Gondor'
else:
validated_chars[i]["culture"] = 'Dúnedain of the North'
# if re.search(r'Ent$', race):
# validated_chars[i]["race"] = 'Ents'
# continue
for old, new in culture_corrections.items():
if old in culture:
validated_chars[i]["culture"] = new
cultures = [x["culture"] for x in validated_chars if 'culture' in x]
set(cultures)
collections.Counter(cultures).most_common()