User:GeneaBot/Code/object human
< User:GeneaBot | Code
# -*- coding: utf-8 -*-
import pywikibot
from object_display import *
from object_item import *
class Human(Item):
#
# Item Fields :
# self.ident : identifier in Wikidata (Qxxxx)
# self.item : dictionnary of labels, descriptions, statements from Wikidata
# self.claimsWanted :
# self.claims : list of claims of item
#
# Fields :
# self.gender : gender of person
# self.siblings : dict of brothers ans sisters
#
# Item Methods :
# self.__repr__ ()
# self.__str__ ()
# self.AddClaim (pProperty, pValue, pReferences=[], pQualifiers=[])
#
# Methods :
# self.__init__ (ident, claimsWanted, source)
# self.GetGender ()
# self.SetParent (gender, parent, source)
# SetSpouse (spouse, source)
# GetSibling ()
#
# static attributes :
#
ClaimsRequired = [ [ 'P31' , 'Q5' ] ] # instance of = human
defaultClaims = { \
'P569': ['None' ], # date of birth
'P570': ['None' ], # date of death
'P19' : ['None' ], # place of birth
'P20' : ['None' ] # place of death
}
optionalClaims = {
'P106':'Q2478141' # occupation
}
#
Gender = {'H': 'Q6581097', 'F': 'Q6581072'}
Sibling = {'H': 'P7', 'F': 'P9'}
#
# static methods :
# Item.Get (Ident, claimsWanted, claimsRequired, source)
#
#
#
def Get(ident, claimsWanted, source):
DisplayMessages.call("O", "Human." + ident + ".Get", 4, "ident=" + str(ident) + ", claimsWanted=" + str(claimsWanted))
DisplayMessages.debug("O", "Human." + ident + ".Get", 2, "ident=" + ident + ", list=" + str(Item.ListIdent))
if ident in Item.ListIdent:
item = Human.List[ident]
item.AddClaimsWanted(claimsWanted, source)
else:
item = Human(ident, claimsWanted, source)
return item
Get = staticmethod(Get)
#
#
#
#
#
def __init__(self, ident, claimsWanted, source):
DisplayMessages.call("O", "Human." + ident + ".__init__", 4, "ident=" + ident + ", claimsWanted=" + str(claimsWanted) + ", source=" + repr(source) )
Item.__init__(self, ident, claimsWanted, Human.ClaimsRequired, source)
self.gender = ''
self.sibling = dict()
self.listTitle = []
self.listFunction = []
self.children = []
#
#
#
# Function Human.GetGender(self)
#
def GetGender(self):
DisplayMessages.call("O", "Human." + self.ident + ".GetGender", 4, "self=" + self.disp[True] )
if self.gender == '':
self.gender = ''
for claim in self.claims:
DisplayMessages.debug("O", "Human." + self.ident + ".GetGender", 9, "self=" + str(self) )
DisplayMessages.debug("O", "Human." + self.ident + ".GetGender", 9, "claim=" + str(claim) )
if claim.mainClaim.property == 'P21':
if claim.mainClaim.value == 'Q6581097':
self.gender = 'H'
elif claim.mainClaim.value == 'Q6581072':
self.gender = 'F'
else:
self.gender = ''
if self.gender == '':
List = self.GetTargetValues('P21')
if len(List) == 1:
if List[0] == 'Q6581097':
self.gender = 'H'
elif List[0] == 'Q6581072':
self.gender = 'F'
else:
self.gender = ''
DisplayMessages.debug("O", "Human." + self.ident + ".GetGender", 7, "gender = " + self.gender )
return self.gender
#
#
#
#
def SetParent(self, gender, parent, source):
DisplayMessages.call("O", "Human." + self.ident + ".SetParent", 4, "self=" + self.disp[True] + \
", gender=" + gender + \
", parent=" + parent.disp[True] )
parent.AddClaim('P21', Human.Gender[gender], source)
prop = ''
if gender == 'H':
prop = 'P22' # father
elif gender == 'F':
prop = 'P25' # mother
if not prop == '':
if self.AddClaim(prop, parent.ident, source):
parent.AddClaim('P40', self.ident, source)
gender = self.GetGender()
propert = Human.Sibling[gender]
children = parent.GetChildren()
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4,
"siblings of " + self.disp[True] + " : " + str(children) )
clmChild = Human.defaultClaims.copy()
clmChild[prop] = []
clmChild[prop].append(parent.ident)
for child in children:
idtChild = child[0]
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4, "Ident child = " + idtChild)
if not idtChild == self.ident:
if not idtChild in self.sibling:
srcChild = child[1]
Child = Human.Get(idtChild, clmChild, srcChild)
genChild = Child.GetGender()
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4, "processing " + Child.disp[True] )
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4, "gender : " + genChild )
proChild = Human.Sibling[genChild]
srcSibling = Source()
srcSibling = srcSibling + srcChild
srcSibling = srcSibling + source
self.AddClaim(proChild, idtChild, srcSibling)
Child.AddClaim(propert, self.ident, srcSibling)
self.sibling[idtChild] = Child
else:
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4, idtChild + " already processed")
else:
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4, "Not sibling, person")
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4, "end : for child in children")
if not self.ident in parent.children:
parent.children.append([self.ident, source])
DisplayMessages.debug("O", "Human." + self.ident + ".SetParent", 4,
"add " + self.disp[True] + " to children of " + parent.disp[True])
#
#
#
#
def GetChildren(self):
DisplayMessages.call("O", "Human." + self.ident + ".GetChildren", 4, "" )
if self.children == []:
self.children = self.GetTargetValuesSources('P40')
DisplayMessages.debug("O", "Human." + self.ident + ".GetChildren", 4,
"list of children = " + str(self.children) )
return self.children
#
#
#
#
def SetSpouse(self, spouse, source):
DisplayMessages.call("O", "Human." + self.ident + ".SetSpouse", 4, "self=" + self.disp[True] + \
", spouse=" + spouse.disp[True] )
if self.AddClaim('P26', spouse.ident, source):
spouse.AddClaim('P26', self.ident, source)
#
#
#
#
def GetSibling(self):
DisplayMessages.call("O", "Human." + self.ident + ".GetSibling", 4, "self=" + self.disp[True])
return self.sibling
#
#
#
#
def SetTitle(self, title, domain, begin, end):
DisplayMessages.call("O", "Human." + self.ident + ".SetTitle", 4, "self=" + self.disp[True] + \
", title=" + GetIdentAndLabel(title.ident, 'Q') + \
", begin=" + begin + ", end=" + end )
self.currentTitle = Dignity(self.ident, self.disp[True], 'P97', title.ident, domain, begin, end)
#
#
#
#
def SetSuccessorForTitle(self, successor):
DisplayMessages.call("O", "Human." + self.ident + ".SetSuccessorForTitle", 4, "self=" + self.disp[True] + \
", successor = " + successor.disp[True])
self.currentTitle.SetSuccessor(successor.currentTitle)
#
#
#
#
def RecordTitle(self, source):
DisplayMessages.call("O", "Human." + self.ident + ".RecordTitle", 4, "self=" + self.disp[True] + \
", source = " + repr(source))
if self.listTitle == []:
self.listTitle = self.GetListDignities('P97')
newClaim = self.currentTitle.Record(self.listTitle, source)
self.claims.append( newClaim )
if newClaim.error:
self.error=True
if newClaim.warning:
self.warning=True
#
#
#
#
def SetFunction(self, function, domain, begin, end):
DisplayMessages.call("O", "Human." + self.ident + ".SetFunction", 4, "self=" + self.disp[True] + \
", function=" + GetIdentAndLabel(function.ident, 'Q') + \
", begin=" + begin + ", end=" + end )
self.currentFunction = Dignity(self.ident, self.disp[True], 'P39', function.ident, domain, begin, end)
#
#
#
#
def SetSuccessorForFunction(self, successor):
DisplayMessages.call("O", "Human." + self.ident + ".SetSuccessorForFunction", 4, "self=" + self.disp[True] + \
", successor = " + successor.disp[True])
self.currentFunction.SetSuccessor(successor.currentFunction)
#
#
#
#
def RecordFunction(self, source):
DisplayMessages.call("O", "Human." + self.ident + ".RecordFunction", 4, "self=" + self.disp[True] + \
", source = " + repr(source))
if self.listFunction == []:
self.listFunction = self.GetListDignities('P39')
newClaim = self.currentFunction.Record(self.listFunction, source)
self.claims.append( newClaim )
if newClaim.error:
self.error=True
if newClaim.warning:
self.warning=True
#
#
#
#
def GetListDignities(self, Property):
DisplayMessages.call("O", "Human." + self.ident + ".GetListDignities", 5, "item(" + self.disp[True] + ')' \
", pProperty=" + Property )
ListValues = []
i = 0
while 1:
try:
claim = self.item.claims[Property][i]
except KeyError: # no claim with this property
break
except IndexError: # all claims with this property have been seen
break
Value = ExtractItem(claim)
dignity = Dignity(self.ident, self.disp[True], Property, Value, "", "", "", "", "", False)
dignity.SetDomain (claim.qualifiers)
dignity.SetBegin (claim.qualifiers)
dignity.SetEnd (claim.qualifiers)
dignity.SetPrevious(claim.qualifiers)
dignity.SetNext (claim.qualifiers)
ListValues.append(dignity)
i += 1
DisplayMessages.debug("O", "Human." + self.ident + ".GetListDignities", 6, "ListValues=" + str(ListValues) )
return ListValues
#
#
#
#
def DisplayDignity(self):
for dignity in self.listFunction:
print(str(dignity))
class Dignity:
#
# Fields :
# property (P39 ou P39)
# ident
# begin
# end
# previous
# next
# warning
# toAdd ?
#
# Methods :
# self.__init__(pProperty, pIdent, pBegin, pEnd, pPrevious, pNext)
# self.SetSuccessor(Next)
# self.Record(Source)
#
#
Previous = {'P39':'P1365', 'P97':'P155' }
Next = {'P39':'P1366', 'P97':'P156' }
#
#
def __init__(self, IdentItem, DisplayItem, pProperty, IdentTarget, pDomain, pBegin, pEnd, pPrevious="", pNext="", toAdd=True):
DisplayMessages.call("O", "Dignity." + IdentItem + ".__init__", 4, \
"IdentItem=" + IdentItem + ", DisplayItem=" + DisplayItem + \
", pProperty=" + pProperty + ", IdentTarget=" + IdentTarget + \
", pBegin=" + pBegin + ", pEnd=" + pEnd + \
", pPrevious=" + pPrevious + ", pNext=" + pNext + ", toAdd=" + str(toAdd) )
self.property = pProperty
self.identItem = IdentItem
self.displayItem = DisplayItem
self.identTarget = IdentTarget
self.domain = pDomain
self.begin = pBegin
lBegin = pBegin.split(' ')
try:
self.yearBegin = int(lBegin[len(lBegin)-1])
except ValueError:
self.yearBegin = 0
self.end = pEnd
lEnd = self.end.split(' ')
try:
self.yearEnd = int(lEnd[len(lEnd)-1])
except ValueError:
self.yearEnd = 0
self.previous = pPrevious
self.next = pNext
self.warning = ""
self.error = ""
self.toAdd = toAdd
#
#
#
#
def __str__(self):
text = '(' + self.property + ':' + self.identTarget + ' (' + self.domain + ')' + \
' begin=' + self.begin + '[' + str(self.yearBegin) + '] ' + \
'end=' + self.end + '[' + str(self.yearEnd) + '] ' + \
' previous=' + self.previous + ' next=' + self.next + ')'
return text
#
#
#
#
def __eq__(self, other):
DisplayMessages.call("O", "Dignity." + self.identItem + ".__eq__", 4, "Compare " + str(self) + " and " + str(other) )
cmp = False
if self.property == other.property and self.identTarget == other.identTarget and self.domain == other.domain:
if self.yearBegin == other.yearBegin or self.yearBegin == '' or other.yearBegin == '':
if self.yearEnd == other.yearEnd or self.yearEnd == '' or other.yearEnd == '':
if self.previous == other.previous or self.previous == '' or other.previous == '':
if self.next == other.next or self.next == '' or other.next == '':
cmp = True
DisplayMessages.call("O", "Dignity." + self.identItem + ".__eq__", 5, "return " + str(cmp) )
return cmp
#
#
#
#
def SetBegin(self, qualifiers):
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetBegin", 4, "")
self.begin = self.SetQualifier('P580', 'date of begin', qualifiers)
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetBegin", 6, "self.next=" + self.begin)
lBegin = self.begin.split(' ')
try:
self.yearBegin = int(lBegin[len(lBegin)-1])
except ValueError:
self.yearBegin = 0
#
#
#
#
def SetEnd(self, qualifiers):
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetEnd", 4, "")
self.end = self.SetQualifier('P582', 'date of end', qualifiers)
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetEnd", 6, "self.next=" + self.end)
lEnd = self.end.split(' ')
try:
self.yearEnd = int(lEnd[len(lEnd)-1])
except ValueError:
self.yearEnd = 0
#
#
#
#
def SetDomain(self, qualifiers):
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetPrevious", 4, "")
self.previous = self.SetQualifier( 'P642', 'previous', qualifiers)
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetPrevious", 6, "self.next=" + self.previous)
#
#
#
#
def SetPrevious(self, qualifiers):
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetPrevious", 4, "")
self.previous = self.SetQualifier( Dignity.Previous[self.property], 'previous', qualifiers)
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetPrevious", 6, "self.next=" + self.previous)
#
#
#
#
def SetNext(self, qualifiers):
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetNext", 4, "")
self.next = self.SetQualifier( Dignity.Next[self.property], 'next', qualifiers)
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetNext", 6, "self.next=" + self.next)
#
#
#
#
def SetQualifier(self, Property, NameProp, Qualifiers):
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetQualifier", 4, "Property=" + Property + ", NameProp=" + NameProp)
Value = ""
if Property in Qualifiers:
ListVal = Qualifiers[Property]
if len(ListVal) > 1:
self.error = "More than one " + NameProp + " (constraint violation)"
elif len(ListVal) == 1:
Value = ExtractItem(ListVal[0])
return Value
#
#
#
#
def SetSuccessor(self, Next):
DisplayMessages.call("O", "Dignity." + self.identItem + ".SetSuccessor", 4, "")
Next.previous = self.identItem
self.next = Next.identItem
if not self.end == Next.begin:
DisplayMessages.warning("O", "Dignity." + self.identItem + ".SetSuccessor",
"several times between " + self.displayItem + " and " + Next.displayItem )
self.warning = "his date of end (" + self.end + ") is not same as date of begin of his successor (" + Next.begin + ")"
Next.warning = "his date of begin (" + Next.begin + ") is not same as date of end of his predecessor (" + self.end + ")"
#
#
#
#
def GetQualifier(self):
qualifier = []
if not self.domain == '' :
qualifier.append( [True, 'P642', self.domain, ''] )
if not self.begin == '' :
qualifier.append( [True, 'P580', self.begin, ''] )
if not self.end == '' :
qualifier.append( [True, 'P582', self.end, ''] )
if not self.previous == '' :
qualifier.append( [True, Dignity.Previous[self.property], self.previous, ''] )
if not self.next == '' :
qualifier.append( [True, Dignity.Next[self.property], self.next, ''] )
return qualifier
#
#
#
#
def Record(self, ListDignity, Source):
DisplayMessages.call("O", "Dignity." + self.identItem + ".Record", 4, "Source=" + repr(Source))
DignityToAdd = True
for dignity in ListDignity:
if dignity == self:
DignityToAdd = False
#
if dignity.begin == '':
dignity.begin = self.begin
elif not dignity.begin == self.begin:
dignity.error = "Try to add a date of begin (" + self.begin + "), but a date is already set (" + dignity.begin + ")"
DisplayMessages.message("O", "Dignity." + self.identItem + ".Record", "Item : " + self.identItem + " ; " + dignity.error)
#
if dignity.end == '':
dignity.end = self.end
elif not dignity.end == self.end:
dignity.error = "Try to add a date of end (" + self.end + "), but a date is already set (" + dignity.end + ")"
DisplayMessages.message("O", "Dignity." + self.identItem + ".Record", "Item : " + self.identItem + " ; " + dignity.error)
#
if dignity.previous == '':
dignity.previous = self.previous
#
if dignity.next == '':
dignity.next = self.next
#
dignity.warning = self.warning
qualifiers = dignity.GetQualifier()
newClaim = Claim("info", dignity.property, dignity.identTarget, Source, qualifiers, dignity.error, dignity.warning)
#
if DignityToAdd:
ListDignity.append(self)
qualifiers = self.GetQualifier()
newClaim = Claim("add", self.property, self.identTarget, Source, qualifiers, self.error, self.warning)
return newClaim
def testHuman(NumTest, Format, Run, Ident, listClaimsWanted, Source):
print( "" )
print( "---------------------------" )
text = "test " + str(NumTest) + ":\n"
text += "testHuman(Format=" + str(Format) + \
", Run=" + str(Run) + \
", Ident=" + Ident + \
", claimsWanted=" + str(listClaimsWanted) + \
", Source=" + repr(Source) + ")"
print( text )
print( "" )
father = ""
mother = ""
spouse = ""
dictClaimsWanted = dict()
for claimWanted in listClaimsWanted:
Property = claimWanted[0]
Value = claimWanted[1]
if Property == 'P22':
father = Value
elif Property == 'P25':
mother = Value
elif Property == 'P26':
spouse = Value
else:
if not Property in dictClaimsWanted:
dictClaimsWanted[Property]= []
dictClaimsWanted[Property].append(Value)
DisplayMessages.debug("T", "testItem", 7, "dictClaimsWanted=" + str(dictClaimsWanted) )
Person = Human.Get(Ident, dictClaimsWanted, Source)
if not father == "":
Father = Human.Get(father, {}, Source)
Person.SetParent('H', Father, Source)
if not mother == "":
Mother = Human.Get(mother, {}, Source)
Person.SetParent('F', Mother, Source)
if not spouse == "":
Spouse = Human.Get(spouse, {}, Source)
Person.SetSpouse(Spouse, Source)
print( Person.display(Format, Run) )
if not father == "":
print( "" )
print( Father.display(Format, Run) )
if not mother == "":
print( "" )
print( Mother.display(Format, Run) )
if not spouse == "":
print( "" )
print( Spouse.display(Format, Run) )
siblings = Person.GetSibling()
for key in siblings:
print( "" )
print( siblings[key].display(Format, Run) )
Person2 = Human.Get(Ident, {}, Source)
#print(type(Person2))
def testTitled(NumTest, Format, Run, Ident, Title, Begin, End, Pred, Succ, source):
print( "" )
print( "---------------------------" )
text = "test " + str(NumTest) + ":\n"
text += "testTitled(Ident=" + Ident + \
", Title=" + str(Title) + \
", Begin=" + Begin + \
", End=" + End + \
", Pred=" + Pred + \
", Succ=" + Succ + \
", source=" + repr(source) + ")"
print( text )
print( "" )
ItemPred = Human.Get(Pred, {}, source)
ItemPers = Human.Get(Ident, {}, source)
ItemSucc = Human.Get(Succ, {}, source)
ItemPred.SetTitle(Title, '', Begin, source)
ItemPers.SetTitle(Title, Begin, End, source)
ItemSucc.SetTitle(Title, End, '', source)
ItemPred.SetSuccessorForTitle(ItemPers)
ItemPers.SetSuccessorForTitle(ItemSucc)
ItemPred.RecTitle(source)
ItemPers.RecTitle(source)
ItemSucc.RecTitle(source)
for item in [ItemPred , ItemPers, ItemSucc]:
print( item.display(Format, Run) )
print( "" )
def TestFunction(NumTest, Format, Run, IdFunction, IdPer1, IdPer2, IdPer3, IdPer4, Date1, Date2, Date3, Date4, Date5):
source = Source()
Function = Item.Get(IdFunction, {}, [ ['P31','Q4164871'], ['P1001','Q787336'], ['P571','circa 280'], ], source)
Human1 = Human.Get(IdPer1, {}, source)
Human1.SetFunction(Function, Date1, Date2)
Human2 = Human.Get(IdPer2, {}, source)
Human2.SetFunction(Function, Date2, Date3)
Human1.SetSuccessorForFunction(Human2)
Human1.RecordFunction(source)
Human3 = Human.Get(IdPer3, {}, source)
Human3.SetFunction(Function, Date3, Date4)
Human2.SetSuccessorForFunction(Human3)
Human2.RecordFunction(source)
Human4 = Human.Get(IdPer4, {}, source)
Human4.SetFunction(Function, Date4, Date5)
Human3.SetSuccessorForFunction(Human4)
Human3.RecordFunction(source)
Human4.RecordFunction(source)
for item in [Human1 , Human2, Human3, Human4]:
text = item.display(Format, Run)
try:
print( text )
except UnicodeEncodeError:
i = 0
while i < len(text):
print(text[:i])
i += 5
#print( "" )
#print( item.DisplayDignity() )
print( "" )
def mainTestHuman(*args):
param = CallParameter(*args)
NumCase = param.GetValue('case', 0)
Display = param.GetValue('display', 'short')
DisplayMessages.SetFileName("TestHuman.txt")
ListDisplay = Display.split(':')
Format = ListDisplay[0]
if len(ListDisplay) >= 2:
Run = ListDisplay[1]
else:
Run = 'simu'
source = Source()
source.AddReference('P248', 'Q13419312', True)
source.AddReference('P854', 'http://fmg.ac/Projects/MedLands/GASCONY.htm', True)
if NumCase == 0 or NumCase == 1:
testHuman(1, Format, Run, 'Q4133930', [], source)
if NumCase == 0 or NumCase == 2:
testHuman(2, Format, Run, 'Q729206', [ ['P22','Q2388146'] ], source)
if NumCase == 0 or NumCase == 3:
testHuman(3, Format, Run, 'Q729206', [ ['P22','Q1350089'] ], source)
if NumCase == 0 or NumCase == 4:
testHuman(4, Format, Run, 'Q21152443', [ ['P22','Q21153682'] ], source)
if NumCase == 0 or NumCase == 5:
testHuman(5, Format, Run, 'Q729206', [ ['P25','Q21106158'] ], source)
if NumCase == 0 or NumCase == 6:
testHuman(6, Format, Run, 'Q729206', [ ['P26','Q735117'] ], source)
if NumCase == 0 or NumCase == 7 or NumCase == 8:
titleBigorre = dict()
titleBigorre['H'] = Item('Q21123038', {}, [ ['P31','Q5737899'] ], source)
titleBigorre['F'] = Item('Q21513679', {}, [ ['P31','Q5737899'] ], source)
source.AddReference('P248', 'Q13419312', True)
source.AddReference('P854', 'http://fmg.ac/Projects/MedLands/GASCONY.htm', True)
if NumCase == 0 or NumCase == 7:
testTitled(7, Format, Run, 'Q4268340', titleBigorre, '956', '1000', 'Q2509451', 'Q4133930', source)
if NumCase == 0 or NumCase == 8:
testTitled(8, Format, Run, 'Q1964624', titleBigorre, '1080', '1095', 'Q2344475', 'Q1941773', source)
if NumCase == 0 or NumCase == 9:
TestFunction(9, Format, Run, 'Q15729481', 'Q1677853', 'Q105981', 'Q122374', 'Q91025', '1823', '1843', '1886', '1899', '1919')
if NumCase == 0 or NumCase == 10:
TestFunction(10, Format, Run, 'Q15729481', 'Q1563911', 'Q1372471', 'Q1563911', 'Q712264', '1504', '1543', '1548', '1550', '1551')
DisplayMessages.End()
if __name__ == "__main__":
mainTestHuman()