Repository navigation
Breaking Changes
-
The public API is the seven functions that the
werpypackage exports:normalize(),wer(),wers(),werp(),werps(),summary()andsummaryp().werpy.__all__lists exactly these seven names. -
The modules that implement these functions are private, and their names start with an underscore. Import each function from the package, for example
from werpy import wer, or call it through the package afterimport werpy, for examplewerpy.wer(). An import from a module path such asfrom werpy.wer import werraisesModuleNotFoundError. -
error_handler()andmetrics()are internal functions. They are not inwerpy.__all__, and they are not attributes of thewerpypackage. -
wer(),wers(),werp(),werps(),summary()andsummaryp()raise an exception when the input is not valid. They do not print a message, and they do not returnNone. -
The six functions raise
werpy.exceptions.WerpyTypeErrorwhen the reference or the hypothesis has the wrong type or kind, andwerpy.exceptions.WerpyValueErrorwhen a reference is blank or when the reference and hypothesis sequences have different numbers of elements.WerpyTypeErrorderives fromWerpyError,TypeErrorandAttributeError.WerpyValueErrorderives fromWerpyError,ValueErrorandZeroDivisionError. -
wers()andwerps()return a list for a pair of strings, with one Word Error Rate in it. For example,werpy.wers('i love cold pizza', 'i love pizza')returns[0.25]. -
Every element of a reference or hypothesis list, tuple or numpy array must be a string.
wer(),wers(),werp(),werps(),summary(),summaryp()andnormalize()raisewerpy.exceptions.WerpyTypeErrorfor an element of any other type, for exampleNone, a number,bytesor a nested list. The message names the argument, the type of the element and its index. -
A numpy array must have one dimension. A numpy array with no dimensions, or with two or more dimensions, raises
WerpyTypeError. -
wer()andwerp()raisewerpy.exceptions.WerpyValueErrorwhen the reference and hypothesis are empty lists, tuples or arrays.wers(),werps(),summary()andsummaryp()return an empty result for this input. -
The weights of
werp(),werps()andsummaryp()must be real numbers that are finite and at least 0. A weight that is not a real number, for example a string,None, abool, a list or a numpy array, raisesWerpyTypeError. A negative, NaN or infinite weight raisesWerpyValueError. A list or array of weights is not applied row by row. -
normalize()raisesWerpyTypeErrorinstead ofTypeErrorfor an input that is not valid. It accepts a string, or a list, tuple or one-dimensional numpy array of strings. A pandasSeries, a set, a generator or adictview raisesWerpyTypeError. Convert such an input to a list first, for examplenormalize(df["text"].tolist())for a column of a pandasDataFrameandnormalize(list(d.values()))for the values of a dictionary. For the lines of a file, pass a list of the lines:with open("transcripts.txt", encoding="utf-8") as f: texts = werpy.normalize([line.rstrip("\n") for line in f])
-
The message for a reference, hypothesis or text of the wrong type names the argument and its type. The message for a string paired with a list, tuple or array names tuples.
-
werp(),werps()andsummaryp()count the errors on the alignment with the lowest weighted cost among the alignments with the fewest edits. When a substitution weighs more than the mean of an insertion and a deletion, this is the alignment with the fewest substitutions. When it weighs less, it is the alignment with the most substitutions. For equal weights, and when a substitution weighs exactly the mean, it is the alignment thatsummary()uses. -
For a reference and a hypothesis that have more than one alignment with the fewest edits, the weighted Word Error Rate of
werp(),werps()andsummaryp()is the lowest weighted cost of these alignments divided by the number of reference words. The insertion, deletion and substitution counts and word lists ofsummaryp()can then differ from those ofsummary(). -
normalize()applies a new rule by default. It puts each text into Unicode Normalization Form C (NFC), removes every character of the Unicode general category for punctuation as well as the ASCII punctuation characters, converts the text to lowercase and puts it into NFC again. Its result changes for texts that hold punctuation outside ASCII, and can change for texts that are not in NFC and for texts whose earlier result was not in NFC, and so can the Word Error Rate of texts normalized with it. The result of every other text, every ASCII text among them, does not change. -
pandas is an optional dependency of werpy.
pip install werpyinstalls werpy, andpip install "werpy[pandas]"also installs pandas.summary()andsummaryp()return a pandas DataFrame by default, as before. When pandas cannot be imported, they raiseImportErrorfor the default output, and its message names thepandasextra and the other values ofoutput. Code that uses pandas because werpy installed it must install pandas itself. -
pip install werpyinstalls werpy alone. werpy accepts one-dimensional numpy arrays of strings as before, from the NumPy that your project installs. Code that uses NumPy because werpy installed it must install NumPy itself. -
werp()on a list, tuple or numpy array of texts applies the weights to the insertions, deletions and substitutions of all the pairs together, and divides the result by the number of words in all the references. For some weights, such as 0.1, the result can differ in the last digit from the result of earlier versions. -
The
werpcolumn ofsummaryp()holds, for each pair, the result ofwerp()for that pair.summaryp()uses the float value of each weight, aswerp()andwerps()do. For very large int weights, the column can differ in the last digit from earlier versions. -
When the weighted errors are too large for a float,
werp(),werps()andsummaryp()compute the rate exactly, return it rounded to the nearest float, and returninfonly when the rate itself is too large for a float, with no warning. For such weights, earlier versions could returninfwhere the rate fits in a float,werp()andwerps()issued the overflowRuntimeWarningof NumPy, andsummaryp()raisedOverflowErrorfor some int weights.
Bug Fixes
-
metrics()and themetrics_fast()andmetrics_wer_only()functions of the compiledwerpy._metricsmodule now raise aValueErrorwhen the reference and hypothesis sequences have different numbers of elements. The message is the one thaterror_handler()raises for the same input. -
The
linetraceMeson build option compiles_metrics.pyx,_weighted.pyxand_normalize_kernel.pyxwith Cython line tracing when the build also sets-Dpython.allow_limited_api=false, for exampleuv build --wheel -Csetup-args=-Dlinetrace=true -Csetup-args=-Dpython.allow_limited_api=false. This wheel is built for the ABI of the build interpreter and is not anabi3wheel. When the limited API is allowed,meson setupstops with an error message that names-Dpython.allow_limited_api=false, because Cython does not support line tracing in the Python limited API. Before this change, a build with-Dlinetrace=truedid not compile. The default build does not change. -
normalize()keeps a lone surrogate in a text instead of raisingUnicodeEncodeError. -
typing.get_type_hints()resolves the annotations ofnormalize(),wer(),wers(),werp(),werps(),summary()andsummaryp()in every environment, with or without NumPy, pandas and polars. At run time, the hint for a numpy array istyping.Any, and so is the hint for the DataFrame of a library that werpy has not imported. Type checkers read the precise types. Before this change,typing.get_type_hints()raisedNameErrorfornormalize().
Enhancements
-
The
pandasextra requirespandas>=2.2.2, and thepolarsextra requirespolars>=1.0.0. -
The
sphinxandsphinx-nefertitirequirements of thedocsextra now carry the environment markerpython_version >= "3.12". On Python 3.11 thedocsextra adds no packages. -
pywer,evaluate,universal-edit-distanceandtorchmetricswere removed from thebenchmarksextra. -
Building werpy no longer requires NumPy.
_metrics.pyxno longer uses the NumPy C API, andnumpywas removed from the build requirements inpyproject.tomland frommeson.build. -
The dynamic programming loops of
calculations(),calculations_fast(),calculations_wer_only()and_calculations_wer_only_reuse_ptr()in_metrics.pyxnow keep the neighboring cell values in local variables. The results are unchanged. -
Added the public
werpy.exceptionsmodule. It definesWerpyError, the base class of the exceptions that werpy raises when an input is not valid, and its subclassesWerpyTypeErrorandWerpyValueError. Import the classes from the module, for examplefrom werpy.exceptions import WerpyError. They are not inwerpy.__all__. -
wer(),wers(),werp(),werps(),summary()andsummaryp()accept tuples of strings in the same way as lists. -
The docstrings of
summary(),summaryp(),werp()andwerps(), the weighted Word Error Rate page and the summarization page state which alignment the counts come from. -
The batch path of
metrics()in_metrics.pyxno longer checks whether a row is a zero-dimensional array.calculations()always returns nine values, so the check was never true. The results are unchanged. -
summary(),summaryp(),werp()andwerps()align a reference and a hypothesis in memory that grows with the number of words in the two texts, so long transcripts can be compared. This memory change does not change any result. -
The docstring of
normalize()and the normalization page state the rule ofnormalize(), with a table of what happens to each kind of character, and say that the rule uses the Unicode database of the running Python. The normalization page no longer says thatnormalize()handles word contractions. The docstrings ofwer(),wers(),werp(),werps(),summary()andsummaryp()and the pages of these functions say that werpy compares words exactly as they are written, and that texts from different sources can be passed throughnormalize()first. -
summary()andsummaryp()take the keyword-only argumentoutput, which selects the form of the results.output="pandas", the default, returns the pandas DataFrame.output="polars"returns a polars DataFrame with the same columns in the same order.output="records"returns a list with one dictionary for each pair of texts, whose keys are the columns in the same order, and works with a plain install of werpy. Anoutputthat is not a string raisesWerpyTypeError, and any other string raisesWerpyValueError. -
The polars DataFrame of
output="polars"has typed columns.werandwerpareFloat64, the counts areInt64,inserted_wordsanddeleted_wordsare lists ofString, and each element ofsubstituted_wordsis aStructwith the fieldsreferenceandhypothesis. Empty lists, tuples or arrays give a DataFrame with no rows and these types. -
The
polarsextra installs polars,pip install "werpy[polars]". When polars cannot be imported,output="polars"raisesImportError, and its message names thepolarsextra andoutput="records". -
The Tests workflow installs the wheel with its
pandasandpolarsextras, and one cell runs the test suite without NumPy, pandas and polars. The coverage job installs pandas and polars, and the type check installs polars. -
The compiled functions of
_metrics.pyxand_weighted.pyxreturn a tuple for a pair of texts and a list of tuples for a list, tuple or numpy array of texts, instead of a numpy array. The values are unchanged. -
summaryp()builds its pandas DataFrame from the rows of its ten columns. The DataFrame does not change. -
Added support for Python 3.15. The
cp311-abi3wheel of each platform is installed and tested on CPython 3.15, the Tests workflow runs the test suite on CPython 3.15 on Linux, macOS and Windows, and theProgramming Language :: Python :: 3.15classifier was added topyproject.toml. -
Wheels are also built for Linux on 64-bit Arm processors with glibc (
manylinux_aarch64) and with musl (musllinux_aarch64), and for Windows on Arm (win_arm64). These wheels are built and tested on Arm runners. As for the other platforms, onecp311-abi3wheel is built for each platform, and it is tested on CPython 3.11 to 3.15. -
The Windows wheels are built with the MSVC compilers.
-
The
testextra installs the packages that the test suite uses:pytest>=9.0.3,numpy>=1.26.4,pandas>=2.2.2andpolars>=1.0.0. With these packages, no test of the suite is skipped. The extra no longer installspytest-cov. -
Added the
Typing :: Typedclassifier topyproject.toml. The package includes thepy.typedmarker and stub files for its compiled modules. -
Added a
Changeloglink to the project URLs inpyproject.toml. It points toCHANGELOG.mdin the repository.