This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdewiki_functions.py
54 lines (46 loc) · 1.84 KB
/
dewiki_functions.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
from threading import Thread
import json
import re
from html2text import html2text as htt
import wikitextparser as wtp
def dewiki(text):
text = wtp.parse(text).plain_text() # wiki to plaintext
text = htt(text) # remove any HTML
text = text.replace('\\n',' ') # replace newlines
text = re.sub('\s+', ' ', text) # replace excess whitespace
return text
def analyze_chunk(text):
try:
if '<redirect title="' in text: # this is not the main article
return None
if '(disambiguation)' in text: # this is not an article
return None
else:
title = text.split('<title>')[1].split('</title>')[0]
title = htt(title)
if ':' in title: # most articles with : in them are not articles we care about
return None
serial = text.split('<id>')[1].split('</id>')[0]
content = text.split('</text')[0].split('<text')[1].split('>', maxsplit=1)[1]
content = dewiki(content)
return {'title': title.strip(), 'text': content.strip(), 'id': serial.strip()}
except Exception as oops:
print(oops)
return None
def save_article(article, savedir):
doc = analyze_chunk(article)
if doc:
print('SAVING:', doc['title'])
filename = doc['id'] + '.json'
with open(savedir + filename, 'w', encoding='utf-8') as outfile:
json.dump(doc, outfile, sort_keys=True, indent=1, ensure_ascii=False)
def process_file_text(filename, savedir):
article = ''
with open(filename, 'r', encoding='utf-8') as infile:
for line in infile:
if '<page>' in line:
article = ''
elif '</page>' in line: # end of article
Thread(target=save_article, args=(article, savedir)).start()
else:
article += line