Skip to content

Instantly share code, notes, and snippets.

@pekochan069
Last active September 8, 2023 16:03
Show Gist options
  • Save pekochan069/a8dce29990775c1bb795d50efada3ff6 to your computer and use it in GitHub Desktop.
Save pekochan069/a8dce29990775c1bb795d50efada3ff6 to your computer and use it in GitHub Desktop.
import os
import glob
from collections import Counter
files_path = "./tags2"
save_path = "./tags2/fixed"
ext = "txt"
len_ext = len(ext) + 1
remove_list = ["emataso1"]
add_list = ["iom", "virtual youtuber", "dress"]
add_back = False
if not os.path.exists(save_path):
os.makedirs(save_path)
tags = []
for file_path in glob.glob(f"{files_path}/*.{ext}"):
current_tags = []
file_path = file_path.replace("\\", "/")
filename = file_path.split("/")[-1][:-len_ext]
with open(file_path, "r", encoding="utf-8") as read_file:
current_tags = read_file.read().split(", ")
for remove_tag in remove_list:
if remove_tag in current_tags:
current_tags.remove(remove_tag)
filtered_add_list = list(filter(lambda x: x not in current_tags, add_list))
if not add_back:
current_tags[:0] = filtered_add_list
else:
current_tags.extend(filtered_add_list)
tags.extend(current_tags)
with open(f"{save_path}/{filename}.{ext}", "w", encoding="utf-8") as save_file:
save_file.write(", ".join(current_tags))
counter = dict(Counter(tags))
counter = dict(sorted(counter.items(), key=lambda x: x[1], reverse=True))
with open(f"{save_path}/results.txt", "w", encoding="utf-8") as result_file:
tags_one_liner = ", ".join(counter.keys())
tags_multi_line = [f"{i} : {j}" for i, j in counter.items()]
lines = [tags_one_liner, "\n\n\n", "\n".join(tags_multi_line)]
result_file.writelines(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment