1+ def read_tag_mappings (filename ):
2+ """Read file and return dictionary of tag -> category mappings"""
3+ mappings = {}
4+ with open (filename , 'r' , encoding = 'utf-8' ) as f :
5+ for line in f :
6+ line = line .strip ()
7+ if line :
8+ # Split on first occurrence of '] ' to handle tags with spaces
9+ parts = line .split ('] ' , 1 )
10+ if len (parts ) == 2 :
11+ category = parts [0 ][1 :] # Remove leading '['
12+ tag = parts [1 ]
13+ mappings [tag ] = category
14+ return mappings
15+
16+ def update_categories (source_file , reference_file , output_file ):
17+ """Update categories in source file based on reference file mappings"""
18+ # Read both files
19+ reference_mappings = read_tag_mappings (reference_file )
20+
21+ # Read and process source file
22+ with open (source_file , 'r' , encoding = 'utf-8' ) as f :
23+ lines = f .readlines ()
24+
25+ # Update categories
26+ updated_lines = []
27+ for line in lines :
28+ line = line .strip ()
29+ if line :
30+ parts = line .split ('] ' , 1 )
31+ if len (parts ) == 2 :
32+ tag = parts [1 ]
33+ if tag in reference_mappings :
34+ # Use new category from reference file
35+ new_category = reference_mappings [tag ]
36+ updated_lines .append (f'[{ new_category } ] { tag } ' )
37+ else :
38+ # Keep original line if tag not found in reference file
39+ updated_lines .append (line )
40+ else :
41+ updated_lines .append (line )
42+
43+ # Write updated content to output file
44+ with open (output_file , 'w' , encoding = 'utf-8' ) as f :
45+ f .write ('\n ' .join (updated_lines ))
46+
47+ if __name__ == '__main__' :
48+ source_file = 'categorized_tags.txt' # File to update
49+ reference_file = 'new_categories_reference.txt' # File with new categories
50+ output_file = 'new_categories_output.txt' # Output file with updated categories
51+
52+ update_categories (source_file , reference_file , output_file )
53+ print (f'Categories updated and saved to { output_file } ' )
0 commit comments