Skip to content
Merged
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 python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashSet;
use std::ops::Deref;

use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
Expand Down Expand Up @@ -58,6 +59,11 @@ impl Yake {
pub fn get_n_best(&self, py: Python, text: &str, n: usize) -> Vec<(String, f64)> {
py.allow_threads(|| get_n_best_sequential(n, text, &self._stopwords, &self._config))
}

#[getter]
fn get_stopwords(&self) -> PyResult<HashSet<String>> {
Ok(HashSet::from_iter(self._stopwords.iter().cloned()))
}
}

fn get_n_best_sequential(
Expand Down
2 changes: 2 additions & 0 deletions python/src/yake_rust/_lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ class Yake:
minimum_chars: int | None = None,
) -> None: ...
def get_n_best(self, text: str, *, n: int) -> list[tuple[str, float]]: ...
@property
def stopwords(self) -> set[str]: ...
17 changes: 17 additions & 0 deletions python/tests/test_yake_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ def test_instantiate_yake_with_custom_stopwords() -> None:
_ = yake_rust.Yake(stopwords={"stop", "word"})


def test_yake_stopwords_attribute__custom() -> None:
stopwords = {"stop", "word"}
yake = yake_rust.Yake(stopwords=stopwords)
assert yake.stopwords == stopwords


def test_yake_stopwords_attribute__language() -> None:
yake = yake_rust.Yake(language="en")
assert yake.stopwords


def test_yake_stopwords_attribute__read_only() -> None:
yake = yake_rust.Yake(language="en")
with pytest.raises(AttributeError, match="stopwords"):
yake.stopwords = set() # type: ignore[misc]


@pytest.mark.parametrize(
"kwargs",
[{}, {"language": "en", "stopwords": {"stopwords"}}, {"bad": 1}],
Expand Down