Skip to content

Commit

Permalink
refactor: move URI to label conversion (and vice versa) to SubjectIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
osma committed Aug 9, 2019
1 parent 22b935a commit 4acc731
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 16 additions & 0 deletions annif/corpus/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ def by_label(self, label):
logger.warning('Unknown subject label "%s"', label)
return None

def uris_to_labels(self, uris):
"""return a list of labels corresponding to the given URIs; unknown
URIs are ignored"""

return [self[subject_id][1]
for subject_id in (self.by_uri(uri) for uri in uris)
if subject_id is not None]

def labels_to_uris(self, labels):
"""return a list of URIs corresponding to the given labels; unknown
labels are ignored"""

return [self[subject_id][0]
for subject_id in (self.by_label(label) for label in labels)
if subject_id is not None]

def save(self, path):
"""Save this subject index into a file."""

Expand Down
16 changes: 4 additions & 12 deletions annif/corpus/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ def _create_document(self, text, uris, labels):
information. URIs for labels and vice versa are looked up from the
subject index, if available."""

sidx = self._subject_index

if not uris and labels and sidx:
uris = set((sidx[subject_id][0]
for subject_id in (sidx.by_label(label)
for label in labels)
if subject_id is not None))
if not labels and uris and sidx:
labels = set((sidx[subject_id][1]
for subject_id in (sidx.by_uri(uri)
for uri in uris)
if subject_id is not None))
if not uris and labels and self._subject_index:
uris = set((self._subject_index.labels_to_uris(labels)))
if not labels and uris and self._subject_index:
labels = set((self._subject_index.uris_to_labels(uris)))

return Document(text=text, uris=uris, labels=labels)

Expand Down

0 comments on commit 4acc731

Please sign in to comment.