def categories(cls, work): """Send out _all_ categories for the work. (So long as the category type has a URI associated with it in Subject.uri_lookup.) """ _db = Session.object_session(work) by_scheme_and_term = dict() identifier_ids = work.all_identifier_ids() classifications = Identifier.classifications_for_identifier_ids( _db, identifier_ids) for c in classifications: subject = c.subject if subject.type in Subject.uri_lookup: scheme = Subject.uri_lookup[subject.type] term = subject.identifier weight_field = AtomFeed.schema_("ratingValue") key = (scheme, term) if not key in by_scheme_and_term: value = dict(term=subject.identifier) if subject.name: value['label'] = subject.name value[weight_field] = 0 by_scheme_and_term[key] = value by_scheme_and_term[key][weight_field] += c.weight # Collapse by_scheme_and_term to by_scheme by_scheme = defaultdict(list) for (scheme, term), value in by_scheme_and_term.items(): by_scheme[scheme].append(value) by_scheme.update(super(VerboseAnnotator, cls).categories(work)) return by_scheme
def explain_identifier(cls, identifier, primary, seen, strength, level): indent = " " * level if primary: ident = "Primary identifier" else: ident = "Identifier" if primary: strength = 1 output = "%s %s: %s/%s (q=%s)" % (indent, ident, identifier.type, identifier.identifier, strength) print output.encode("utf8") _db = Session.object_session(identifier) classifications = Identifier.classifications_for_identifier_ids( _db, [identifier.id]) for classification in classifications: subject = classification.subject genre = subject.genre if genre: genre = genre.name else: genre = "(!genre)" #print "%s %s says: %s/%s %s w=%s" % ( # indent, classification.data_source.name, # subject.identifier, subject.name, genre, classification.weight #) seen.add(identifier) for equivalency in identifier.equivalencies: if equivalency.id in seen: continue seen.add(equivalency.id) output = equivalency.output cls.explain_identifier(output, False, seen, equivalency.strength, level+1)