"""Original whole-context G2P and indexed local literal matching.""" from pathlib import Path import json import sqlite3 import pypinyin from . import pronunciation, phonetic_math, phonetic_query, char_matches class LocalFrontend: def __init__(self, root): if pypinyin.__version__ != '0.55.0': raise ValueError('This trained frontend requires pypinyin==0.55.0') root = Path(root) vocab = json.loads((root / 'assets/pronunciation_vocab.json').read_text()) tokens = vocab['tokens'] if len(tokens) != 427 or len(set(tokens)) != 427: raise ValueError('The complete trained 427-phoneme vocabulary is required') self.symbols = {token: i for i, token in enumerate(tokens)} self.database = sqlite3.connect((root / 'assets/nc11.sqlite').resolve().as_uri() + '?mode=ro&immutable=1', uri=True, check_same_thread=False) self.database.execute('PRAGMA query_only=ON') def analyze_current_local_view(self, local_text, *, observed_version, audio_time_us, received_audio_time_us, source_audio_eof, asr_done, cells): if type(local_text) is not str or len(local_text) > 64 or asr_done: raise ValueError('A bounded actual nonfinal raw view is required') cells = tuple(sorted(set(cells))) if len(cells) > 9 or audio_time_us > received_audio_time_us: raise ValueError('Invalid context or audio clock') raw = pypinyin.lazy_pinyin(local_text, style=pypinyin.Style.NORMAL, errors=lambda chars: list(chars)) mapping = phonetic_math.whole_context_mapping(local_text, raw, pronunciation.default_pinyin_tokens(local_text), pronunciation.normalize_pronunciation_tokens, self.symbols) families, proof = phonetic_query.query_local_families(self.database, mapping, list(cells)) analysis = phonetic_math.analyze_consistency(mapping, families, include_matches=True) valid = not mapping['alignment_uncertain'] and bool(proof['scope']['mapped_region_ids']) and any( not char['barrier'] for char in mapping['characters']) status = 'LOCAL64_ORIGINAL_CHARACTER_MAPPING_VALID' if valid else 'LOCAL64_ORIGINAL_CHARACTER_MAPPING_INVALID' return char_matches.PartialMatches(observed_version, audio_time_us, received_audio_time_us, status, valid, local_text, cells, mapping, families, analysis, proof, phonetic_math) def close(self): self.database.close()