### Project Logging Utilities (Python) Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Provides functions and a context manager for configuring and using the project's logger. Includes methods for getting a logger instance, setting logging levels and formats, and a context manager for timing operations. ```python def get_logger(name: ...=...) -> ...: """Get the project logger.""" pass def setup_logger(level: ...=..., format_string: ...=..., handler: ...=...) -> ...: """Configure the project logger.""" pass class TimingContext: """Timing context manager.""" def __init__(self, operation: ..., logger: ...=..., level: ...=..., callback: ...=...): """Initialize the timing context.""" pass def __enter__(self) -> ...: """Enter the timing block and record the start time.""" pass def __exit__(self, exc_type, exc_val, exc_tb) -> ...: """Exit the timing block, compute elapsed time, and optionally write to logger/callback.""" pass def log_timing(operation: ...=..., logger: ...=..., level: ...=...): """Timing decorator.""" pass def log_timing.decorator(func: ...) -> ...: """Wrap any function with timing logic.""" pass def log_timing.decorator.wrapper(*args, **kwargs) -> ...: """Actual wrapper: time before/after the call via `TimingContext`.""" pass def enable_debug_logging() -> ...: """Enable debug logging.""" pass def enable_timing_logging() -> ...: """Enable timing logging.""" pass ``` -------------------------------- ### Chaining Correctors for Mixed Language Text in Phonofix Source: https://github.com/joneshong/phonofix/blob/main/README.md This example demonstrates how to handle mixed-language input text using Phonofix by manually chaining multiple language-specific correctors. It shows creating separate correctors for Chinese and English, then applying them sequentially to the input string. This allows for proper noun correction in a combined Chinese and English text, outputting a fully corrected string. ```python from phonofix import ChineseEngine, EnglishEngine ch = ChineseEngine().create_corrector({"台北車站": ["北車"]}) en = EnglishEngine().create_corrector({"Python": ["Pyton"]}) text = "我在北車用Pyton寫code" text = en.correct(text, full_context=text) text = ch.correct(text, full_context=text) print(text) # Output: 我在台北車站用Python寫code ``` -------------------------------- ### ChineseEngine: Get Backend Statistics Source: https://context7.com/joneshong/phonofix/llms.txt Retrieves backend cache statistics and initialization status for the ChineseEngine. This is useful for performance monitoring and debugging, providing insights into cache utilization and engine readiness. ```python from phonofix import ChineseEngine engine = ChineseEngine() # Check initialization status is_ready = engine.is_initialized() print(f"Engine initialized: {is_ready}") # Output: Engine initialized: True # Get backend statistics stats = engine.get_backend_stats() print(f"Backend stats: {stats}") # Output: { # 'cache_size': 1234, # 'hits': 5678, # 'misses': 90, # 'hit_rate': 0.984 # } ``` -------------------------------- ### Fail Policy - Production vs Evaluation Mode - Python Source: https://context7.com/joneshong/phonofix/llms.txt This Python example explains the `fail_policy` parameter, controlling error handling. The 'degrade' policy (default) is suitable for production, allowing the process to continue by degrading. The 'raise' policy is useful for evaluation or CI environments to catch errors immediately. ```python from phonofix import EnglishEngine engine = EnglishEngine() corrector = engine.create_corrector(["TensorFlow", "Python"]) # Production mode: degrade on error, emit events result = corrector.correct( "Learning tensor flow", mode="production", # Equivalent to fail_policy="degrade" silent=False ) # Evaluation mode: raise on error for CI/testing try: result = corrector.correct( "Learning tensor flow", mode="evaluation", # Equivalent to fail_policy="raise" fail_policy="raise" ) except Exception as e: print(f"Error caught: {e}") ``` -------------------------------- ### Correct English Proper Nouns with Phonofix Source: https://github.com/joneshong/phonofix/blob/main/README.md This example shows how to use the `EnglishEngine` for correcting English proper nouns, which relies on external tools like `espeak-ng`. A corrector is set up with English proper nouns and their phonetic representations. The `correct` method then processes the input text to standardize or correct these nouns. The corrected English text is printed as output. ```python from phonofix import EnglishEngine engine = EnglishEngine() corrector = engine.create_corrector({"TensorFlow": ["Ten so floor"], "Python": ["Pyton"]}) print(corrector.correct("I use Pyton to write Ten so floor code")) # Output: I use Python to write TensorFlow code ``` -------------------------------- ### Protected Terms Exclusion List - Python Source: https://context7.com/joneshong/phonofix/llms.txt This Python example shows how to use the `protected_terms` parameter to prevent specific words or phrases from being corrected. Any term listed in `protected_terms` will remain unchanged, even if it matches a known alias or keyword. ```python from phonofix import ChineseEngine engine = ChineseEngine() corrector = engine.create_corrector( ["台北車站"], protected_terms=["北側", "車站"] # These won't be corrected ) result = corrector.correct("車站北側有商店") print(result) # Output: 車站北側有商店 (protected terms unchanged) ``` -------------------------------- ### Phonetic Conversion Benchmarking (Python) Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Scripts for benchmarking the performance of phonetic conversion functions (G2P). Includes functions to benchmark individual word conversions and batch conversions, along with helper functions for IPA conversion and an old conversion method. ```python def benchmark_g2p(convert_func: ..., words: ..., name: ..., iterations: ...=...) -> ...: """Benchmark per-word G2P function performance.""" pass def benchmark_g2p_batch(convert_batch_func: ..., words: ..., name: ..., iterations: ...=...) -> ...: """Benchmark batch G2P function performance (convert multiple strings at once).""" pass def main() -> ...: pass def main.to_ipa(word: ...) -> ...: pass def main.to_ipa_batch(words: ...) -> ...: pass def main.old_convert(text: ...) -> ...: pass ``` -------------------------------- ### Lazy-load Symbols in phonofix/__init__.py Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Implements PEP 562 for lazy loading of top-level public symbols in the phonofix package. This includes the __getattr__ function for dynamic symbol retrieval and __dir__ to expose these symbols for IDEs and dir() calls. ```python def __getattr__(name: ...) -> ... # Lazy-load top-level public symbols (PEP 562). def __dir__() -> ... # Expose lazy-loaded symbols to IDE/dir(). ``` -------------------------------- ### Japanese Indexing API Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Functions for building search indexes, including exact matchers and fuzzy buckets for efficient searching of Japanese terms. ```APIDOC ## Indexing Functions ### `first_romaji_group(romaji: ...) -> ...` Get the first-sound group for romaji. ### `build_search_index(*, phonetic: ..., tokenizer: ..., term_mapping: ...) -> ...` Build the search index. ### `build_exact_matcher(search_index: ...) -> ...` Build an exact-match index for surface aliases (Aho-Corasick). ### `build_fuzzy_buckets(*, search_index: ...) -> ...` Build a bucket index for cheap pruning. ``` -------------------------------- ### ChineseEngine: Timing Callbacks for Performance Monitoring Source: https://context7.com/joneshong/phonofix/llms.txt Configures timing callbacks for the ChineseEngine to monitor operation durations. This allows integration with observability systems by logging the time taken for specific engine operations. ```python from phonofix import ChineseEngine def timing_callback(operation: str, elapsed: float): print(f"Operation '{operation}' took {elapsed*1000:.2f}ms") engine = ChineseEngine( verbose=True, # Enable debug logging on_timing=timing_callback ) corrector = engine.create_corrector(["台北車站", "牛奶", "發揮"]) # Output: Operation 'create_corrector' took 45.23ms result = corrector.correct("我在北車買了流奶") # Output: Operation 'correct' took 12.34ms ``` -------------------------------- ### Japanese Fuzzy Generation API Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Provides functionalities for generating fuzzy variants of Japanese text, including normalization, romaji expansion, and kana confusion. ```APIDOC ## JapaneseFuzzyGenerator Class ### Description Japanese fuzzy variant generator (surface-only, optional). ### Methods - `__init__(self, config: ...=..., backend: ...=..., *, enable_representative_variants: ...=..., max_phonetic_states: ...=...) -> ...` Initialize the Japanese fuzzy variant generator. - `generate_variants(self, term: ..., max_variants: ...=...) -> ...` Generate Japanese fuzzy variants for the input term (surface variants). - `_to_hiragana_reading(self, text: ...) -> ...` Convert Japanese text to a hiragana reading string. - `_to_romaji(self, text: ...) -> ...` Convert Japanese text (kana/kanji) to romaji. - `_phonetic_key(self, text: ...) -> ...` Get the candidate's phonetic key (normalized romaji). - `_romaji_rule_variants(self, romaji: ...) -> ...` Generate a small set of rule-based romaji variants (Hepburn/Kunrei, long vowels, sokuon, nasal). - `_kana_confusion_variants(self, hira: ...) -> ...` Generate kana-level confusion variants (more expensive, optional). ### Sub-methods - `_kana_confusion_variants.options(ch: ...) -> ...` Get replacement options for a single kana (including the original). ## Helper Functions ### `_kata_to_hira(text: ...) -> ...` Convert katakana to hiragana (kana only; other chars unchanged). ### `_hira_to_kata(text: ...) -> ...` Convert hiragana to katakana (kana only; other chars unchanged). ### `_has_japanese_script(text: ...) -> ...` Roughly detect whether text contains Japanese scripts (kana or common Kanji ranges). ### `_normalize_romaji(romaji: ..., config: ...) -> ...` Normalize romaji to produce a stable phonetic key. ### `_romaji_variants(base: ..., config: ..., *, max_states: ...) -> ...` Do limited expansion in the romaji space (avoid combinatorial explosion). ``` -------------------------------- ### Japanese Tokenizer API Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Implements a tokenizer specifically for Japanese text, enabling word splitting and index retrieval. ```APIDOC ## JapaneseTokenizer Class ### Description Japanese tokenizer. ### Methods - `__init__(self, backend: ...=...) -> ...` Initialize the Japanese tokenizer. - `tokenize(self, text: ...) -> ...` Split Japanese text into a list of words. - `get_token_indices(self, text: ...) -> ...` Get the start/end indices of each word in the original text. ``` -------------------------------- ### Japanese Phonetic System API Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Implements the phonetic system for Japanese, handling conversion to romaji, similarity scoring, and fuzzy matching. ```APIDOC ## JapanesePhoneticSystem Class ### Description Japanese phonetic system. ### Methods - `__init__(self, backend: ...=...) -> ...` Initialize the Japanese phonetic system. - `to_phonetic(self, text: ...) -> ...` Convert Japanese text to romaji. - `calculate_similarity_score(self, phonetic1: ..., phonetic2: ...) -> ...` Calculate romaji similarity score. - `_normalize_phonetic(self, phonetic: ...) -> ...` Normalize romaji for fuzzy matching. - `are_fuzzy_similar(self, phonetic1: ..., phonetic2: ...) -> ...` Determine whether two romaji strings are fuzzily similar. - `get_tolerance(self, length: ...) -> ...` Choose tolerance based on text length. ``` -------------------------------- ### PhoneticBackend Abstract Base Class in phonofix/backend/base.py Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Defines the abstract base class (ABC) for phonetic backends, outlining essential methods for converting text to phonetic representations. It includes methods for initialization, checking initialization status, retrieving cache statistics, and clearing the cache. ```python class PhoneticBackend(ABC): # Phonetic backend abstract base class (ABC). def to_phonetic(self, text: ...) -> ...: # Convert text to a phonetic representation. def is_initialized(self) -> ...: # Check whether the backend is initialized. def initialize(self) -> ...: # Initialize the backend. def get_cache_stats(self) -> ...: # Get cache statistics. def clear_cache(self) -> ...: # Clear the cache. ``` -------------------------------- ### Japanese Replacements API Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Utilities for applying text replacements and resolving conflicts within candidate sets for Japanese text. ```APIDOC ## Replacement Functions ### `resolve_conflicts(*, candidates: ..., logger: ...=...) -> ...` Resolve candidate conflicts (lower score wins). ### `apply_replacements(*, text: ..., candidates: ..., emit_replacement: ..., logger: ..., silent: ...=..., trace_id: ...=...) -> ...` Apply corrections and log (rebuild string to avoid index shifting). ``` -------------------------------- ### Observability with Event Handlers - Python Source: https://context7.com/joneshong/phonofix/llms.txt This Python code demonstrates how to implement event handlers for monitoring corrections, errors, and degradation events. The `on_event` parameter accepts a callback function that processes `CorrectionEvent` objects. Silent mode can be enabled to suppress logger output while still emitting events. ```python from phonofix import ChineseEngine from phonofix.core.events import CorrectionEvent def event_handler(event: CorrectionEvent): if event["type"] == "replacement": print(f"Replaced '{event['original']}' with '{event['replacement']}' " f"at position {event['start']}-{event['end']}") elif event["type"] == "degraded": print(f"Degraded to {event['fallback']}: {event['degrade_reason']}") elif event["type"] == "fuzzy_error": print(f"Fuzzy error at {event['stage']}: {event['exception_message']}") engine = ChineseEngine() corrector = engine.create_corrector( ["台北車站", "牛奶"], on_event=event_handler ) # Silent mode: no stdout, but events still fire result = corrector.correct( "我在北車買流奶", silent=True, trace_id="req-12345" ) # Event output: # Replaced '北車' with '台北車站' at position 2-4 # Replaced '流奶' with '牛奶' at position 5-7 ``` -------------------------------- ### Correct Japanese Proper Nouns with Phonofix Source: https://github.com/joneshong/phonofix/blob/main/README.md This snippet illustrates the usage of `JapaneseEngine` for phonetic-similarity substitution in Japanese. It involves initializing the engine, creating a corrector with Japanese terms and their Romaji phonetic equivalents, and then using the `correct` method to process input text. The output is the corrected Japanese text, potentially including standardized proper nouns. ```python from phonofix import JapaneseEngine engine = JapaneseEngine() corrector = engine.create_corrector({"会議": ["kaigi"], "ロボット": ["robotto"]}) print(corrector.correct("明日のkaigiに参加します")) # Output: 明日の会議に参加します print(corrector.correct("新しいrobottoのkaihatsu")) # Example: can also correct other terms # Output: 新しいロボットのkaihatsu ``` -------------------------------- ### Japanese Scoring API Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Provides a function to calculate the final score for Japanese text candidates, considering error ratios and context. ```APIDOC ## Scoring Function ### `calculate_final_score(*, error_ratio: ..., item: ..., has_context: ..., context_distance: ...=...) -> ...` Calculate final score (lower is better). ``` -------------------------------- ### Aho-Corasick String Matching (Python) Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Implements the Aho-Corasick algorithm for efficient multi-pattern string matching. It includes node representation for the trie, the main matcher class, methods to add patterns, build the automaton, and iterate through matches in a given text. ```python class _Node(Generic[...]): """Aho-Corasick trie node (internal).""" pass class AhoCorasick(Generic[...]): """Aho-Corasick multi-pattern string matcher.""" def __init__(self) -> ...: """Create the matcher. After calling `add()`, call `build()` before matching (or let `iter_matches` auto-build).""" pass def add(self, word: ..., value: ...) -> ...: """Add a pattern.""" pass def build(self) -> ...: """Build fail links (convert the trie into a linearly scannable automaton).""" pass def iter_matches(self, text: ...) -> ...: """Iterate and yield matches.""" pass ``` -------------------------------- ### Weight System for Prioritization - Python Source: https://context7.com/joneshong/phonofix/llms.txt This Python snippet illustrates the weight system for controlling substitution priority. By assigning different weights to potential corrections, users can influence which correction is chosen when multiple candidates match. Higher weights increase tolerance and prioritization. ```python from phonofix import ChineseEngine engine = ChineseEngine() corrector = engine.create_corrector({ "台北車站": { "aliases": ["北車"], "weight": 0.5 # High priority landmark }, "台北市": { "aliases": [], "weight": 0.1 # Lower priority } }) result = corrector.correct("我在北車等你") print(result) # Output: 我在台北車站等你 (higher weight wins) ``` -------------------------------- ### Translation Client API Call (Python) Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md A function to call a local translation API. It takes text and a target language as input and returns the translated text. This is a client-side function for interacting with a translation service. ```python def translate_text(text, target_lang=...): """Call the local translation API.""" pass ``` -------------------------------- ### Japanese Type Definitions Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Defines the type hints for various data structures used within the Japanese language module, such as index items and candidate drafts. ```APIDOC ## Type Definitions ### `JapaneseIndexItem(TypedDict)` Single index item (term or alias). ### `JapaneseCandidateDraft(TypedDict)` Candidate draft. ### `JapaneseCandidate(TypedDict)` Final candidate. ``` -------------------------------- ### English Fuzzy Generator: Surface and Representative Variants Source: https://context7.com/joneshong/phonofix/llms.txt Generates English variants with surface transformations (e.g., spacing, capitalization) and optional representative variants (phonetic alternatives). It demonstrates generating both types of variants and comparing their outputs. ```python from phonofix import EnglishEngine from phonofix.languages.english.fuzzy_generator import EnglishFuzzyGenerator engine = EnglishEngine() # Safe mode: surface variants only (spacing, caps) generator_safe = EnglishFuzzyGenerator(enable_representative_variants=False) # Representative mode: includes phonetic alternatives generator_repr = EnglishFuzzyGenerator(enable_representative_variants=True) # Compare variant generation safe_variants = generator_safe.generate_variants("TensorFlow", max_variants=20) repr_variants = generator_repr.generate_variants("TensorFlow", max_variants=20) print(f"Safe variants: {safe_variants}") # Output: ['tensorflow', 'tensor flow', 'tensor-flow', 'TensorFlow'] print(f"Repr variants count: {len(repr_variants)}") # Output: Repr variants count: 12 print(f"Repr variants (first 10): {repr_variants[:10]}") # Includes phonetically similar alternatives ``` -------------------------------- ### Correct Chinese Proper Nouns with Phonofix Source: https://github.com/joneshong/phonofix/blob/main/README.md This snippet demonstrates how to use the `ChineseEngine` from the Phonofix library to correct Chinese proper nouns. It requires creating an engine, defining a corrector with a dictionary of canonical forms and their phonetic aliases, and then applying the correction to input text. The output is the corrected Chinese text. ```python from phonofix import ChineseEngine engine = ChineseEngine() corrector = engine.create_corrector({"台北車站": ["北車", "胎北車站"]}) print(corrector.correct("我在北車等你")) # Output: 我在台北車站等你 ``` -------------------------------- ### Context Exclusion with exclude_when - Python Source: https://context7.com/joneshong/phonofix/llms.txt This Python code demonstrates how to use the `exclude_when` parameter to prevent incorrect corrections based on context. When specified keywords are detected, the correction is blocked even if other matching keywords are present. This enhances the precision of the corrector. ```python from phonofix import ChineseEngine engine = ChineseEngine() corrector = engine.create_corrector({ "心電圖設備": { "aliases": ["心電圖社北", "心電圖社備"], "keywords": ["醫院", "檢查", "醫療"], "exclude_when": ["公司", "銷售", "新創"] } }) result1 = corrector.correct("醫院有心電圖社備") print(result1) # Output: 醫院有心電圖設備 (has keywords, no exclusions) result2 = corrector.correct("心電圖社備公司成立") print(result2) # Output: 心電圖社備公司成立 (excluded due to "公司") ``` -------------------------------- ### Chain Multiple Language Correctors - Python Source: https://context7.com/joneshong/phonofix/llms.txt This snippet demonstrates how to manually chain multiple language correctors for mixed-language text processing. It requires creating separate engines and correctors for each language and then applying them sequentially. The order of chaining is crucial for accurate results. ```python from phonofix import ChineseEngine, EnglishEngine # Create separate engines ch_engine = ChineseEngine() en_engine = EnglishEngine() # Create correctors for each language ch_corrector = ch_engine.create_corrector({ "台北車站": ["北車"] }) en_corrector = en_engine.create_corrector({ "Python": ["Pyton", "Pyson"], "TensorFlow": ["Ten so floor", "tensor flow"] }) # Chain correctors: English first, then Chinese text = "我在北車用Pyton寫code" text = en_corrector.correct(text, full_context=text) text = ch_corrector.correct(text, full_context=text) print(text) # Output: 我在台北車站用Python寫code ``` -------------------------------- ### EnglishEngine - Create English Phonetic Corrector Source: https://context7.com/joneshong/phonofix/llms.txt Creates an English corrector using IPA (International Phonetic Alphabet) phonetic similarity matching. Ideal for correcting ASR misheard proper nouns. ```APIDOC ## EnglishEngine - Create English Phonetic Corrector ### Description Creates an English corrector using IPA (International Phonetic Alphabet) phonetic similarity matching. Ideal for correcting ASR misheard proper nouns where technical terms are transcribed as phonetically similar common words. ### Method POST (Implicit - creating a corrector object) ### Endpoint `/phonofix/english/corrector` (Conceptual) ### Parameters #### Request Body - **dictionary** (dict) - Required - A dictionary where keys are canonical English terms and values are lists of phonetically similar aliases or phrases. ### Request Example (Basic) ```json { "TensorFlow": ["tensor flow", "tens are flow", "ten so flow"], "Kubernetes": ["cooper net ease", "cube and at ease"], "scikit-learn": ["psychic learn", "sigh kit learn"] } ``` ### Request Example (Advanced) ```json { "EKG": { "aliases": ["1 kg", "one kg", "1kg"], "keywords": ["medical", "heart", "patient", "monitor"], "exclude_when": ["weight", "heavy", "kilogram", "weighs"], "weight": 0.5 } } ``` ### Response #### Success Response (200) A corrector object that can be used to correct text. #### Response Example ```json { "message": "Corrector created successfully" } ``` ``` -------------------------------- ### ChineseEngine - Create Chinese Phonetic Corrector Source: https://context7.com/joneshong/phonofix/llms.txt Creates a Chinese corrector using Pinyin fuzzy matching with support for Taiwan accent features. It supports manual aliases, context keywords, and weight-based prioritization. ```APIDOC ## ChineseEngine - Create Chinese Phonetic Corrector ### Description Creates a Chinese corrector using Pinyin fuzzy matching with support for Taiwan accent features (n/l confusion, f/h confusion, r/l confusion). The engine automatically generates phonetic variants based on fuzzy Pinyin rules and supports manual aliases, context keywords, and weight-based prioritization. ### Method POST (Implicit - creating a corrector object) ### Endpoint `/phonofix/chinese/corrector` (Conceptual) ### Parameters #### Request Body - **dictionary** (list or dict) - Required - A list of canonical terms or a dictionary with canonical terms as keys and their configurations (aliases, keywords, weight) as values. ### Request Example (Simple) ```json [ "台北車站", "牛奶", "發揮" ] ``` ### Request Example (Advanced) ```json { "永和豆漿": { "aliases": ["永豆", "勇豆", "永鬥"], "keywords": ["吃", "喝", "買", "宵夜", "早餐"], "weight": 0.3 }, "勇者鬥惡龍": { "aliases": ["勇鬥", "永鬥"], "keywords": ["玩", "遊戲", "攻略", "破關"], "weight": 0.2 } } ``` ### Response #### Success Response (200) A corrector object that can be used to correct text. #### Response Example ```json { "message": "Corrector created successfully" } ``` ``` -------------------------------- ### JapaneseEngine - Create Japanese Phonetic Corrector Source: https://context7.com/joneshong/phonofix/llms.txt Creates a Japanese corrector using Romaji phonetic matching. Handles long vowel omissions, geminate errors, and particle mistakes common in ASR systems. ```APIDOC ## JapaneseEngine - Create Japanese Phonetic Corrector ### Description Creates a Japanese corrector using Romaji phonetic matching. Handles long vowel omissions, geminate errors, and particle mistakes common in ASR systems. ### Method POST (Implicit - creating a corrector object) ### Endpoint `/phonofix/japanese/corrector` (Conceptual) ### Parameters #### Request Body - **dictionary** (list or dict) - Required - A list of canonical Japanese terms (in Romaji or Kana) or a dictionary with canonical terms as keys and their configurations (aliases, keywords, weight) as values. ### Request Example (Simple) ```json [ "会議", "プロジェクト", "エンジニア" ] ``` ### Request Example (Advanced) ```json { "箸": { "aliases": ["hashi"], "keywords": ["食べる", "ご飯", "使う", "持つ"], "weight": 0.5 }, "橋": { "aliases": ["hashi"], "keywords": ["渡る", "川", "長い", "建設"], "weight": 0.5 }, "端": { "aliases": ["hashi"], "keywords": ["歩く", "道", "隅"], "weight": 0.5 } } ``` ### Response #### Success Response (200) A corrector object that can be used to correct text. #### Response Example ```json { "message": "Corrector created successfully" } ``` ``` -------------------------------- ### Create Japanese Phonetic Corrector with Phonofix Source: https://context7.com/joneshong/phonofix/llms.txt Creates a Japanese corrector using Romaji phonetic matching, addressing common ASR errors like long vowel omissions and geminate errors. It supports basic Romaji to Japanese conversion and advanced context-aware homophone disambiguation using keywords and weights. ```Python from phonofix import JapaneseEngine # Create engine instance engine = JapaneseEngine() # Basic usage: Romaji to Japanese corrector = engine.create_corrector([ "会議", # kaigi "プロジェクト", # purojekuto "エンジニア" # enjinia ]) result = corrector.correct("明日のkaigiに参加します") print(result) # Output: 明日の会議に参加します # Context-aware homophone disambiguation corrector = engine.create_corrector({ "箸": { "aliases": ["hashi"], "keywords": ["食べる", "ご飯", "使う", "持つ"], "weight": 0.5 }, "橋": { "aliases": ["hashi"], "keywords": ["渡る", "川", "長い", "建設"], "weight": 0.5 }, "端": { "aliases": ["hashi"], "keywords": ["歩く", "道", "隅"], "weight": 0.5 } }) result1 = corrector.correct("hashiを使ってご飯を食べる") print(result1) # Output: 箸を使ってご飯を食べる result2 = corrector.correct("川のhashiを渡る") print(result2) # Output: 川の橋を渡る ``` -------------------------------- ### Chinese Fuzzy Generator - Variant Coverage - Python Source: https://context7.com/joneshong/phonofix/llms.txt This Python snippet introduces the `ChineseFuzzyGenerator` for creating phonetic variants of Chinese text. It highlights two strategies: 'representative' for broader fuzzy matching and 'safe' for more conservative transformations. This is used for generating diverse inputs for fuzzy matching. ```python from phonofix import ChineseEngine from phonofix.languages.chinese.fuzzy_generator import ChineseFuzzyGenerator engine = ChineseEngine() ``` -------------------------------- ### Chinese Fuzzy Generator: Safe vs. Representative Variants Source: https://context7.com/joneshong/phonofix/llms.txt Generates Chinese variants using a fuzzy generator. The safe mode provides minimal variants, while the representative mode offers broader coverage with more variants. It compares the counts and lists of variants generated by both modes. ```python from phonofix import ChineseEngine from phonofix.languages.chinese.fuzzy_generator import ChineseFuzzyGenerator engine = ChineseEngine() # Safe mode: only safe transformations (minimal variants) generator_safe = ChineseFuzzyGenerator( backend=engine.backend, enable_representative_variants=False ) # Representative mode: broader coverage (more variants) generator_repr = ChineseFuzzyGenerator( backend=engine.backend, enable_representative_variants=True ) # Compare variant generation safe_variants = generator_safe.generate_variants("台北車站", max_variants=20) repr_variants = generator_repr.generate_variants("台北車站", max_variants=20) print(f"Safe variants count: {len(safe_variants)}") # Output: Safe variants count: 3 print(f"Representative variants count: {len(repr_variants)}") # Output: Representative variants count: 15 print(f"Safe variants: {safe_variants[:5]}") print(f"Repr variants: {repr_variants[:10]}") ``` -------------------------------- ### Create English Phonetic Corrector with Phonofix Source: https://context7.com/joneshong/phonofix/llms.txt Creates an English corrector using IPA (International Phonetic Alphabet) phonetic similarity. It's ideal for correcting ASR misheard proper nouns and technical terms. Supports basic correction and advanced context-aware features with keywords and exclusion rules to refine accuracy. ```Python from phonofix import EnglishEngine # Create engine instance (requires espeak-ng system package) engine = EnglishEngine() # Basic usage: correct ASR mishearings corrector = engine.create_corrector({ "TensorFlow": ["tensor flow", "tens are flow", "ten so flow"], "Kubernetes": ["cooper net ease", "cube and at ease"], "scikit-learn": ["psychic learn", "sigh kit learn"] }) text = "I use Ten so floor and cooper net ease for ML" result = corrector.correct(text) print(result) # Output: I use TensorFlow and Kubernetes for ML # Context-aware correction with keywords and exclusions corrector = engine.create_corrector({ "EKG": { "aliases": ["1 kg", "one kg", "1kg"], "keywords": ["medical", "heart", "patient", "monitor"], "exclude_when": ["weight", "heavy", "kilogram", "weighs"], "weight": 0.5 } }) result1 = corrector.correct("Check the patient's 1 kg reading") print(result1) # Output: Check the patient's EKG reading result2 = corrector.correct("This box weighs 1 kg") print(result2) # Output: This box weighs 1 kg (no correction due to exclude_when) ``` -------------------------------- ### Create Chinese Phonetic Corrector with Phonofix Source: https://context7.com/joneshong/phonofix/llms.txt Creates a Chinese corrector using Pinyin fuzzy matching, supporting Taiwan accent features. It handles simple lists of terms and advanced configurations with manual aliases, context keywords, and weight-based prioritization. This engine is useful for proper noun correction and homophone disambiguation in Chinese text. ```Python from phonofix import ChineseEngine # Create engine instance (reusable singleton) engine = ChineseEngine() # Simple usage: list of canonical terms corrector = engine.create_corrector(["台北車站", "牛奶", "發揮"]) result = corrector.correct("我在北車買了流奶,他花揮了才能") print(result) # Output: 我在台北車站買了牛奶,他發揮了才能 # Advanced usage: manual aliases with context keywords corrector = engine.create_corrector({ "永和豆漿": { "aliases": ["永豆", "勇豆", "永鬥"], "keywords": ["吃", "喝", "買", "宵夜", "早餐"], "weight": 0.3 }, "勇者鬥惡龍": { "aliases": ["勇鬥", "永鬥"], "keywords": ["玩", "遊戲", "攻略", "破關"], "weight": 0.2 } }) result = corrector.correct("我去買勇鬥當宵夜") print(result) # Output: 我去買永和豆漿當宵夜 result = corrector.correct("這款永鬥的攻略很難找") print(result) # Output: 這款勇者鬥惡龍的攻略很難找 ``` -------------------------------- ### Check Japanese Character (Python) Source: https://github.com/joneshong/phonofix/blob/main/snapshot.md Determines if a given character is a Japanese character (hiragana or katakana). This utility is useful for text processing tasks involving Japanese language. ```python def is_japanese_char(char: ...) -> ...: """Determine whether a character is Japanese (hiragana/katakana).""" pass ``` -------------------------------- ### Chinese Fuzzy Generator: Homophone Filtering Source: https://context7.com/joneshong/phonofix/llms.txt Filters Chinese homophone duplicates from a list of terms using phonetic key deduplication. This prevents dictionary bloat by retaining only the first occurrence of each phonetic representation. ```python from phonofix import ChineseEngine from phonofix.languages.chinese.fuzzy_generator import ChineseFuzzyGenerator engine = ChineseEngine() generator = ChineseFuzzyGenerator(backend=engine.backend) # Manual alias list with potential homophones manual_terms = [ "台北車站", "太北車站", "胎北車站", "臺北車站", # Same pinyin as 台北車站 "台北市" ] result = generator.filter_homophones(manual_terms) print(f"Kept: {result['kept']}") # Output: Kept: ['台北車站', '太北車站', '胎北車站', '台北市'] print(f"Filtered (duplicates): {result['filtered']}") # Output: Filtered: [('臺北車站', 'tai2bei3che1zhan4', '台北車站')] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.