|
| 1 | +from typing import Any, Sequence |
| 2 | + |
| 3 | +from .text_row import TextRowFlags |
| 4 | + |
| 5 | + |
| 6 | +class NParallelTextRow: |
| 7 | + def __init__(self, text_id: str, n_refs: Sequence[Sequence[Any]]): |
| 8 | + if len([n_ref for n_ref in n_refs if n_ref is not None and len(n_ref) > 0]) == 0: |
| 9 | + raise ValueError(f"Refs must be provided but n_refs={n_refs}") |
| 10 | + self._text_id = text_id |
| 11 | + self._n_refs = n_refs |
| 12 | + self._n = len(n_refs) |
| 13 | + self.n_segments: Sequence[Sequence[str]] = [[] for _ in range(0, self._n)] |
| 14 | + self.n_flags: Sequence[TextRowFlags] = [TextRowFlags.SENTENCE_START for _ in range(0, self._n)] |
| 15 | + |
| 16 | + @property |
| 17 | + def text_id(self) -> str: |
| 18 | + return self._text_id |
| 19 | + |
| 20 | + @property |
| 21 | + def ref(self) -> Any: |
| 22 | + return self._n_refs[0][0] |
| 23 | + |
| 24 | + @property |
| 25 | + def n_refs(self) -> Sequence[Sequence[Any]]: |
| 26 | + return self._n_refs |
| 27 | + |
| 28 | + def is_sentence_start(self, i: int) -> bool: |
| 29 | + return TextRowFlags.SENTENCE_START in self.n_flags[i] |
| 30 | + |
| 31 | + def is_in_range(self, i: int) -> bool: |
| 32 | + return TextRowFlags.IN_RANGE in self.n_flags[i] |
| 33 | + |
| 34 | + def is_range_start(self, i: int) -> bool: |
| 35 | + return TextRowFlags.RANGE_START in self.n_flags[i] |
| 36 | + |
| 37 | + @property |
| 38 | + def is_empty(self): |
| 39 | + return sum([1 for s in self.n_segments if len(s) == 0]) == 0 |
| 40 | + |
| 41 | + def text(self, i: int) -> str: |
| 42 | + return " ".join(self.n_segments[i]) |
| 43 | + |
| 44 | + def invert(self) -> "NParallelTextRow": |
| 45 | + inverted_row = NParallelTextRow(self._text_id, list(reversed(self._n_refs))) |
| 46 | + inverted_row.n_flags = list(reversed(self.n_flags)) |
| 47 | + return inverted_row |
0 commit comments