Merge pull request #346 from prculley/soundex

A new Soundex filter for matching 'name' with people using Soundex
This commit is contained in:
Sam Manzi 2017-02-04 10:50:33 +11:00 committed by GitHub
commit ea133b3e9f
3 changed files with 105 additions and 1 deletions

View File

@ -109,6 +109,7 @@ from ._matchidof import MatchIdOf
from ._regexpidof import RegExpIdOf
from ._changedsince import ChangedSince
from ._isrelatedwith import IsRelatedWith
from ._hassoundexname import HasSoundexName
#-------------------------------------------------------------------------
#
@ -189,5 +190,6 @@ editor_rule_list = [
Disconnected,
ChangedSince,
IsRelatedWith,
HasSoundexName,
]

View File

@ -0,0 +1,86 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2017 Paul Culley <paulr2787_at_gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#-------------------------------------------------------------------------
#
# Standard Python modules
#
#-------------------------------------------------------------------------
from ....const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
from .. import Rule
from ....lib.nameorigintype import NameOriginType
from gramps.gen.soundex import soundex
#-------------------------------------------------------------------------
#
# HasNameOf
#
#-------------------------------------------------------------------------
class HasSoundexName(Rule):
"""Rule that checks for full or partial name matches"""
labels = [_('Name:')]
name = _('Soundex match of People with the <name>')
description = _("Soundex Match of people with a specified name. First "
"name, Surname, Call name, and Nickname are searched in "
"primary and alternate names.")
category = _('General filters')
allow_regex = False
def apply(self, db, person):
self.sndx = soundex(self.list[0])
for name in [person.get_primary_name()] + person.get_alternate_names():
if self._match_name(name):
return True
return False
def _match_name(self, name):
if self.list[0]:
if soundex(str(name.get_first_name())) == self.sndx:
return True
elif soundex(str(name.get_surname())) == self.sndx:
return True
elif soundex(str(name.get_call_name())) == self.sndx:
return True
elif soundex(str(name.get_nick_name())) == self.sndx:
return True
elif soundex(str(name.get_family_nick_name())) == self.sndx:
return True
else:
for surn in name.get_surname_list():
if self._match_surname(surn):
return True
return False
def _match_surname(self, surn):
if soundex(str(surn.get_surname())) == self.sndx:
return True
if surn.get_origintype().value == NameOriginType.PATRONYMIC:
if soundex(str(surn.get_surname())) == self.sndx:
return True
return False

View File

@ -36,7 +36,7 @@ from gramps.gen.filters.rules.person import (
IsDuplicatedAncestorOf, IsRelatedWith, HasIdOf, IsDefaultPerson, IsFemale,
IsMale, MissingParent, MultipleMarriages, NeverMarried, NoBirthdate,
NoDeathdate, PeoplePrivate, PeoplePublic, PersonWithIncompleteEvent,
RelationshipPathBetweenBookmarks)
RelationshipPathBetweenBookmarks, HasNameOf, HasSoundexName)
TEST_DIR = os.path.abspath(os.path.join(DATA_DIR, "tests"))
EXAMPLE = os.path.join(TEST_DIR, "example.gramps")
@ -564,6 +564,22 @@ class BaseTest(unittest.TestCase):
b'D3WJQCCGV58IP8PNHZ', b'Q8HKQC3VMRM1M6M7ES',
]))
def test_hassoundexname(self):
"""
Test HasSoundexName rule.
"""
rule = HasSoundexName(['garner'])
self.assertEqual(len(self.filter_with_rule(rule)), 73)
def test_hasnameof(self):
"""
Test HasNameOf rule.
"""
rule = HasNameOf(['Lewis', 'Garner', 'Dr.', 'Sr', 'Anderson',
'Big Louie', 'von', 'Zieliński', None, None, None])
self.assertEqual(self.filter_with_rule(rule), set([
b'GNUJQCL9MD64AM56OH']))
if __name__ == "__main__":
unittest.main()