Skip to content

Release 4.0.0

Latest

Choose a tag to compare

@rossarmstrong rossarmstrong released this 05 Nov 14:05
v4.0.0
92116d4

Breaking Changes

  • The public API is the seven functions that the werpy package exports: normalize(), wer(), wers(), werp(), werps(), summary() and summaryp(). 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 after import werpy, for example werpy.wer(). An import from a module path such as from werpy.wer import wer raises ModuleNotFoundError.

  • error_handler() and metrics() are internal functions. They are not in werpy.__all__, and they are not attributes of the werpy package.

  • wer(), wers(), werp(), werps(), summary() and summaryp() raise an exception when the input is not valid. They do not print a message, and they do not return None.

  • The six functions raise werpy.exceptions.WerpyTypeError when the reference or the hypothesis has the wrong type or kind, and werpy.exceptions.WerpyValueError when a reference is blank or when the reference and hypothesis sequences have different numbers of elements. WerpyTypeError derives from WerpyError, TypeError and AttributeError. WerpyValueError derives from WerpyError, ValueError and ZeroDivisionError.

  • wers() and werps() 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() and normalize() raise werpy.exceptions.WerpyTypeError for an element of any other type, for example None, a number, bytes or 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() and werp() raise werpy.exceptions.WerpyValueError when the reference and hypothesis are empty lists, tuples or arrays. wers(), werps(), summary() and summaryp() return an empty result for this input.

  • The weights of werp(), werps() and summaryp() must be real numbers that are finite and at least 0. A weight that is not a real number, for example a string, None, a bool, a list or a numpy array, raises WerpyTypeError. A negative, NaN or infinite weight raises WerpyValueError. A list or array of weights is not applied row by row.

  • normalize() raises WerpyTypeError instead of TypeError for an input that is not valid. It accepts a string, or a list, tuple or one-dimensional numpy array of strings. A pandas Series, a set, a generator or a dict view raises WerpyTypeError. Convert such an input to a list first, for example normalize(df["text"].tolist()) for a column of a pandas DataFrame and normalize(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() and summaryp() 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 that summary() 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() and summaryp() is the lowest weighted cost of these alignments divided by the number of reference words. The insertion, deletion and substitution counts and word lists of summaryp() can then differ from those of summary().

  • 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 werpy installs werpy, and pip install "werpy[pandas]" also installs pandas. summary() and summaryp() return a pandas DataFrame by default, as before. When pandas cannot be imported, they raise ImportError for the default output, and its message names the pandas extra and the other values of output. Code that uses pandas because werpy installed it must install pandas itself.

  • pip install werpy installs 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 werp column of summaryp() holds, for each pair, the result of werp() for that pair. summaryp() uses the float value of each weight, as werp() and werps() 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() and summaryp() compute the rate exactly, return it rounded to the nearest float, and return inf only when the rate itself is too large for a float, with no warning. For such weights, earlier versions could return inf where the rate fits in a float, werp() and werps() issued the overflow RuntimeWarning of NumPy, and summaryp() raised OverflowError for some int weights.

Bug Fixes

  • metrics() and the metrics_fast() and metrics_wer_only() functions of the compiled werpy._metrics module now raise a ValueError when the reference and hypothesis sequences have different numbers of elements. The message is the one that error_handler() raises for the same input.

  • The linetrace Meson build option compiles _metrics.pyx, _weighted.pyx and _normalize_kernel.pyx with Cython line tracing when the build also sets -Dpython.allow_limited_api=false, for example uv 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 an abi3 wheel. When the limited API is allowed, meson setup stops 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=true did not compile. The default build does not change.

  • normalize() keeps a lone surrogate in a text instead of raising UnicodeEncodeError.

  • typing.get_type_hints() resolves the annotations of normalize(), wer(), wers(), werp(), werps(), summary() and summaryp() in every environment, with or without NumPy, pandas and polars. At run time, the hint for a numpy array is typing.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() raised NameError for normalize().

Enhancements

  • The pandas extra requires pandas>=2.2.2, and the polars extra requires polars>=1.0.0.

  • The sphinx and sphinx-nefertiti requirements of the docs extra now carry the environment marker python_version >= "3.12". On Python 3.11 the docs extra adds no packages.

  • pywer, evaluate, universal-edit-distance and torchmetrics were removed from the benchmarks extra.

  • Building werpy no longer requires NumPy. _metrics.pyx no longer uses the NumPy C API, and numpy was removed from the build requirements in pyproject.toml and from meson.build.

  • The dynamic programming loops of calculations(), calculations_fast(), calculations_wer_only() and _calculations_wer_only_reuse_ptr() in _metrics.pyx now keep the neighboring cell values in local variables. The results are unchanged.

  • Added the public werpy.exceptions module. It defines WerpyError, the base class of the exceptions that werpy raises when an input is not valid, and its subclasses WerpyTypeError and WerpyValueError. Import the classes from the module, for example from werpy.exceptions import WerpyError. They are not in werpy.__all__.

  • wer(), wers(), werp(), werps(), summary() and summaryp() accept tuples of strings in the same way as lists.

  • The docstrings of summary(), summaryp(), werp() and werps(), the weighted Word Error Rate page and the summarization page state which alignment the counts come from.

  • The batch path of metrics() in _metrics.pyx no 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() and werps() 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 of normalize(), 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 that normalize() handles word contractions. The docstrings of wer(), wers(), werp(), werps(), summary() and summaryp() 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 through normalize() first.

  • summary() and summaryp() take the keyword-only argument output, 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. An output that is not a string raises WerpyTypeError, and any other string raises WerpyValueError.

  • The polars DataFrame of output="polars" has typed columns. wer and werp are Float64, the counts are Int64, inserted_words and deleted_words are lists of String, and each element of substituted_words is a Struct with the fields reference and hypothesis. Empty lists, tuples or arrays give a DataFrame with no rows and these types.

  • The polars extra installs polars, pip install "werpy[polars]". When polars cannot be imported, output="polars" raises ImportError, and its message names the polars extra and output="records".

  • The Tests workflow installs the wheel with its pandas and polars extras, 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.pyx and _weighted.pyx return 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-abi3 wheel 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 the Programming Language :: Python :: 3.15 classifier was added to pyproject.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, one cp311-abi3 wheel 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 test extra installs the packages that the test suite uses: pytest>=9.0.3, numpy>=1.26.4, pandas>=2.2.2 and polars>=1.0.0. With these packages, no test of the suite is skipped. The extra no longer installs pytest-cov.

  • Added the Typing :: Typed classifier to pyproject.toml. The package includes the py.typed marker and stub files for its compiled modules.

  • Added a Changelog link to the project URLs in pyproject.toml. It points to CHANGELOG.md in the repository.