Package gen :: Package lib :: Module genderstats
[frames] | no frames]

Source Code for Module gen.lib.genderstats

  1  # 
  2  # Gramps - a GTK+/GNOME based genealogy program 
  3  # 
  4  # Copyright (C) 2000-2005  Donald N. Allingham 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the Free Software 
 18  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 19  # 
 20   
 21  # $Id: genderstats.py 10103 2008-02-24 13:55:55Z acraphae $ 
 22   
 23  """ 
 24  Gender statistics kept in GRAMPS database. 
 25  """ 
 26   
 27  #------------------------------------------------------------------------- 
 28  # 
 29  # GRAMPS modules 
 30  # 
 31  #------------------------------------------------------------------------- 
 32  from gen.lib.person import Person 
 33   
 34  #------------------------------------------------------------------------- 
 35  # 
 36  #  
 37  # 
 38  #------------------------------------------------------------------------- 
39 -class GenderStats:
40 """ 41 Class for keeping track of statistics related to Given Name vs. Gender. 42 43 This allows the tracking of the liklihood of a person's given name 44 indicating the gender of the person. 45 """
46 - def __init__ (self, stats={}):
47 if stats == None: 48 self.stats = {} 49 else: 50 self.stats = stats
51
52 - def save_stats(self):
53 return self.stats
54
55 - def _get_key (self, person):
56 name = person.get_primary_name().get_first_name() 57 return self._get_key_from_name (name)
58
59 - def _get_key_from_name (self, name):
60 return name.split (' ')[0].replace ('?', '')
61
62 - def name_stats (self, name):
63 if self.stats.has_key (name): 64 return self.stats[name] 65 return (0, 0, 0)
66
67 - def count_person (self, person, undo = 0):
68 if not person: 69 return 70 # Let the Person do their own counting later 71 72 name = self._get_key (person) 73 if not name: 74 return 75 76 gender = person.get_gender() 77 (male, female, unknown) = self.name_stats (name) 78 if not undo: 79 increment = 1 80 else: 81 increment = -1 82 83 if gender == Person.MALE: 84 male += increment 85 elif gender == Person.FEMALE: 86 female += increment 87 elif gender == Person.UNKNOWN: 88 unknown += increment 89 90 self.stats[name] = (male, female, unknown) 91 return
92
93 - def uncount_person (self, person):
94 return self.count_person (person, undo = 1)
95
96 - def guess_gender (self, name):
97 name = self._get_key_from_name (name) 98 if not name or not self.stats.has_key (name): 99 return Person.UNKNOWN 100 101 (male, female, unknown) = self.stats[name] 102 if unknown == 0: 103 if male and not female: 104 return Person.MALE 105 if female and not male: 106 return Person.FEMALE 107 108 if male > (2 * female): 109 return Person.MALE 110 111 if female > (2 * male): 112 return Person.FEMALE 113 114 return Person.UNKNOWN
115