/
to_jsonl.py
278 lines (223 loc) · 8.66 KB
/
to_jsonl.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import json
import codecs
import fnmatch
import logging
import itertools
from multiprocessing import Pool
import bs4
from tqdm import tqdm
def recursive_iglob(rootdir='.', pattern='*'):
"""Recursive version of iglob.
Taken from https://gist.github.com/whophil/2a999bcaf0ebfbd6e5c0d213fb38f489
"""
for root, dirnames, filenames in os.walk(rootdir):
for filename in fnmatch.filter(filenames, pattern):
yield os.path.join(root, filename)
def fix_unclosed(tag_name, html):
return re.sub(r'(<{}.*[^/-])>'.format(tag_name), r'\1 />', html)
def convert_jptimes(input_file, content):
content = fix_unclosed('meta', content)
content = fix_unclosed('link', content)
doc = bs4.BeautifulSoup(content, 'html.parser')
file_name_components = input_file.split('/')
date = '/'.join(file_name_components[2:5])
categories = file_name_components[5:-1]
file_name = file_name_components[-1]
url = 'https://' + input_file
try:
author = doc.find('meta', attrs={'name': 'author'})['content']
except TypeError:
try:
author = doc.find('a', attrs={'class': 'author'}).text
except AttributeError:
logging.warning('jp:No author in {}'.format(input_file))
# Extracting title
title = doc.find('meta', property='og:title')
if not title:
logging.error('no title for {}'.format(input_file))
#print(doc.find_all('meta'))
#input()
return
title = re.sub(r'\s+', ' ', title['content']).strip()
title = re.sub(r'\| The Japan Times', '', title)
if not len(title):
logging.error('no title for {}'.format(input_file))
return
# Extracting headline
headline = doc.find('meta', property='og:description')
if not headline:
logging.error('no headline for {}'.format(input_file))
return
headline = re.sub(r'\s+', ' ', headline['content']).strip()
if not len(headline):
logging.error('no headline for {}'.format(input_file))
return
# Extracting article content
body = doc.find('div', attrs={'id': 'jtarticle'})
if not body:
logging.error('no body for {}'.format(input_file))
return
body = re.sub(r'\s+', ' ', body.get_text(separator=' ')).strip()
if not len(body):
logging.error('no body for {}'.format(input_file))
return
# Extracting keywords
keywords = doc.find('meta', attrs={'name': 'keywords'})
if keywords is None:
logging.error('no keywords for {}'.format(input_file))
return
keywords = re.sub(r'\s+', ' ', keywords['content']).strip()
keywords = keywords.split(', ')
# remove empty keywords
keywords = [k.split(';') for k in keywords if k]
if not keywords:
logging.error('no keywords for {}'.format(input_file))
return
return {
'title': title, 'headline': '', 'abstract': body,
'keyword': keywords, 'file_name': file_name,
'date': date, 'categories': categories, 'url': url,
'author': author
}
def convert_nytimes(input_file, content):
doc = bs4.BeautifulSoup(content, 'html.parser')
file_name_components = input_file.split('/')
date = '/'.join(file_name_components[1:4])
categories = file_name_components[4:-1]
file_name = '.'.join(file_name_components[-1].split('.')[:-1])
url = 'http://' + input_file
# Removing script and style tags
for script in doc(['script', 'style', 'link', 'button']):
script.decompose()
try:
# Before 2013
author = doc.find('meta', attrs={'name': 'author'})['content']
except TypeError:
# After 2013
if not doc.find('meta', attrs={'name': 'byl'}):
logging.warning('ny:No author in {}'.format(input_file))
return None
author = doc.find('meta', attrs={'name': 'byl'})['content']
author = author.replace('By ', '')
# Extracting title
title = doc.find('meta', property='og:title')
if not title:
logging.error('no title for {}'.format(input_file))
return
title = re.sub(r'\s+', ' ', title['content']).strip()
if not len(title):
logging.error('no title for {}'.format(input_file))
return
# Extracting headline
headline = doc.find('meta', property='og:description')
if not headline:
logging.error('no headline for {}'.format(input_file))
return
headline = re.sub(r'\s+', ' ', headline['content']).strip()
if not len(headline):
logging.error('no headline for {}'.format(input_file))
return
# Extracting article content
body = doc.find('section', attrs={'name': 'articleBody'})
if not body:
body = doc.find_all('p', attrs={'class': 'story-body-text story-content'})
if not body:
logging.error('no body for {}'.format(input_file))
return
else:
body = ' '.join([re.sub(r'\s+', ' ', p.get_text(separator=' ')).strip()
for p in body])
else:
body = re.sub(r'\s+', ' ', body.get_text(separator=' ')).strip()
if not len(body):
logging.error('no body for {}'.format(input_file))
return
# Extracting keywords
keywords = doc.find('meta', attrs={'name': 'news_keywords'})
if keywords is None:
keywords = doc.find('meta', attrs={'name': 'keywords'})
if not keywords:
logging.error('no keywords for {}'.format(input_file))
return
keywords = re.sub(r'\s+', ' ', keywords['content']).strip()
keywords = keywords.split(',')
# remove empty keywords
keywords = [k.split(';') for k in keywords if k]
if not keywords:
logging.error('no keywords for {}'.format(input_file))
return
return {
'title': title, 'headline': headline, 'abstract': body,
'keyword': keywords, 'file_name': file_name,
'date': date, 'categories': categories, 'url': url,
'author': author
}
def process_file(input_file, id_=None):
# Loading soup
with open(input_file) as g:
content = g.read()
if 'nytimes' in input_file:
res = convert_nytimes(input_file, content)
elif 'japantimes' in input_file:
res = convert_jptimes(input_file, content)
else:
logging.error(
'Unrecognised file type : {}'.format(input_file))
if res and id_:
res['id'] = id_
return res
if __name__ == '__main__':
import argparse
def arguments():
parser = argparse.ArgumentParser(
description='Converts html files to jsonl using a filelist')
parser.add_argument(
'-f', '--filelist', type=argparse.FileType('r'),
help='Filelist file. If not given convert every found '
'file into `dataset.jsonl` without id')
parser.add_argument(
'-p', '--parallel', type=int,
help='Number of thread to use for parallel processing')
args = parser.parse_args()
return args
args = arguments()
logging.basicConfig(level=logging.INFO)
logging.info('start converting...')
articles_processed = 0
output_file = 'dataset.jsonl'
jptimes_dir = 'www.japantimes.co.jp/'
nytimes_dir = 'www.nytimes.co.jp/'
webarchive_dir = 'web.archive.org/'
if args.filelist:
files = (l.strip().split('\t') for l in args.filelist)
files = ((i, p.replace('https://', '')) for i, p in files)
files = [(i, p) for i, p in files if os.path.isfile(p)]
args.filelist.close()
output_file = args.filelist.name.replace('url.filelist', 'jsonl')
else:
files = itertools.chain(
#recursive_iglob(rootdir=jptimes_dir, pattern='[!.]*'),
#recursive_iglob(rootdir=nytimes_dir, pattern='*.html')
recursive_iglob(rootdir=webarchive_dir, pattern='*.html')
)
files = [(None, p) for p in files]
def process_file_(args):
return process_file(args[1], id_=args[0])
logging.info('Writing to {}'.format(output_file))
with codecs.open(output_file, 'w', 'utf-8') as f:
with Pool(args.parallel) as pool:
process = pool.imap_unordered(process_file_, files)
for res in tqdm(process):
if res:
f.write(json.dumps(res) + '\n')
articles_processed += 1
logging.info('Converted {} articles'.format(articles_processed))
if args.filelist and articles_processed != len(files):
logging.info(
'There are {} missing articles. Please (re)try downloading '
'articles using download script'.format(len(files) - articles_processed)
)