Package edu.isi.karma.modeling.semantictypes

Source Code of edu.isi.karma.modeling.semantictypes.SemanticTypeColumnModel

/*******************************************************************************
* Copyright 2012 University of Southern California
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This code was developed by the Information Integration Group as part
* of the Karma project at the Information Sciences Institute of the
* University of Southern California.  For more information, publications,
* and related projects, please see: http://www.isi.edu/integration
******************************************************************************/
package edu.isi.karma.modeling.semantictypes;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;

import edu.isi.karma.controller.update.SemanticTypesUpdate;
import edu.isi.karma.modeling.alignment.Alignment;
import edu.isi.karma.modeling.ontology.OntologyManager;
import edu.isi.karma.rep.alignment.Label;
import edu.isi.karma.rep.alignment.Node;
import edu.isi.karma.rep.alignment.NodeType;
import edu.isi.karma.util.Jsonizable;
import edu.isi.karma.util.Util;

public class SemanticTypeColumnModel implements Jsonizable {

  private final HashMap<String, Double> scoreMap = new HashMap<String, Double>();

  public SemanticTypeColumnModel(List<SemanticTypeLabel> labels) {
    for (int i = 0; i < labels.size(); i++) {
      SemanticTypeLabel label = labels.get(i);
      scoreMap.put(label.getLabel(), (double)label.getScore());
    }
  }

  public HashMap<String, Double> getScoreMap() {
    return scoreMap;
  }

  public Double getScoreForLabel(String label) {
    return scoreMap.get(label);
  }

  public void write(JSONWriter writer) throws JSONException {
    writer.object();
    writer.array();
    for (String label : scoreMap.keySet()) {
      writer.object();
      writer.key(SemanticTypesUpdate.JsonKeys.FullType.name()).value(label);
      writer.key("probability").value(scoreMap.get(label));
      writer.endObject();
    }
    writer.endArray();
    writer.endObject();
  }

  public JSONObject getAsJSONObject(OntologyManager ontMgr, Alignment alignment) throws JSONException {
    JSONObject obj = new JSONObject();
    JSONArray arr = new JSONArray();
   
    /** Create a set of node ids of internal nodes of Steiner Tree **/
    Set<String> steinerTreeNodeIds = new HashSet<String>();
    if (alignment != null && !alignment.isEmpty()) {
      for (Node node: alignment.getSteinerTree().vertexSet()) {
        if (node.getType() == NodeType.InternalNode) {
          steinerTreeNodeIds.add(node.getId());
        }
      }
    }

    // Need to sort
    HashMap<String, Double> sortedMap = Util.sortHashMap(scoreMap);

    for (String label : sortedMap.keySet()) {
      double probability = scoreMap.get(label);
      // Check if the type contains domain
      if(label.contains("|")){
        Label domainURI = ontMgr.getUriLabel(label.split("\\|")[0]);
        Label typeURI = ontMgr.getUriLabel(label.split("\\|")[1]);
        if(domainURI == null || typeURI == null)
          continue;
       
        String clazzLocalNameWithPrefix = domainURI.getDisplayName();
        if(domainURI.getPrefix() == null)
          clazzLocalNameWithPrefix = domainURI.getUri() + "/" + domainURI.getLocalName();
         
        int graphLastIndex = alignment.getLastIndexOfNodeUri(domainURI.getUri());
        if (graphLastIndex == -1) { // No instance present in the graph
          insertSemanticTypeSuggestion(arr, clazzLocalNameWithPrefix + "1 (add)",
              domainURI.getUri(), domainURI.getUri() + "1",
              typeURI.getDisplayName(), label.split("\\|")[1], probability);
        } else {
          boolean hasLastNodeFromSteinerTree = false;
          for (int i=1; i<= graphLastIndex; i++) {
           
            if (steinerTreeNodeIds.contains(domainURI.getUri() + i)) {
              insertSemanticTypeSuggestion(arr, clazzLocalNameWithPrefix + i,
                  domainURI.getUri(),
                  domainURI.getUri() + i,
                  typeURI.getDisplayName(), label.split("\\|")[1], probability);
              if (i == graphLastIndex)
                hasLastNodeFromSteinerTree = true;
            } else {
              Node graphNode = alignment.getNodeById(domainURI.getUri() + i);
              if (graphNode != null)
                insertSemanticTypeSuggestion(arr, clazzLocalNameWithPrefix + i + " (add)",
                  graphNode.getUri(),
                  graphNode.getId(),
                  typeURI.getDisplayName(), label.split("\\|")[1], probability);
            }
          }
          // Add an option to add one more node for the domain
          if (hasLastNodeFromSteinerTree)
            insertSemanticTypeSuggestion(arr, clazzLocalNameWithPrefix + (graphLastIndex+1) + " (add)",
                domainURI.getUri(), domainURI.getUri() + (graphLastIndex+1),
              typeURI.getDisplayName(), label.split("\\|")[1], probability);
        }
      } else {
        Label typeURI = ontMgr.getUriLabel(label);
        if(typeURI == null)
          continue;
       
        String clazzLocalNameWithPrefix = typeURI.getDisplayName();
       
        int graphLastIndex = alignment.getLastIndexOfNodeUri(typeURI.getUri());
        if (graphLastIndex == -1) { // No instance present in the graph
          insertSemanticTypeSuggestion(arr, "", "", clazzLocalNameWithPrefix + "1 (add)", typeURI.getUri(), typeURI.getUri() + "1", probability);
        } else {
          boolean hasLastNodeFromSteinerTree = false;
          for (int i=1; i<= graphLastIndex; i++) {
            if (steinerTreeNodeIds.contains(typeURI.getUri() + (graphLastIndex))) {
              insertSemanticTypeSuggestion(arr, "", "", clazzLocalNameWithPrefix + i, typeURI.getUri(), typeURI.getUri() + i, probability);
            } else {
              Node graphNode = alignment.getNodeById(typeURI.getUri() + i);
              if (graphNode != null)
                insertSemanticTypeSuggestion(arr, "", "", clazzLocalNameWithPrefix + i + " (add)", graphNode.getUri(), graphNode.getId(), probability);
            }
          }
          // Add an option to add one more node for the domain
          if (hasLastNodeFromSteinerTree)
            insertSemanticTypeSuggestion(arr, "", "", clazzLocalNameWithPrefix + (graphLastIndex+1) + " (add)", typeURI.getUri(), typeURI.getUri() + (graphLastIndex+1), probability);
        }
      }
    }
    obj.put("Labels", arr);
    return obj;
  }
 
  private void insertSemanticTypeSuggestion(JSONArray arr, String domainDisplayLabel,
      String domainUri, String domainId, String typeDisplayLabel, String type, double probability) throws JSONException {
    JSONObject typeObj = new JSONObject();
    typeObj.put(SemanticTypesUpdate.JsonKeys.DisplayDomainLabel.name(), domainDisplayLabel);
    typeObj.put(SemanticTypesUpdate.JsonKeys.DomainUri.name(), domainUri);
    typeObj.put(SemanticTypesUpdate.JsonKeys.DomainId.name(), domainId);
    typeObj.put(SemanticTypesUpdate.JsonKeys.DisplayLabel.name(), typeDisplayLabel);
    typeObj.put(SemanticTypesUpdate.JsonKeys.FullType.name(), type);
    typeObj.put("Probability", probability);
    arr.put(typeObj);
  }

}
TOP

Related Classes of edu.isi.karma.modeling.semantictypes.SemanticTypeColumnModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.