-
Notifications
You must be signed in to change notification settings - Fork 3
/
nyt.py
executable file
·197 lines (168 loc) · 6.82 KB
/
nyt.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
#!/usr/bin/python3 -u
# -*- coding: utf-8 -*-
#
# Reads files from the NYT corpus. Supports reading from a TAR archive.
#
# Usage: nyt.py -h
#
# Changes:
# 2021-11-02 (rja)
# - small code cleanups
# - made output of filename optional
# 2018-07-10 (rja)
# - added alternative year extraction
# 2018-05-18 (rja)
# - restricted directory input to XML files
# - fixed URL extraction
# - added date extraction
# 2017-08-17 (rja)
# - made file path handling os independent
# 2017-06-14 (rja)
# - added support to extract desk and (normalised) author information
# - fixed extraction of heading
# 2017-05-12 (rja)
# - initial version copied from theof.py
from __future__ import print_function
import re
import xml.etree.ElementTree as ET
import tarfile
import argparse
import os
version = "0.0.5"
# remove line breaks and tabs from text
re_ws = re.compile('[\n\t\r]+')
# convert NYT XML to text
def gen_parts(files, heading, text, url, category, desk, author, date, filename):
for f, fname in files:
result = []
tree = ET.parse(f)
root = tree.getroot()
if filename:
result.append(fname)
if heading:
h = ""
for block in root.findall("./body/body.head/hedline"):
h = ET.tostring(block, encoding="utf-8", method="text").decode("utf-8")
result.append(h)
if text:
t = ""
for block in root.findall("./body/body.content/"):
if block.attrib["class"] == "full_text":
# strip XML
t = t + "\n" + ET.tostring(block, encoding="utf-8", method="text").decode("utf-8")
result.append(t)
if url:
u = ""
for tag in root.findall("./head/pubdata"):
if tag.attrib["ex-ref"]:
u = tag.attrib["ex-ref"]
result.append(u)
if category:
c = ""
for tag in root.findall("./head/meta"):
if tag.attrib["name"] == "online_sections":
c = tag.attrib["content"]
result.append(c)
if desk:
d = ""
for tag in root.findall("./head/meta"):
if tag.attrib["name"] == "dsk":
d = tag.attrib["content"]
result.append(d)
if author:
a = ""
for tag in root.findall("./body/body.head/byline"):
if tag.attrib["class"] == "normalized_byline":
a = ET.tostring(tag, encoding="utf-8", method="text").decode("utf-8")
# always try to normalise author names
a = a.strip().title()
result.append(a)
if date:
y = ""
m = ""
d = ""
for tag in root.findall("./head/meta"):
if tag.attrib["name"] == "publication_year":
y = tag.attrib["content"]
if tag.attrib["name"] == "publication_month":
m = tag.attrib["content"]
if len(m) < 2:
m = "0" + m
if tag.attrib["name"] == "publication_day_of_month":
d = tag.attrib["content"]
if len(d) < 2:
d = "0" + d
ymd = "-".join([y, m, d])
if len(ymd) == 10:
result.append(ymd)
else:
# sometimes the date is found as <pubdata date.publication="19910121T000000" ...>
for tag in root.findall("./head/pubdata"):
if tag.attrib["date.publication"]:
pubdate = tag.attrib["date.publication"]
y = pubdate[:4]
m = pubdate[4:6]
d = pubdate[6:8]
ymd = "-".join([y, m, d])
if len(ymd) == 10:
result.append(ymd)
else:
result.append("")
yield result
# apply the regex
def gen_grep(parts, reg):
regex = re.compile(reg)
for part in parts:
# skip filename
if any([regex.search(part[i]) for i in range(1, len(part))]):
yield part
# remove control characters
def gen_rm_ctrl(texts):
for parts in texts:
yield [re_ws.sub(' ', p).strip() for p in parts]
# generate a list of files from various input
def gen_files(paths):
for path in paths:
if os.path.splitext(path)[1] == ".xml":
# XML input: just one file
yield open(path, "rt"), path
elif os.path.isdir(path):
# read all files in the directory
for fname in os.listdir(path):
# restrict to XML files
if os.path.splitext(fname)[1] == ".xml":
fname = os.path.join(path, fname)
if os.path.isfile(fname):
yield open(fname, "rt"), fname
else:
# read TAR file
tar = tarfile.open(path, "r:gz")
for tarinfo in tar:
if tarinfo.isreg():
yield tar.extractfile(tarinfo), tarinfo.name
def print_lines(parts, sep='\t'):
for parts in parts:
print(sep.join(parts))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Reading the NYT corpus.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('input', type=str, help='input TAR/XML file or directory', nargs='+')
parser.add_argument('-a', '--author', action="store_true", help='extract author')
parser.add_argument('-b', '--body', action="store_true", help='extract body text')
parser.add_argument('-c', '--category', action="store_true", help='extract category')
parser.add_argument('-d', '--desk', action="store_true", help='extract desk')
parser.add_argument('-e', '--date', action="store_true", help='extract date')
parser.add_argument('-f', '--filename', action="store_true", help='output filename')
parser.add_argument('-t', '--title', action="store_true", help='extract title')
parser.add_argument('-u', '--url', action="store_true", help='extract URL')
parser.add_argument('-g', '--grep', type=str, metavar="REGEX", help='match regex')
parser.add_argument('-s', '--separator', type=str, metavar="SEP", help='output column separator', default='\t')
parser.add_argument('-n', '--normalise-ws', action="store_true", help='normalise whitespace')
parser.add_argument('-v', '--version', action="version", version="%(prog)s " + version)
args = parser.parse_args()
files = gen_files(args.input)
parts = gen_parts(files, args.title, args.body, args.url, args.category, args.desk, args.author, args.date, args.filename)
if args.grep:
parts = gen_grep(parts, args.grep)
if args.normalise_ws:
parts = gen_rm_ctrl(parts)
print_lines(parts, args.separator)