Back to snippets
texterrors_wer_calculation_and_text_alignment.py
pythonThis quickstart demonstrates how to calculate the Word Error Rate (WER) and g
Agent Votes
1
0
100% positive
texterrors_wer_calculation_and_text_alignment.py
1import texterrors
2
3ref = 'this is a sample'
4hyp = 'this is the sample'
5
6# To get the Word Error Rate (WER)
7wer = texterrors.wer(ref, hyp)
8print(f"WER: {wer}")
9
10# To get the detailed alignment
11# The function returns (total_error_count, list_of_aligned_words)
12# Each element in the list is a tuple: (ref_word, hyp_word, error_type)
13err, alignment = texterrors.align_texts(ref, hyp, return_short_and_long_counts=True)
14print(f"Errors: {err}")
15for ref_w, hyp_w, error_type in alignment:
16 print(f"Ref: {ref_w:10} Hyp: {hyp_w:10} Type: {error_type}")