def parseFactorFromFileDict(fileDict, variableDomainsDict=None, prefix=None): if prefix is None: prefix = '' if variableDomainsDict is None: variableDomainsDict = {} for line in fileDict['variableDomainsDict'].split('\n'): variable, domain = line.split(' : ') variableDomainsDict[variable] = domain.split(' ') # construct a dict from names to factors and # load a factor from the test file for each unconditionedVariables = [] for variable in fileDict[prefix + "unconditionedVariables"].split(' '): unconditionedVariable = variable.strip() unconditionedVariables.append(unconditionedVariable) conditionedVariables = [] for variable in fileDict[prefix + "conditionedVariables"].split(' '): conditionedVariable = variable.strip() if variable != '': conditionedVariables.append(conditionedVariable) if 'constructRandomly' not in fileDict or fileDict[ 'constructRandomly'] == 'False': currentFactor = bayesNet.Factor(unconditionedVariables, conditionedVariables, variableDomainsDict) for line in fileDict[prefix + 'FactorTable'].split('\n'): assignments, probability = line.split(" = ") assignmentList = [ assignment for assignment in assignments.split(', ') ] assignmentsDict = {} for assignment in assignmentList: var, value = assignment.split(' : ') assignmentsDict[var] = value currentFactor.setProbability(assignmentsDict, float(probability)) elif fileDict['constructRandomly'] == 'True': currentFactor = bayesNet.constructAndFillFactorRandomly( unconditionedVariables, conditionedVariables, variableDomainsDict) return currentFactor
def parseFactorFromFileDict(fileDict, variableDomainsDict=None, prefix=None): if prefix is None: prefix = '' if variableDomainsDict is None: variableDomainsDict = {} for line in fileDict['variableDomainsDict'].split('\n'): variable, domain = line.split(' : ') variableDomainsDict[variable] = domain.split(' ') # construct a dict from names to factors and # load a factor from the test file for each unconditionedVariables = [] for variable in fileDict[prefix + "unconditionedVariables"].split(' '): unconditionedVariable = variable.strip() unconditionedVariables.append(unconditionedVariable) conditionedVariables = [] for variable in fileDict[prefix + "conditionedVariables"].split(' '): conditionedVariable = variable.strip() if variable != '': conditionedVariables.append(conditionedVariable) if 'constructRandomly' not in fileDict or fileDict['constructRandomly'] == 'False': currentFactor = bayesNet.Factor(unconditionedVariables, conditionedVariables, variableDomainsDict) for line in fileDict[prefix + 'FactorTable'].split('\n'): assignments, probability = line.split(" = ") assignmentList = [assignment for assignment in assignments.split(', ')] assignmentsDict = {} for assignment in assignmentList: var, value = assignment.split(' : ') assignmentsDict[var] = value currentFactor.setProbability(assignmentsDict, float(probability)) elif fileDict['constructRandomly'] == 'True': currentFactor = bayesNet.constructAndFillFactorRandomly(unconditionedVariables, conditionedVariables, variableDomainsDict) return currentFactor