textmetrics
textmetrics compares text, builds diffs, and calculates English readability scores in Gleam.
Use it to rank spelling suggestions, measure edits between strings, compare line sequences, or calculate reading-level estimates. String comparisons normalize Unicode to NFC and operate on grapheme clusters. The package runs on Erlang and JavaScript; readability scores use an English syllable heuristic.
Installation
gleam add textmetrics@0
Requires Gleam 1.14 or later.
Usage
import gleam/int
import gleam/io
import textmetrics
pub fn main() {
textmetrics.levenshtein("kitten", "sitting")
|> int.to_string
|> io.println
}
Running this with gleam run prints 3: the minimum number of insertions, deletions, and substitutions needed to turn one string into the other. did_you_mean uses this distance to select candidates and keeps their input order when distances tie.
The API reference covers distances, similarity scores, longest common subsequences, Myers and patience diffs, word counts, and readability formulas. All functions use the textmetrics import.
Input and result conventions
String comparison normalizes inputs to NFC using the target runtime. Generic list operations compare their elements directly. Candidate ranking returns the original candidate strings. Similarity scores range from 0.0 to 1.0. Different strings can receive the same maximum score.
Unified diff rendering takes an edit script of individual lines without line terminators. It emits LF-terminated lines and does not preserve whether a file originally lacked its final newline.
Readability functions return a Result containing the raw formula value, which may fall outside customary display ranges. They return an error for input with no words; smog also requires 30 sentences. smog_extrapolated permits shorter samples. The syllable heuristic does not detect language, and its reading-level estimates are intended for English.
Contributing
See CONTRIBUTING.md for development and release verification, and the testing guide for the test methods. Report reproducible problems in GitHub Issues.
License
MIT.