-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from abstractqqq/str_lcs
added lcs_seq
- Loading branch information
Showing
8 changed files
with
315 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/// Polars Series-wise generic str distancers | ||
use rapidfuzz::distance::{ | ||
lcs_seq, | ||
osa, | ||
levenshtein, | ||
damerau_levenshtein, | ||
jaro | ||
}; | ||
use polars::{prelude::{arity::binary_elementwise_values, DataType, Float64Chunked, Series, StringChunked, UInt32Chunked}, series::IntoSeries}; | ||
use pyo3_polars::export::polars_core::{ | ||
utils::rayon::prelude::{IntoParallelIterator, ParallelIterator}, | ||
POOL, | ||
}; | ||
|
||
use crate::utils::split_offsets; | ||
|
||
// Str Distance Related Helper Functions | ||
pub trait StdBatchedStrDistancer { | ||
fn distance(&self, s: &str) -> u32; | ||
fn normalized_similarity(&self, s:&str) -> f64; | ||
} | ||
|
||
macro_rules! StdBatchedStrDistanceImpl { | ||
($batch_struct: ty) => { | ||
impl StdBatchedStrDistancer for $batch_struct{ | ||
fn distance(&self, s:&str) -> u32 { | ||
self.distance(s.chars()) as u32 | ||
} | ||
|
||
fn normalized_similarity(&self, s:&str) -> f64 { | ||
self.normalized_similarity(s.chars()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
StdBatchedStrDistanceImpl!(lcs_seq::BatchComparator<char>); | ||
StdBatchedStrDistanceImpl!(osa::BatchComparator<char>); | ||
StdBatchedStrDistanceImpl!(levenshtein::BatchComparator<char>); | ||
StdBatchedStrDistanceImpl!(damerau_levenshtein::BatchComparator<char>); | ||
StdBatchedStrDistanceImpl!(jaro::BatchComparator<char>); | ||
|
||
// ------------------------------------------------------------------------------------- | ||
|
||
pub fn generic_batched_distance<T>( | ||
batched: T, | ||
ca: &StringChunked, | ||
parallel: bool, | ||
) -> Series | ||
where | ||
T: StdBatchedStrDistancer + std::marker::Sync | ||
{ | ||
let out: UInt32Chunked = if parallel { | ||
let n_threads = POOL.current_num_threads(); | ||
let splits = split_offsets(ca.len(), n_threads); | ||
let chunks_iter = splits | ||
.into_par_iter() | ||
.map(|(offset, len)| { | ||
let s1 = ca.slice(offset as i64, len); | ||
let out: UInt32Chunked = s1 | ||
.apply_nonnull_values_generic(DataType::UInt32, |s| { | ||
batched.distance(s) | ||
}); | ||
out.downcast_iter().cloned().collect::<Vec<_>>() | ||
}); | ||
let chunks = POOL.install(|| chunks_iter.collect::<Vec<_>>()); | ||
UInt32Chunked::from_chunk_iter(ca.name(), chunks.into_iter().flatten()) | ||
} else { | ||
ca.apply_nonnull_values_generic(DataType::UInt32, |s| | ||
batched.distance(s) | ||
) | ||
}; | ||
out.into_series() | ||
} | ||
|
||
pub fn generic_batched_sim<T>( | ||
batched: T, | ||
ca: &StringChunked, | ||
parallel: bool, | ||
) -> Series | ||
where | ||
T: StdBatchedStrDistancer + std::marker::Sync | ||
{ | ||
let out: Float64Chunked = if parallel { | ||
let n_threads = POOL.current_num_threads(); | ||
let splits = split_offsets(ca.len(), n_threads); | ||
let chunks_iter = splits | ||
.into_par_iter() | ||
.map(|(offset, len)| { | ||
let s1 = ca.slice(offset as i64, len); | ||
let out: Float64Chunked = s1 | ||
.apply_nonnull_values_generic(DataType::Float64, |s| batched.normalized_similarity(s)); | ||
out.downcast_iter().cloned().collect::<Vec<_>>() | ||
}); | ||
let chunks = POOL.install(|| chunks_iter.collect::<Vec<_>>()); | ||
Float64Chunked::from_chunk_iter(ca.name(), chunks.into_iter().flatten()) | ||
} else { | ||
ca.apply_nonnull_values_generic(DataType::Float64, |s| batched.normalized_similarity(s)) | ||
}; | ||
out.into_series() | ||
} | ||
|
||
pub fn generic_binary_distance( | ||
func: fn(&str, &str) -> u32, | ||
ca1: &StringChunked, | ||
ca2: &StringChunked, | ||
parallel: bool | ||
) -> Series { | ||
let out: UInt32Chunked = if parallel { | ||
let n_threads = POOL.current_num_threads(); | ||
let splits = split_offsets(ca1.len(), n_threads); | ||
let chunks_iter= splits | ||
.into_par_iter() | ||
.map(|(offset, len)| { | ||
let s1 = ca1.slice(offset as i64, len); | ||
let s2 = ca2.slice(offset as i64, len); | ||
let out: UInt32Chunked = binary_elementwise_values(&s1, &s2, func); | ||
out.downcast_iter().cloned().collect::<Vec<_>>() | ||
}); | ||
let chunks = POOL.install(|| chunks_iter.collect::<Vec<_>>()); | ||
UInt32Chunked::from_chunk_iter(ca1.name(), chunks.into_iter().flatten()) | ||
} else { | ||
binary_elementwise_values(ca1, ca2, func) | ||
}; | ||
out.into_series() | ||
} | ||
|
||
pub fn generic_binary_sim( | ||
func: fn(&str, &str) -> f64, | ||
ca1: &StringChunked, | ||
ca2: &StringChunked, | ||
parallel: bool | ||
) -> Series { | ||
let out: Float64Chunked = if parallel { | ||
let n_threads = POOL.current_num_threads(); | ||
let splits = split_offsets(ca1.len(), n_threads); | ||
let chunks_iter= splits | ||
.into_par_iter() | ||
.map(|(offset, len)| { | ||
let s1 = ca1.slice(offset as i64, len); | ||
let s2 = ca2.slice(offset as i64, len); | ||
let out: Float64Chunked = binary_elementwise_values(&s1, &s2, func); | ||
out.downcast_iter().cloned().collect::<Vec<_>>() | ||
}); | ||
let chunks = POOL.install(|| chunks_iter.collect::<Vec<_>>()); | ||
Float64Chunked::from_chunk_iter(ca1.name(), chunks.into_iter().flatten()) | ||
} else { | ||
binary_elementwise_values(ca1, ca2, func) | ||
}; | ||
out.into_series() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use polars::prelude::*; | ||
use pyo3_polars::derive::{polars_expr, CallerContext}; | ||
use super::generic_str_distancer::{ | ||
generic_batched_distance, | ||
generic_batched_sim, | ||
generic_binary_distance, | ||
generic_binary_sim | ||
}; | ||
use rapidfuzz::distance::lcs_seq; | ||
|
||
#[inline(always)] | ||
fn lcs_seq(s1: &str, s2: &str) -> u32 { | ||
lcs_seq::distance(s1.chars(), s2.chars()) as u32 | ||
} | ||
|
||
#[inline(always)] | ||
fn lcs_seq_sim(s1: &str, s2: &str) -> f64 { | ||
lcs_seq::normalized_similarity(s1.chars(), s2.chars()) | ||
} | ||
|
||
#[polars_expr(output_type=UInt32)] | ||
fn pl_lcs_seq(inputs: &[Series], context: CallerContext) -> PolarsResult<Series> { | ||
let ca1 = inputs[0].str()?; | ||
let ca2 = inputs[1].str()?; | ||
let parallel = inputs[2].bool()?; | ||
let parallel = parallel.get(0).unwrap(); | ||
let can_parallel = parallel && !context.parallel(); | ||
if ca2.len() == 1 { | ||
let r = ca2.get(0).unwrap(); | ||
let batched = lcs_seq::BatchComparator::new(r.chars()); | ||
Ok(generic_batched_distance(batched, ca1, can_parallel)) | ||
} else if ca1.len() == ca2.len() { | ||
Ok(generic_binary_distance(lcs_seq, ca1, ca2, can_parallel)) | ||
} else { | ||
Err(PolarsError::ShapeMismatch( | ||
"Inputs must have the same length or one of them must be a scalar.".into(), | ||
)) | ||
} | ||
} | ||
|
||
#[polars_expr(output_type=Float64)] | ||
fn pl_lcs_sim(inputs: &[Series], context: CallerContext) -> PolarsResult<Series> { | ||
let ca1 = inputs[0].str()?; | ||
let ca2 = inputs[1].str()?; | ||
let parallel = inputs[2].bool()?; | ||
let parallel = parallel.get(0).unwrap(); | ||
let can_parallel = parallel && !context.parallel(); | ||
if ca2.len() == 1 { | ||
let r = ca2.get(0).unwrap(); | ||
let batched = lcs_seq::BatchComparator::new(r.chars()); | ||
Ok(generic_batched_sim(batched, ca1, can_parallel)) | ||
} else if ca1.len() == ca2.len() { | ||
Ok(generic_binary_sim(lcs_seq_sim, ca1, ca2, can_parallel)) | ||
} else { | ||
Err(PolarsError::ShapeMismatch( | ||
"Inputs must have the same length or one of them must be a scalar.".into(), | ||
)) | ||
} | ||
} |
Oops, something went wrong.