forked from datadesk/star-wars-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swa.py
404 lines (362 loc) · 14 KB
/
swa.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
from __future__ import annotations
from collections import OrderedDict
import argparse
from utils import file_names
from pathlib import Path
import csv
from typing import List, Optional, Tuple, Dict
HERE = Path(__file__).parent
DATA = HERE / "data"
def filter_files(filtered: List[int]):
file_paths = []
if args.only_movies:
for i in args.only_movies:
if i not in range(1, 9):
raise Exception(
f"Filter part number {i} is not in range 1-8"
)
file_paths.append(file_names[i-1])
else:
file_paths = file_names
file_paths = map(lambda p: DATA / p, file_paths)
return file_paths
def count_words(line):
return len(line[3].strip().split(' '))
def wc(args):
global file_names
wc = 0
character = args.character
file_paths = filter_files(args.only_movies)
for path in file_paths:
matched_characters = set()
sub_wc = 0
reader = csv.reader(path.open('r', encoding='UTF-8'))
if character is None:
for line in reader:
sub_wc += count_words(line)
print(f'{sub_wc} words in file {path.name}')
else:
lower_character = character.strip().lower()
for line in reader:
if lower_character in line[0].strip().lower() :
matched_characters.add(line[0])
sub_wc += count_words(line)
print(
f'Character '
f'{matched_characters if len(matched_characters) > 0 else character} '
f'has {sub_wc} words in file {path.name}'
)
wc += sub_wc
def are_characters_in_line(line: List[str], lower_characters: List[str], orig_characters: Optional[List[str]] = None, resolve_groups=True):
speaker = line[0].strip().lower()
speaked_to = line[1].strip().lower()
if resolve_groups:
is_speaker = [orig_characters[i] for i, x in enumerate(lower_characters) if x in speaker]
is_speaked_to = [orig_characters[i] for i, x in enumerate(lower_characters) if x in speaked_to]
if len(is_speaker) > 0 and len(is_speaked_to) > 0:
return is_speaker, is_speaked_to
else:
return None
else:
is_speaker = [x for x in lower_characters if x in speaker]
is_speaked_to = [x for x in lower_characters if x in speaked_to]
if len(is_speaker) > 0 and len(is_speaked_to) > 0:
return is_speaker, is_speaked_to
else:
return None
def wc_matrix(args):
matrix = args.characters
if not isinstance(matrix, list):
raise Exception("Invalid character data")
if len(matrix) < 2:
raise Exception("A matrix needs at least two characters to make sense")
print(f"wc matrix: [{' ✖ '.join(matrix)}]")
file_paths = list(filter_files(args.only_movies))
lower_matrix = list(map(lambda x: x.strip().lower(), matrix))
class Matrix:
def __init__(self):
self.dict = {}
def add(self, first: str, second: str, amt: int):
self.dict.setdefault(first, dict())
self.dict[first].setdefault(second, 0)
self.dict[first][second] += amt
def merge(self, other: Matrix):
for other_1st_key in other:
for other_2nd_key in other[other_1st_key]:
self.add(other_1st_key, other_2nd_key, other[other_1st_key][other_2nd_key])
def __getitem__(self, item: str):
return self.dict[item]
def __iter__(self):
return iter(self.dict)
def __len__(self):
return len(self.dict)
def print(self, file_name: Optional[str] = None):
if len(self) == 0:
print("No such combinations found")
elif file_name:
for first_key in self:
speaker = first_key
for second_key in self[first_key]:
speaked_to = second_key
print(f'{file_name};{speaker};{speaked_to};{self[first_key][second_key]}')
else:
for first_key in self:
speaker = first_key
for second_key in self[first_key]:
speaked_to = second_key
print(f'{speaker};{speaked_to};{self[first_key][second_key]}')
result_matrix = Matrix()
for path in file_paths:
sub_wc = Matrix()
reader = csv.reader(path.open('r', encoding='UTF-8'))
for line in reader:
rs = are_characters_in_line(line, lower_matrix, matrix if not args.dont_resolve_groups else None, resolve_groups=not args.dont_resolve_groups)
if rs:
is_speaker = rs[0]
is_speaked_to = rs[1]
if not args.dont_resolve_groups:
for sp in is_speaker:
for spkd in is_speaked_to:
sub_wc.add(sp, spkd, count_words(line))
else:
sub_wc.add(line[0], line[1], count_words(line))
if args.only_movies or args.group_per_movie:
if len(sub_wc) > 0:
sub_wc.print(path.name)
result_matrix.merge(sub_wc)
if not args.group_per_movie:
result_matrix.print()
def convert(args):
import re
format = args.format
class Format:
def __init__(self, format: str):
self.raw_format = format
self.replacers: OrderedDict[int, Format.Replace] = OrderedDict()
self.placeholder: List[Optional[str]] = []
self.__parse_raw_format()
class Replace:
def __init__(self, range: Tuple[int, int], quote: Optional[str]=None):
self.range = range
self.quote = quote
def __parse_raw_format(self):
found_matches = re.finditer(r'\$((?P<number>[0-9]+)|(?P<part_id>partId))', self.raw_format)
any_found = False
last_end = 0
for match in found_matches:
any_found = True
matched_group_name = next(x[0] for x in match.groupdict().items() if x[1] is not None)
rnge = match.span(0)
if matched_group_name == 'number':
part_id = int(match.group(1))
if part_id not in range(1, 9):
raise Exception(f"Invalid column number {part_id} not in range <1-8>!")
# check for surrounding quotes for escaping in self.parse
quote = None
if self.raw_format[rnge[0]-1] == self.raw_format[rnge[1]]:
quote = self.raw_format[rnge[1]]
self.replacers[part_id] = self.Replace(rnge, quote)
elif matched_group_name == 'part_id':
self.replacers["part_id"] = self.Replace(rnge)
pass
self.placeholder.append(self.raw_format[last_end:rnge[0]])
self.placeholder.append(None)
last_end = rnge[1]
self.placeholder.append(self.raw_format[last_end:])
if not any_found:
raise Exception("Invalid format. No column numbers, e.g. \"$1\" found!")
def parse(self, line: List[str], file_name: str, mappings: Optional[Dict[str, str]] = None):
new_line = self.placeholder
i = -1
for key in self.replacers:
i += 2
if key == 'part_id':
value = str(file_names.index(file_name) + 1)
else:
quote = self.replacers[key].quote
value = line[key-1]
if quote:
if args.auto_escape:
value = value.translate(str.maketrans({quote: f"\\{quote}"}))
elif args.sql_escape:
value = value.translate(str.maketrans({"'": "''"}))
if mappings:
if value in mappings:
value = mappings[value]
new_line[i] = value
return ''.join(new_line)
file_paths = list(filter_files(args.only_movies))
print(f'Format: "{format}"; files: {[x.name for x in file_paths]}')
format = Format(format)
mappings = None
characters = None
lower_characters = None
if args.characters:
characters = args.characters
enum_characters = enumerate(characters)
is_found_equal_sign = next(((i, x) for i, x in enum_characters if '=' in x), None)
if is_found_equal_sign:
i = is_found_equal_sign[0]
x = is_found_equal_sign[1]
orig, new = x.split('=')
mappings = dict()
mappings[orig] = new
characters[i] = orig
for i, x in enum_characters:
if '=' in x:
orig, new = x.split('=')
mappings[orig] = new
characters[i] = orig
lower_characters = list(map(lambda x: x.strip().lower(), args.characters))
for path in file_paths:
reader = csv.reader(path.open('r', encoding='UTF-8'))
if args.group_per_movie:
print(f'{path.name}:')
if args.characters:
for line in reader:
rs = are_characters_in_line(
line,
lower_characters,
characters if not args.dont_resolve_groups else None,
resolve_groups=not args.dont_resolve_groups
)
if rs:
is_speaker = rs[0]
is_speaked_to = rs[1]
# losing info about groups right now
line[0] = is_speaker[0]
line[1] = is_speaked_to[0]
new_line = format.parse(line, path.name, mappings)
print(new_line)
else:
for line in reader:
new_line = format.parse(line, path.name, mappings)
if args.group_per_movie:
print('\n')
if __name__ == "__main__":
if not DATA.exists():
raise Exception("Data folder does not exist")
parser = argparse.ArgumentParser(
description="stats about LA Times data"
)
subparsers = parser.add_subparsers(
dest='subparser_name',
help="following commands available"
)
word_count_parser = subparsers.add_parser(
"word_count",
help="count words",
aliases=["wc"]
)
word_count_parser.add_argument(
"--only-movies", "-f",
nargs="*",
default=None,
type=int,
choices=range(1, 9),
help="Only which movies"
)
word_count_parser.add_argument(
"character",
type=str,
help="Specify character, case-insensitive substring of CSV format!"
)
word_count_parser.set_defaults(func=wc)
word_count_matrix_parser = subparsers.add_parser(
"word_count_matrix",
help="given a list of people create a matrix of word count spoken to each other",
aliases=["wcm"]
)
word_count_matrix_parser.add_argument(
"characters",
type=str,
nargs="+",
help="Specify character, case-insensitive substring of CSV format!"
)
word_count_matrix_parser.add_argument(
"--only-movies", "-f",
nargs="*",
default=None,
type=int,
choices=range(1, 9),
help="Only which movies"
)
word_count_matrix_parser.add_argument(
"--dont-resolve-groups", "--drg",
action='store_const',
const=True,
help="Leave speaking to groups as is"
)
word_count_matrix_parser.add_argument(
"--group-per-movie", "--gpm",
action='store_const',
const=True,
help="Add another level to matrix by dividing the data for each movie separately"
)
word_count_matrix_parser.set_defaults(func=wc_matrix)
convert_to_format_parser = subparsers.add_parser(
"convert",
help="convert to a format specified by you",
aliases=["cvrt"]
)
convert_to_format_parser.add_argument(
"format",
type=str,
help='Specify the given format, e.g. "($1,$2,$3,$4)" '
'where commas and parentheses are left as is, but $1, $2 etc. '
'are replaced with the given column numbers.'
'Indexing starts at 1!'
'You also have variables: $partId'
)
convert_to_format_parser.add_argument(
"--only-movies", "-f",
nargs="*",
default=None,
type=int,
choices=range(1, 9),
help="Only which movies"
)
# convert_to_format_parser.add_argument(
# "--output", "-o",
# nargs="?",
# default=None,
# required=False,
# help="Specify to write to file, if group per movie then multiple files"
# )
convert_to_format_parser.add_argument(
"--group-per-movie", "--gpm",
action='store_const',
const=True,
help="Group per movie"
)
convert_to_format_parser.add_argument(
"--characters", "-c",
nargs="*",
help="Filter characters, case-insensitive substring of CSV format!"
)
convert_to_format_parser.add_argument(
"--dont-resolve-groups", "--drg",
action='store_const',
const=True,
help="Leave speaking to groups as is"
)
escape_group = convert_to_format_parser.add_mutually_exclusive_group()
escape_group.add_argument(
"--auto-escape", "--ae",
action='store_const',
const=True,
help="Escape non-partid variabels if they're surrounded by the same characters on both sides"
)
escape_group.add_argument(
"--sql-escape", "--se",
action='store_const',
const=True,
help="Use '' instead of \\' to escape '"
)
convert_to_format_parser.set_defaults(func=convert)
args = parser.parse_args()
if args.subparser_name:
args.func(args)
else:
parser.print_help()
print(f"\nSupply a command!")