Skip to content

add Python 3.13t support #5

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- '3.11'
- '3.12'
- '3.13'
- '3.13t'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
Expand Down Expand Up @@ -69,6 +70,7 @@ jobs:
- '3.11'
- '3.12'
- '3.13'
- '3.13t'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
Expand Down Expand Up @@ -110,6 +112,7 @@ jobs:
- '3.11'
- '3.12'
- '3.13'
- '3.13t'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
Expand Down Expand Up @@ -144,6 +147,7 @@ jobs:
- '3.11'
- '3.12'
- '3.13'
- '3.13t'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
Expand Down Expand Up @@ -190,6 +194,7 @@ jobs:
- '3.11'
- '3.12'
- '3.13'
- '3.13t'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
Expand Down Expand Up @@ -234,6 +239,7 @@ jobs:
- '3.11'
- '3.12'
- '3.13'
- '3.13t'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[package]
name = "py_rust_stemmers"
version = "0.1.5"
version = "0.1.6"
edition = "2021"

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

[dependencies]
pyo3 = { version = "0.18", features = ["extension-module"] }
pyo3 = { version = "0.25", features = ["extension-module"] }
rust-stemmers = "1.2.0"
rayon = "1.6"

[dev-dependencies]
pyo3 = { version = "0.18", features = ["extension-module"] }
pyo3 = { version = "0.25", features = ["extension-module"] }

[profile.release]
opt-level = 3
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "py_rust_stemmers"
version = "0.1.5"
version = "0.1.6"
description = "Fast and parallel snowball stemmer"

# Include a long description
Expand Down
32 changes: 19 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct SnowballStemmer {
#[pymethods]
impl SnowballStemmer {
#[new]
fn new(lang: &str) -> Self {
fn new(lang: &str) -> PyResult<Self> {
let algorithm = match lang.to_lowercase().as_str() {
"arabic" => Algorithm::Arabic,
"danish" => Algorithm::Danish,
Expand All @@ -33,10 +33,11 @@ impl SnowballStemmer {
"swedish" => Algorithm::Swedish,
"tamil" => Algorithm::Tamil,
"turkish" => Algorithm::Turkish,
_ => panic!("Unsupported language: {}", lang),
// throw exception instead of crashing, preserve prior test behavior
_ => return Err(pyo3::exceptions::PyValueError::new_err(format!("Unsupported language: {}", lang))),
};
let stemmer = Stemmer::create(algorithm);
SnowballStemmer { stemmer }
Ok(SnowballStemmer { stemmer })
}

#[inline(always)]
Expand All @@ -45,26 +46,31 @@ impl SnowballStemmer {
}

#[inline(always)]
pub fn stem_words_parallel(&self, inputs: Vec<&str>) -> Vec<String> {
inputs
.into_par_iter()
.map(|word| self.stemmer.stem(word).into_owned())
.collect()
pub fn stem_words_parallel(&self, py: Python<'_>, inputs: Vec<String>) -> PyResult<Vec<String>> {
// release GIL
py.allow_threads(|| {
let result = inputs
.par_iter()
.map(|word| self.stemmer.stem(word.as_str()).into_owned())
.collect();
Ok(result)
})
}

// refactor to Vec<String> based on the discussion(s) here: https://github.com/PyO3/pyo3/discussions/4830
#[inline(always)]
pub fn stem_words(&self, inputs: Vec<&str>) -> Vec<String> {
pub fn stem_words(&self, inputs: Vec<String>) -> Vec<String> {
inputs
.into_iter()
.map(|word| self.stemmer.stem(word))
.iter()
.map(|word| self.stemmer.stem(word.as_str()))
.map(|stemmed| stemmed.into_owned())
.collect()
}
}

/// This module is required for the Python interpreter to access the Rust functions.
#[pymodule]
fn py_rust_stemmers(_py: Python, m: &PyModule) -> PyResult<()> {
fn py_rust_stemmers(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SnowballStemmer>()?;
Ok(())
}
}
7 changes: 4 additions & 3 deletions tests/speedtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

s = stemmer('english')
b = time.perf_counter()
for _ in range(loops):
# only time a fraction of these, 500k takes 10 minutes
for _ in range(loops // 100):
for word in words:
stemmed = s.stemWord(word.encode('utf-8'))
print("Time taken snowballstemmer with PyStemmer installed:", time.perf_counter() - b)
stemmed = s.stemWord(word)
print("Time taken snowballstemmer with PyStemmer installed:", (time.perf_counter() - b) * 100)

18 changes: 10 additions & 8 deletions tests/test_py_rust_stemers.py → tests/test_py_rust_stemmers.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import unittest
import py_rust_stemers
from py_rust_stemmers import SnowballStemmer

class TestRustStemmer(unittest.TestCase):

def test_english_stemming(self):
s = SnowballStemmer('english')
words = ["fruitlessly", "happiness", "computations"]
expected = ["fruitless", "happi", "comput"]
result = py_rust_stemers.rust_stem(words, "english")
result = [s.stem_word(w) for w in words]
self.assertEqual(result, expected)

def test_spanish_stemming(self):
s = SnowballStemmer('spanish')
words = ["frutalmente", "felicidad", "computaciones"]
expected = ["frutal", "felic", "comput"]
result = py_rust_stemers.rust_stem(words, "spanish")
result = [s.stem_word(w) for w in words]
self.assertEqual(result, expected)

def test_empty_input(self):
words = []
expected = []
result = py_rust_stemers.rust_stem(words, "english")
s = SnowballStemmer('english')
expected = ['']
result = [s.stem_word("")]
self.assertEqual(result, expected)

def test_invalid_language(self):
words = ["fruitlessly", "happiness", "computations"]
with self.assertRaises(ValueError):
py_rust_stemers.rust_stem(words, "invalid_lang")
s = SnowballStemmer('invalid_lang')

if __name__ == '__main__':
unittest.main()