Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index to find which alignment operators are in which lines #24

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion python/stringalign/evaluation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections import Counter, deque
from collections import Counter, defaultdict, deque
from collections.abc import Generator, Mapping
from dataclasses import dataclass
from itertools import chain
Expand Down Expand Up @@ -210,6 +210,24 @@ def alignment_operators(self) -> Counter[AlignmentOperation]:
def confusion_matrix(self) -> StringConfusionMatrix:
return sum((le.confusion_matrix for le in self.line_errors), start=StringConfusionMatrix.get_empty())

@property
def line_error_raw_lookup(self) -> dict[AlignmentOperation, frozenset[LineError]]:
out = defaultdict(set)
for line_error in self.line_errors:
for alignment_op in line_error.raw_alignment:
out[alignment_op].add(line_error)

return {k: frozenset(v) for k, v in out.items()}

@property
def line_error_aggregated_lookup(self) -> dict[AlignmentOperation, frozenset[LineError]]:
out = defaultdict(set)
for line_error in self.line_errors:
for alignment_op in line_error.alignment:
out[alignment_op].add(line_error)

return {k: frozenset(v) for k, v in out.items()}

@classmethod
def from_strings(
cls,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from stringalign.align import Replace
from stringalign.evaluation import TranscriptionEvaluator


def test_raw_lookup():
evaluator = TranscriptionEvaluator.from_strings(
references=["abc", "def", "aaa"],
predictions=["bbc", "deg", "abb"],
)
assert evaluator.line_error_aggregated_lookup[Replace("b", "a")] == frozenset({evaluator.line_errors[0]})
assert evaluator.line_error_aggregated_lookup[Replace("g", "f")] == frozenset({evaluator.line_errors[1]})
assert evaluator.line_error_aggregated_lookup[Replace("bb", "aa")] == frozenset({evaluator.line_errors[2]})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from stringalign.align import Replace
from stringalign.evaluation import TranscriptionEvaluator


def test_raw_lookup():
evaluator = TranscriptionEvaluator.from_strings(
references=["abc", "def", "aaa"],
predictions=["bbc", "deg", "abb"],
)
assert evaluator.line_error_raw_lookup[Replace("b", "a")] == frozenset(
{evaluator.line_errors[0], evaluator.line_errors[2]}
)
assert evaluator.line_error_raw_lookup[Replace("g", "f")] == frozenset(
{
evaluator.line_errors[1],
}
)
Loading