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

fix score method in SentenceEmbeddingFilter #71

Merged
merged 1 commit into from
Apr 16, 2024
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
3 changes: 2 additions & 1 deletion opusfilter/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def _score_chunk(self, chunk):

def score(self, pairs):
for chunk in grouper(pairs, self.chunksize):
return self._score_chunk(chunk)
for score in self._score_chunk(chunk):
yield score

def accept(self, score):
return all(similarity >= self.threshold for similarity in score)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from opusfilter import ConfigurationError
from opusfilter.embeddings import *
from opusfilter.pipeline import FilterPipeline


try:
Expand Down Expand Up @@ -80,3 +81,17 @@ def test_bilingual_margin_ratios(self):
results = [testfilter.accept(x) for x in testfilter.score(self.bi_inputs)]
for result, correct in zip(results, expected):
self.assertEqual(result, correct)

def test_chunking(self):
testfilter = SentenceEmbeddingFilter(languages=self.bi_langs, threshold=0.4, chunksize=19)
inputs = 50 * self.bi_inputs
expected = 50 * [True, True, False, False]
results = [testfilter.accept(x) for x in testfilter.score(inputs)]
for result, correct in zip(results, expected):
self.assertEqual(result, correct)
pipeline = FilterPipeline(filters=[testfilter])
pipeline.chunksize = 30
filtered = list(pipeline.filter(inputs))
self.assertEqual(len(filtered), len([x for x in expected if x]))
scores = list(pipeline.score(inputs))
self.assertEqual(len(scores), len(expected))
Loading