<jupyter_start><jupyter_text>from : https://martin-thoma.com/word-error-rate-calculation/<jupyter_code>def wer(r, h):
    """
    Calculation of WER with Levenshtein distance.

    Works only for iterables up to 254 elements (uint8).
    O(nm) time ans space complexity.

    Parameters
    ----------
    r : list
    h : list

    Returns
    -------
    int

    Examples
    --------
    >>> wer("who is there".split(), "is there".split())
    1
    >>> wer("who is there".split(), "".split())
    3
    >>> wer("".split(), "who is there".split())
    3
    """
    # initialisation
    import numpy
    d = numpy.zeros((len(r)+1)*(len(h)+1), dtype=numpy.uint8)
    d = d.reshape((len(r)+1, len(h)+1))
    for i in range(len(r)+1):
        for j in range(len(h)+1):
            if i == 0:
                d[0][j] = j
            elif j == 0:
                d[i][0] = i

    # computation
    for i in range(1, len(r)+1):
        for j in range(1, len(h)+1):
            if r[i-1] == h[j-1]:
                d[i][j] = d[i-1][j-1]
            else:
                substitution = d[i-1][j-1] + 1
                insertion    = d[i][j-1] + 1
                deletion     = d[i-1][j] + 1
                d[i][j] = min(substitution, insertion, deletion)

    return d[len(r)][len(h)]
print wer("who is there".split(), "is there".split())
print wer("who is there".split(), "".split())
print wer("".split(), "who is there".split())
import numpy as np
list_ref = []

with open('../data/processed/IEMOCAP/four_category/FC_trans.txt') as f:
    list_ref = f.readlines()
list_ref = [ x.strip() for x in list_ref ]
list_ref = [ [ t for t in x.split(' ')] for x in list_ref ]
print len(list_ref)
print list_ref[0]
list_target = []


with open('../data/processed/IEMOCAP/four_category_G/FC_trans.txt') as f:
    list_target = f.readlines()
list_target = [ x.strip() for x in list_target ]
list_target = [ [ t for t in x.split(' ')] for x in list_target ]
print len(list_target)
print list_target[0]
list_wer = []

for i in xrange( len(list_ref )) :
    list_wer.append( wer(list_ref[i], list_target[i])  )
print np.mean(list_wer)    
list_wer_lower = []
list_target_lower = []

list_ref_lower = [ [t.lower() for t in x] for x in list_ref ]
list_target_lower = [ [t.lower() for t in x] for x in list_target ]

list_wer_lower = []

for i in xrange( len(list_ref_lower )) :
    list_wer_lower.append( wer(list_ref_lower[i], list_target_lower[i])  )
print np.mean(list_wer_lower)    
# just check
list_wer_check = []

for i in xrange( len(list_ref )) :
    list_wer_check.append( wer(list_ref[i], list_ref[i])  )
print np.mean(list_wer_check)<jupyter_output>0.0
{'fraction_non_alphanumeric': 0.17069486404833836, 'fraction_numerical': 0.013972809667673716, 'mean_word_length': 2.5557046979865774, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '478988', 'license': 'MIT', 'n_tokens_mistral': 1010, 'n_tokens_neox': 979, 'n_words': 238, 'path': 'analysis/WER_calculator.ipynb', 'repo_name': 'ggzhang0071/multimodal-speech-emotion', 'size': '2648'}