Skip to content

MWE

burmesenlp.mwe.BMWEEngine

BMWEEngine(lexicon: Optional[Lexicon] = None, validator: Optional[MWEValidator] = None, *, autoload_idioms: bool = True)

Burmese Multi-Word Expression engine (post-tokenization merge).

Source code in src/burmesenlp/mwe/engine.py
def __init__(
    self,
    lexicon: Optional[Lexicon] = None,
    validator: Optional[MWEValidator] = None,
    *,
    autoload_idioms: bool = True,
):
    self._lexicon = lexicon if lexicon is not None else Lexicon.default()
    self._trie = MWETrie()
    self._validator: MWEValidator = (
        validator if validator is not None else AcceptAllValidator()
    )
    if autoload_idioms:
        path = default_idioms_path()
        if path:
            try:
                n = self.load(path, category="IDIOM")
                logger.info("Loaded %d MWE entries from %s", n, path)
            except (OSError, ValueError, json.JSONDecodeError) as exc:
                logger.warning("Could not autoload idioms from %s: %s", path, exc)

load

load(path: str, *, category: Optional[str] = None, priority: int = 0, allow_unigrams: bool = False, use_cache: bool = True, write_cache_on_miss: bool = True) -> int

Load a JSON/TXT MWE list into the trie. Returns entry count.

Source code in src/burmesenlp/mwe/engine.py
def load(
    self,
    path: str,
    *,
    category: Optional[str] = None,
    priority: int = 0,
    allow_unigrams: bool = False,
    use_cache: bool = True,
    write_cache_on_miss: bool = True,
) -> int:
    """Load a JSON/TXT MWE list into the trie. Returns entry count."""
    return load_into_trie(
        self._trie,
        path,
        self._lexicon,
        category=category,
        priority=priority,
        allow_unigrams=allow_unigrams,
        use_cache=use_cache,
        write_cache_on_miss=write_cache_on_miss,
    )

process

process(tokens: Sequence[str]) -> List[str]

Return merged token strings for downstream POS/chunking.

Source code in src/burmesenlp/mwe/engine.py
def process(self, tokens: Sequence[str]) -> List[str]:
    """Return merged token strings for downstream POS/chunking."""
    merged, _ = self.process_detailed(tokens)
    return merged

process_detailed

process_detailed(tokens: Sequence[str]) -> Tuple[List[str], List[MWEToken]]

Greedy left-to-right MWE merge; return strings + span metadata.

Source code in src/burmesenlp/mwe/engine.py
def process_detailed(
    self,
    tokens: Sequence[str],
) -> Tuple[List[str], List[MWEToken]]:
    """Greedy left-to-right MWE merge; return strings + span metadata."""
    tokens = list(tokens)
    if not tokens:
        return [], []

    out: List[str] = []
    spans: List[MWEToken] = []
    i = 0
    n = len(tokens)
    while i < n:
        candidates = self._trie.search(tokens, i)
        if not candidates:
            out.append(tokens[i])
            i += 1
            continue
        best = choose(candidates)
        if self._validator.validate(best, tokens, i):
            end = i + len(best.tokens) - 1
            merged_text = "".join(best.tokens)
            merged_index = len(out)
            out.append(merged_text)
            spans.append(
                MWEToken(
                    text=merged_text,
                    tokens=best.tokens,
                    category=best.category,
                    start=i,
                    end=end,
                    priority=best.priority,
                    pos=_resolve_entry_pos(best),
                    index=merged_index,
                )
            )
            i = end + 1
        else:
            out.append(tokens[i])
            i += 1
    return out, spans

burmesenlp.mwe.MWEEntry dataclass

MWEEntry(text: str, tokens: Tuple[str, ...], category: str, priority: int = 0, pos: Optional[str] = None)

A multi-word expression loaded into the trie.

burmesenlp.mwe.MWEToken dataclass

MWEToken(text: str, tokens: Tuple[str, ...], category: str, start: int, end: int, priority: int = 0, pos: Optional[str] = None, index: Optional[int] = None)

A merged MWE span over a pre-MWE word token sequence.