def renewal_for(self, registration): """Find a renewal for this registration. If there's more than one, find the best one. """ renewals = [] renewal = None for regnum in registration.regnums: renum = regnum.replace("-", "") if regnum in self.renewals: renewals.extend(self.renewals[regnum]) if renewals: renewals, disposition = self.best_renewal(registration, renewals) registration.disposition = disposition else: registration.disposition = "Not renewed." renewals = [] if not renewals: # We'll count it as a decent match if we can find a # renewal based solely on title/author. # # n.b. right now there seem to be no such matches. key = registration.renewal_key renewals_for_key = self.renewals_by_key[key] if renewals_for_key: renewals, disposition = self.best_renewal( registration, renewals_for_key) registration.disposition = "Possibly renewed, based solely on title/author match." if not renewals: # We'll count it as a tentative match if there has _ever_ been a renewal # for a book with a nearly-identical title. title = Registration._normalize_text( registration.title) or registration.title renewals_for_title = self.renewals_by_title[title] if renewals_for_title: renewals, disposition = self.best_renewal( registration, renewals_for_title) registration.disposition = "Possibly renewed, based solely on title match." for renewal in renewals: # These renewals have been matched; they should not be # output in the list of unmatched renewals. self.used_renewals.add(renewal) return renewals
def __init__(self, renewals_input_path): self.renewals = defaultdict(list) self.renewals_by_title = defaultdict(list) self.renewals_by_key = defaultdict(list) for i in open(renewals_input_path): renewal = Renewal(**json.loads(i)) regnum = renewal.regnum if not regnum: regnum = [] if not isinstance(regnum, list): regnum = [regnum] for r in regnum: r = (r or "").replace("-", "") self.renewals[r].append(renewal) title = Registration._normalize_text( renewal.title) or renewal.title self.renewals_by_title[title].append(renewal) self.renewals_by_key[renewal.renewal_key].append(renewal) self.used_renewals = set()