Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aalbacetef/strcompare
strcompare is a library for comparing strings using Hamming, Levenshtein, and Damerau-Levenshtein metrics.
https://github.com/aalbacetef/strcompare
damerau-levenshtein hamming-distance levenshtein levenshtein-distance string-comparison zig
Last synced: 26 days ago
JSON representation
strcompare is a library for comparing strings using Hamming, Levenshtein, and Damerau-Levenshtein metrics.
- Host: GitHub
- URL: https://github.com/aalbacetef/strcompare
- Owner: aalbacetef
- License: bsd-3-clause
- Created: 2024-05-10T15:17:18.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-09-27T22:19:12.000Z (4 months ago)
- Last Synced: 2024-11-07T17:13:15.360Z (3 months ago)
- Topics: damerau-levenshtein, hamming-distance, levenshtein, levenshtein-distance, string-comparison, zig
- Language: Zig
- Homepage:
- Size: 31.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - strcompare 🗒️ string comparison library implementing the Hamming, Levenshtein, Damerau-Levenshtein metrics
README
![CI status](https://github.com/aalbacetef/strcompare/actions/workflows/ci.yml/badge.svg) [![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
# strcompare
`strcompare` is a zig library implementing 3 of the most common string comparison algorithms:
- Hamming distance
- Levenshtein distance
- Damerau-Levenshtein distance (specifically, the optimal string alignment distance)## Usage
The library provides two convenience functions: `similarity` and `distance`.
Both functions accept a `Metrics` argument, one of:
```zig
pub const Metrics = enum {
Damerau,
Hamming,
Levenshtein,
};
```#### distance
`distance` will return the edit distance, a `u64`.
It accepts the following arguments:
- alloc: an `std.mem.Allocator`
- metric: one of the `Metrics`
- a, b: strings to compareExample:
```zig
const a = "kitten";
const b = "sitting";const d = distance(alloc, Metrics.Levenshtein, a, b);
std.debug.print("distance: {d}\n", .{d});
//
// distance: 3
//
```#### similarity
`similarity` returns a weighted distance, that is, an `f64` from `0.0` to `1.0`.
It will return `0.0` if the strings differ completely and `1.0` if they are identical.It accepts the following arguments:
- alloc: an `std.mem.Allocator`
- metric: one of the `Metrics`
- a, b: strings to compareExample:
```zig
const a = "kitten";
const b = "sitting";const d = similarity(alloc, Metrics.Levenshtein, a, b);
std.debug.print("similarity: {d:.0}%\n", .{d*100.0});
//
// similarity: 57%
//
```## Installing
After acquiring the repo's code, run:
```bash
zig fetch --save ./path/to/strcompare
```then add the following to your `build.zig`
```zig
const pkg = b.dependency("strcompare", .{});
exe.root_module.addImport("strcompare", pkg.module("strcompare"));```
Do the same for your tests:
```zig
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});exe_unit_tests.root_module.addImport("strcompare", pkg.module("strcompare"));
```
You should now be able to import the module with:
```zig
const strcompare = @import("strcompare");
const Metrics = strcompare.Metrics;
const distance = strcompare.distance;
const similarity = strcompare.similarity;fn dist() !void {
const a = "a";
const b = "bb";
const alloc = std.heap.page_allocator;const d = try distance(alloc, Metrics.Levenshtein, a, b);
std.debug.print("distance: {d}\n", .{d});
}```