Skip to content

Commit ca7eb0a

Browse files
Merge pull request #58 from quesurifn/stopwords_attribute
Add read-only stopwords attribute to Yake python class
2 parents cd1f0d5 + f5b0adb commit ca7eb0a

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

python/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::HashSet;
2+
use std::ops::Deref;
23

34
use pyo3::exceptions::PyTypeError;
45
use pyo3::prelude::*;
@@ -58,6 +59,11 @@ impl Yake {
5859
pub fn get_n_best(&self, py: Python, text: &str, n: usize) -> Vec<(String, f64)> {
5960
py.allow_threads(|| get_n_best_sequential(n, text, &self._stopwords, &self._config))
6061
}
62+
63+
#[getter]
64+
fn get_stopwords(&self) -> PyResult<HashSet<String>> {
65+
Ok(HashSet::from_iter(self._stopwords.iter().cloned()))
66+
}
6167
}
6268

6369
fn get_n_best_sequential(

python/src/yake_rust/_lib.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ class Yake:
3434
minimum_chars: int | None = None,
3535
) -> None: ...
3636
def get_n_best(self, text: str, *, n: int) -> list[tuple[str, float]]: ...
37+
@property
38+
def stopwords(self) -> set[str]: ...

python/tests/test_yake_rust.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ def test_instantiate_yake_with_custom_stopwords() -> None:
3131
_ = yake_rust.Yake(stopwords={"stop", "word"})
3232

3333

34+
def test_yake_stopwords_attribute__custom() -> None:
35+
stopwords = {"stop", "word"}
36+
yake = yake_rust.Yake(stopwords=stopwords)
37+
assert yake.stopwords == stopwords
38+
39+
40+
def test_yake_stopwords_attribute__language() -> None:
41+
yake = yake_rust.Yake(language="en")
42+
assert yake.stopwords
43+
44+
45+
def test_yake_stopwords_attribute__read_only() -> None:
46+
yake = yake_rust.Yake(language="en")
47+
with pytest.raises(AttributeError, match="stopwords"):
48+
yake.stopwords = set() # type: ignore[misc]
49+
50+
3451
@pytest.mark.parametrize(
3552
"kwargs",
3653
[{}, {"language": "en", "stopwords": {"stopwords"}}, {"bad": 1}],

0 commit comments

Comments
 (0)