Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add github actions workflows + pre-commit hooks #12

Merged
merged 5 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Cargo Check
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
check:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.43.0
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions-rs/cargo@v1
with:
command: check
28 changes: 28 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Clippy
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
clippy:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.43.0
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
28 changes: 28 additions & 0 deletions .github/workflows/fmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Rust Format
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
fmt:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.43.0
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Cargo Test
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.43.0
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions-rs/cargo@v1
with:
command: test
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- id: check-merge-conflict
- id: detect-private-key
- id: no-commit-to-branch
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: fmt
- id: cargo-check
- id: clippy
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/run/current-system/sw/bin/python"
}
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fuzzy string matching like a boss. It uses Levenshtein Distance to calculate the

**Note: This project was originally named `fuzzyrusty`. Someone else cloned and published it to crates.io https://crates.io/crates/fuzzyrusty. _We do not control that crate._ This is why we have changed the name.**

## Installation
## Installation
`fuzzywuzzy` is currently available through GitHub or crates.io.

For the latest stable releas, add this to your `Cargo.toml`:
Expand Down Expand Up @@ -34,21 +34,21 @@ assert_eq!(fuzz::partial_ratio("this is a test", "this is a test!"), 100);
```
### Token Sort Ratio
```rust
assert_eq!(fuzz::ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear"), 91);
assert_eq!(fuzz::ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear"), 91);
assert_eq!(fuzz::token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear", true, true), 100);
```
### Token Set Ratio
```rust
assert_eq!(fuzz::ratio("fuzzy was a bear", "fuzzy fuzzy was a bear"), 84);
assert_eq!(fuzz::ratio("fuzzy was a bear", "fuzzy fuzzy was a bear"), 84);
assert_eq!(fuzz::token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear", true, true), 100);
```
### Process
```rust
assert_eq!(process::extract_one(
"cowboys",
&["Atlanta Falcons", "Dallas Cowboys", "New York Jets"],
&utils::full_process,
&fuzz::wratio,
0,
assert_eq!(process::extract_one(
"cowboys",
&["Atlanta Falcons", "Dallas Cowboys", "New York Jets"],
&utils::full_process,
&fuzz::wratio,
0,
), Some(("Dallas Cowboys".to_string(), 90)));
```
```
20 changes: 7 additions & 13 deletions src/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ pub fn partial_ratio(s1: &str, s2: &str) -> u8 {
let blocks = utils::get_matching_blocks(&shorter, &longer);
let mut max: u8 = 0;
for (i, j, _) in blocks {
let long_start = if j > i {
j - i
} else {
0
};
let long_start = if j > i { j - i } else { 0 };
let long_end = std::cmp::min(long_start + shorter.len(), longer.len());
let long_substr = &longer[long_start..long_end];
let r = ratio(&shorter, long_substr);
Expand Down Expand Up @@ -113,36 +109,34 @@ fn token_set(s1: &str, s2: &str, partial: bool, force_ascii: bool, full_process:
let intersect_str = intersection.join(" ");
let diff1to2_str = diff1to2.join(" ");
let diff2to1_str = diff2to1.join(" ");
let combined_1to2 = if diff1to2_str.len() > 0 {
let combined_1to2 = if !diff1to2_str.is_empty() {
intersect_str.to_string() + &diff1to2_str
} else {
intersect_str.to_string()
};
let combined_2to1 = if diff2to1_str.len() > 0 {
let combined_2to1 = if !diff2to1_str.is_empty() {
intersect_str.to_string() + &diff2to1_str
} else {
intersect_str.to_string()
};
if partial {
vec![
*vec![
partial_ratio(&intersect_str, &combined_1to2),
partial_ratio(&intersect_str, &combined_2to1),
partial_ratio(&combined_1to2, &combined_2to1),
]
.iter()
.max()
.unwrap()
.clone()
} else {
vec![
*vec![
ratio(&intersect_str, &combined_1to2),
ratio(&intersect_str, &combined_2to1),
ratio(&combined_1to2, &combined_2to1),
]
.iter()
.max()
.unwrap()
.clone()
}
}

Expand Down Expand Up @@ -235,15 +229,15 @@ pub fn wratio(s1: &str, s2: &str, force_ascii: bool, full_process: bool) -> u8 {
return vec![base as f64, partial, ptsor, ptser]
.iter()
.cloned()
.fold(0. / 0., f64::max)
.fold(f64::NAN, f64::max)
.round() as u8;
}
let tsor = token_sort_ratio(p1r, p2r, true, false) as f64 * UNBASE_SCALE;
let tser = token_set_ratio(p1r, p2r, true, false) as f64 * UNBASE_SCALE;
vec![base as f64, tsor, tser]
.iter()
.cloned()
.fold(0. / 0., f64::max)
.fold(f64::NAN, f64::max)
.round() as u8
}

Expand Down
6 changes: 3 additions & 3 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
I: IntoIterator<Item = T>,
T: AsRef<str>,
P: Fn(&str, bool) -> String,
S: Fn(&str, &str, bool, bool) -> u8
S: Fn(&str, &str, bool, bool) -> u8,
{
let processed_query: String = processor(query, false);
if processed_query.is_empty() {
Expand Down Expand Up @@ -54,7 +54,7 @@ where
I: IntoIterator<Item = T>,
T: AsRef<str>,
P: Fn(&str, bool) -> String,
S: Fn(&str, &str, bool, bool) -> u8
S: Fn(&str, &str, bool, bool) -> u8,
{
let best = extract_without_order(query, choices, processor, scorer, score_cutoff);
if best.is_empty() {
Expand Down Expand Up @@ -133,4 +133,4 @@ mod tests {
assert_eq!(best.as_str(), get_baseball_strings()[0])
}
}
}
}