✨ dinglehopper: Include number of characters and words in JSON report
parent
6987a8e1e2
commit
745095e52c
@ -1,21 +1,36 @@
|
|||||||
from __future__ import division
|
from __future__ import division
|
||||||
|
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
from uniseg.graphemecluster import grapheme_clusters
|
from uniseg.graphemecluster import grapheme_clusters
|
||||||
|
|
||||||
from qurator.dinglehopper.edit_distance import distance
|
from qurator.dinglehopper.edit_distance import distance
|
||||||
|
|
||||||
|
|
||||||
def character_error_rate(reference, compared):
|
def character_error_rate_n(reference, compared) -> Tuple[float, int]:
|
||||||
d = distance(reference, compared)
|
"""
|
||||||
if d == 0:
|
Compute character error rate.
|
||||||
return 0
|
|
||||||
|
|
||||||
|
:return: character error rate and length of the reference
|
||||||
|
"""
|
||||||
|
d = distance(reference, compared)
|
||||||
n = len(list(grapheme_clusters(unicodedata.normalize('NFC', reference))))
|
n = len(list(grapheme_clusters(unicodedata.normalize('NFC', reference))))
|
||||||
if n == 0:
|
|
||||||
return float('inf')
|
|
||||||
|
|
||||||
return d/n
|
if d == 0:
|
||||||
|
return 0, n
|
||||||
|
if n == 0:
|
||||||
|
return float('inf'), n
|
||||||
|
return d/n, n
|
||||||
|
|
||||||
# XXX Should we really count newlines here?
|
# XXX Should we really count newlines here?
|
||||||
|
|
||||||
|
|
||||||
|
def character_error_rate(reference, compared) -> float:
|
||||||
|
"""
|
||||||
|
Compute character error rate.
|
||||||
|
|
||||||
|
:return: character error rate
|
||||||
|
"""
|
||||||
|
cer, _ = character_error_rate_n(reference, compared)
|
||||||
|
return cer
|
||||||
|
Loading…
Reference in New Issue