Skip to content

Commit 620b516

Browse files
committed
move out of metrics.
1 parent 1e97f92 commit 620b516

File tree

6 files changed

+42
-57
lines changed

6 files changed

+42
-57
lines changed

docs/source/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ The :mod:`torchaudio` package consists of I/O, popular datasets and common audio
1212
functional
1313
transforms
1414
datasets
15-
metrics
1615
models
1716
sox_effects
1817
compliance.kaldi

docs/source/metrics.rst

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/torchaudio_unittest/metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from parameterized import parameterized
2-
from torchaudio.metrics import levenshtein_distance
2+
from torchaudio.functional import levenshtein_distance
33
from torchaudio_unittest import common_utils
44

55

torchaudio/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
compliance,
55
datasets,
66
kaldi_io,
7-
metrics,
87
utils,
98
sox_effects,
109
transforms

torchaudio/functional.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import math
44
from typing import Optional, Tuple
5+
from collections.abc import Sequence
56
import warnings
67

78
import torch
@@ -41,6 +42,7 @@
4142
'mask_along_axis_iid',
4243
'sliding_window_cmn',
4344
'vad',
45+
'levenshtein_distance',
4446
]
4547

4648

@@ -2330,3 +2332,42 @@ def vad(
23302332
res = waveform[:, pos - samplesLen_ns + flushedLen_ns:]
23312333
# unpack batch
23322334
return res.view(shape[:-1] + res.shape[-1:])
2335+
2336+
2337+
def levenshtein_distance(r: Sequence, h: Sequence) -> int:
2338+
"""
2339+
Calculate the Levenshtein distance between two sequences.
2340+
2341+
The function computes an edit distance allowing deletion, insertion and substitution.
2342+
The result is an integer. Users may want to normalize by the length of the reference.
2343+
2344+
This can be used to compute the edit distance for instance between two strings,
2345+
or two list of words. Note that, if a string and a list of words is provided, the distance
2346+
will be computed between the "string" sequence, and the "list of words" sequence.
2347+
2348+
Args:
2349+
r (Sequence): the reference sequence to compare.
2350+
h (Sequence): the hypothesis sequence (e.g. the predicted sequence) to compare.
2351+
Returns:
2352+
int: The distance between the reference and the hypothesis.
2353+
"""
2354+
2355+
# Initialisation
2356+
dold = list(range(len(h) + 1))
2357+
dnew = list(0 for _ in range(len(h) + 1))
2358+
2359+
# Computation
2360+
for i in range(1, len(r) + 1):
2361+
dnew[0] = i
2362+
for j in range(1, len(h) + 1):
2363+
if r[i - 1] == h[j - 1]:
2364+
dnew[j] = dold[j - 1]
2365+
else:
2366+
substitution = dold[j - 1] + 1
2367+
insertion = dnew[j - 1] + 1
2368+
deletion = dold[j] + 1
2369+
dnew[j] = min(substitution, insertion, deletion)
2370+
2371+
dnew, dold = dold, dnew
2372+
2373+
return dold[-1]

torchaudio/metrics.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)