St Cloud State University Logic Inferencing Program Python Code Project
Question Description
I’m working on a Python question and need a sample draft to help me understand better.
Please write a logic inferencing program using Python which can handle family relationship. The input files are (t60,t61,t69) given below. Each input file contains lines of facts (each ended with period .) of family relationships and lines of questions (each ended with question mark ?) about family relationships in any order. The questions should be answered based on all previous facts in the same file. The facts always have the format <person1>s <relation> is <person2>, e.g., Marthas husband is John. The questions always have the format Who is <person>s <relation>?, e.g., Who is Johns father? Since some names can be either female or male, so you cannot infer the gender of a person by her/his name, instead, you need to infer gender by the family relationship. All the possible relationships are mother, father, husband, wife, son, daughter, brother, and sister. Assume each one has at most 1 mother and/or 1 father, and each one has at most one spouse (husband or wife), but one can have any number of siblings.
Here is the pseudocode for the functions. For each test case, call relation(f), and relation() needs to call propagate before answering every query.
genders = {
‘mother’:’F’, ‘father’:’M’, ‘son’:’M’, ‘daughter’:’F’, ‘brother’:’M’,
‘sister’:’F’, ‘husband’:’M’, ‘wife’:’F’
}
reverses = {
‘mother’:’child’, ‘father’:’child’, ‘son’:’parent’, ‘daughter’:’parent’,
‘brother’:’sibling’, ‘sister’:’sibling’, ‘husband’:’spouse’, ‘wife’:’spouse’
}
supers = {
‘mother’:’parent’, ‘father’:’parent’, ‘son’:’child’, ‘daughter’:’child’,
‘brother’:’sibling’, ‘sister’:’sibling’, ‘husband’:’spouse’, ‘wife’:’spouse’
}
def propogate(hash):
for all person, add each person’s child’s siblings as the person’s children
for all person, add each person’s spouse’s children as the person’s children
for all person, add each person’s parent’s spouse as a person’s parent
for all person, add each person’s sibling’s parents as a person’s parent
def relation(f):
hash = {}
hash[“gender”] = {}
hash[“parent”] = {}
hash[“spouse”] = {}
hash[“child”] = {}
hash[“sibling”] = {}
print(f, ‘:’)
for each line from input file f:
if line ends with ‘.’: # handle fact
add ‘super’ relationship (refer supers dict above)
check if conflict genders, if no, set gender, otherwise, return error
handle relationship of wife/husband by checking genders and
set genders for spouse
add ‘reverse’ relationship (refer reverses dict above)
elif line ends with ‘?’: # handle query
call propogate(hash)
if supers[relationship] of the queried person match the gender:
output answers
else:
output ‘Unknown’
else:
return(“ERROR”)
else:
return(“ERROR”)
The t69 is an illegal test data, so you need report error and handle the next file is exists. Both t60.dat and t61.dat is legal tests, and t61.dat also test for whitespace convention. For t60.dat, you should answer the first questions with Martha and the second question with Edward. For t60.dat, you should answer Martha and Unknown.
t60
Edward’s son is David.
John’s daughter is Mary.
Martha’s husband is John.
Mary’s sister is Catherine.
Who is Catherine’s mother?
David’s brother is John.
Edward’s wife is Jennifer.
Who is John’s father?
t 61
Edward’s son is David.
John’s daughter is Mary.
Martha’s husband is John .
Mary’s sister is Catherine.
Who is Catherine’s mother ?
Edward’s wife is Jennifer.
Who is John’s father?
t69
123 Edward’s son is David.
John’s daughter is Mary. Martha’s husband is John.
Mary’s uncle Catherine.
Catherine’s mother?
David’s brother is John.
Edward’s wife is Jennifer.
Who is John’s father?
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."