import argparse import re from pathlib import Path def load_json_dict(path: str): import json from pathlib import Path dict = {} if not Path(path).exists(): return dict try: with open(path, encoding='utf-8') as f: dict = json.load(f) except Exception: print(f"Failed to open dictionary file: {path}") return dict return dict danbooru_ja_dict = load_json_dict('e621_ja_dict.json') | load_json_dict('danbooru_ja_dict.json') def danbooru_tags_to_jas(tags: list[str]): from rapidfuzz.process import extractOne from rapidfuzz.utils import default_process A1111_SPECIAL_SYNTAX_RE = re.compile(r"\s*<[^>]+>\s*") keys = list(danbooru_ja_dict.keys()) jas = [] for tag in tags: tag = str(tag).strip() if A1111_SPECIAL_SYNTAX_RE.fullmatch(tag): continue s = default_process(str(tag)) e1 = extractOne(s, keys, processor=default_process, score_cutoff=90.0) if e1: jas.extend(danbooru_ja_dict[e1[0]].copy()) return jas def danbooru_to_ja(input_tag, input_file, output_file, is_append): if input_file and Path(input_file).exists(): try: with open(input_file, 'r', encoding='utf-8') as f: input_tag = f.read() except Exception: print(f"Failed to open input file: {input_file}") tags = [tag.strip() for tag in input_tag.split(",")] if input_tag else [] ja_tags = danbooru_tags_to_jas(tags) output_tags = tags + ja_tags if is_append else ja_tags output_tag = ", ".join(output_tags) if output_file: try: with open(output_file, mode='w', encoding="utf-8") as f: f.write(output_tag) except Exception: print(f"Failed to write output file: {output_file}") else: print(output_tag) return output_tag if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--tags", default=None, type=str, required=False, help="Input tags.") parser.add_argument("--file", default=None, type=str, required=False, help="Input tags from a text file.") parser.add_argument("--out", default=None, type=str, help="Output to text file.") parser.add_argument("--append", default=False, type=bool, help="Whether the output contains the input tags or not.") args = parser.parse_args() assert (args.tags, args.file) != (None, None), "Must provide --tags or --file!" danbooru_to_ja(args.tags, args.file, args.out, args.append) # Usage: # python danbooru_to_ja.py --tags "1girl, oomuro sakurako, solo, sitting, starry sky" # python danbooru_to_ja.py --file inputtag.txt # python danbooru_to_ja.py --file inputtag.txt --append True # Datasets: https://huggingface.co/datasets/p1atdev/danbooru-ja-tag-pair-20240715 # Datasets: https://github.com/ponapon280/danbooru-e621-converter