Refine code
Browse files- wikipedia.py +28 -9
wikipedia.py
CHANGED
@@ -24,7 +24,7 @@ import xml.etree.cElementTree as etree
|
|
24 |
from urllib.parse import quote
|
25 |
import mwparserfromhell
|
26 |
from tqdm import tqdm
|
27 |
-
|
28 |
import datasets
|
29 |
|
30 |
logger = datasets.logging.get_logger(__name__)
|
@@ -1025,16 +1025,35 @@ class Wikipedia(datasets.GeneratorBasedBuilder):
|
|
1025 |
|
1026 |
print("Parsing and cleaning Wikipedia examples")
|
1027 |
|
1028 |
-
|
1029 |
-
|
1030 |
-
|
1031 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1032 |
|
1033 |
-
|
1034 |
-
for item in parse_and_clean(filepath):
|
1035 |
-
if item is not None:
|
1036 |
-
yield item
|
1037 |
|
|
|
|
|
1038 |
|
1039 |
def _parse_and_clean_wikicode(raw_content, parser, language):
|
1040 |
"""Strips formatting and unwanted sections from raw page content."""
|
|
|
24 |
from urllib.parse import quote
|
25 |
import mwparserfromhell
|
26 |
from tqdm import tqdm
|
27 |
+
import multiprocess
|
28 |
import datasets
|
29 |
|
30 |
logger = datasets.logging.get_logger(__name__)
|
|
|
1025 |
|
1026 |
print("Parsing and cleaning Wikipedia examples")
|
1027 |
|
1028 |
+
result_q = multiprocess.Queue()
|
1029 |
+
processes = []
|
1030 |
+
|
1031 |
+
with multiprocess.Manager() as manager:
|
1032 |
+
for filepath in filepaths:
|
1033 |
+
def parse_and_clean():
|
1034 |
+
content = _extract_content(filepath)
|
1035 |
+
for obj in tqdm(content):
|
1036 |
+
result_q.put(_clean_content(obj, language=language))
|
1037 |
+
result_q.put(ProcessDone())
|
1038 |
+
|
1039 |
+
p = multiprocess.Process(target=parse_and_clean, args=(examples,))
|
1040 |
+
p.start()
|
1041 |
+
processes.append(p)
|
1042 |
+
|
1043 |
+
n = len(filepaths)
|
1044 |
+
complete = 0
|
1045 |
+
while complete < n:
|
1046 |
+
obj = result_q.get()
|
1047 |
+
if isinstance(obj, ProcessDone):
|
1048 |
+
complete += 1
|
1049 |
+
continue
|
1050 |
+
if obj is None:
|
1051 |
+
continue
|
1052 |
|
1053 |
+
yield obj
|
|
|
|
|
|
|
1054 |
|
1055 |
+
for p in processes:
|
1056 |
+
p.join()
|
1057 |
|
1058 |
def _parse_and_clean_wikicode(raw_content, parser, language):
|
1059 |
"""Strips formatting and unwanted sections from raw page content."""
|