-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Add
.str.count_matches()
(#2580)
Adds a method to count the number of appearances of some patterns in a column of strings. An example usage is for dirty word counting for preprocessing data.
- Loading branch information
Showing
13 changed files
with
308 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[dependencies] | ||
aho-corasick = "1.1.3" | ||
arrow2 = {workspace = true, features = [ | ||
"chrono-tz", | ||
"compute_take", | ||
|
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
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,89 @@ | ||
use common_error::{DaftError, DaftResult}; | ||
|
||
use daft_core::{datatypes::Field, schema::Schema, DataType, Series}; | ||
use daft_dsl::{ | ||
functions::{ScalarFunction, ScalarUDF}, | ||
ExprRef, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
struct CountMatchesFunction { | ||
pub(super) whole_words: bool, | ||
pub(super) case_sensitive: bool, | ||
} | ||
|
||
#[typetag::serde] | ||
impl ScalarUDF for CountMatchesFunction { | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"count_matches" | ||
} | ||
|
||
fn to_field(&self, inputs: &[ExprRef], schema: &Schema) -> DaftResult<Field> { | ||
match inputs { | ||
[data, _] => match data.to_field(schema) { | ||
Ok(field) => match &field.dtype { | ||
DataType::Utf8 => Ok(Field::new(field.name, DataType::UInt64)), | ||
a => Err(DaftError::TypeError(format!( | ||
"Expects inputs to count_matches to be utf8, but received {a}", | ||
))), | ||
}, | ||
Err(e) => Err(e), | ||
}, | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 2 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series]) -> DaftResult<Series> { | ||
match inputs { | ||
[data, patterns] => { | ||
data.utf8_count_matches(patterns, self.whole_words, self.case_sensitive) | ||
} | ||
_ => Err(DaftError::ValueError(format!( | ||
"Expected 2 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
} | ||
|
||
pub fn utf8_count_matches( | ||
input: ExprRef, | ||
patterns: ExprRef, | ||
whole_words: bool, | ||
case_sensitive: bool, | ||
) -> ExprRef { | ||
ScalarFunction::new( | ||
CountMatchesFunction { | ||
whole_words, | ||
case_sensitive, | ||
}, | ||
vec![input, patterns], | ||
) | ||
.into() | ||
} | ||
|
||
#[cfg(feature = "python")] | ||
pub mod python { | ||
use daft_dsl::python::PyExpr; | ||
use pyo3::{pyfunction, PyResult}; | ||
|
||
#[pyfunction] | ||
pub fn utf8_count_matches( | ||
expr: PyExpr, | ||
patterns: PyExpr, | ||
whole_words: bool, | ||
case_sensitive: bool, | ||
) -> PyResult<PyExpr> { | ||
let expr = | ||
super::utf8_count_matches(expr.into(), patterns.into(), whole_words, case_sensitive); | ||
Ok(expr.into()) | ||
} | ||
} |
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
Oops, something went wrong.