-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use batch_size parameter with keybert.backend.SentenceTransformerBack…
…end (#210)
- Loading branch information
Showing
5 changed files
with
87 additions
and
24 deletions.
There are no files selected for viewing
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,3 +1,4 @@ | ||
from ._base import BaseEmbedder | ||
from ._sentencetransformers import SentenceTransformerBackend | ||
|
||
__all__ = ["BaseEmbedder"] | ||
__all__ = ["BaseEmbedder", "SentenceTransformerBackend"] |
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,41 @@ | ||
import pytest | ||
from keybert import KeyBERT | ||
from keybert.backend import SentenceTransformerBackend | ||
import sentence_transformers | ||
|
||
from sklearn.feature_extraction.text import CountVectorizer | ||
from .utils import get_test_data | ||
|
||
|
||
doc_one, doc_two = get_test_data() | ||
|
||
|
||
@pytest.mark.parametrize("keyphrase_length", [(1, i + 1) for i in range(5)]) | ||
@pytest.mark.parametrize( | ||
"vectorizer", [None, CountVectorizer(ngram_range=(1, 1), stop_words="english")] | ||
) | ||
def test_single_doc_sentence_transformer_backend(keyphrase_length, vectorizer): | ||
"""Test whether the keywords are correctly extracted""" | ||
top_n = 5 | ||
|
||
model_name = "paraphrase-MiniLM-L6-v2" | ||
st_model = sentence_transformers.SentenceTransformer(model_name, device="cpu") | ||
|
||
kb_model = KeyBERT(model=SentenceTransformerBackend(st_model, batch_size=128)) | ||
|
||
keywords = kb_model.extract_keywords( | ||
doc_one, | ||
keyphrase_ngram_range=keyphrase_length, | ||
min_df=1, | ||
top_n=top_n, | ||
vectorizer=vectorizer, | ||
) | ||
|
||
assert model_name in kb_model.model.embedding_model.tokenizer.name_or_path | ||
assert isinstance(keywords, list) | ||
assert isinstance(keywords[0], tuple) | ||
assert isinstance(keywords[0][0], str) | ||
assert isinstance(keywords[0][1], float) | ||
assert len(keywords) == top_n | ||
for keyword in keywords: | ||
assert len(keyword[0].split(" ")) <= keyphrase_length[1] |