Skip to content

Commit

Permalink
add option to skip correct pairs in visualization (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikvaessen authored Jun 16, 2023
1 parent 372e0ea commit 44254c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion jiwer/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@


def visualize_alignment(
output: Union[WordOutput, CharacterOutput], show_measures: bool = True
output: Union[WordOutput, CharacterOutput],
show_measures: bool = True,
skip_correct: bool = True,
) -> str:
"""
Visualize the output of [jiwer.process_words][process.process_words] and
Expand All @@ -43,6 +45,7 @@ def visualize_alignment(
output: The processed output of reference and hypothesis pair(s).
show_measures: If enabled, the visualization will include measures like the WER
or CER
skip_correct: If enabled, the visualization will exclude correct reference and hypothesis pairs
Returns:
(str): The visualization as a string
Expand Down Expand Up @@ -101,6 +104,9 @@ def visualize_alignment(

final_str = ""
for idx, (gt, hp, chunks) in enumerate(zip(references, hypothesis, alignment)):
if skip_correct and len(chunks) == 1 and chunks[0].type == "equal":
continue

final_str += f"sentence {idx+1}\n"
final_str += _construct_comparison_string(
gt, hp, chunks, include_space_seperator=not is_cer
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jiwer"
version = "3.0.1"
version = "3.0.2"
description = "Evaluate your speech-to-text system with similarity measures such as word error rate (WER)"
authors = ["Nik Vaessen <[email protected]>"]
readme = "README.md"
Expand Down
20 changes: 20 additions & 0 deletions tests/test_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ def test_multiple_sentences(self):
)
self.assertEqual(alignment, correct_alignment)

def test_skip_correct(self):
correct_alignment = (
"sentence 2\n"
"REF: one\n"
"HYP: 1\n"
" S\n"
"\n"
"sentence 3\n"
"REF: two\n"
"HYP: 2\n"
" S\n"
)
alignment = jiwer.visualize_alignment(
jiwer.process_words(
["perfect", "one", "two", "three"], ["perfect", "1", "2", "three"]
),
show_measures=False,
)
self.assertEqual(alignment, correct_alignment)


class TestAlignmentVisualizationCharacters(unittest.TestCase):
def test_insertion(self):
Expand Down

0 comments on commit 44254c4

Please sign in to comment.