|
| 1 | +""" Dynamically generate synonyms for docs to better type match""" |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import typesense |
| 5 | + |
| 6 | +from typesense_indexer import TYPESENSE_CONFIG |
| 7 | + |
| 8 | +client = typesense.Client(TYPESENSE_CONFIG) |
| 9 | + |
| 10 | +def get_folder_hierarchy(root: str) -> dict: |
| 11 | + """Recursively build folder hierarchy from root, ignoring unwanted folders.""" |
| 12 | + hierarchy = {} |
| 13 | + for entry in os.scandir(root): |
| 14 | + if entry.is_dir() and entry.name not in ("__pycache__", ".git", ".venv"): |
| 15 | + hierarchy[entry.name] = get_folder_hierarchy(entry.path) |
| 16 | + return hierarchy |
| 17 | + |
| 18 | +def generate_synonyms(name: str) -> list[str]: |
| 19 | + """ |
| 20 | + Generate multiple synonym forms for a folder/component name: |
| 21 | + - flattened lowercase: reactflow |
| 22 | + - lowercase spaced: react flow |
| 23 | + - title case spaced: React Flow |
| 24 | + - underscore: react_flow |
| 25 | + - hyphen: react-flow |
| 26 | + - camelcase: ReactFlow |
| 27 | + """ |
| 28 | + clean_name = name.replace("_", " ").replace("-", " ").strip() |
| 29 | + words = clean_name.split() |
| 30 | + |
| 31 | + synonyms = set() |
| 32 | + |
| 33 | + # 1) Flattened lowercase |
| 34 | + synonyms.add("".join(w.lower() for w in words)) |
| 35 | + # 2) Lowercase spaced |
| 36 | + synonyms.add(" ".join(w.lower() for w in words)) |
| 37 | + # 3) Title case spaced |
| 38 | + synonyms.add(" ".join(w.capitalize() for w in words)) |
| 39 | + # 4) Original underscore |
| 40 | + synonyms.add("_".join(w.lower() for w in words)) |
| 41 | + # 5) Original hyphen |
| 42 | + synonyms.add("-".join(w.lower() for w in words)) |
| 43 | + # 6) CamelCase |
| 44 | + synonyms.add("".join(w.capitalize() for w in words)) |
| 45 | + |
| 46 | + return list(synonyms) |
| 47 | + |
| 48 | +def flatten_hierarchy(hierarchy: dict) -> dict: |
| 49 | + """ |
| 50 | + Flatten nested folder hierarchy into a single dict with synonyms |
| 51 | + keyed by flattened lowercase name. |
| 52 | + """ |
| 53 | + flat = {} |
| 54 | + |
| 55 | + def recurse(subtree): |
| 56 | + for key, value in subtree.items(): |
| 57 | + # Flatten key for canonical form |
| 58 | + key_flat = "".join(key.lower().split("_")).replace("-", "") |
| 59 | + flat[key_flat] = {"synonyms": generate_synonyms(key)} |
| 60 | + if value: |
| 61 | + recurse(value) |
| 62 | + |
| 63 | + recurse(hierarchy) |
| 64 | + return flat |
| 65 | + |
| 66 | +def main() -> bool: |
| 67 | + try: |
| 68 | + docs_root = pathlib.Path("docs") |
| 69 | + hierarchy = get_folder_hierarchy(docs_root) |
| 70 | + SYNONYMS = flatten_hierarchy(hierarchy) |
| 71 | + |
| 72 | + print("Upserting new synonyms to Typesense ...") |
| 73 | + for canonical, info in SYNONYMS.items(): |
| 74 | + client.collections["docs"].synonyms.upsert( |
| 75 | + canonical, |
| 76 | + {"synonyms": info["synonyms"]} |
| 77 | + ) |
| 78 | + |
| 79 | + print("Synonyms synced successfully!") |
| 80 | + return True |
| 81 | + |
| 82 | + except Exception as e: |
| 83 | + print("Error syncing synonyms:", e) |
| 84 | + return False |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + success = main() |
| 88 | + exit(0 if success else 1) |
0 commit comments