Skip to content

Commit

Permalink
Refactor: Rename AnalysisHit to SubjectSuggestion. Part of #267
Browse files Browse the repository at this point in the history
  • Loading branch information
osma committed Apr 5, 2019
1 parent 218afec commit 9b48094
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion annif/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _suggest(self, text, project, params):

def suggest(self, text, project, params=None):
"""Suggest subjects for the input text and return a list of subjects
represented as a list of AnalysisHit objects."""
represented as a list of SubjectSuggestion objects."""
beparams = dict(self.params)
if params is not None:
beparams.update(params)
Expand Down
8 changes: 4 additions & 4 deletions annif/backend/dummy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Dummy backend for testing basic interaction of projects and backends"""


from annif.hit import AnalysisHit, ListAnalysisResult
from annif.hit import SubjectSuggestion, ListAnalysisResult
from . import backend


Expand All @@ -16,9 +16,9 @@ def initialize(self):

def _suggest(self, text, project, params):
score = float(params.get('score', 1.0))
return ListAnalysisResult([AnalysisHit(uri=self.uri,
label=self.label,
score=score)],
return ListAnalysisResult([SubjectSuggestion(uri=self.uri,
label=self.label,
score=score)],
project.subjects)

def learn(self, corpus, project):
Expand Down
4 changes: 2 additions & 2 deletions annif/backend/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import collections
import os.path
import annif.util
from annif.hit import AnalysisHit, ListAnalysisResult
from annif.hit import SubjectSuggestion, ListAnalysisResult
from annif.exception import NotInitializedException
import fastText
from . import backend
Expand Down Expand Up @@ -127,7 +127,7 @@ def _suggest_chunks(self, chunktexts, project):
results = []
for score, label in best_labels[:limit]:
subject = self._label_to_subject(project, label)
results.append(AnalysisHit(
results.append(SubjectSuggestion(
uri=subject[0],
label=subject[1],
score=score / len(chunktexts)))
Expand Down
8 changes: 4 additions & 4 deletions annif/backend/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import requests
import requests.exceptions
from annif.hit import AnalysisHit, ListAnalysisResult
from annif.hit import SubjectSuggestion, ListAnalysisResult
from . import backend


Expand Down Expand Up @@ -35,9 +35,9 @@ def _suggest(self, text, project, params):
results = response

try:
return ListAnalysisResult([AnalysisHit(uri=h['uri'],
label=h['label'],
score=h['score'])
return ListAnalysisResult([SubjectSuggestion(uri=h['uri'],
label=h['label'],
score=h['score'])
for h in results
if h['score'] > 0.0],
project.subjects)
Expand Down
2 changes: 1 addition & 1 deletion annif/backend/pav.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _normalize_hits(self, hits, source_project):
else: # default to raw score
score = hit.score
pav_result.append(
annif.hit.AnalysisHit(
annif.hit.SubjectSuggestion(
uri=hit.uri,
label=hit.label,
score=score))
Expand Down
8 changes: 4 additions & 4 deletions annif/hit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import numpy as np


AnalysisHit = collections.namedtuple('AnalysisHit', 'uri label score')
SubjectSuggestion = collections.namedtuple('AnalysisHit', 'uri label score')
WeightedHits = collections.namedtuple('WeightedHits', 'hits weight')


class HitFilter:
"""A reusable filter for filtering AnalysisHit objects."""
"""A reusable filter for filtering SubjectSuggestion objects."""

def __init__(self, limit=None, threshold=0.0):
self._limit = limit
Expand All @@ -29,7 +29,7 @@ class AnalysisResult(metaclass=abc.ABCMeta):
@property
@abc.abstractmethod
def hits(self):
"""Return the hits as an ordered sequence of AnalysisHit objects,
"""Return the hits as an ordered sequence of SubjectSuggestion objects,
highest scores first."""
pass # pragma: no cover

Expand Down Expand Up @@ -110,7 +110,7 @@ def _vector_to_hits(self):
continue # we can skip the remaining ones
subject = self._subject_index[subject_id]
hits.append(
AnalysisHit(
SubjectSuggestion(
uri=subject[0],
label=subject[1],
score=score))
Expand Down
2 changes: 1 addition & 1 deletion annif/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def vectorizer(self):

def suggest(self, text, backend_params=None):
"""Suggest subjects the given text by passing it to the backend. Returns a
list of AnalysisHit objects ordered by decreasing score."""
list of SubjectSuggestion objects ordered by decreasing score."""

logger.debug('Suggesting subjects for text "%s..." (len=%d)',
text[:20], len(text))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ def test_evaluation_batch(subject_index):
gold_set = annif.corpus.SubjectSet.from_string(
'<http://www.yso.fi/onto/yso/p10849>\tarkeologit')
hits1 = annif.hit.ListAnalysisResult([
annif.hit.AnalysisHit(
annif.hit.SubjectSuggestion(
uri='http://www.yso.fi/onto/yso/p10849',
label='arkeologit',
score=1.0)], subject_index)
batch.evaluate(hits1, gold_set)
hits2 = annif.hit.ListAnalysisResult([
annif.hit.AnalysisHit(
annif.hit.SubjectSuggestion(
uri='http://www.yso.fi/onto/yso/p1747',
label='egyptologit',
score=1.0)], subject_index)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_hit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Unit tests for hit processing in Annif"""

from annif.hit import AnalysisHit, AnalysisResult, LazyAnalysisResult, \
from annif.hit import SubjectSuggestion, AnalysisResult, LazyAnalysisResult, \
ListAnalysisResult, HitFilter
from annif.corpus import SubjectIndex
import numpy as np
Expand All @@ -9,9 +9,9 @@
def generate_hits(n, subject_index):
hits = []
for i in range(n):
hits.append(AnalysisHit(uri='http://example.org/{}'.format(i),
label='hit {}'.format(i),
score=1.0 / (i + 1)))
hits.append(SubjectSuggestion(uri='http://example.org/{}'.format(i),
label='hit {}'.format(i),
score=1.0 / (i + 1)))
return ListAnalysisResult(hits, subject_index)


Expand All @@ -31,7 +31,7 @@ def test_hitfilter_threshold(subject_index):

def test_hitfilter_zero_score(subject_index):
orighits = ListAnalysisResult(
[AnalysisHit(uri='uri', label='label', score=0.0)],
[SubjectSuggestion(uri='uri', label='label', score=0.0)],
subject_index)
hits = HitFilter()(orighits)
assert isinstance(hits, AnalysisResult)
Expand All @@ -54,11 +54,11 @@ def test_analysishits_vector(document_corpus):
subjects = SubjectIndex(document_corpus)
hits = ListAnalysisResult(
[
AnalysisHit(
SubjectSuggestion(
uri='http://www.yso.fi/onto/yso/p7141',
label='sinetit',
score=1.0),
AnalysisHit(
SubjectSuggestion(
uri='http://www.yso.fi/onto/yso/p6479',
label='viikingit',
score=0.5)],
Expand All @@ -79,7 +79,7 @@ def test_analysishits_vector_notfound(document_corpus):
subjects = SubjectIndex(document_corpus)
hits = ListAnalysisResult(
[
AnalysisHit(
SubjectSuggestion(
uri='http://example.com/notfound',
label='not found',
score=1.0)],
Expand Down

0 comments on commit 9b48094

Please sign in to comment.