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

Rayon + PyO3 piece #74

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ members = [
"src/rest_api_postgres/rust",
"src/parallelism/rust",
"src/pyo3_mock_data",
"src/pyo3_rayon",
]
resolver = "2"
17 changes: 17 additions & 0 deletions src/pyo3_rayon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "pyo3_rayon"
version = "0.1.0"
edition = "2021"

[lib]
name = "pyo3_rayon"
crate-type = ["cdylib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
csv = "1.3.0"
pyo3 = { version = "0.20.2", features = ["abi3-py38", "extension-module"] }
rayon = "1.8.1"
regex = "1.10.3"
serde = { version = "1.0.196", features = ["derive"] }
1 change: 1 addition & 0 deletions src/pyo3_rayon/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
maturin==1.4.0
67 changes: 67 additions & 0 deletions src/pyo3_rayon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use pyo3::prelude::*;
use rayon::prelude::*;
use regex::{Captures, Regex};
use std::error::Error;

#[derive(FromPyObject)]
struct Record {
id: u32,
content: String,
}

struct RecordProcessed {
id: u32,
n_m: u64,
n_f: u64,
}

fn clean_text(text: &str) -> String {
let pattern1 = Regex::new(r"([’'])(s|d|ll)").unwrap();
// Replace pattern with text
let matched = pattern1.replace_all(text, |capture: &Captures| match &capture[2] {
"s" => " is",
"d" => " had",
"ll" => " will",
_ => "<unk>",
});
// Remove non-alphabetic characters
let pattern2 = Regex::new(r"[^a-zA-Z\s]").unwrap();
let clean_text = pattern2.replace_all(&matched, "");
let result: String = clean_text.to_lowercase();
result
}

fn count_gendered_pronouns(text: &str) -> Result<(usize, usize), Box<dyn Error>> {
let clean_text = clean_text(text);
let tokens = clean_text.split_whitespace().collect::<Vec<&str>>();
let n_m = tokens
.par_iter()
.filter(|&x| *x == "he" || *x == "him" || *x == "his")
.count();
let n_f = tokens
.par_iter()
.filter(|&x| *x == "she" || *x == "her" || *x == "hers")
.count();
Ok((n_m, n_f))
}

#[pyfunction(signature = (records))]
fn get_pronoun_counts(records: Vec<Record>) -> PyResult<Vec<RecordProcessed>> {
let mut result = vec![];
for record in records {
let (n_m, n_f) = count_gendered_pronouns(&record.content).unwrap();
let record_processed = RecordProcessed {
id: record.id,
n_m: n_m as u64,
n_f: n_f as u64,
};
result.push(record_processed);
}
Ok(result)
}

#[pymodule]
fn pyo3_rayon(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(get_pronoun_counts, m)?)?;
Ok(())
}
Loading