-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalign_documents.py
More file actions
332 lines (257 loc) · 12.8 KB
/
Copy pathalign_documents.py
File metadata and controls
332 lines (257 loc) · 12.8 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import sys
import json
import re
from collections import Counter
def plot(plot_statistics, plot_alignments):
# plot statistics/plot_alignments: list of (year, number) -tuples
import matplotlib.pyplot as plt
plt.subplot(1, 2, 1)
x=[year for year, number in plot_statistics]
y=[number for year, number in plot_statistics]
plt.plot(x, y, 'ko-')
plt.title('Existing statistics')
plt.xlabel('Year')
plt.ylabel('Number of files')
plt.subplot(1, 2, 2)
x=[year for year, number in plot_alignments]
y=[number for year, number in plot_alignments]
plt.plot(x, y, 'ro-')
plt.title('Aligned statistics')
plt.xlabel('Year')
plt.ylabel('Number of files')
#plt.show()
def read_json(fname):
documents={}
with open(fname, "rt", encoding="utf-8") as f:
for i, document in enumerate(f):
document=document.strip()
if not document:
continue
try:
data=json.loads(document)
except json.decoder.JSONDecodeError:
print("Invalid line:", document, file=sys.stderr)
if data["topic-id"] is not None: # index based on topic-id
key=data["topic-id"]
else:
key=data["timestamp"][:8] # index based on date
if key not in documents:
documents[key]=[]
documents[key].append(data)
return documents
endresult=re.compile("\([0-9]+(\u2013|-)[0-9]+,\s?[0-9]+(\u2013|-)[0-9]+,\s?[0-9]+(\u2013|-)[0-9]+", re.UNICODE) # (1–1, 1–1, 1–0
teams_regex=re.compile("(?=([A-ZÜÅÄÖa-züåäö\-0-9]+\u2013[A-ZÜÅÄÖa-züåäö\-0-9]+))", re.UNICODE) # Lukko–TPS
score_regex=re.compile("[0-9]+(\u2013|-)[0-9]+.*\([0-9]+(\u2013|-)[0-9]+,\s?[0-9]+(\u2013|-)[0-9]+,\s?[0-9]+(\u2013|-)[0-9]+", re.UNICODE) # (1–1, 1–1, 1–0
known_teams_txt = open("teams_curated.txt").read().split('\n')
known_teams_txt = [line for line in known_teams_txt if not line.startswith('#')]
known_teams = [(team.split('|')[0], re.compile("(\W|^)("+team+")(\W|$)", re.IGNORECASE)) for team in known_teams_txt if team] # Compiled
known_teams_src = {team.split('|')[0]: "(\W|^)("+team+")(\W|$)" for team in known_teams_txt if team} # Uncompiled
def extract_teams(txt):
teams=[]
for line in txt.split("\n"):
line=line.strip()
for team, pat in known_teams:
match = pat.search(line)
if match:
teams.append((match.start(), team))
if len(teams) >= 2:
break
if len(teams) >= 2:
break
if endresult.search(line): # this is a correct line: Lukko–TPS 3–2 (0–0, 3–0, 0–2)
break
### Extracting team names by contextual pattern (high precision)
"""try:
hits=re.findall(teams_regex, line[:score_regex.search(line).start()])
except AttributeError:
print("No match:",line)
if not hits:
continue
home,guest=hits[0].split("\u2013")
teams.append(home)
teams.append(guest)"""
### Extracting team names by contextual pattern (high recall)
"""teams = line[:endresult.search(line).start()]
teams = teams.replace('\u2013v.','-v.')
teams = teams.replace('\u2013vuot','-vuot')
teams = teams.split('\u2013')"""
return [x for _,x in sorted(teams)]
def separate_statistics(stats_txt):
# returns a list of game statistics (originally one statistics file can have multiple games, now split those)
# Identifies new game by possible team name pairs, not scores (which might be missing or placed on separate line)
statistics=[]
lines=[]
for line in stats_txt.split("\n"):
line=line.strip()
if re.search(u'[A-Za-zÅÄÖåäö]\u2013[A-Za-zÅÄÖåäö]', line[:25]): # possible team occurrence # TODO: what if team name includes numbers?
for team, pat in known_teams:
if pat.search(line.split(u'\u2013')[0]):
# New stat starts
if lines: # Not the first stat in doc
statistics.append('\n'.join(lines))
lines = []
lines.append(line)
break
else:
if lines:
lines.append(line)
elif lines:
lines.append(line)
if lines:
statistics.append('\n'.join(lines))
return statistics
def align(statistics, news):
aligned_documents={}
counter=0
uncounter=0
years=Counter()
known_teams = set()
import copy
for key, stat_documents in statistics.items():
teams=[]
for d in stat_documents: # iterate over all statistics from this one day or topic-id
game_stats=separate_statistics(d["text"])
for gi, game in enumerate(game_stats): # iterate over games
curr_teams=extract_teams(game)
if len(curr_teams) != 2:
print("Team extraction error:", curr_teams, file=sys.stderr)
print("Game",gi,game+'\n', file=sys.stderr)
print("--",file=sys.stderr)
print(d["text"]+'\n', file=sys.stderr)
print(file=sys.stderr)
continue
"""
### Code for cleaning team name candidates from high-recall version of extract_teams(), for building list of known teams
found_new = False
for team in curr_teams:
if team not in known_teams:
team = re.sub(".*maaottelu ", "", team)
team = re.sub(".*: ", "", team)
team = re.sub("\(?[0-9]+\-v\.\)?", "", team)
team = re.sub("(n alle )?[0-9]+\-vuotiaat", "", team)
team = re.sub("[0-9]+\-vuotiaiden", "", team)
team = re.sub("[0-9]+v.? ?$", "", team)
team = re.sub(" U[0-9]+ ?$", "", team)
team = re.sub(" j\.a\.?", "", team, re.IGNORECASE)
team = re.sub(" rl\.?", "", team, re.IGNORECASE)
team = re.sub(" vl\.?", "", team, re.IGNORECASE)
team = re.sub(" je\.?", "", team, re.IGNORECASE)
team = re.sub(" [0-9]\.? *", "", team)
team = team.strip()
if len(team) > 1:
print("New team:", team)
known_teams.add(team)
found_new = True
if found_new:
print(game.split('\n')[0][:50])
print()"""
assert len(curr_teams)>=2
articles=[]
tries = 0
tmpkey = copy.copy(key)
while not articles:
if tmpkey not in news:
break
for article in news[tmpkey]: #try to find news articles labeled with this same key, skip statistics
if article['article-type'] == 'statistics':#is_statistics(article["text"]):
continue
# try to find team mentions
if match_articles(article["text"], curr_teams): # correct news article
articles.append(article)
#print(curr_teams)
#print(article['text'])
#print()
if not articles:
try:
tmpkey = "%s" % (int(tmpkey)+1) # Lazy iteration of days, doesn't yield many more hits anyway
except ValueError: # Is probably not a date
break
tries += 1
if tries >= 2: # Expected publishing lag
break
#print()
if not articles:
#print()#print("No articles found:", key, curr_teams)
uncounter += 1
continue
#if len(articles) > 1:
# print("Multiple matches:", len(articles), key, curr_teams)
i = 0
#while True:
# #new_key = "%s-%d-%d" % (tmpkey,gi,i)
# new_key = "%s-%s-%s-%d" % (tmpkey, curr_teams[0], curr_teams[1], i)
new_key = "%s-%s-%s" % (tmpkey, curr_teams[0], curr_teams[1])
if new_key not in aligned_documents:
aligned_documents[new_key]={"teams": curr_teams, "statistics": [], "news_articles": articles}
#i += 1
#aligned_documents[new_key]={"game_idx": gi, "teams": curr_teams, "statistics": game, "game_hash":"%.6d" % (abs(hash(game)) % 10**6), "stat_file": d["file_name"], "news_articles": articles}
aligned_documents[new_key]["statistics"].append({"game_idx": gi, "text": game, "game_hash":"%.6d" % (abs(hash(game)) % 10**6), "file_name": d["file_name"]})
counter+=1
years.update([int(articles[0]["timestamp"][:4])])
#open("known_teams3", "w").write('\n'.join(list(known_teams))) # Save harvested team name candidates
print("Number of aligned rounds:", len(aligned_documents.keys()), file=sys.stderr)
print("Years (alignments):", sorted(years.items()))
print(counter, uncounter)
return aligned_documents, sorted(years.items())
def match_articles(news_text, team_query, threshold=7):
for team in team_query:
team_regex=re.compile("(?=("+known_teams_src[team]+r"))", re.UNICODE) # TODO: should we include also stems?
if not team_regex.search(news_text[:250]):
return False
# if score_regex.search(news_text):
# return False
if len(teams_regex.findall(news_text)[:250]) > threshold:
#print("Multiple matches:", news_text[:150])
return False
if re.search("([Pp]itkäveto)|([Tt]ulosveto)|([Vv]oittajaveto)|([Ll]iigatulokset)|([Jj]ääkiekkotulos)|([Pp]istepörssin kärki\:)|([Pp]istepörssi\:)|([Rr]ahapelit)|(SM-[Ll]iiga\:)|(SM-liigan tulokset\:)|(pistetilasto\:)|(SM-jääkiekko\:)|([Tt]ulosvedon)|([M|m]aalivahdit\:)|([Oo]tteluparit\:)", news_text):
return False
if re.search("(tuloksia)|(tulokset)|(taulukko)|(tilastoja)|(tilastot)|(järjestys)|(pörssi:?)|(pörssejä)|(siirrot)|(ohjelma)|(Taitoluistelua) ?[\n\/]", news_text):
return False
return True
def is_statistics(txt):
# ottelupöytäkirja must include:
# 1) a line with "1. erä"
# 2) a line with lopputulos with two team names mentioned (e.g. Lukko–TPS 3–2 (0–0, 3–0, 0–2))
if "1. erä:" not in txt and "1.erä:" not in txt:
return False
if len(extract_teams(txt)) < 2:
return False
return True
def find_statistics(news):
# exctract all ottelupöytäkirja from the news article dump
statistics={}
years=Counter()
for key, documents in news.items():
for i, d in enumerate(documents):
if is_statistics(d["text"]):
d["article-type"]="statistics"
if key not in statistics:
statistics[key]=[]
statistics[key].append(d)
years.update([int(d["timestamp"][:4])])
else:
d["article-type"]="news"
print("Number of rounds with statistics:", len(statistics.keys()))
print("Years (statistics):", sorted(years.items()))
return statistics, sorted(years.items())
def main(args):
# read combined news article + statistics data from a given json
# returns a dictionary where key: topic-id or timestamp, value: list of articles/statistics (dictonaries) assigned to a topic-id, or timestamp
news_documents = read_json(args.json)
# find all statistics (ottelupöytäkirja) from news documents
# returns a dictionary where key: topic-id or timestamp, value: list of statistics dictonaries
statistics, plot_statistics = find_statistics(news_documents)
# align statistics and articles
# returns a dictionary where key: topic-id or timestamp, value: list of aligned statistics and news articles (with article-type marked)
aligned_documents, plot_alignments = align(statistics, news_documents)
# extra visualization to make sure we do not have weird missing data
plot(plot_statistics, plot_alignments)
# save
with open("aligned_documents.json", "wt", encoding="utf-8") as f:
json.dump(aligned_documents, f, indent=2, sort_keys=True)
if __name__=="__main__":
import argparse
argparser = argparse.ArgumentParser(description='')
argparser.add_argument('--json', type=str, default="/home/jmnybl/stt-data-clean/all_news_stats.jsonl", help='json filename')
args = argparser.parse_args()
main(args)