1
+ # Use this script to merge together multiple text-edits.json files together
2
+ # It expects all .json files to be in a folder called 'text-edits' next to this script
3
+ # It outputs to a file called merged-translations.json
4
+ # Files will be output in alphabetical order, so it is recommended to prepend each file with the chapter number
5
+ # If there are any conflicts, and exception will be raised.
6
+ #
7
+ # This script is usually never needed, unless translators have been swapping out the text-edits.json for each chapter
8
+ # rather than maintaining a common text-edits.json for all chapters.
9
+
10
+ import os
11
+ import json
12
+
13
+
14
+ class Fragment :
15
+ order = 0
16
+
17
+ def __init__ (self , fragment_as_dictionary : dict [str , str ]):
18
+ self .current_english = fragment_as_dictionary ['CurrentEnglish' ]
19
+ self .current_japanese = fragment_as_dictionary ['CurrentJapanese' ]
20
+ self .new_english = fragment_as_dictionary ['NewEnglish' ]
21
+ self .new_japanese = fragment_as_dictionary ['NewJapanese' ]
22
+ self .discriminator = fragment_as_dictionary .get ('Discriminator' )
23
+ self .order = Fragment .order
24
+ Fragment .order += 1
25
+
26
+ # Generate a key for this Fragment, used later to check for conflicting fragments
27
+ self .key = self .current_english + self .current_japanese
28
+ if self .discriminator is not None :
29
+ self .key += str (self .discriminator )
30
+
31
+
32
+ def equals (self , other : 'Fragment' ):
33
+ return (
34
+ self .current_english == other .current_english and
35
+ self .current_japanese == other .current_japanese and
36
+ self .new_english == other .new_english and
37
+ self .new_japanese == other .new_japanese and
38
+ self .discriminator == other .discriminator
39
+ )
40
+
41
+ def __repr__ (self ) -> str :
42
+ return f"{ self .order } ce: { self .current_english } cj: { self .current_japanese } ne: { self .new_english } nj: { self .new_japanese } d: { self .discriminator } "
43
+
44
+ def as_dict (self ):
45
+ retval = {
46
+ 'CurrentEnglish' : self .current_english ,
47
+ 'CurrentJapanese' : self .current_japanese ,
48
+ 'NewEnglish' : self .new_english ,
49
+ 'NewJapanese' : self .new_japanese ,
50
+ }
51
+
52
+ if self .discriminator is not None :
53
+ retval ['Discriminator' ] = self .discriminator
54
+
55
+ return retval
56
+
57
+ def merge (all_translations : dict [str , Fragment ], fragment : Fragment ):
58
+
59
+ if not fragment .key in all_translations :
60
+ all_translations [fragment .key ] = fragment
61
+ else :
62
+ existing_item = all_translations [fragment .key ]
63
+
64
+ if existing_item .equals (fragment ):
65
+ print (f"Skipping duplicate item { fragment } " )
66
+ else :
67
+ raise Exception (f"Warning: non duplicate item existing:{ existing_item } new: { fragment } " )
68
+
69
+
70
+
71
+ in_folder = "text-edits"
72
+
73
+ files = os .listdir (in_folder )
74
+
75
+ all_fragments = [] # type: list[Fragment]
76
+
77
+ for filename in files :
78
+ path = os .path .join (in_folder , filename )
79
+ print (f"Parsing { path } " )
80
+
81
+ with open (path , encoding = 'utf-8' ) as f :
82
+ chapter_list_dict = json .loads (f .read ())
83
+
84
+ all_fragments .extend (Fragment (f ) for f in chapter_list_dict )
85
+
86
+ all_translations = {}
87
+
88
+ # Merge all fragments into one dict, ignoring duplicates
89
+ for f in all_fragments :
90
+ print (f .current_english )
91
+ merge (all_translations , f )
92
+ print ()
93
+
94
+ # Convert to list and sort by 'order' which is the order the fragments were loaded
95
+ sorted_translations = list (sorted (all_translations .values (), key = lambda f : f .order ))
96
+ for item in sorted_translations :
97
+ print (item )
98
+
99
+ with open ("merged-translations.json" , 'w' , encoding = 'utf-8' ) as out :
100
+ out .write (json .dumps ([f .as_dict () for f in sorted_translations ], indent = 4 , ensure_ascii = False ))
0 commit comments