| |
| |
| |
| |
| """Generate a self-contained HTML dictionary search page. |
| |
| Reads dictionary.txt, NFC-normalizes entries, and embeds them in |
| a standalone HTML file with search functionality (exact, prefix, |
| substring matches). |
| |
| Usage: |
| uv run src/build_dict_search.py --dict path/to/dictionary.txt |
| uv run src/build_dict_search.py # uses default path |
| """ |
|
|
| import argparse |
| import html |
| import unicodedata |
| from pathlib import Path |
|
|
| DEFAULT_DICT = ( |
| "/home/claude-code/projects/workspace_underthesea/tree-1/" |
| "models/word_segmentation/udd_ws_v1_1-20260211_034002/dictionary.txt" |
| ) |
|
|
|
|
| def nfc(text): |
| return unicodedata.normalize("NFC", text) |
|
|
|
|
| def load_dictionary(dict_path): |
| entries = [] |
| with open(dict_path, encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if line: |
| entries.append(nfc(line)) |
| entries.sort() |
| return entries |
|
|
|
|
| def build_html(entries): |
| |
| |
| js_lines = [] |
| for entry in entries: |
| escaped = html.escape(entry) |
| js_lines.append(escaped) |
| dict_joined = "\n".join(js_lines) |
|
|
| return f"""<!DOCTYPE html> |
| <html lang="vi"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>Vietnamese Dictionary Search ({len(entries):,} entries)</title> |
| <style> |
| * {{ box-sizing: border-box; margin: 0; padding: 0; }} |
| body {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; padding: 20px; max-width: 800px; margin: 0 auto; background: #fafafa; }} |
| h1 {{ font-size: 1.3em; margin-bottom: 4px; color: #333; }} |
| .subtitle {{ color: #666; font-size: 0.9em; margin-bottom: 16px; }} |
| .search-box {{ position: relative; margin-bottom: 16px; }} |
| input {{ width: 100%; padding: 12px 16px; font-size: 16px; border: 2px solid #ddd; border-radius: 8px; outline: none; }} |
| input:focus {{ border-color: #4CAF50; }} |
| .results {{ background: white; border-radius: 8px; border: 1px solid #e0e0e0; }} |
| .section {{ padding: 12px 16px; border-bottom: 1px solid #f0f0f0; }} |
| .section:last-child {{ border-bottom: none; }} |
| .section-title {{ font-size: 0.8em; font-weight: 600; color: #888; text-transform: uppercase; margin-bottom: 6px; }} |
| .match {{ padding: 4px 0; font-size: 15px; }} |
| .match.exact {{ font-weight: 600; color: #2e7d32; }} |
| .match mark {{ background: #fff9c4; padding: 0 1px; border-radius: 2px; }} |
| .no-results {{ padding: 16px; color: #999; text-align: center; }} |
| .stats {{ color: #999; font-size: 0.85em; padding: 8px 16px; text-align: right; }} |
| .hidden {{ display: none; }} |
| </style> |
| </head> |
| <body> |
| <h1>Vietnamese Dictionary Search</h1> |
| <p class="subtitle">{len(entries):,} entries</p> |
| <div class="search-box"> |
| <input type="text" id="q" placeholder="Type a word to search..." autofocus autocomplete="off"> |
| </div> |
| <div id="results" class="results hidden"></div> |
| |
| <script> |
| const RAW = `{dict_joined}`; |
| const DICT = RAW.split("\\n"); |
| const DICT_SET = new Set(DICT); |
| |
| // Decode HTML entities for matching |
| function decode(s) {{ |
| const el = document.createElement("span"); |
| el.innerHTML = s; |
| return el.textContent; |
| }} |
| |
| const DICT_DECODED = DICT.map(decode); |
| |
| const input = document.getElementById("q"); |
| const resultsDiv = document.getElementById("results"); |
| |
| let debounceTimer; |
| input.addEventListener("input", () => {{ |
| clearTimeout(debounceTimer); |
| debounceTimer = setTimeout(doSearch, 150); |
| }}); |
| |
| function doSearch() {{ |
| const q = input.value.trim().toLowerCase(); |
| if (!q) {{ |
| resultsDiv.classList.add("hidden"); |
| return; |
| }} |
| |
| const exact = []; |
| const prefix = []; |
| const substring = []; |
| const MAX = 50; |
| |
| for (let i = 0; i < DICT_DECODED.length; i++) {{ |
| const entry = DICT_DECODED[i]; |
| const lower = entry.toLowerCase(); |
| if (lower === q) {{ |
| exact.push(i); |
| }} else if (lower.startsWith(q)) {{ |
| if (prefix.length < MAX) prefix.push(i); |
| }} else if (lower.includes(q)) {{ |
| if (substring.length < MAX) substring.push(i); |
| }} |
| if (prefix.length >= MAX && substring.length >= MAX && exact.length > 0) break; |
| }} |
| |
| let html = ""; |
| |
| if (exact.length > 0) {{ |
| html += '<div class="section"><div class="section-title">Exact match</div>'; |
| for (const i of exact) {{ |
| html += '<div class="match exact">' + highlight(DICT_DECODED[i], q) + "</div>"; |
| }} |
| html += "</div>"; |
| }} |
| |
| if (prefix.length > 0) {{ |
| html += '<div class="section"><div class="section-title">Prefix matches (' + prefix.length + ')</div>'; |
| for (const i of prefix) {{ |
| html += '<div class="match">' + highlight(DICT_DECODED[i], q) + "</div>"; |
| }} |
| html += "</div>"; |
| }} |
| |
| if (substring.length > 0) {{ |
| html += '<div class="section"><div class="section-title">Substring matches (' + substring.length + ')</div>'; |
| for (const i of substring) {{ |
| html += '<div class="match">' + highlight(DICT_DECODED[i], q) + "</div>"; |
| }} |
| html += "</div>"; |
| }} |
| |
| if (!html) {{ |
| html = '<div class="no-results">No matches found</div>'; |
| }} |
| |
| const total = exact.length + prefix.length + substring.length; |
| html += '<div class="stats">' + total + " result(s)</div>"; |
| |
| resultsDiv.innerHTML = html; |
| resultsDiv.classList.remove("hidden"); |
| }} |
| |
| function highlight(text, query) {{ |
| const lower = text.toLowerCase(); |
| const idx = lower.indexOf(query); |
| if (idx < 0) return escapeHtml(text); |
| return escapeHtml(text.substring(0, idx)) |
| + "<mark>" + escapeHtml(text.substring(idx, idx + query.length)) + "</mark>" |
| + escapeHtml(text.substring(idx + query.length)); |
| }} |
| |
| function escapeHtml(s) {{ |
| return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); |
| }} |
| </script> |
| </body> |
| </html>""" |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Generate standalone dictionary search HTML page" |
| ) |
| parser.add_argument( |
| "--dict", |
| default=DEFAULT_DICT, |
| help=f"Path to dictionary.txt (default: {DEFAULT_DICT})", |
| ) |
| parser.add_argument( |
| "--output", |
| default="src/dict_search.html", |
| help="Output HTML path (default: src/dict_search.html)", |
| ) |
| args = parser.parse_args() |
|
|
| dict_path = Path(args.dict) |
| if not dict_path.exists(): |
| print(f"ERROR: {dict_path} not found") |
| return |
|
|
| entries = load_dictionary(dict_path) |
| print(f"Loaded {len(entries)} dictionary entries from {dict_path}") |
|
|
| html_content = build_html(entries) |
| output_path = Path(args.output) |
| output_path.parent.mkdir(parents=True, exist_ok=True) |
| output_path.write_text(html_content, encoding="utf-8") |
|
|
| size_kb = output_path.stat().st_size / 1024 |
| print(f"Written {output_path} ({size_kb:.0f} KB)") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|