Skip to content

Global graph optimizer step by step

Roberto Zanoli edited this page May 20, 2015 · 1 revision

In order to use the tool:

  1. Define your graph as a DirectedOntologyGraph

  2. Choose an edge learner

  3. Provide known entailing and non-entailing edges(optional).

  4. Define score for unknown edges (default, 0).

  5. Create an UntypedPredicateGraphLearner instance

  6. Apply the 'learn' method, in order to get a set of optimized components

The following program apply the EfficientlyCorrectHtlLearner on a given EntailmentGraphRaw of the transduction layer:

	public EntailmentGraphCollapsed optimizeGraph(EntailmentGraphRaw workGraph, Double confidenceThreshold) throws GraphOptimizerException{
		DirectedOntologyGraph graph = new DirectedOneMappingOntologyGraph("work graph");

		/* Commented out: 
		 * Here we only add nodes connected by edges in the graph, and we consider all existing edges to denote entailment. 
		 * We should 1) add "orphan" nodes, not connected with any other node.
		 *           2) check whether edges hold ENTAILMENT relation or not  
		 *           3) consider adding edges NOT present in the graph as non-entailment edges with some confidence.
		 * In addition, we should consider all the edges with confidence < confidenceThreshold as non-entailment    
		 */

		HashMap<String, Integer> nodeIndex = new HashMap<String, Integer>(); 
		int i = 1;
		for (EntailmentUnit node : workGraph.vertexSet()) {
			nodeIndex.put(node.getText(), i);
			RelationNode rNode = new RelationNode(i++,node.getText());
			graph.addNode(rNode);
		}
		for (EntailmentRelation edge : workGraph.edgeSet()) {
			Double confidence = detectConfidence(workGraph, edge.getSource(), edge.getTarget(), confidenceThreshold);
			if (confidence == TEDecision.CONFIDENCE_NOT_AVAILABLE)
				throw new GraphOptimizerException("Unavaliable score was detected.");
			RelationNode sourceNode = new RelationNode(nodeIndex.get(edge.getSource().getText()),edge.getSource().getText());
			RelationNode targetNode = new RelationNode(nodeIndex.get(edge.getTarget().getText()),edge.getTarget().getText());
			try {
				graph.addEdge(new RuleEdge(sourceNode, targetNode,confidence));
			} catch (Exception e) {
				throw new GraphOptimizerException("Problem when adding edge "+edge.getSource().getText()+" -> "+edge.getTarget().getText()+" with confidence = "+confidence+".\n"+e);
			}
		}
		
		Set<AbstractOntologyGraph> componnetGraphs;
		try {
			componnetGraphs = graphLearner.learn(graph);
		} catch (Exception e) {
			throw new GraphOptimizerException("Problem with global optimization.\n"+ExceptionUtils.getFullStackTrace(e));
		}
		EntailmentGraphCollapsed ret = new EntailmentGraphCollapsed();

		/* Commented out: 
		 * Here we only add nodes connected by edges in the optimized graph, while we need all the nodes from original graph to be covered in the output. 
		 * We should 1) add "orphan" nodes, not connected with any other node.
		 *           2) collapse paraphrasing nodes into equivalence classes
		 */
		EntailmentGraphRaw tmpRawGraph = new EntailmentGraphRaw();
		for (EntailmentUnit node : workGraph.vertexSet()){
			tmpRawGraph.addVertex(node);
		}
		for (AbstractOntologyGraph componnetGraph : componnetGraphs) {
			for (AbstractRuleEdge componentEdge : componnetGraph.getEdges()) {
				if (componentEdge.score() >= confidenceThreshold) { //TODO: do we need the threshold here? Should it be confidenceThreshold or just 0 (to retain only entailment edges)? 				
					EntailmentUnit source = workGraph.getVertex(componentEdge.from().description());
					EntailmentUnit target=  workGraph.getVertex(componentEdge.to().description());
					EntailmentRelation edge = new EntailmentRelation(source, target, new TEDecisionWithConfidence(componentEdge.score(), DecisionLabel.Entailment));
					tmpRawGraph.addEdge(source, target, edge);
				}
			}
		}
		
		ret = new SimpleGraphOptimizer().optimizeGraph(tmpRawGraph, 0.0); //collapse paraphrasing nodes
		return ret;
	}
Clone this wiki locally