Datasets:
File size: 2,542 Bytes
fcdac52 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# Usage
The most simple use-case is computing the word error rate between two strings:
```python
from jiwer import wer
reference = "hello world"
hypothesis = "hello duck"
error = wer(reference, hypothesis)
```
Similarly, to get other measures:
```python
import jiwer
reference = "hello world"
hypothesis = "hello duck"
wer = jiwer.wer(reference, hypothesis)
mer = jiwer.mer(reference, hypothesis)
wil = jiwer.wil(reference, hypothesis)
# faster, because `compute_measures` only needs to perform the heavy lifting once:
output = jiwer.process_words(reference, hypothesis)
wer = output.wer
mer = output.mer
wil = output.wil
```
You can also compute the WER over multiple sentences:
```python
from jiwer import wer
reference = ["hello world", "i like monthy python"]
hypothesis = ["hello duck", "i like python"]
error = wer(reference, hypothesis)
```
We also provide the character error rate:
```python
import jiwer
reference = ["i can spell", "i hope"]
hypothesis = ["i kan cpell", "i hop"]
error = jiwer.cer(reference, hypothesis)
# if you also want the alignment
output = jiwer.process_characters(reference, hypothesis)
error = output.cer
```
# Alignment
With `jiwer.process_words`, you also get the alignment between the reference and hypothesis.
We provide the alignment as a list of `(op, ref_start_idx, ref_idx_end, hyp_idx_start, hyp_idx_end)`, where `op` is one of
`equal`, `replace`, `delete`, or `insert`.
This looks like the following:
```python3
import jiwer
out = jiwer.process_words("short one here", "shoe order one")
print(out.alignments)
# [[[AlignmentChunk(type='insert', ref_start_idx=0, ref_end_idx=0, hyp_start_idx=0, hyp_end_idx=1), ...]]
```
To visualize the alignment, you can use `jiwer.visualize_alignment()`
For example:
```python3
import jiwer
out = jiwer.process_words(
["short one here", "quite a bit of longer sentence"],
["shoe order one", "quite bit of an even longest sentence here"],
)
print(jiwer.visualize_alignment(out))
```
Gives the following output
```text
sentence 1
REF: **** short one here
HYP: shoe order one ****
I S D
sentence 2
REF: quite a bit of ** **** longer sentence ****
HYP: quite * bit of an even longest sentence here
D I I S I
number of sentences: 2
substitutions=2 deletions=2 insertions=4 hits=5
mer=61.54%
wil=74.75%
wip=25.25%
wer=88.89%
```
Note that it also possible to visualize the character-level alignment, simply use the output of `jiwer.process_characters()` instead.
|