def get_by_name(self, name, session=None, riding=None, election=None, party=None, saveAlternate=True, strictMatch=False): """ Return a Politician by name. Uses a bunch of methods to try and deal with variations in names. If given any of a session, riding, election, or party, returns only politicians who match. If given session and optinally riding, tries to match the name more laxly. saveAlternate: If we have Thomas Mulcair and we match, via session/riding, to Tom Mulcair, save Tom in the alternate names table strictMatch: Even if given a session, don't try last-name-only matching. """ # Alternate names for a pol are in the InternalXref table. Assemble a list of possibilities poss = PoliticianInfo.sr_objects.filter(schema='alternate_name', value=parsetools.normalizeName(name)) if len(poss) >= 1: # We have one or more results if session or riding or party: # We've been given extra criteria -- see if they match result = None for p in poss: # For each possibility, assemble a list of matching Members members = ElectedMember.objects.filter(politician=p.politician) if riding: members = members.filter(riding=riding) if session: members = members.filter(sessions=session) if party: members = members.filter(party=party) if len(members) >= 1: if result: # we found another match on a previous journey through the loop # can't disambiguate, raise exception raise Politician.MultipleObjectsReturned(name) # We match! Save the result. result = members[0].politician if result: return result elif election: raise Exception("Election not implemented yet in Politician get_by_name") else: # No extra criteria -- return what we got (or die if we can't disambiguate) if len(poss) > 1: raise Politician.MultipleObjectsReturned(name) else: return poss[0].politician if session and not strictMatch: # We couldn't find the pol, but we have the session and riding, so let's give this one more shot # We'll try matching only on last name match = re.search(r'\s([A-Z][\w-]+)$', name.strip()) # very naive lastname matching if match: lastname = match.group(1) pols = self.get_query_set().filter(name_family=lastname, electedmember__sessions=session).distinct() if riding: pols = pols.filter(electedmember__riding=riding) if len(pols) > 1: if riding: raise Exception("DATA ERROR: There appear to be two politicians with the same last name elected to the same riding from the same session... %s %s %s" % (lastname, session, riding)) elif len(pols) == 1: # yes! pol = pols[0] if saveAlternate: pol.add_alternate_name(name) # save the name we were given as an alternate return pol raise Politician.DoesNotExist("Could not find politician named %s" % name)
def get_by_name(self, name, session=None, riding=None, election=None, party=None, saveAlternate=True, strictMatch=False): """ Return a Politician by name. Uses a bunch of methods to try and deal with variations in names. If given any of a session, riding, election, or party, returns only politicians who match. If given session and optinally riding, tries to match the name more laxly. saveAlternate: If we have Thomas Mulcair and we match, via session/riding, to Tom Mulcair, save Tom in the alternate names table strictMatch: Even if given a session, don't try last-name-only matching. """ # Alternate names for a pol are in the InternalXref table. Assemble a list of possibilities poss = PoliticianInfo.sr_objects.filter(schema='alternate_name', value=parsetools.normalizeName(name)) if len(poss) >= 1: # We have one or more results if session or riding or party: # We've been given extra criteria -- see if they match result = None for p in poss: # For each possibility, assemble a list of matching Members members = ElectedMember.objects.filter(politician=p.politician) if riding: members = members.filter(riding=riding) if session: members = members.filter(sessions=session) if party: members = members.filter(party=party) if len(members) >= 1: if result: # we found another match on a previous journey through the loop # can't disambiguate, raise exception raise Politician.MultipleObjectsReturned(name) # We match! Save the result. result = members[0].politician if result: return result elif election: raise Exception("Election not implemented yet in Politician get_by_name") else: # No extra criteria -- return what we got (or die if we can't disambiguate) if len(poss) > 1: raise Politician.MultipleObjectsReturned(name) else: return poss[0].politician if session and not strictMatch: # We couldn't find the pol, but we have the session and riding, so let's give this one more shot # We'll try matching only on last name match = re.search(r'\s([A-Z][\w-]+)$', name.strip()) # very naive lastname matching if match: lastname = match.group(1) pols = self.get_queryset().filter(name_family=lastname, electedmember__sessions=session).distinct() if riding: pols = pols.filter(electedmember__riding=riding) if len(pols) > 1: if riding: raise Exception("DATA ERROR: There appear to be two politicians with the same last name elected to the same riding from the same session... %s %s %s" % (lastname, session, riding)) elif len(pols) == 1: # yes! pol = pols[0] if saveAlternate: pol.add_alternate_name(name) # save the name we were given as an alternate return pol raise Politician.DoesNotExist("Could not find politician named %s" % name)
def add_alternate_name(self, name): normname = parsetools.normalizeName(name) if normname not in self.alternate_names(): self.set_info_multivalued('alternate_name', normname)
def filter_by_name(self, name): """Returns a list of politicians matching a given name.""" return [ i.politician for i in PoliticianInfo.sr_objects.filter( schema='alternate_name', value=parsetools.normalizeName(name)) ]
def add_alternate_name(self, name): self.set_info_multivalued('alternate_name', parsetools.normalizeName(name))
def filter_by_name(self, name): return [i.politician for i in PoliticianInfo.sr_objects.filter(schema='alternate_name', value=parsetools.normalizeName(name))]
def filter_by_name(self, name): """Returns a list of politicians matching a given name.""" return [i.politician for i in PoliticianInfo.sr_objects.filter(schema='alternate_name', value=parsetools.normalizeName(name))]
def addAlternateName(self, name): name = parsetools.normalizeName(name) # check if exists if InternalXref.objects.filter(schema='pol_names', target_id=self.id, text_value=name).count() == 0: InternalXref(schema='pol_names', target_id=self.id, text_value=name).save()
def filterByName(self, name): return [self.get_query_set().get(pk=x.target_id) for x in InternalXref.objects.filter(schema='pol_names', text_value=parsetools.normalizeName(name))]