Skip to content

Commit

Permalink
Merge edbeae8 into dc37e4f
Browse files Browse the repository at this point in the history
  • Loading branch information
osma authored Apr 4, 2018
2 parents dc37e4f + edbeae8 commit 9f224e8
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 26 deletions.
3 changes: 2 additions & 1 deletion annif/backend/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class DummyBackend(backend.AnnifBackend):

def _analyze(self, text, project, params):
score = float(params.get('score', 1.0))
return [AnalysisHit('http://example.org/dummy', 'dummy', score)]
return [AnalysisHit(uri='http://example.org/dummy',
label='dummy', score=score)]
2 changes: 1 addition & 1 deletion annif/backend/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class HTTPBackend(backend.AnnifBackend):
def _analyze(self, text, project, params):
data = {'text': text, 'project': params['project']}
req = requests.post(params['endpoint'], data=data)
return [AnalysisHit(h['uri'], h['label'], h['score'])
return [AnalysisHit(uri=h['uri'], label=h['label'], score=h['score'])
for h in req.json()
if h['score'] > 0.0]
7 changes: 3 additions & 4 deletions annif/backend/tfidf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ def _merge_chunk_results(self, chunk_results, project):
subject = project.subjects[subject_id]
results.append(
AnalysisHit(
subject[0],
subject[1],
score /
len(chunk_results)))
uri=subject[0],
label=subject[1],
score=score / len(chunk_results)))
return results

def _analyze(self, text, project, params):
Expand Down
11 changes: 4 additions & 7 deletions annif/corpus/subject.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
"""A directory of files as a subject corpus"""


import collections
import glob
import os.path
import re


class Subject:
def __init__(self, uri, label, text):
self.uri = uri
self.label = label
self.text = text
Subject = collections.namedtuple('Subject', 'uri label text')


class SubjectDirectory:
Expand All @@ -25,7 +22,7 @@ def __iter__(self):
with open(filename) as subjfile:
uri, label = subjfile.readline().strip().split(' ', 1)
text = ' '.join(subjfile.readlines())
yield Subject(uri, label, text)
yield Subject(uri=uri, label=label, text=text)


class SubjectIndex:
Expand Down Expand Up @@ -64,6 +61,6 @@ def file_as_corpus(path):
for line in subjfile:
uri, label = line.strip().split(None, 1)
uri = uri[1:-1]
yield Subject(uri, label, None)
yield Subject(uri=uri, label=label, text=None)

return cls(file_as_corpus(path))
12 changes: 2 additions & 10 deletions annif/hit.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
"""Class representing a single hit from analysis."""

import collections

class AnalysisHit:
"""A single hit resulting from analysis."""

def __init__(self, uri, label, score):
self.uri = uri
self.label = label
self.score = score

def dump(self):
"""return this object as a dict"""
return {'uri': self.uri, 'label': self.label, 'score': self.score}
AnalysisHit = collections.namedtuple('AnalysisHit', 'uri label score')
3 changes: 1 addition & 2 deletions annif/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def _merge_hits(cls, hits_by_uri):
merged_hits = []
for score_hits in hits_by_uri.values():
total = sum([sh[0] for sh in score_hits])
hit = annif.hit.AnalysisHit(
score_hits[0][1].uri, score_hits[0][1].label, total)
hit = score_hits[0][1]._replace(score=total)
merged_hits.append(hit)
return merged_hits

Expand Down
2 changes: 1 addition & 1 deletion annif/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def analyze(project_id, text, limit, threshold):
return "Project '{}' not found".format(project_id), 404

hits = project.analyze(text, limit, threshold)
return [hit.dump() for hit in hits]
return [hit._asdict() for hit in hits]

0 comments on commit 9f224e8

Please sign in to comment.