Problem 1 Please write a logic inferencing program using Python which can handle family relationship name it p6.py or p6.ipynb (prefer python). The input files are t6*.dat in the same directory as...

1 answer below »
Please see instructions for this assignment in p6


Problem 1 Please write a logic inferencing program using Python which can handle family relationship name it p6.py or p6.ipynb (prefer python). The input files are t6*.dat in the same directory as your p6.py or p6.ipynb. 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 “’s is ”, e.g., “Martha’s husband is John”. The questions always have the format “Who is ’s ?”, e.g., “Who is John’s 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 to make your life easier. 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”.
Answered Same DayOct 11, 2021

Answer To: Problem 1 Please write a logic inferencing program using Python which can handle family relationship...

Sudipta answered on Oct 14 2021
146 Votes
{
"cells": [
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"coll
apsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123 Edward's son is David.\n",
"John's daughter is Mary. Martha's husband is John.\n",
"Mary's uncle Catherine.\n",
"Catherine's mother?\n",
"David's brother is John.\n",
"Edward's wife is Jennifer.\n",
"Who is John's father?\n",
"ERROR\n"
]
}
],
"source": [
"import csv\n",
"import pandas as pd\n",
"data=[i.strip() for i in open('t69.dat').readlines()]\n",
"for i in data:\n",
" print(i)\n",
"print(\"ERROR\") \n"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false,
"scrolled": true
},
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here