-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
51 lines (44 loc) · 1.46 KB
/
parser.py
File metadata and controls
51 lines (44 loc) · 1.46 KB
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import multiprocessing
import json
import html.parser
import re
import sys
import unicodedata
class HTMLTextExtractor(html.parser.HTMLParser):
def __init__(self):
super(HTMLTextExtractor, self).__init__()
self.result = []
def handle_data(self, d):
self.result.append(d)
def get_text(self):
return ' '.join(self.result)
def collectResults(ret_cnt):
total_line = 595037
print("Processed {}% of Posts".format(str(100.0 * ret_cnt[1] / total_line)))
outfile = open("/tmp/washington_post.txt", "a", encoding="utf-8")
outfile.write(str(ret_cnt[0])+"\n")
def genPosts(post, cnt):
# ret = {}
sentence = HTMLTextExtractor()
if "contents" in post:
for block in post["contents"]:
if ("content" in block):
if (block["type"] == "sanitized_html"):
sentence.feed(str(block["content"]))
paragraph = sentence.get_text()
paragraph = unicodedata.normalize("NFKD",paragraph)
paragraph = paragraph.replace("\n", "")
# ret[post["id"]] = paragraph
return [paragraph, cnt]
if __name__ == "__main__":
pool = multiprocessing.Pool(multiprocessing.cpu_count() + 2)
wpfile = open(sys.argv[1], "r", encoding="utf-8")
posts = wpfile.readlines()
cnt = 0
for post in posts:
cnt += 1
pool.apply_async(genPosts, (json.loads(post), cnt,), callback = collectResults)
pool.close()
pool.join()