def assertComponentAddition(self, componentAdditionJSON, initiatingClauseHash=None): if 'stem' in componentAdditionJSON['component_addition']['target']['component']['stem']: stems = self.retrieveComponent(componentAdditionJSON['component_addition']['target']['component']['stem']['stem'], componentAdditionJSON['component_addition']['target']['component']['stem']['branch'], assertBranches=True, initiatingClauseHash=initiatingClauseHash) if isinstance(stems, set): stem = self.context.findLastReferenced(stems) #raise Exception('assertComponentAssignment: Too many potential stems') else: stem = stems elif 'concept' in componentAdditionJSON['component_addition']['target']['component']['stem']: stem = self.assertConcept(componentAdditionJSON['component_addition']['target']['component']['stem'], initiatingClauseHash) else: raise Exception('assertComponentAddition: Unknown component structure') branchPhrase = utilities.camelCase(componentAdditionJSON['component_addition']['target']['component']['branch']) branches = self.retrieveComponent(componentAdditionJSON['component_addition']['target']['component']['stem'], componentAdditionJSON['component_addition']['target']['component']['branch'], assertBranches=True, initiatingClauseHash=initiatingClauseHash) def checkIfNegativeAssignment(concept): if re.match('^!', concept['concept']): affirmativeConcept = concept['concept'][1:] self.removeComponentAssignment(stem, branchPhrase, branches, affirmativeConcept, initiatingClauseHash) return True else: return False if isinstance(componentAdditionJSON['component_addition']['addition'], list): componentAdditionJSON['component_addition']['addition'][:] = [x for x in componentAdditionJSON['component_addition']['addition'] if not checkIfNegativeAssignment(x)] else: if checkIfNegativeAssignment(componentAdditionJSON['component_addition']['addition']): return unspecifiedBranches = [] if branches: if isinstance(branches, set): for branch in branches: if branch.name == branchPhrase: unspecifiedBranches.append(branch) else: if branch.name == branchPhrase: unspecifiedBranches.append(branches) assignments = list() uninstantiatedAssignments = list() if isinstance(componentAdditionJSON['component_addition']['addition'], list): for concept in componentAdditionJSON['component_addition']['addition']: x = self.context.queryNounPhrases(concept['concept']) if x: assignments.extend(x) else: uninstantiatedAssignments.append(concept['concept']) else: x = self.context.queryNounPhrases(componentAdditionJSON['component_addition']['addition']['concept']) if x: assignments.extend(x) else: uninstantiatedAssignments.append(componentAdditionJSON['component_addition']['addition']['concept']) for uninstantiatedAssignment in uninstantiatedAssignments: assignment = self.context.newNounPhrase(uninstantiatedAssignment, initiatingClauseHash) #assignment.classify(utilities.sanitize(branchPhrase).split()[-1]) assignments.append(assignment) for assignment in assignments: if len(unspecifiedBranches) > 0: unspecifiedBranch = unspecifiedBranches.pop() unspecifiedBranch.name = 'unspecified' + unspecifiedBranch.name[0].upper() + unspecifiedBranch.name[1:] self.context.mergeConcepts(assignment, unspecifiedBranch, initiatingClauseHash) else: self.context.setComponent(stem, branchPhrase, assignment, initiatingClauseHash)
def __init__(self, name=None, type=None, bootstrapVocabulary=None): if name: if getattr(self, "isQuantity", False): self.name = str(name) else: self.name = utilities.camelCase(name) else: self.name = None if type: self.classify(utilities.sanitize(type)) if bootstrapVocabulary == True: Concept.bootstrapVocabulary = True Concept.wordnetClassifier = WordNetClassifier() Concept.taxonomy.classifiers.append(Concept.wordnetClassifier) elif bootstrapVocabulary == False: Concept.bootstrapVocabulary = False
def synonyms(self, phrase=None): if not phrase: if not self.name: return None else: phrase = self.name phrase = utilities.camelCase(phrase) listOfSetsWithPhrase = [] for key in self.thesaurus: if phrase in self.thesaurus[key]: if self.thesaurus[key] not in listOfSetsWithPhrase: listOfSetsWithPhrase.append(self.thesaurus[key]) if len(listOfSetsWithPhrase) == 0: self.thesaurus[phrase] = {phrase} elif len(listOfSetsWithPhrase) > 1: mergedSet = set.union(*listOfSetsWithPhrase) for element in mergedSet: self.thesaurus[element] = mergedSet elif phrase not in self.thesaurus: self.thesaurus[phrase] = listOfSetsWithPhrase[0] return self.thesaurus[phrase]
def __init__(self, concept, context): self.name = concept.name self.hashcode = None for hash in context.conceptHashTable: if context.conceptHashTable[hash] is concept: self.hashcode = hash self.synonyms = concept.synonyms().copy() self.parents = concept.parents() self.ancestors = concept.ancestors() self.descendants = concept.descendants() if utilities.camelCase(concept.name) in self.synonyms: self.synonyms.remove(utilities.camelCase(concept.name)) self.states = list() if concept in context.stateGraph: descriptors = context.stateGraph.successors(concept) for descriptor in descriptors: self.states.append((descriptor.name, 100)) if concept in context.potentialActionGraph: potentialDescriptorEdges = context.potentialStateGraph.out_edges(concept, data=True) if concept in context.potentialStateGraph else [] for potentialDescriptorEdge in potentialDescriptorEdges: self.states.append((potentialDescriptorEdge[1].name, int(potentialDescriptorEdge[2]['weight']))) def combineStates(stateTuples): evidence = dict() for stateTuple in stateTuples: if not stateTuple[0] in evidence: evidence[stateTuple[0]] = int(stateTuple[1]) else: if evidence[stateTuple[0]] < 100: evidence[stateTuple[0]] = evidence[stateTuple[0]] + int(stateTuple[1]) else: evidence[stateTuple[0]] = 100 states = list() for state in evidence: states.append((state, evidence[state])) return states self.states = combineStates(self.states) self.components = list() if concept in context.componentGraph: for componentEdge in context.componentGraph.out_edges(concept, data=True): self.components.append((componentEdge[2]['label'], componentEdge[1].name, 100)) if concept in context.potentialComponentGraph: potentialComponentEdges = context.potentialComponentGraph.out_edges(concept, data=True) if concept in context.potentialComponentGraph else [] for potentialComponentEdge in potentialComponentEdges: self.components.append((potentialComponentEdge[2]['label'], potentialComponentEdge[1].name, potentialComponentEdge[2]['weight'])) def combineComponents(componentTuples): evidence = dict() for componentTuple in componentTuples: if not componentTuple[0] in evidence: evidence[componentTuple[0]] = dict() if not componentTuple[1] in evidence[componentTuple[0]]: evidence[componentTuple[0]][componentTuple[1]] = int(componentTuple[2]) else: if evidence[componentTuple[0]][componentTuple[1]] < 100: evidence[componentTuple[0]][componentTuple[1]] = evidence[componentTuple[0]][componentTuple[1]] + int(componentTuple[2]) else: evidence[componentTuple[0]][componentTuple[1]] = 100 components = list() for branchPhrase in evidence: for component in evidence[branchPhrase]: components.append((branchPhrase, component, evidence[branchPhrase][component])) return components self.components = combineComponents(self.components) self.componentOf = list() if concept in context.componentGraph: for componentEdge in context.componentGraph.in_edges(concept, data=True): self.componentOf.append((componentEdge[2]['label'], componentEdge[0].name, 100)) if concept in context.potentialComponentGraph: potentialComponentOfEdges = context.potentialComponentGraph.in_edges(concept, data=True) if concept in context.potentialComponentGraph else [] for potentialComponentOfEdge in potentialComponentOfEdges: self.componentOf.append((potentialComponentOfEdge[2]['label'], potentialComponentOfEdge[0].name, potentialComponentOfEdge[2]['weight'])) self.componentOf = combineComponents(self.componentOf) self.actions = list() if concept in context.actionGraph: acts = set(context.actionGraph.neighbors(concept)) for actor_act in context.actionGraph.out_edges(concept): for act_target in context.actionGraph.out_edges(actor_act[1]): self.actions.append((act_target[0].name, act_target[1].name, 100)) acts.remove(act_target[0]) for act in acts: self.actions.append((act.name, 'None', 100)) if concept in context.potentialActionGraph: potentialActs = set(map(lambda x: x.name, context.potentialActionGraph.neighbors(concept))) for potential_actor_act in context.potentialActionGraph.out_edges(concept): for potential_act_target in context.potentialActionGraph.out_edges(potential_actor_act[1], data=True): self.actions.append((potential_act_target[0].name, potential_act_target[1].name, potential_act_target[2]['weight'])) if potential_act_target[0].name in potentialActs: potentialActs.remove(potential_act_target[0].name) for potentialAct in potentialActs: weight = context.potentialActionGraph.in_edges(potentialAct, data=True)[0][2]['weight'] self.actions.append((potentialAct.name, 'None', weight)) def combineActions(actionTuples): evidence = dict() for actionTuple in actionTuples: if not actionTuple[0] in evidence: evidence[actionTuple[0]] = dict() if not actionTuple[1] in evidence[actionTuple[0]]: evidence[actionTuple[0]][actionTuple[1]] = int(actionTuple[2]) else: if evidence[actionTuple[0]][actionTuple[1]] < 100: evidence[actionTuple[0]][actionTuple[1]] = evidence[actionTuple[0]][actionTuple[1]] + int(actionTuple[2]) else: evidence[actionTuple[0]][actionTuple[1]] = 100 actions = list() for act in evidence: for target in evidence[act]: actions.append((act, target, evidence[act][target])) return actions self.actions = combineActions(self.actions) self.actedOnBy = list() if concept in context.actionGraph: for act_target in context.actionGraph.in_edges(concept): for actor_act in context.actionGraph.in_edges(act_target[0]): self.actedOnBy.append((act_target[0].name, actor_act[0].name, 100)) if concept in context.potentialActionGraph: for potential_act_target in context.potentialActionGraph.in_edges(concept): for potential_actor_act in context.potentialActionGraph.in_edges(potential_act_target[0], data=True): self.actedOnBy.append((potential_act_target[0].name, potential_actor_act[0].name, potential_actor_act[2]['weight'])) self.actedOnBy = combineActions(self.actedOnBy)