Last active
May 1, 2020 20:10
-
-
Save The0x539/ef8c37e2a21b139e13185f091f849d03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Collect a mapping of style -> appearances | |
appearances = {} | |
for i in range(1, 51): | |
with open(f'[Treebeard] Kemono no Souja Erin BD - {i:02}.ass', 'r') as f: | |
for line in f: | |
if line.startswith('Style:'): | |
line = line.strip() # Get rid of stray newlines | |
line = line[7:] # And the Style: prefix | |
if line not in appearances: | |
appearances[line] = [] | |
appearances[line].append(i) | |
# Collect a mapping of appearances -> styles | |
categories = {} | |
for style, eps in appearances.items(): | |
eps = tuple(eps) # Python dictionary keys need to be immutable | |
if eps not in categories: | |
categories[eps] = [] | |
categories[eps].append(style) | |
# input: [1, 2, 3, 5, 6, 8, 10, 11, 12, 13] | |
# output: [[1, 3], [5, 6], [8, 8], [10, 13]] | |
def groups_from_eps(eps): | |
groups = [[eps[0], eps[0]]] | |
for ep in eps[1:]: | |
if ep == 1 + groups[-1][1]: | |
groups[-1][1] = ep | |
else: | |
groups.append([ep, ep]) | |
return groups | |
# input: [5, 5] or [6, 20] | |
# output: '05' or '06-20' | |
str_from_group = lambda group: f'{group[0]:02}' if group[0] == group[1] else f'{group[0]:02}-{group[1]:02}' | |
# input: [[1, 3], [5, 6], [8, 8], [10, 13]] | |
# output: '01-03, 05-06, 08, 10-13' | |
str_from_groups = lambda groups: ', '.join(str_from_group(g) for g in groups) | |
# input: [1, 2, 3, 5, 6, 8, 10, 11, 12, 13] | |
# output: '01-03, 05-06, 08, 10-13' | |
str_from_eps = lambda eps: str_from_groups(groups_from_eps(eps)) | |
for eps in sorted(categories.keys(), key=len, reverse=True): | |
print(str_from_eps(eps) + ':') | |
for style in categories[eps]: | |
print('\t' + style) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment