-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.py
482 lines (431 loc) · 15.8 KB
/
statistics.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# anarchism and gender
# statistics.py
# standard imports
import argparse
import logging
from datetime import datetime
import spacy
# useful stuff
from collections import Counter
from string import punctuation
import numpy as np
import pandas as pd
from spacy.matcher import Matcher
from tqdm import tqdm
# db
from sqlalchemy import create_engine, func
from sqlalchemy.orm import sessionmaker
# project specific
from db import Base, Journal, Issue, Page
# arguments
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("--dump-db", help="don't run chrome headless", action="store_true")
# logging
FORMAT = "%(asctime)-15s %(levelname)s %(message)s"
logging.basicConfig(
filename=f"log/{datetime.now()}_statistics.log", format=FORMAT, level=20
)
logger = logging.getLogger("anarchism")
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter(FORMAT)
# add formatter to ch
ch.setFormatter(formatter)
class TqdmHandler(logging.StreamHandler):
def __init__(self):
logging.StreamHandler.__init__(self)
def emit(self, record):
msg = self.format(record)
tqdm.write(msg)
th = TqdmHandler()
th.setFormatter(formatter)
# add ch to logger
# logger.addHandler(ch)
logger.addHandler(th)
# search parameters
JOURNAL_TYPE = "journal"
SEARCH_TEXT = "Anarchis*"
DATE_FROM = "01.01.1898"
DATE_TO = "31.12.1898"
relevant_journals_ids = (
# 25, # Agramer Zeitung
12, # Arbeiter Zeitung
57, # Arbeiterwille
# 36, # Bregenzer Tagblatt
# 30, # Bukowiner Rundschau
23, # Das Vaterland
1, # Deutsches Volksblatt
# 3, # Grazer Tagblatt
# 31, # Grazer Volksblatt
# 32, # Innsbrucker Nachrichtenn
# 33, # Kuryer Lwowski
# 22, # Kärtner Zeitungn
# 48, # Leitmeritzer Zeitung
# 16, # Linzer Volksblatt
# 4, # Mährisch-Schlesische Presse
# 34, # Mährisches Tagblatt
54, # Mödlinger Zeitung
11, # Neue Freie Presse
# 7, # Neues Wiener Journal
# 5, # Neues Wiener Tagblatt
24, # Pester Lloyd
# 26, # Prager Abendblatt
27, # Prager Tagblatt
# 17, # Reichspost
# 18, # Salzburger Chronik
# 28, # Salzburger Volksblatt
# 13, # Linzer Tages-Post
# 8, # Teplotz-Schönnauer Anzeiger
38, # Volksblatt für Stadt und Land
# 46, # Vorarlberger Landes-Zeitung
# 14, # Vorarlberger Volksblatt
43, # Wiener Neueste Nachrichten
# 29, # Wiener Zeitung
# 35, # (Neuigkeits) Welt Blatt
# 72, # Znaimer Tagblatt
# 19, # Znaimer Wochenblatt
)
def get_db_session(echo=False):
engine = create_engine(
f"sqlite:///{SEARCH_TEXT.replace('*','')}_{DATE_FROM}-{DATE_TO}.db",
encoding="utf-8",
echo=echo,
)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
return Session()
def dump_relevant_text(search_pattern, dump_file):
matcher = Matcher(nlp.vocab)
# pattern
for search_text in search_pattern:
pattern = [
# {"POS": "ADJ", "OP": "*"},
# {"POS": "DET", "OP": "*"},
# {"IS_PUNCT": True, "OP": "*"},
{"LOWER": search_text.lower()},
# {"IS_PUNCT": True},
]
matcher.add(f"{search_text}_pattern".upper(), None, pattern)
# get every sentence including the search text
nlp_dict = {
"issue_id": [],
"journal_id": [],
"issue_date": [],
"match_id": [],
"sentence": [],
"window": [],
# 'subtree': [],
}
for (issue_id, journal_id, issue_date, issue_text) in issue_query:
text = issue_text.decode("utf-8")
if len(text) > nlp.max_length:
logger.warning(
f"Skipping issue {issue_id} w/ jounral id: {journal_id}, because text length {len(text)} > {nlp.max_length}."
)
continue
doc = nlp(text) # load text
matches = matcher(doc)
window_length = 100
for match_id, start, end in matches:
search_text_token = doc[start]
sentence = search_text_token.sent
subtree_list = []
start_pos = start - window_length
if start < 0:
start_pos = 0
window_text = doc[start_pos : end + window_length].text
logger.debug(f"Match found: '{window_text}' {issue_date}.")
# for token in search_text_token.subtree:
# subtree_list.append(token.text)
nlp_dict["issue_id"].append(issue_id)
nlp_dict["journal_id"].append(journal_id)
nlp_dict["issue_date"].append(issue_date)
nlp_dict["match_id"].append(match_id)
nlp_dict["sentence"].append(sentence)
nlp_dict["window"].append(window_text)
df = pd.DataFrame(data=nlp_dict)
df.to_csv(dump_file)
def allow_token(t):
if t.text.lower().startswith("anarchis"):
return False
if t.like_num:
return False
if t.is_punct:
return False
if t.text.lower() in nlp.Defaults.stop_words:
return False
return True
def get_most_common_token_pos(dataframe, token_pos="NOUN", counter_limit=20):
token_pos_list = []
for index, row in dataframe.iterrows():
# get noun chunks per sentence
doc = nlp(row["sentence"])
# noun_list += [nc for nc in doc.noun_chunks]
token_pos_list += [
token.text.lower()
for token in doc
if token.pos_ == token_pos and allow_token(token)
]
return Counter(token_pos_list).most_common(counter_limit)
def get_most_common_entities(dataframe, counter_limit=20):
entity_list = []
for index, row in dataframe.iterrows():
# get noun chunks per sentence
doc = nlp(row["window"])
# noun_list += [nc for nc in doc.noun_chunks]
entity_list += [
ent.text.lower()
for ent in [ents for ents in doc.ents]
if ent.text.lower() not in nlp.Defaults.stop_words
]
return Counter(entity_list).most_common(counter_limit)
def get_most_common_lists(
dataframe, issue_date_start, issue_date_inter, issue_date_stop, counter_limit=20
):
first_df = dataframe[
(dataframe["issue_date"] >= issue_date_start)
& (dataframe["issue_date"] < issue_date_inter)
]
second_df = dataframe[
(dataframe["issue_date"] >= issue_date_inter)
& (dataframe["issue_date"] < issue_date_stop)
]
first_most_common_noun = get_most_common_token_pos(first_df, "NOUN", counter_limit)
second_most_common_noun = get_most_common_token_pos(
second_df, "NOUN", counter_limit
)
first_most_common_adj = get_most_common_token_pos(first_df, "ADJ", counter_limit)
second_most_common_adj = get_most_common_token_pos(second_df, "ADJ", counter_limit)
first_most_common_entities = get_most_common_entities(first_df, counter_limit)
second_most_common_entities = get_most_common_entities(second_df, counter_limit)
return (
first_most_common_noun,
second_most_common_noun,
first_most_common_adj,
second_most_common_adj,
first_most_common_entities,
second_most_common_entities,
)
def print_latex_table(journal_word_frequency, word_type, caption, label_prefix):
for journal_title in journal_word_frequency:
print("\\begin{table}[h!]")
print("\\centering")
print("\\begin{tabular}{ | l | l | l | l | }")
print("\\hline")
print(
"\\multicolumn{2}{|l|}{Vor September} & \\multicolumn{2}{|l|}{Ab September} \\tabularnewline"
)
print("\\hline")
print(f"{word_type} & Anzahl & {word_type} & Anzahl\\\\")
print("\\hline")
rows_1 = []
rows_2 = []
for word, frequency in sorted(
journal_word_frequency[journal_title]["prior"].items(),
key=lambda item: item[1],
reverse=True,
):
rows_1.append(f"{word} & {frequency} &")
for word, frequency in sorted(
journal_word_frequency[journal_title]["post"].items(),
key=lambda item: item[1],
reverse=True,
):
rows_2.append(f"{word} & {frequency} \\\\")
for i, row in enumerate(rows_1):
if i < len(rows_2):
print(row, rows_2[i])
else:
print(row, "- & - \\\\")
print("\\hline")
print("\\end{tabular}")
print("\\caption{" + caption + " in: \\textit{" + str(journal_title) + "}}")
print(
"\\label{tbl:"
+ label_prefix
+ "_"
+ str(journal_title).lower().replace(" ", "_")
+ "}"
)
print("\\end{table}")
print("")
if __name__ == "__main__":
t1 = datetime.now()
args = parser.parse_args()
if args.verbose:
logger.setLevel(10)
session = get_db_session(args.verbose)
# journal stats
journal_query = session.query(
Journal.journal_id, Journal.title, Journal.language, Journal.publication_place
)
journal_stats = [(j[0], j[1], j[2], j[3]) for j in journal_query]
journals_df = pd.DataFrame(
journal_stats,
columns=["id", "title", "language", "pub place"],
)
logger.debug(journals_df.describe(include="all"))
# issue stats
issue_query = session.query(
Issue.issue_id, Issue.journal_id, Issue.issue_date, Issue.text
)
issue_stats = [(i[0], i[1], i[2], i[3]) for i in issue_query]
issue_df = pd.DataFrame(
issue_stats,
columns=["id", "journal_id", "date", "text"],
)
logger.info(issue_df.describe(include="all"))
# nlp stuff
nlp = spacy.load("de_core_news_lg")
with open("stop_words.txt", "r") as f:
nlp.Defaults.stop_words |= {word for word in f.read().split("\n")}
dump_file = f"tmp/{SEARCH_TEXT.replace('*', '')}_{DATE_FROM}-{DATE_TO}.csv"
search_pattern = [
"anarchismus",
"anarchist",
"anarchistin",
"anarchisten",
"anarchistinnen",
]
if args.dump_db:
dump_relevant_text(search_pattern, dump_file)
# load dataframe from csv
df = pd.DataFrame()
try:
df = pd.read_csv(dump_file, delimiter=";", parse_dates=["issue_date"])
except FileNotFoundError:
logger.error(f"File: '{dump_file}' not found.")
issue_date_start = datetime(year=1898, month=1, day=1, hour=0, minute=0, second=0)
issue_date_inter = datetime(year=1898, month=9, day=1, hour=0, minute=0, second=0)
issue_date_end = datetime(
year=1898, month=12, day=31, hour=23, minute=59, second=59
)
counter_limit = 10
start_inter_nouns = []
inter_end_nouns = []
start_inter_adjs = []
inter_end_adjs = []
start_inter_ents = []
inter_end_ents = []
journal_word_frequency_ents = {}
journal_word_frequency_nouns = {}
journal_word_frequency_adjs = {}
for journal_id in tqdm(relevant_journals_ids):
journal_df = df[(df["journal_id"] == journal_id)]
journal_title = journals_df[(journals_df["id"] == journal_id)]["title"].values[
0
]
journal_word_frequency_nouns.update(
{
str(journal_title): {
"prior": {},
"post": {},
}
}
)
journal_word_frequency_adjs.update(
{
str(journal_title): {
"prior": {},
"post": {},
}
}
)
journal_word_frequency_ents.update(
{
str(journal_title): {
"prior": {},
"post": {},
}
}
)
(
most_common_start_inter_noun,
most_common_inter_end_noun,
most_common_start_inter_adj,
most_common_inter_end_adj,
most_common_start_inter_ents,
most_common_inter_end_ents,
) = get_most_common_lists(
journal_df,
issue_date_start,
issue_date_inter,
issue_date_end,
counter_limit,
)
logger.debug(f"{counter_limit} most common nouns for journal: {journal_title}:")
for (word, frequency) in most_common_start_inter_noun:
if word not in search_pattern and frequency > 1:
start_inter_nouns.append(word)
logger.debug(f"{word}: {frequency}")
journal_word_frequency_nouns[str(journal_title)]["prior"].update(
{word: frequency}
)
logger.debug(f"{counter_limit} most common nouns for journal: {journal_title}:")
for (word, frequency) in most_common_inter_end_noun:
if word not in search_pattern and frequency > 1:
inter_end_nouns.append(word)
logger.debug(f"{word}: {frequency}")
journal_word_frequency_nouns[str(journal_title)]["post"].update(
{word: frequency}
)
logger.debug(f"{counter_limit} most common adjs for journal: {journal_title}:")
for (word, frequency) in most_common_start_inter_adj:
if word not in search_pattern and frequency > 1:
start_inter_adjs.append(word)
logger.debug(f"{word}: {frequency}")
journal_word_frequency_adjs[str(journal_title)]["prior"].update(
{word: frequency}
)
logger.debug(f"{counter_limit} most common ajds for journal: {journal_title}:")
for (word, frequency) in most_common_inter_end_adj:
if word not in search_pattern and frequency > 1:
inter_end_adjs.append(word)
logger.debug(f"{word}: {frequency}")
journal_word_frequency_adjs[str(journal_title)]["post"].update(
{word: frequency}
)
logger.debug(
f"{counter_limit} most common named entities for journal: {journal_title}:"
)
for (word, frequency) in most_common_start_inter_ents:
if word not in search_pattern and frequency > 1:
start_inter_ents.append(word)
logger.debug(f"{word}: {frequency}")
journal_word_frequency_ents[str(journal_title)]["prior"].update(
{word: frequency}
)
logger.debug(
f"{counter_limit} most common named entities for journal: {journal_title}:"
)
for (word, frequency) in most_common_inter_end_ents:
if word not in search_pattern and frequency > 1:
inter_end_ents.append(word)
logger.debug(f"{word}: {frequency}")
journal_word_frequency_ents[str(journal_title)]["post"].update(
{word: frequency}
)
logger.info("Printing entity frequencies per journal")
print_latex_table(
journal_word_frequency_ents,
"Entität",
"Entitäten im Suchintervall",
"ent_window",
)
logger.info("Printing noun frequencies per journal")
print_latex_table(
journal_word_frequency_nouns, "Nomen", "Nomen im Suchsatz", "noun_sent"
)
logger.info("Printing adj frequencies per journal")
print_latex_table(
journal_word_frequency_adjs, "Adjektiv", "Adjektive im Suchsatz", "adj_sent"
)
# logger.info(f"most common prior words: {sorted(set(start_inter_words))}")
# logger.info(f"most common post words: {sorted(set(inter_end_words))}")
# logger.info(f"most common prior entities: {sorted(set(start_inter_ents))}")
# logger.info(f"most common post entities: {sorted(set(inter_end_ents))}")
session.close()
logger.info(f"Completed. Processing took {(datetime.now() - t1).seconds}s.")