|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Convert a Pokemon name txt file to a JSON slug list. |
| 4 | +Each line of the txt file is converted to a lowercase slug and validated against |
| 5 | +the National Pokedex database. |
| 6 | +
|
| 7 | +The pokemon name txt file format is one line per pokemon name. |
| 8 | +Its content can be downloaded from a site like Serebii. |
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import json |
| 13 | +import re |
| 14 | +import unicodedata |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | + |
| 18 | +def normalize_pokemon_name(name: str) -> str: |
| 19 | + """ |
| 20 | + Convert a Pokemon name to a slug format. |
| 21 | + - Converts to lowercase |
| 22 | + - Removes accents/diacritics |
| 23 | + - Handles special characters |
| 24 | + """ |
| 25 | + # Remove accents and normalize unicode characters |
| 26 | + # NFD = Canonical Decomposition |
| 27 | + nfd = unicodedata.normalize('NFD', name) |
| 28 | + # Filter out combining characters (accents) |
| 29 | + name_no_accents = ''.join(char for char in nfd if unicodedata.category(char) != 'Mn') |
| 30 | + |
| 31 | + # Convert to lowercase |
| 32 | + slug = name_no_accents.lower() |
| 33 | + |
| 34 | + # Handle special cases |
| 35 | + # Replace apostrophes with '-d' for Farfetch'd and Sirfetch'd |
| 36 | + slug = slug.replace("'d", "-d") |
| 37 | + |
| 38 | + # Replace periods and spaces with hyphens for Mr. Mime, Mime Jr., Mr. Rime |
| 39 | + slug = slug.replace(". ", "-") # Handle "Mr. " -> "mr-" |
| 40 | + slug = slug.replace(" ", "-") # Handle remaining spaces |
| 41 | + slug = slug.rstrip(".") # Remove trailing periods (e.g., "mime-jr." -> "mime-jr") |
| 42 | + |
| 43 | + return slug |
| 44 | + |
| 45 | + |
| 46 | +def main(input_txt: Path, database_json: Path, output_json: Path): |
| 47 | + """ |
| 48 | + Convert Pokemon names from txt file to JSON slugs. |
| 49 | +
|
| 50 | + Args: |
| 51 | + input_txt: Path to input text file with Pokemon names (one per line) |
| 52 | + database_json: Path to National Pokedex JSON database for validation |
| 53 | + output_json: Path to output JSON file |
| 54 | + """ |
| 55 | + |
| 56 | + # Load the National Pokedex database |
| 57 | + with open(database_json, 'r', encoding='utf-8') as f: |
| 58 | + national_pokedex = json.load(f) |
| 59 | + |
| 60 | + # Convert to set for faster lookup |
| 61 | + national_pokedex_set = set(national_pokedex) |
| 62 | + |
| 63 | + # Read the input txt file and convert names |
| 64 | + pokemon_slugs = [] |
| 65 | + not_found = [] |
| 66 | + |
| 67 | + with open(input_txt, 'r', encoding='utf-8') as f: |
| 68 | + for line_num, line in enumerate(f, 1): |
| 69 | + line = line.strip() |
| 70 | + if not line: |
| 71 | + continue |
| 72 | + |
| 73 | + # Pokemon name is the entire line |
| 74 | + pokemon_name = line |
| 75 | + |
| 76 | + # Convert to slug |
| 77 | + slug = normalize_pokemon_name(pokemon_name) |
| 78 | + |
| 79 | + # Validate against database |
| 80 | + if slug in national_pokedex_set: |
| 81 | + pokemon_slugs.append(slug) |
| 82 | + print(f"✓ {pokemon_name} -> {slug}") |
| 83 | + else: |
| 84 | + not_found.append((pokemon_name, slug)) |
| 85 | + print(f"✗ {pokemon_name} -> {slug} (NOT FOUND in database)") |
| 86 | + |
| 87 | + # Report results |
| 88 | + print(f"\n{'='*60}") |
| 89 | + print(f"Total Pokemon processed: {len(pokemon_slugs) + len(not_found)}") |
| 90 | + print(f"Successfully validated: {len(pokemon_slugs)}") |
| 91 | + print(f"Not found in database: {len(not_found)}") |
| 92 | + |
| 93 | + if not_found: |
| 94 | + print(f"\nPokemon not found in database:") |
| 95 | + for name, slug in not_found: |
| 96 | + print(f" - {name} ({slug})") |
| 97 | + |
| 98 | + # Write output JSON |
| 99 | + with open(output_json, 'w', encoding='utf-8') as f: |
| 100 | + json.dump(pokemon_slugs, f, indent=4, ensure_ascii=False) |
| 101 | + |
| 102 | + print(f"\n✓ Output written to: {output_json}") |
| 103 | + print(f" Total entries: {len(pokemon_slugs)}") |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + parser = argparse.ArgumentParser( |
| 108 | + description="Convert Pokemon name txt file to JSON slug list.", |
| 109 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 110 | + epilog=""" |
| 111 | +Example usage: |
| 112 | + %(prog)s input.txt Pokedex-National.json output.json |
| 113 | + %(prog)s Pokedex-Lumiose.txt Pokedex-National.json Pokedex-Lumiose.json |
| 114 | + """ |
| 115 | + ) |
| 116 | + |
| 117 | + parser.add_argument( |
| 118 | + 'input_txt', |
| 119 | + type=Path, |
| 120 | + help='Input text file with Pokemon names (one per line)' |
| 121 | + ) |
| 122 | + |
| 123 | + parser.add_argument( |
| 124 | + 'database_json', |
| 125 | + type=Path, |
| 126 | + help='National Pokedex JSON database file for slug validation' |
| 127 | + ) |
| 128 | + |
| 129 | + parser.add_argument( |
| 130 | + 'output_json', |
| 131 | + type=Path, |
| 132 | + help='Output JSON file to save the slug list' |
| 133 | + ) |
| 134 | + |
| 135 | + args = parser.parse_args() |
| 136 | + |
| 137 | + main(args.input_txt, args.database_json, args.output_json) |
0 commit comments