-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilDEDI.py
More file actions
370 lines (322 loc) · 13.8 KB
/
Copy pathutilDEDI.py
File metadata and controls
370 lines (322 loc) · 13.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
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
"""
utilDEDI.py
jsonl read/write for the DEDI programs and other utilities
NOTE 19.10.22: Those customized writeedit()/writesrc() -- originally there is provide a more readable
JSONL format for manual editing, which wasn't needed after a while -- were a bad idea,
or at least should have used a more systematic (and compatible) way of dealing with None
Programmer: Philip A. Schrodt <schrodt735@gmail.com>
This code is covered under the MIT license: http://opensource.org/licenses/MIT
REVISION HISTORY:
31-Jul-2019: Initial version
07-Aug-2019: Added timestamp()
22-Apr-2020: Added read_autocodes()
=========================================================================================================
"""
import subprocess
import datetime
import json
import os
#MONTH_INFIX = "201503"
MONTH_INFIX = "202212"
#MONTH_INFIX = "201913"
MONTH_SUFFIX = "-" + MONTH_INFIX + ".jsonl"
VERSION = "1.8b1"
# Autocode tuple indices
AUTOCODE = 0
AUTODMND = 1
AUTOCONT = 2
AUTOCASE = 3
AUTOREJT = 4
AUTOREMV = 5
recOrder = ["ccode", "status",
"+date", "comment", "country",
"+id", "icewsid",
"-headline",
"-text",
"+size", "sizeCategory",
"+protesterdemand", "stateresponse",
"+protest", "protesterviolence", "protesteridentity",
"+event", "eventText",
"-location",
"+region", "version", "language", "publication", "year", "enddate", "citation", "codedDate", "coder"]
srcOrder = ["ccode", "status", "+id",
"+date", "comment", "country", "region", "event", "eventText",
"-headline",
"-text",
"+size", "sizeCategory",
"+protesterdemand", "stateresponse",
"+protesterviolence", "protesteridentity",
"-location",
"+region", "version", "language", "publication", "year", "enddate", "citation"]
def read_file(filename):
""" returns next record in a line-delimited JSON file """
jstr = ""
for line in open(filename, "r"):
if line.startswith("}"):
# print(jstr) # debug: uncomment to find badly formed cases, or put this into a try/except
adict = json.loads(jstr + "}")
yield adict
jstr = ""
else:
if "\t" in line:
line = line.replace("\t", "\\t")
jstr += line[:-1].strip()
def writeedit(rec, fout):
""" Write combined record """
fout.write('{\n')
for fl in recOrder[:-1]:
if fl.startswith("-"):
fl = fl[1:]
fout.write('\n"' + fl + '":\n')
if fl == "location":
fout.write(json.dumps(rec[fl]) + ",")
else:
fout.write(json.dumps(rec[fl], indent=2, sort_keys=True ) + ",")
elif fl.startswith("+"):
fout.write("\n")
fl = fl[1:]
fout.write('"' + fl + '": "' + str(rec[fl]) + '", ')
else:
if fl == "eventText":
fout.write(json.dumps(rec[fl]) + ",")
else:
fout.write('"' + fl + '": "' + str(rec[fl]) + '", ')
fl = recOrder[-1]
fout.write('"' + fl + '": "' + str(rec[fl]) + '"\n}\n')
def writesrc(rec, fout):
""" Write original record """
fout.write('{\n')
for fl in srcOrder[:-1]:
if fl.startswith("-"):
fl = fl[1:]
fout.write('\n"' + fl + '":\n')
if fl == "location":
fout.write(json.dumps(rec[fl]) + ",")
else:
fout.write(json.dumps(rec[fl], indent=2, sort_keys=True ) + ",")
elif fl.startswith("+"):
fout.write("\n")
fl = fl[1:]
fout.write('"' + fl + '": "' + str(rec[fl]) + '", ')
else:
if fl == "eventText":
fout.write('"eventText": '+ json.dumps(rec[fl]) + ",")
elif fl == "language":
fout.write('"language": "'+ rec[fl][0] + '", ')
else:
if rec[fl]:
fout.write('"' + fl + '": "' + str(rec[fl]) + '", ')
else:
fout.write('"' + fl + '": null, ')
fl = srcOrder[-1]
if rec[fl]:
fout.write('"' + fl + '": "' + str(rec[fl]) + '"\n}\n')
else:
fout.write('"' + fl + '": null\n}\n')
def timestamp():
return '-' + datetime.datetime.now().strftime("%Y%m%d")[2:] + "-" + datetime.datetime.now().strftime("%H%M%S") + ".jsonl"
def newdate(isodate, forward = False):
"""move the date back one day
Note: Python 3.7 has a "datetime.fromisoformat()" function to do the conversion without the string conversions. Though now I've written them..."""
if forward:
thedate = datetime.date(int(isodate[:4]), int(isodate[5:7]), int(isodate[-2:])) + datetime.timedelta(days = 1)
else:
thedate = datetime.date(int(isodate[:4]), int(isodate[5:7]), int(isodate[-2:])) - datetime.timedelta(days = 1)
return thedate.isoformat(), thedate
def read_autocodes(demandset = None, fromplovigy = False):
auto ={}
autoshort = {}
for autofile in ["autocodes-DEDI.txt", "autocodes-" + MONTH_INFIX + "-DEDI.txt"]:
if not os.path.isfile(autofile):
print("\aError: could not locate", autofile + ". Exiting")
exit()
for line in open(autofile,"r"):
if line.startswith("-- STOP"):
break
if not fromplovigy and line.startswith("-- SEL"):
break
if len(line) < 32 or line.startswith("#"):
continue
rec = json.loads(line[:-1])
if demandset:
for li in rec['demands']:
if li not in demandset and li[7].upper() + li[8:] not in demandset: # second option is a constructed "Oppose"
print("Error: Unrecognized add-demand in", rec)
exit()
if "remove" in rec:
for li in rec['remove']:
if li not in demandset and li[7].upper() + li[8:] not in demandset: # second option is a constructed "Oppose"
print("Error: Unrecognized remove-demand in", rec)
exit()
if autofile != "autocodes-DEDI.txt":
rec['yrmon'] = MONTH_INFIX
if 'yrmon' not in rec or rec['yrmon'] == MONTH_INFIX:
if "keyseq" in rec:
shortkey = rec['keyseq'][1]
if shortkey not in autoshort:
autoshort[shortkey] = {}
if 'continuation' in rec:
autoshort[shortkey][rec['ccode']] = (rec['demands'], rec['continuation'])
else:
autoshort[shortkey][rec['ccode']] = (rec['demands'], False)
if "targetstr" in rec:
if "$" in rec['targetstr']:
if "$p" in rec['targetstr']:
targlist = [rec['targetstr'].replace("$p", li) for li in ["people", "demonstrators", "protesters"]]
elif "$d" in rec['targetstr']:
targlist = [rec['targetstr'].replace("$d", li) for li in ["protest", "demonstration", "rally", "march",
"sit in", "sit-in"]]
elif "$e" in rec['targetstr']:
targlist = [rec['targetstr'].replace("$e", li) for li in
["protesting", "demonstrating", "rallying", "gathering", "marching",
"protested", "demonstrated", "rallied", "gathered", "marched",
"protest", "demonstrate"]
]
elif "$t" in rec['targetstr']:
targlist = [rec['targetstr'].replace("$t", li) for li in ["price", "tax", "rent"]]
else:
print("\aError: Unrecognized substitution token in autodemand file:\n" + line[:-1])
print("Exiting")
exit()
else:
targlist = [rec['targetstr']]
for targ in targlist:
if ("case" not in rec or rec['case'] != "True") and not targ.islower():
print("\aError: Upper-cased target without `case = True` in autodemand file:\n" + line[:-1])
print("Exiting")
exit()
if targ not in auto:
auto[targ] = []
auto[targ].append((rec['ccode'], rec['demands'], 'continuation' in rec, 'case' in rec,
'reject' in rec, rec.get("remove")))
elif "keyseq" not in rec:
print("\aMissing 'targetstr' and 'keyseq' fields in autocodes-DEDI.txt:\n", line,"Exiting")
exit()
return auto, autoshort
def check_autopat(wrd, thetext):
if "*" in wrd:
part = wrd.partition("*")
idx = thetext.find(part[0])
if idx >= 0:
ida = thetext.find(part[2],idx + len(part[0]))
# print("'"+thetext[idx + len(part[0]):ida]+"'")
return (ida > 0 and thetext[idx + len(part[0]):ida].count(" ")<= 3)
else:
return False
elif "?" in wrd:
part = wrd.partition("?")
idx = thetext.find(part[0])
if idx >= 0:
ida = thetext.find(part[2],idx + len(part[0]))
return (ida > 0 and ida - idx - len(part[0])<= 1)
return False
else:
return (wrd in thetext)
def mergerecords(rec, mrec):
""" Merge mrec into rec """
def mergefield(field):
# print(field)
if mrec[field] and rec[field]:
for li in mrec[field]:
if li not in rec[field]:
rec[field].append(li)
elif mrec[field] and not rec[field]:
rec[field] = mrec[field]
def mergelocation():
citylist = [li['city'] for li in rec['location']]
for li in mrec['location']:
if li['city'] not in citylist:
rec['location'].append(li)
def mergeboolean(field):
if mrec[field] or rec[field]:
rec[field] = True
mergefield('icewsid')
mergefield('headline')
mergefield('text')
mergefield('event')
mergefield('eventText')
mergefield('size')
if rec['sizeCategory'] == "None" or rec['sizeCategory'] == "": # 19.08.26 this is a kludge... <20.05.19> and probably no longer needed
rec['sizeCategory'] = []
mergefield('sizeCategory')
mergefield('protesterdemand')
mergefield('protesteridentity')
mergefield('publication')
mergefield('citation')
mergefield('language')
mergefield('stateresponse')
mergelocation()
mergeboolean('continuation')
mergeboolean('protesterviolence')
rec['status'] = "accept"
return(rec)
def set_nskip(filename, filerec_name):
""" common routine for skipping previously coded records """
nskip = -1
havefile = False
if os.path.exists(filerec_name):
with open(filerec_name,'r') as frec:
line = frec.readline()
while line: # go through the entire file to get the last entry
if filename in line:
nskip = int(line.split()[1])
havefile = True
line = frec.readline()
else:
print("The record file", filerec_name, "is missing; please find or create it")
exit()
if nskip < 0 and havefile:
print("All records in", filename, "have been coded")
answ = input("Do you want to restart at the beginning of the file? (Y/N) -->")
if answ in ['Y','y']:
nskip = -1
else:
print("Please select another file: exiting")
exit()
if nskip == -1:
print("Starting at the beginning of", filename)
else:
print("Skipping first", nskip,"records in",filename)
answ = input("Press return to start...")
return nskip
serial = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" # labels for organize
def parse_orgstring(orgstrg):
""" make sure this will parse """
if "/" in orgstrg:
scrlist = [li.strip().upper() for li in orgstrg.split("/")]
else:
scrlist = [li.replace("[", "").replace(","," ").strip().upper() for li in orgstrg.split("]")]
orglist = []
for li in scrlist:
if not li:
continue
for la in li.split():
# fout.write(la + "\n")
if "-" in la:
orglist.append(la)
continue
if len(la) > 1 or serial.find(la) < 0:
return None
orglist.append(li)
# fout.write("\nMk1 " + str(orglist) + "\n")
return orglist
def get_stamp():
try:
for line in open("codedfiles.list." + MONTH_INFIX + ".txt","r"):
if line.startswith("Stamp: "):
suffix = line[7:-1] # get last suffix entry
return suffix
except:
print("\a\aCould not find the required file 'codedfiles.list." + MONTH_INFIX + ".txt': exiting")
exit()
def get_splitlist():
suffix = get_stamp()
splitlist = []
ka = 0
while True:
filename = "protest-coded-split-" + suffix + "-" + serial[ka] + ".jsonl"
if os.path.isfile(filename):
splitlist.append(filename)
ka += 1
else:
return splitlist