Implement minimum edit distance

This commit is contained in:
GHOSCHT 2024-10-26 13:46:58 +02:00
commit 5dddda3e3f
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82
7 changed files with 254 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

11
.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
/target
/result
/result-lib
.direnv
# Added by cargo
#
# already existing elements were commented out
#/target

89
Cargo.lock generated Normal file
View file

@ -0,0 +1,89 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "matrixmultiply"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a"
dependencies = [
"autocfg",
"rawpointer",
]
[[package]]
name = "min-edit-dist"
version = "0.1.0"
dependencies = [
"ndarray",
]
[[package]]
name = "ndarray"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "882ed72dce9365842bf196bdeedf5055305f11fc8c03dee7bb0194a6cad34841"
dependencies = [
"matrixmultiply",
"num-complex",
"num-integer",
"num-traits",
"portable-atomic",
"portable-atomic-util",
"rawpointer",
]
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "portable-atomic"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2"
[[package]]
name = "portable-atomic-util"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90a7d5beecc52a491b54d6dd05c7a45ba1801666a5baad9fdbfc6fef8d2d206c"
dependencies = [
"portable-atomic",
]
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"

7
Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "min-edit-dist"
version = "0.1.0"
edition = "2021"
[dependencies]
ndarray = "0.16.1"

48
flake.lock Normal file
View file

@ -0,0 +1,48 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1729665710,
"narHash": "sha256-AlcmCXJZPIlO5dmFzV3V2XF6x/OpNWUV8Y/FMPGd8Z4=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "2768c7d042a37de65bb1b5b3268fc987e534c49d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1729909612,
"narHash": "sha256-eXqxxbOagphPfjPptSlv0pQONB3fH15CQ4G8uCu1BW4=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "17cadbc36da05e75197d082decb382a5f4208e30",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

58
flake.nix Normal file
View file

@ -0,0 +1,58 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
rust-overlay,
}: let
supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
forEachSupportedSystem = f:
nixpkgs.lib.genAttrs supportedSystems (system:
f {
pkgs = import nixpkgs {
inherit system;
overlays = [rust-overlay.overlays.default self.overlays.default];
};
});
in {
overlays.default = final: prev: {
rustToolchain = let
rust = prev.rust-bin;
in
if builtins.pathExists ./rust-toolchain.toml
then rust.fromRustupToolchainFile ./rust-toolchain.toml
else if builtins.pathExists ./rust-toolchain
then rust.fromRustupToolchainFile ./rust-toolchain
else
rust.stable.latest.default.override {
extensions = ["rust-src" "rustfmt"];
};
};
devShells = forEachSupportedSystem ({pkgs}: {
default = pkgs.mkShell {
packages = with pkgs; [
rustToolchain
openssl
pkg-config
cargo-deny
cargo-edit
cargo-watch
rust-analyzer
];
env = {
# Required by rust-analyzer
RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";
};
};
});
};
}

40
src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use ndarray::Array2;
use std::cmp;
fn main() {
let x: &str = "intention";
let y: &str = "execution";
let mut matrix: Array2<i32> = Array2::zeros((x.len() + 1, y.len() + 1));
for i in 1..=x.len() {
matrix[[i, 0]] = i as i32;
}
for j in 1..=y.len() {
matrix[[0, j]] = j as i32;
}
for i in 1..=x.len() {
for j in 1..=y.len() {
let sub_cost = if x.chars().nth(i - 1) != y.chars().nth(j - 1) {
2
} else {
0
};
let yellow = matrix[[i - 1, j]] + 1;
let blue = matrix[[i, j - 1]] + 1;
let green = matrix[[i - 1, j - 1]] + sub_cost;
matrix[[i, j]] = cmp::min(cmp::min(yellow, blue), green);
}
}
println!("{}", matrix);
println!(
"Minimum edit distance between {} and {}: {}",
x,
y,
matrix[[x.len(), y.len()]]
);
}