-
Notifications
You must be signed in to change notification settings - Fork 1
/
freq_count_place_cleanfirstpart.py
executable file
·144 lines (107 loc) · 3.67 KB
/
freq_count_place_cleanfirstpart.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
#!/usr/bin/env python3.5
import requests
import json
from lxml import etree
import time
import random
SRU_QUERY = 'http://jsru.kb.nl/sru/sru?query='
SRU_QUERY += '"%s" and ppn exact "%s" '
SRU_QUERY += 'and date within "01-01-%s 31-12-%s" '
SRU_QUERY += 'and type exact "%s"'
SRU_QUERY += '&x-collection=DDD_artikel&maximumRecords=10'
SRU_QUERY += '&startRecord=%s'
NER = "http://ner.kbresearch.nl/?url=%s"
WANTED_PLACES = []
WANTED_PPNS = []
WANTED_RANGES = []
WANTED_TYPES = ["illustratie met onderschrift",
"artikel",
"advertentie",
"familiebericht"]
CLEAN = "http://kbresearch.nl/remove_leading_place/?url=%s&query=%s"
def read_ppns(filename='corpus.csv'):
global WANTED_PPNS
with open(filename) as fh:
data = fh.read()
i = 0
for line in data.split('\n'):
if line.find(',') > -1:
i += 1
ppn = line.split(',')[1][1:-1]
WANTED_PPNS.append(ppn)
if i > 2: # LIMIT NR OF PPNS
return
def read_places(filename='places_exceptions.csv'):
global WANTED_PLACES
with open(filename) as fh:
data = fh.read()
for line in data.split('\n'):
if line.find(',') > -1 and not line.find('naam') > -1:
place = line.split(',')[1][1:-1]
WANTED_PLACES.append(place)
def read_years(filename='years.txt'):
global WANTED_RANGES
years = []
'''
with open(filename) as fh:
data = fh.read()
for line in data.split('\n'):
if not line == 'Years':
years.append(str(line))
for i in range(0, len(years)-1):
if i >0:
WANTED_RANGES.append([str(int(years[i]) + 1), years[i + 1]])
else:
WANTED_RANGES.append([years[i], years[i+1]])
'''
for i in range(1865, 1995):
WANTED_RANGES.append(str(i))
def exec_clean(identifier, target='Dordrecht'):
r = requests.get(CLEAN % (identifier, target))
return r.json()
def exec_query(ppn, place, idate, atype, start=0, identifiers=[]):
done = False
while not done:
try:
date_start = date_end = idate
sru = SRU_QUERY % (place, ppn, date_start, date_end, atype, str(start))
data = requests.get(sru)
data = etree.fromstring(data.content)
done = True
except:
time.sleep(random.random())
for item in data.iter():
#print(item.tag, item.attrib, item.text)
if item.tag.endswith('numberOfRecords'):
result_count = int(item.text)
if item.tag.endswith('identifier'):
identifiers.append(item.text)
if start + 10 < result_count and result_count > 0:
identifier = exec_query(ppn, place, idate, atype, start + 10, identifiers)
return identifiers
else:
return identifiers
#return ("akaakaka")
if __name__ == '__main__':
read_ppns()
read_places()
read_years()
result = []
for ppn in WANTED_PPNS:
for place in WANTED_PLACES:
for date in WANTED_RANGES:
atype = "artikel"
result = exec_query(ppn, place, date, atype, 0, [])
total_hits = len(result)
total_verified_hits = 0
for i in result:
if exec_clean(i, place.lower()):
total_verified_hits += 1
print("atype: %s, ppn: %s, place: %s, date: %s, hits_string: %s, hits_clean: %s" % (atype, ppn, place, date, total_hits, total_verified_hits))
'''
with open('freq.json', 'w') as fh:
fh.write(json.dumps(result))
output:
37631091X Eindhoven 1900 1909 2068
ppn, place, start_date, end_date, freq
'''