mirror of
https://gitee.com/openharmony/third_party_rust_strsim-rs
synced 2024-11-23 07:39:51 +00:00
Add some rustdoc comments
This commit is contained in:
parent
089f3cc083
commit
fc0c17bf23
@ -1,7 +1,7 @@
|
||||
[package]
|
||||
|
||||
name = "strsim"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
authors = ["Danny Guo <dannyguo91@gmail.com>"]
|
||||
description = """
|
||||
Implementations of string similarity metrics.
|
||||
|
@ -10,7 +10,7 @@ Rust implementations of [string similarity metrics]. Best efforts will be made t
|
||||
```toml
|
||||
# Cargo.toml
|
||||
[dependencies]
|
||||
strsim = "0.1.0"
|
||||
strsim = "0.1.1"
|
||||
```
|
||||
|
||||
### Usage
|
||||
@ -43,7 +43,7 @@ fn main() {
|
||||
|
||||
### Version
|
||||
|
||||
0.1.0
|
||||
0.1.1
|
||||
|
||||
### License
|
||||
|
||||
|
22
src/lib.rs
22
src/lib.rs
@ -1,3 +1,7 @@
|
||||
|
||||
//! This library implements string similarity metrics. Currently includes
|
||||
//! Hamming, Levenshtein, Jaro, and Jaro-Winkler.
|
||||
|
||||
#![feature(test, core, collections)]
|
||||
|
||||
extern crate test;
|
||||
@ -5,13 +9,17 @@ extern crate test;
|
||||
use std::cmp::{max, min};
|
||||
use std::collections::Bitv;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum StrSimError {
|
||||
DifferentLengthArgs
|
||||
}
|
||||
|
||||
pub type HammingResult = Result<usize, StrSimError>;
|
||||
|
||||
#[doc = "
|
||||
Calculates the number of positions in the two strings where the characters
|
||||
differ. Returns an error if the strings have different lengths.
|
||||
"]
|
||||
pub fn hamming(a: &str, b: &str) -> HammingResult {
|
||||
if a.len() != b.len() {
|
||||
Err(StrSimError::DifferentLengthArgs)
|
||||
@ -23,6 +31,10 @@ pub fn hamming(a: &str, b: &str) -> HammingResult {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc = "
|
||||
Calculates the Jaro similarity between two strings. The returned value
|
||||
is between 0.0 and 1.0 (where 1.0 means the strings are equal).
|
||||
"]
|
||||
pub fn jaro(a: &str, b: &str) -> f64 {
|
||||
if a == b { return 1.0; }
|
||||
if a.len() == 0 || b.len() == 0 { return 0.0; }
|
||||
@ -70,6 +82,9 @@ pub fn jaro(a: &str, b: &str) -> f64 {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc = "
|
||||
Similar to Jaro but gives a boost to strings that have a common prefix.
|
||||
"]
|
||||
pub fn jaro_winkler(a: &str, b: &str) -> f64 {
|
||||
let jaro_distance = jaro(a, b);
|
||||
|
||||
@ -89,6 +104,11 @@ pub fn jaro_winkler(a: &str, b: &str) -> f64 {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[doc = "
|
||||
Calculates the minimum number of insertions, deletions, and substitutions
|
||||
required to change one string into the other.
|
||||
"]
|
||||
pub fn levenshtein(a: &str, b: &str) -> usize {
|
||||
if a == b { return 0; }
|
||||
else if a.len() == 0 { return b.len(); }
|
||||
|
Loading…
Reference in New Issue
Block a user