From 5dddda3e3fe11a817c7a0813920bba5ce3d5b508 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sat, 26 Oct 2024 13:46:58 +0200 Subject: [PATCH] Implement minimum edit distance --- .envrc | 1 + .gitignore | 11 +++++++ Cargo.lock | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 +++++ flake.lock | 48 +++++++++++++++++++++++++++++ flake.nix | 58 ++++++++++++++++++++++++++++++++++ src/main.rs | 40 ++++++++++++++++++++++++ 7 files changed, 254 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/main.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a34dbe --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/target +/result +/result-lib +.direnv + + +# Added by cargo +# +# already existing elements were commented out + +#/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a0b4784 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..70773d2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "min-edit-dist" +version = "0.1.0" +edition = "2021" + +[dependencies] +ndarray = "0.16.1" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..d246169 --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..53ffdb0 --- /dev/null +++ b/flake.nix @@ -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"; + }; + }; + }); + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..239feb9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,40 @@ +use ndarray::Array2; +use std::cmp; + +fn main() { + let x: &str = "intention"; + let y: &str = "execution"; + + let mut matrix: Array2 = 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()]] + ); +}