NPL:RUST: MIN-EDIT-DISTANCE - ALGORITHM

 The Algorithm:




Rust implementation:


use
std::cmp::min; fn min_edit_distance(source: &str, target: &str) -> usize { let n = source.chars().count(); let m = target.chars().count(); let mut d = vec![vec![0; m + 1]; n + 1]; for i in 0..=n { d[i][0] = i; } for j in 0..=m { d[0][j] = j; } for i in 1..=n { for j in 1..=m { let cost = if source.chars().nth(i - 1) == target.chars().nth(j - 1) { 0 } else { 1 }; d[i][j] = min( d[i - 1][j] + 1, min(d[i][j - 1] + 1, d[i - 1][j - 1] + cost), ); } } d[n][m] } fn main() { let args: Vec<String> = std::env::args().collect(); if args.len() != 3 { println!("Usage: {} <source> <target>", args[0]); return; } let source = &args[1]; let target = &args[2]; let distance = min_edit_distance(source, target); println!("The minimum edit distance between '{}' and '{}' is {}.", source, target, distance); }

How to compile:


  1. Create a new project in rust:  cargo new mind_dist
  2. edit src/main.rs and paste the code
  3. build the program  cargo build --release
  4. use the program target/mind_dist <source> <target>

Comentarios

Entradas populares de este blog

[MACHINE LEARNING] Un breve ejemplo de uso de JupyterLab

[RUST][BOTS][TELEGRAM] Como crear y explotar un bot de Telefram en un canal de Telegram

[Idiomas][Italiano] Rutina Semanal de Estudio de Italiano (3 horas/semana)