File size: 1,328 Bytes
8518b75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c941a2
 
 
8518b75
8c941a2
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
import json
import os

from clean import clean

def dump_stats(s, day):
    stats = {}
    if os.path.exists('stats.json'):
        stats = json.load(open('stats.json', 'r', encoding='utf-8'))
    else:
        stats = {'visualnovels': {}}
    if 'visualnovels' not in stats:
        stats['visualnovels'] = {}
    stats['visualnovels'][f'VA-11 HALL-A {str(day)}'] = s
    with open('stats.json', 'w', encoding='utf-8') as f:
        json.dump(stats, f)

def worker_parse(filename, out_filename, stats=True):
    data = json.load(open(filename, 'r', encoding='utf-8'))
    for day in range(len(data)):
        msgs = ['⁂', f'[Title: VA-11 HALL-A; Description: Day {day}]', '⁂']
        for entry in data[day]:
            if entry['type'] == 'dialogue':
                msgs.append(f'{entry["character"]}: {clean(entry["text"])}')
                msgs[-1] = msgs[-1].replace('  ', ' ').replace('\n', '')
        # split filename to include day number
        outfname = '.' + out_filename.split('.')[1] + f'_{day}.txt'
        with open(outfname, 'w', encoding='utf-8') as out_file:
            out_file.write('\n'.join(msgs))
        if stats:
            dump_stats(len(msgs), day)


def parse():
    worker_parse('./raw/visualnovel/valhalla.txt', './data/visualnovel/valhalla.txt')

if __name__ == '__main__':
    parse()