Skip to content

Commit

Permalink
Implement batched suggest in SVC backend. Fixes #667
Browse files Browse the repository at this point in the history
  • Loading branch information
osma committed Feb 6, 2023
1 parent cc6dfcf commit 1c587bd
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions annif/backend/svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ def _scores_to_suggestions(self, scores, params):
)
return ListSuggestionResult(results)

def _suggest(self, text, params):
self.debug(
'Suggesting subjects for text "{}..." (len={})'.format(text[:20], len(text))
)
vector = self.vectorizer.transform([text])
if vector.nnz == 0: # All zero vector, empty result
return ListSuggestionResult([])
confidences = self._model.decision_function(vector)[0]
def _suggest_batch(self, texts, params):
vector = self.vectorizer.transform(texts)
confidences = self._model.decision_function(vector)
# convert to 0..1 score range using logistic function
scores = scipy.special.expit(confidences)
return self._scores_to_suggestions(scores, params)
scores_list = scipy.special.expit(confidences)
return [
ListSuggestionResult([])
if row.nnz == 0
else self._scores_to_suggestions(scores, params)
for scores, row in zip(scores_list, vector)
]

0 comments on commit 1c587bd

Please sign in to comment.