gramps/src/RelLib/GenderStats.py
Alex Roitman 849e569d45 * src/RelLib.py: Remove file (replaced by the package dir).
* src/RelLib: Add directory.
* src/RelLib/__init__.py, src/RelLib/_helper.py,
src/RelLib/_secondary.py, src/RelLib/Researcher.py,
src/RelLib/GenderStats.py, src/RelLib/Person.py,
src/RelLib/Family.py, src/RelLib/Event.py, src/RelLib/Place.py,
src/RelLib/Source.py, src/RelLib/MediaObject.py,
src/RelLib/Repository.py, src/RelLib/Makefile.am,
src/RelLib/.cvsignore: Add to CVS.
* src/configure.in: Create Makefile in src/RelLib.


svn: r5587
2005-12-20 02:38:07 +00:00

79 lines
2.1 KiB
Python

from Person import Person
class GenderStats:
"""
Class for keeping track of statistics related to Given Name vs.
Gender. This allows the tracking of the liklihood of a person's
given name indicating the gender of the person.
"""
def __init__ (self,stats={}):
if stats == None:
self.stats = {}
else:
self.stats = stats
def save_stats(self):
return self.stats
def _get_key (self, person):
name = person.get_primary_name().get_first_name()
return self._get_key_from_name (name)
def _get_key_from_name (self, name):
return name.split (' ')[0].replace ('?', '')
def name_stats (self, name):
if self.stats.has_key (name):
return self.stats[name]
return (0, 0, 0)
def count_person (self, person, db, undo = 0):
if not person:
return
# Let the Person do their own counting later
name = self._get_key (person)
if not name:
return
gender = person.get_gender()
(male, female, unknown) = self.name_stats (name)
if not undo:
increment = 1
else:
increment = -1
if gender == Person.MALE:
male += increment
elif gender == Person.FEMALE:
female += increment
elif gender == Person.UNKNOWN:
unknown += increment
self.stats[name] = (male, female, unknown)
return
def uncount_person (self, person):
return self.count_person (person, None, undo = 1)
def guess_gender (self, name):
name = self._get_key_from_name (name)
if not name or not self.stats.has_key (name):
return Person.UNKNOWN
(male, female, unknown) = self.stats[name]
if unknown == 0:
if male and not female:
return Person.MALE
if female and not male:
return Person.FEMALE
if male > (2 * female):
return Person.MALE
if female > (2 * male):
return Person.FEMALE
return Person.UNKNOWN