2020-07-06 15:39:31 +00:00
|
|
|
from typing import List
|
|
|
|
import numpy
|
|
|
|
from thinc.types import Ragged
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2020-11-03 15:24:38 +00:00
|
|
|
from .align import get_alignments
|
2020-08-04 14:29:18 +00:00
|
|
|
|
2020-07-06 15:39:31 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Alignment:
|
|
|
|
x2y: Ragged
|
|
|
|
y2x: Ragged
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_indices(cls, x2y: List[List[int]], y2x: List[List[int]]) -> "Alignment":
|
|
|
|
x2y = _make_ragged(x2y)
|
|
|
|
y2x = _make_ragged(y2x)
|
|
|
|
return Alignment(x2y=x2y, y2x=y2x)
|
2020-07-25 13:01:15 +00:00
|
|
|
|
2020-07-06 15:39:31 +00:00
|
|
|
@classmethod
|
|
|
|
def from_strings(cls, A: List[str], B: List[str]) -> "Alignment":
|
2020-11-03 15:24:38 +00:00
|
|
|
x2y, y2x = get_alignments(A, B)
|
2020-07-06 15:39:31 +00:00
|
|
|
return Alignment.from_indices(x2y=x2y, y2x=y2x)
|
|
|
|
|
|
|
|
|
|
|
|
def _make_ragged(indices):
|
|
|
|
lengths = numpy.array([len(x) for x in indices], dtype="i")
|
|
|
|
flat = []
|
|
|
|
for x in indices:
|
|
|
|
flat.extend(x)
|
|
|
|
return Ragged(numpy.array(flat, dtype="i"), lengths)
|