@Test
 public void testSave() throws BadRequestException {
   folderResourceMap.save();
   Model model = ModelFactory.createDefaultModel();
   model.read(folderResourceMap.getGraphAsInputStream(RDFFormat.RDFXML), null);
   Resource r = model.getResource(folderResourceMap.getUri().toString());
   Assert.assertTrue(r.hasProperty(ORE.describes, model.getResource(folder.getUri().toString())));
   Assert.assertTrue(r.hasProperty(RDF.type, ORE.ResourceMap));
 }
示例#2
0
  /**
   * Builds a model from a turtle representation in a file
   *
   * @param path
   */
  protected Model readModel(String path) {
    Model model = null;
    if (path != null) {
      model = ModelFactory.createDefaultModel();
      InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);

      String fakeUri = "http://w3c.github.io/ldp-testsuite/fakesubject";
      // Even though null relative URIs are used in the resource representation file,
      // the resulting model doesn't keep them intact. They are changed to "file://..." if
      // an empty string is passed as base to this method.
      model.read(inputStream, fakeUri, "TURTLE");

      // At this point, the model should contain a resource named
      // "http://w3c.github.io/ldp-testsuite/fakesubject" if
      // there was a null relative URI in the resource representation
      // file.
      Resource subject = model.getResource(fakeUri);
      if (subject != null) {
        ResourceUtils.renameResource(subject, "");
      }

      try {
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return model;
  }
示例#3
0
  public Spec(final String spec_filename, final String spec_uri) {
    if (null == spec_filename) {
      throw new RuntimeException("Can't create spec and filename was null");
    }
    Logger log = Logger.getLogger(Spec.class.getName());

    try {
      log.log(Level.INFO, "Attempting to create Spec from {0}", spec_filename);
      Path pt = new Path(spec_filename);
      FileSystem fs = FileSystem.get(new Configuration());
      log.log(Level.INFO, "Opening {0}", spec_filename);
      FSDataInputStream stream = fs.open(pt);
      _model = ModelFactory.createDefaultModel();
      //            _model.setNsPrefixes(getPrefixes());
      RDFReader reader = this._model.getReader("TURTLE");
      log.log(Level.INFO, "Reading {0}", spec_filename);
      reader.read(this._model, stream, "http://put.a.base.in.your.spec/");
      log.log(Level.INFO, "Successfully created Spec from {0}", spec_filename);
      _specResource = _model.getResource(spec_uri);
      if (!_model.containsResource(_specResource)) {
        log.log(
            Level.WARNING,
            "Spec file {0} does not contain spec {1}",
            new Object[] {spec_filename, spec_uri});
        throw new RuntimeException(
            "Spec file " + spec_filename + " does not contain spec " + spec_uri);
      }
    } catch (IOException ex) {
      log.log(Level.SEVERE, "Caught IOException, converting to RuntimeException", ex);
      throw new RuntimeException(ex);
    }
  }
  /**
   * If the {@link ConfigurationProperties} has a name for the initial admin user, create the user
   * and add it to the model.
   */
  protected void createInitialAdminUser(Model model) {
    String initialAdminUsername = ConfigurationProperties.getProperty("initialAdminUser");
    if (initialAdminUsername == null) {
      return;
    }

    // A hard-coded MD5 encryption of "defaultAdmin"
    String initialAdminPassword = "******";

    String vitroDefaultNs = DEFAULT_DEFAULT_NAMESPACE;

    Resource user = model.createResource(vitroDefaultNs + "defaultAdminUser");
    model.add(
        model.createStatement(
            user,
            model.createProperty(VitroVocabulary.RDF_TYPE),
            model.getResource(VitroVocabulary.USER)));
    model.add(
        model.createStatement(
            user,
            model.createProperty(VitroVocabulary.USER_USERNAME),
            model.createTypedLiteral(initialAdminUsername)));
    model.add(
        model.createStatement(
            user,
            model.createProperty(VitroVocabulary.USER_MD5PASSWORD),
            model.createTypedLiteral(initialAdminPassword)));
    model.add(
        model.createStatement(
            user,
            model.createProperty(VitroVocabulary.USER_ROLE),
            model.createTypedLiteral("role:/50")));
  }
示例#5
0
  @Test
  public void testUpdate() throws Exception {
    Resource rhmRes = model.getResource(FakeRDFModel.rhm);
    Property name = model.getProperty(foaf + "name");

    Statement nameSt = rhmRes.listProperties(name).nextStatement();
    String originalName = nameSt.getLiteral().getString();

    // assume update is always first remove and then add
    model.remove(nameSt);
    model.add(rhmRes, name, "TESTNAME");

    assert changes.size() == 2;
    assert rhmRes.listProperties(name).toList().size() == 1;
    assert rhmRes.listProperties(name).nextStatement().getLiteral().getString().equals("TESTNAME");

    changes.undo();

    assert changes.size() == 2;
    assert rhmRes.listProperties(name).toList().size() == 1;
    assert rhmRes
        .listProperties(name)
        .nextStatement()
        .getLiteral()
        .getString()
        .equals(originalName);

    changes.redo();

    assert changes.size() == 2;
    assert rhmRes.listProperties(name).toList().size() == 1;
    assert rhmRes.listProperties(name).nextStatement().getLiteral().getString().equals("TESTNAME");
  }
示例#6
0
  @Test
  public void testDelete() throws Exception {
    Resource rhmRes = model.getResource(FakeRDFModel.rhm);

    Model toRemove = ModelFactory.createDefaultModel();
    toRemove.add(model.listStatements(rhmRes, null, (RDFNode) null));
    toRemove.add(model.listStatements(null, null, rhmRes));

    model.remove(toRemove);

    Model foo = changes.get(0).getChange();

    assert changes.size() == 1;
    assert !model.containsResource(rhmRes);

    changes.undo();

    assert changes.size() == 1;
    assert model.containsResource(rhmRes);
    assert model.containsAll(foo);

    changes.redo();

    assert changes.size() == 1;
    assert !model.containsResource(rhmRes);
  }
  public static void main(String[] args) {
    Store store = HBaseRdfFactory.connectStore("Store/hbaserdf-simple.ttl");
    store.getTableFormatter().format();

    Model model = HBaseRdfFactory.connectDefaultModel(store);

    model.add(
        model.createResource("http://example.org/person#John"),
        VCARD.FN,
        model.asRDFNode(Node.createLiteral("John Smith")));
    model.add(
        model.createResource("http://example.org/person#John"),
        VCARD.EMAIL,
        model.asRDFNode(Node.createLiteral("*****@*****.**")));
    model.add(
        model.createResource("http://example.org/person#Jim"),
        VCARD.FN,
        model.asRDFNode(Node.createLiteral("Jim Mason")));
    model.add(
        model.createResource("http://example.org/person#Jim"),
        VCARD.EMAIL,
        model.asRDFNode(Node.createLiteral("*****@*****.**")));
    model.add(
        model.createResource("http://example.org/person#Bob"),
        VCARD.FN,
        model.asRDFNode(Node.createLiteral("Bob Brown")));
    model.add(
        model.createResource("http://example.org/person#Bob"),
        VCARD.EMAIL,
        model.asRDFNode(Node.createLiteral("*****@*****.**")));

    StmtIterator iter = model.listStatements();
    while (iter.hasNext()) {
      System.out.println(iter.next().toString());
    }

    iter = model.getResource("http://example.org/person#John").listProperties();
    while (iter.hasNext()) {
      System.out.println(iter.next().toString());
    }

    ResIterator resIter = model.listSubjects();
    while (resIter.hasNext()) {
      System.out.println(resIter.next().toString());
    }

    String query =
        " PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> "
            + " SELECT ?x "
            + " WHERE "
            + " { "
            + " 		<http://example.org/person#John> vcard:FN ?x "
            + " } ";

    QueryExecution qe = QueryExecutionFactory.create(query, model);
    ResultSet rs = qe.execSelect();
    ResultSetFormatter.out(rs);
  }
 public static EConcept getEConcept(QuerySolution qs, Model model) {
   EConcept con = new EConcept();
   String uri = qs.get("?concept").toString().trim();
   String id = model.getResource(uri).getLocalName();
   String name = qs.get("?c_name").toString().trim();
   con.setCid(id);
   con.setName(StringExchanger.getCommonString(name));
   return con;
 }
  /** @param model */
  public PreSummaryStatistics(Model model) {
    super(model);

    for (String out : nonStrProcessorOutputs) {
      if (WfDescRdfUtils.isTopLevelVisible(model.getResource(out), model)) {
        topLevelVisibleOutputPorts.add(out);
      }

      if (WfDescRdfUtils.isBookmarked(model.getResource(out), model)) {
        bookmarkedIntermediaryProcessorOutputPorts.add(out);
      }
    }

    groundTruthImportant.addAll(topLevelVisibleOutputPorts);
    groundTruthImportant.addAll(bookmarkedIntermediaryProcessorOutputPorts);

    groundTruthUnimportant.addAll(nonStrProcessorOutputs);
    groundTruthUnimportant.removeAll(groundTruthImportant);
  }
  public boolean doGet(
      MappedResource resource,
      Property property,
      boolean isInverse,
      HttpServletRequest request,
      HttpServletResponse response,
      Configuration config)
      throws IOException {
    Model descriptions = getAnonymousPropertyValues(resource, property, isInverse);
    if (descriptions.size() == 0) {
      return false;
    }

    Resource r = descriptions.getResource(resource.getWebURI());
    List resourceDescriptions = new ArrayList();
    StmtIterator it =
        isInverse ? descriptions.listStatements(null, property, r) : r.listProperties(property);
    while (it.hasNext()) {
      Statement stmt = it.nextStatement();
      RDFNode value = isInverse ? stmt.getSubject() : stmt.getObject();
      if (!value.isAnon()) {
        continue;
      }
      resourceDescriptions.add(
          new ResourceDescription((Resource) value.as(Resource.class), descriptions, config));
    }

    Model description = getResourceDescription(resource);
    ResourceDescription resourceDescription =
        new ResourceDescription(resource, description, config);

    String title =
        resourceDescription.getLabel()
            + (isInverse ? " ? " : " ? ")
            + config.getPrefixes().getNsURIPrefix(property.getNameSpace())
            + ":"
            + property.getLocalName();
    VelocityHelper template = new VelocityHelper(getServletContext(), response);
    Context context = template.getVelocityContext();
    context.put("project_name", config.getProjectName());
    context.put("project_link", config.getProjectLink());
    context.put("title", title);
    context.put("server_base", config.getWebApplicationBaseURI());
    context.put("sparql_endpoint", resource.getDataset().getDataSource().getEndpointURL());
    context.put("back_uri", resource.getWebURI());
    context.put("back_label", resourceDescription.getLabel());
    context.put(
        "rdf_link",
        isInverse ? resource.getInversePathDataURL(property) : resource.getPathDataURL(property));
    context.put("resources", resourceDescriptions);
    template.renderXHTML("pathpage.vm");
    return true;
  }
 @Test
 public void testSaveFolderEntryData() throws BadRequestException {
   InputStream is =
       getClass().getClassLoader().getResourceAsStream(FolderBuilder.DEFAULT_FOLDER_PATH);
   researchObject.aggregate("new-resource", is, "text/plain");
   is =
       getClass()
           .getClassLoader()
           .getResourceAsStream("model/ro/folder_resource_map/folder_entry.rdf");
   FolderEntry fe = folder.createFolderEntry(is);
   folderResourceMap.saveFolderEntryData(fe);
   Model model = ModelFactory.createDefaultModel();
   model.read(folderResourceMap.getGraphAsInputStream(RDFFormat.RDFXML), null);
   Resource r = model.getResource(fe.getUri().toString());
   Assert.assertTrue(r.hasProperty(RDF.type, RO.FolderEntry));
   r = model.getResource(fe.getProxyFor().getUri().toString());
   Assert.assertTrue(
       r.hasProperty(ORE.isAggregatedBy, model.getResource(fe.getProxyIn().getUri().toString())));
   r = model.getResource(fe.getProxyIn().getUri().toString());
   Assert.assertTrue(
       r.hasProperty(ORE.aggregates, model.getResource(fe.getProxyFor().getUri().toString())));
 }
 public static ISCB_Resource getEResource(QuerySolution qs, Model model) {
   ISCB_Resource res = new ISCB_Resource();
   String uri = qs.get("?resource").toString().trim();
   String id = model.getResource(uri).getLocalName();
   String name = qs.get("?r_name").toString().trim();
   String difficulty = qs.get("?r_difficulty").toString().trim();
   String fileLocation = qs.get("?r_fileLocation").toString().trim();
   res.setRid(id);
   res.setName(StringExchanger.getCommonString(name));
   res.setDifficulty(StringExchanger.getCommonString(difficulty));
   res.setFileLocation(StringExchanger.getCommonString(fileLocation));
   return res;
 }
示例#13
0
 private void unifyRDFSVersion(String ns) {
   for (Iterator it = Jena.cloneIt(model.listStatements()); it.hasNext(); ) {
     Statement s = (Statement) it.next();
     Resource newSubject = s.getSubject();
     Property newPredicate = s.getPredicate();
     RDFNode newObject = s.getObject();
     boolean changed = false;
     if (ns.equals(newSubject.getNameSpace())) {
       changed = true;
       newSubject = model.getResource(RDFS.getURI() + newSubject.getLocalName());
     }
     if (ns.equals(newPredicate.getNameSpace())) {
       changed = true;
       newPredicate = model.getProperty(RDFS.getURI() + newPredicate.getLocalName());
     }
     if (newObject.canAs(Resource.class)) {
       Resource oldResource = (Resource) newObject.as(Resource.class);
       if (ns.equals(oldResource.getNameSpace())) {
         changed = true;
         newObject = model.getResource(RDFS.getURI() + oldResource.getLocalName());
       }
     }
     if (changed) {
       model.add(newSubject, newPredicate, newObject);
       if (log.isLoggable(Level.FINE)) {
         log.fine(
             "Replaced deprecated triple "
                 + s
                 + " with "
                 + newSubject
                 + ", "
                 + newPredicate
                 + ", "
                 + newObject);
       }
       it.remove();
     }
   }
 }
 public static ELearner getELearner(QuerySolution qs, Model model) {
   ELearner el = new ELearner();
   String uri = qs.get("?elearner").toString().trim();
   String id = model.getResource(uri).getLocalName();
   String name = qs.get("?el_name").toString().trim();
   String grade = qs.get("?el_grade").toString().trim();
   String email = qs.get("?el_email").toString().trim();
   String address = qs.get("?el_address").toString().trim();
   el.setId(id);
   el.setName(StringExchanger.getCommonString(name));
   el.setGrade(StringExchanger.getCommonString(grade));
   el.setEmail(StringExchanger.getCommonString(email));
   el.setAddress(StringExchanger.getCommonString(address));
   return el;
 }
 public Collection<URI> getSupportedFacets(URI needUri) throws NoSuchNeedException {
   List<URI> ret = new LinkedList<URI>();
   Need need = DataAccessUtils.loadNeed(needRepository, needUri);
   Model content = rdfStorageService.loadContent(need);
   if (content == null) return ret;
   Resource baseRes = content.getResource(content.getNsPrefixURI(""));
   StmtIterator stmtIterator = baseRes.listProperties(WON.HAS_FACET);
   while (stmtIterator.hasNext()) {
     RDFNode object = stmtIterator.nextStatement().getObject();
     if (object.isURIResource()) {
       ret.add(URI.create(object.toString()));
     }
   }
   return ret;
 }
示例#16
0
 /**
  * Gets Jena statement selector corresponding to the NXRelations statement.
  *
  * @param graph the jena graph
  * @param nuxStatement NXRelations statement
  * @return jena statement selector
  */
 private static SimpleSelector getJenaSelector(Model graph, Statement nuxStatement) {
   com.hp.hpl.jena.rdf.model.Resource subjResource = null;
   com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStatement.getSubject());
   if (subject != null && subject.isURI()) {
     subjResource = graph.getResource(subject.getURI());
   }
   Property predProp = null;
   com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStatement.getPredicate());
   if (predicate != null && predicate.isURI()) {
     predProp = graph.getProperty(predicate.getURI());
   }
   com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStatement.getObject());
   RDFNode objRDF = null;
   if (object != null) {
     objRDF = graph.asRDFNode(object);
   }
   return new SimpleSelector(subjResource, predProp, objRDF);
 }
示例#17
0
  private void registerResources() {
    try {
      String personURI = "http://example.org/JohnSmith";
      Model model = ModelFactory.createDefaultModel();
      model.createResource(personURI).addProperty(VCARD.FN, "John Smith");

      // TODO mein Test
      model
          .getResource(personURI)
          .addProperty(
              VCARD.N,
              model
                  .createResource()
                  .addProperty(VCARD.Given, "John")
                  .addProperty(VCARD.Family, "Smith"));
      // Ende TODO

      resources.put(prefix + "JohnSmith", model);

      URI resourceTargetUri =
          new URI(
              "http://"
                  + EntityManager.SSP_DNS_NAME
                  //                                    + ":" + EntityManager.SSP_HTTP_SERVER_PORT
                  + prefix
                  + "JohnSmith");

      EntityManager.getInstance().entityCreated(resourceTargetUri, this);

      if (log.isDebugEnabled()) {
        log.debug("[SimpleBackend] Successfully added new resource at " + resourceTargetUri);
      }

    } catch (URISyntaxException e) {
      log.fatal("[SimpleBackend] This should never happen.", e);
    }
  }
示例#18
0
 /**
  * @see org.caboto.jena.db.Database#updateProperty(java.lang.String, java.lang.String,
  *     com.hp.hpl.jena.rdf.model.Property, com.hp.hpl.jena.rdf.model.RDFNode)
  */
 public boolean updateProperty(String uri, String resourceUri, Property property, RDFNode value) {
   try {
     log.info("Updting property in model: " + uri + " " + reasoner);
     Data data = getData();
     Model m = data.getModel(uri);
     if (!m.containsResource(ResourceFactory.createResource(resourceUri))) {
       m.close();
       data.close();
       return false;
     }
     Resource resource = m.getResource(resourceUri);
     if (resource.hasProperty(property)) {
       resource.getProperty(property).changeObject(value);
     } else {
       resource.addProperty(property, value);
     }
     m.close();
     data.close();
     return true;
   } catch (DataException e) {
     e.printStackTrace();
     return false;
   }
 }
 /**
  * ************************************************************************** THE ID of the
  * ontology is the same as its URI This method is used to exchange the standard uri to the id.
  *
  * @param qs
  * @param model
  * @param column eg:"?elearner"
  * @return id
  */
 public static String getIdByURI(QuerySolution qs, Model model, String column) {
   String uri = qs.get(column).toString();
   String id = model.getResource(uri).getLocalName();
   return id;
 }
示例#20
0
 /* (non-Javadoc)
  * @see org.dllearner.algorithms.qtl.impl.QueryTreeFactory#getQueryTree(java.lang.String, com.hp.hpl.jena.rdf.model.Model, int)
  */
 @Override
 public RDFResourceTree getQueryTree(String example, Model model, int maxDepth) {
   return createTree(model.getResource(example), model, maxDepth);
 }
示例#21
0
  public Resource lookUp(String pLookUpText, String[] pRangeTypes) {

    Model model = null;
    Resource resultResc = null;
    String line = null;
    String result = "";
    BufferedReader reader = null;
    HttpURLConnection lConnection = null;
    URL lUrl = null;

    // KeywordSearch?QueryString=string&QueryClass=string&MaxHits=string";
    // logger.debug(this.urlService+"?QueryString=" + pLookUpText +
    // "&MaxHits="+this.MaxHits+"&QueryClass=");
    try {
      lUrl =
          new URL(
              this.urlService
                  + "?QueryString="
                  + URLEncoder.encode(pLookUpText, "UTF-8")
                  + "&MaxHits="
                  + this.MaxHits
                  + "&QueryClass=");
      // set up out communications stuff
      lConnection = null;
      // Set up the initial connection
      lConnection = (HttpURLConnection) lUrl.openConnection();
      lConnection.setRequestMethod("GET");
      lConnection.setDoOutput(true);
      lConnection.setReadTimeout(30000);
      lConnection.connect();

      // logger.info("Encoding:" + lConnection.getContentEncoding());

      reader = new BufferedReader(new InputStreamReader(lConnection.getInputStream()));
      while ((line = reader.readLine()) != null) {
        result += line;
      }

    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    if (result != "") {
      String patternStr = "<ArrayOfResult(.*?)</ArrayOfResult>";
      Pattern pattern = Pattern.compile(patternStr);
      Matcher matcher = pattern.matcher(result);

      if (matcher.find()) {

        String groupStr = matcher.group(0);
        // logger.debug("		 matcher.groupCount() "+ matcher.groupCount());

        StringReader xml = new StringReader(groupStr);
        DbpediaDigester digester = new DbpediaDigester();

        ArrayOfResult arrayRes = digester.digestArrayOfResult(xml);
        List<Result> results = arrayRes.getResults();
        // logger.debug("		results.size() "+ results.size());
        if (results.size() > 0) {
          Iterator<Result> it;
          // compare results with types provided in the parameters

          if (pRangeTypes != null) {
            it = results.iterator();
            while (it.hasNext() && resultResc == null) {
              Result r = it.next();
              //	compare if the classes are the same type as requested in the parameters
              Iterator<SemanticClass> itClass = r.getSemanticClasses().iterator();
              boolean found = false;
              while (itClass.hasNext()) {
                SemanticClass mclass = itClass.next();
                for (int i = 0; i < pRangeTypes.length; i++) {
                  // compare that the type set is contained in the type got (lower case comperison)
                  // logger.debug("			Compare "+ mclass.getMlabel().toLowerCase() + " with "
                  // +pRangeTypes[i].toLowerCase());
                  if (mclass.getMlabel().toLowerCase().contains(pRangeTypes[i].toLowerCase())) {
                    logger.debug(
                        "			LOOKUP found type "
                            + pLookUpText
                            + "	"
                            + pRangeTypes[i]
                            + " CLASS "
                            + mclass.getMlabel());
                    if (doubleCheckSimilarity) {
                      logger.debug("			DOUBLE CHECK ");
                      if (similartityAlgorithm.getSimilarity(pLookUpText, r.getMlabel())
                          >= similarityMinScore) {
                        logger.debug("				OK");
                        model = DBPediaResult.createModelFromLookUp(r);
                        resultResc = model.getResource(r.getUri());
                        found = true;
                      }
                    } else {

                      model = DBPediaResult.createModelFromLookUp(r);
                      resultResc = model.getResource(r.getUri());
                      found = true;
                    }
                  }
                }
                if (found) break;
              }
            }
            // logger.debug("		result = "+ result);
          }
          // else{
          if (model == null && obligatoryClass == false) {
            // compare similarity
            Result res = null;
            double maxScore = 0.0;
            it = results.iterator();
            // 	gets the result that is more similar
            while (it.hasNext()) {
              Result r = it.next();
              double s =
                  similartityAlgorithm.getSimilarity(
                      pLookUpText.toLowerCase(), r.getMlabel().toLowerCase());
              if (s > maxScore) {
                res = r;
                maxScore = s;
              }
            }
            if (maxScore >= similarityMinScore) {
              logger.debug(
                  "		LOOKUP found similarity "
                      + pLookUpText
                      + "	"
                      + res.getUri()
                      + " SCORE "
                      + maxScore);
              model = DBPediaResult.createModelFromLookUp(res);
              resultResc = model.getResource(res.getUri());
            }
            //
          }

        } else logger.debug("		LOOKUP result 0");
      } else logger.debug("		LOOKUP matcher empty");
    } else logger.debug("		LOOKUP result empty");

    return resultResc;
  }
示例#22
0
  private String doRename(String oldNamespace, String newNamespace) {
    String uri = null;
    String result = null;
    Integer counter = 0;
    Boolean namespacePresent = false;
    RDFService rdfService = ModelAccess.on(getServletContext()).getRDFService();
    try {
      Model baseOntModel =
          RDFServiceGraph.createRDFServiceModel(new RDFServiceGraph(rdfService, ABOX_ASSERTIONS));
      OntModel ontModel = ModelAccess.on(getServletContext()).getOntModel();
      List<String> urisToChange = new LinkedList<String>();
      ontModel.enterCriticalSection(Lock.READ);
      try {
        Iterator<Individual> indIter = ontModel.listIndividuals();
        while (indIter.hasNext()) {
          Individual ind = indIter.next();
          String namespace = ind.getNameSpace();
          if (namespace != null) {
            if (oldNamespace.equals(namespace)) {
              uri = ind.getURI();
              urisToChange.add(uri);
              namespacePresent = true;
            }
          }
        }
      } finally {
        ontModel.leaveCriticalSection();
      }
      if (!namespacePresent) {
        result = "no resources renamed";
        return result;
      }
      for (String oldURIStr : urisToChange) {
        long time1 = System.currentTimeMillis();
        Resource res = baseOntModel.getResource(oldURIStr);
        long time2 = System.currentTimeMillis();
        String newURIStr = null;
        Pattern p = Pattern.compile(oldNamespace);
        String candidateString = res.getURI();
        Matcher matcher = p.matcher(candidateString);
        newURIStr = matcher.replaceFirst(newNamespace);
        long time3 = System.currentTimeMillis();
        log.debug("time to get new uri: " + Long.toString(time3 - time2));
        log.debug("Renaming " + oldURIStr + " to " + newURIStr);

        String whereClause =
            "} WHERE { \n"
                + "  GRAPH <"
                + ABOX_ASSERTIONS
                + "> { \n"
                + "   { <"
                + oldURIStr
                + "> ?p <"
                + oldURIStr
                + "> } \n "
                + "     UNION \n"
                + "   { <"
                + oldURIStr
                + "> ?q ?o } \n "
                + "     UNION \n"
                + "   { ?s ?r <"
                + oldURIStr
                + "> } \n"
                + "  } \n"
                + "}";

        String removeQuery =
            "CONSTRUCT { \n"
                + "   <"
                + oldURIStr
                + "> ?p <"
                + oldURIStr
                + "> . \n "
                + "   <"
                + oldURIStr
                + "> ?q ?o . \n "
                + "   ?s ?r <"
                + oldURIStr
                + "> \n"
                + whereClause;

        String addQuery =
            "CONSTRUCT { \n"
                + "   <"
                + newURIStr
                + "> ?p <"
                + newURIStr
                + "> . \n "
                + "   <"
                + newURIStr
                + "> ?q ?o . \n "
                + "   ?s ?r <"
                + newURIStr
                + "> \n"
                + whereClause;
        try {
          ChangeSet cs = rdfService.manufactureChangeSet();
          cs.addAddition(
              rdfService.sparqlConstructQuery(addQuery, RDFService.ModelSerializationFormat.N3),
              RDFService.ModelSerializationFormat.N3,
              ABOX_ASSERTIONS);
          cs.addRemoval(
              rdfService.sparqlConstructQuery(removeQuery, RDFService.ModelSerializationFormat.N3),
              RDFService.ModelSerializationFormat.N3,
              ABOX_ASSERTIONS);
          rdfService.changeSetUpdate(cs);
        } catch (RDFServiceException e) {
          throw new RuntimeException(e);
        }

        long time4 = System.currentTimeMillis();
        log.debug(" time to rename : " + Long.toString(time4 - time3));
        log.debug(" time for one resource: " + Long.toString(time4 - time1));
        counter++;
      }
      result = counter.toString() + " resources renamed";
      return result;
    } finally {
      if (rdfService != null) {
        rdfService.close();
      }
    }
  }
示例#23
0
  /**
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    Model m = ModelFactory.createDefaultModel();

    CSVReader reader = new CSVReader(new FileReader("Book2.csv"));
    String[] nextLine;
    String[] properties = reader.readNext();

    String ns = "http://aksw.org/Saleh/resource/";

    // for each property
    for (String property : properties)
      // create property
      m.createProperty(property);

    int i = 1;

    HashMap<String, String> created = new HashMap<>();

    while ((nextLine = reader.readNext()) != null) {

      Resource cl;

      if (!created.containsKey(nextLine[0])) {

        created.put(nextLine[0], ns + "Class" + i);

        // create classes
        cl =
            m.createResource(
                ns + "Class" + i, m.getResource("http://www.w3.org/2000/01/rdf-schema#Class"));

        m.add(cl, m.getProperty("http://www.w3.org/2000/01/rdf-schema#label"), nextLine[0]);

      } else {
        cl = m.getResource(created.get(nextLine[0]));
      }

      // create instance
      Resource r = m.createResource(ns + "Drug" + i, cl);

      // for each column
      for (int j = 1; j < nextLine.length; j++)
        // create the triple
        m.add(r, m.getProperty(properties[j]), nextLine[j]);

      i++;
    }
    // print turtle file
    m.write(System.out, "TURTLE");

    // uncomment these to print all statements
    //		Iterator<Statement> it = m.listStatements();
    //		while (it.hasNext())
    //			System.out.println(it.next());

    reader.close();

    // save to file
    try {
      FileOutputStream fout = new FileOutputStream("output.ttl");
      m.write(fout, "TURTLE");
    } catch (IOException e) {
      System.out.println("Exception caught" + e.getMessage());
      e.printStackTrace();
    }
  }
 public Resource CreateResource(String resourceName) {
   if (hbaseModel.containsResource(hbaseModel.createResource(NS + resourceName))) {
     return hbaseModel.getResource(resourceName);
   }
   return hbaseModel.createResource(NS + resourceName);
 }
示例#25
0
 /**
  * @param predicates: list of targeted predicates to enrich the model It calls
  *     getTriplesWithObjectsAreURI() method retrieving list of triples in model having URI-typed
  *     objects. For each object of them, it is checked if it is in DBpedia (can be extended later)
  *     then calls getURIInfo() method to dereference the URI-typed object in HashMap and retrieve
  *     the targeted predicates values "if exist", it iterates over the HashMap and add them to the
  *     resources in the model.
  */
 private static void addAdditionalPropertiesUsingBlankNode(Map<String, String> predicates) {
   // Map <predicate,value> save each interesting predicate of the URI object
   Map<Property, List<RDFNode>> resourceInterestingInfoExtension =
       new HashMap<Property, List<RDFNode>>();
   // Map<object,objectResourceData> to save each object with its related data resource and be
   // retrieved whenever a URI object data needed to be added for extension
   Map<RDFNode, Resource> objectFilledResource = new HashMap<RDFNode, Resource>();
   // Get list of unique URI objects in the data source as http://dbpedia.org/resource/XXXX
   List<RDFNode> urisObjects = getURIObjects();
   // Get information for each single distinct objectURI according to interesting predicates
   logger.info("Number of unique URI object to find extension: " + urisObjects.size());
   if (urisObjects.size() > 0) {
     // The object resource that will have each URI object extended data
     Resource object = null;
     int count = 1;
     // For each URI object a resource is created, filled with information,add the object with its
     // data resource into map
     for (RDFNode uriObject : urisObjects) {
       logger.info("Predicate " + count++ + " of " + urisObjects.size() + ":" + uriObject);
       // Create a resource with empty node
       object = localModel.createResource();
       // Retrieve all interesting <predicate,object> info. for such URI object
       resourceInterestingInfoExtension = DereferencingModule.getURIInfo(uriObject);
       // Add information to the resource
       for (Property key : resourceInterestingInfoExtension.keySet()) {
         // add the new properties to the new triple
         List<RDFNode> subjects = resourceInterestingInfoExtension.get(key);
         for (RDFNode subject : subjects) {
           if (subject.isLiteral()) {
             object.addProperty(key, subject.asLiteral().toString());
           } else {
             object.addProperty(key, subject);
           }
         }
       }
       // Add to list of object's resource that is filled with information
       objectFilledResource.put(uriObject, object);
     }
   } else { // Otherwise no URI objects to be extended
     return;
   }
   List<Statement> triplesWithURIsObjects = getTriplesWithURIObjects();
   logger.info("Starting model enriching");
   if (triplesWithURIsObjects.size() > 0) {
     Resource object = null;
     // iterate over each triple to dereference each URI object and add its information to its
     // resource subject
     for (Statement triple : triplesWithURIsObjects) {
       // create new triple with empty node as its subject where this subject will be an object of
       // the targeted resource to be extended
       if (!objectFilledResource.containsKey(triple.getSubject())) {
         object = objectFilledResource.get(triple.getObject());
         objectsDerefModelAdded.put(triple.getObject(), object);
         // Attach the object's resource to this subject
         Resource resource = localModel.getResource(triple.getSubject().getURI());
         resource.addProperty(defaultOutputProperty, object);
         resourceInterestingInfoExtension = null;
       }
     }
   }
 }
示例#26
0
  /**
   * @param uri : the URI to be dereferenced
   * @param predicates : targeted predicates to be added to enrich the model
   * @return This method retrieves list of values for targeted predicates for a URI-typed object for
   *     each URI-typed object, through content negotiation an open connection is done retrieving
   *     its predicates/values. An iteration is made over targeted predicates. For each predicate
   *     list of statements with the targeted predicate is retrieved and extracting its value in
   *     order to be added to hashmap<predicate,Value>
   */
  @SuppressWarnings("unchecked")
  private static HashMap<Property, List<RDFNode>> getURIInfo(RDFNode p) {
    String uri = p.asResource().getURI();
    // to store each predicate and its value
    HashMap<Property, List<RDFNode>> resourceFocusedInfo = new HashMap<Property, List<RDFNode>>();

    if (demo) { // Deserialize the results if exists (For Demo purpose)
      if (useCache) {
        try {
          HashMap<String, List<String>> ser = new HashMap<String, List<String>>();
          File file = new File("resourceFocusedInfo.ser");
          if (file.exists()) {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            ser = (HashMap<String, List<String>>) in.readObject();
            in.close();
            // convert every object back from string
            for (String prop : ser.keySet()) {
              List<String> l = ser.get(prop);
              List<RDFNode> nodes = new ArrayList<RDFNode>();
              for (String n : l) {
                nodes.add(ResourceFactory.createResource(n));
              }
              resourceFocusedInfo.put(ResourceFactory.createProperty(prop), nodes);
            }
            return resourceFocusedInfo;
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    // define local model to have the data of the URI and extract focused info through built sparql
    // query
    List<RDFNode> values = new ArrayList<RDFNode>();
    try {
      URLConnection conn = new URL(uri).openConnection();
      conn.setRequestProperty("Accept", "application/rdf+xml");
      conn.setRequestProperty("Accept-Language", "en");
      Model model = ModelFactory.createDefaultModel();
      InputStream in = conn.getInputStream();
      model.read(in, null);
      for (Property inputProperty : inputProperties) {
        for (Statement st :
            model.listStatements(model.getResource(uri), inputProperty, (RDFNode) null).toList()) {
          RDFNode value = st.getObject();
          if (value.isLiteral()) {
            if (value.asLiteral().getLanguage().toLowerCase().equals("en")
                || value.asLiteral().getLanguage().toLowerCase().equals("")) {
              values.add(value);
            }
          } else {
            values.add(value);
          }
        }
        resourceFocusedInfo.put(inputProperty, values);
        values = new ArrayList<RDFNode>(); // create new list for new predicate
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (demo) { // serialize the output (for Demo purpose)
      try {
        HashMap<String, List<String>> ser = new HashMap<String, List<String>>();
        FileOutputStream fileOut = new FileOutputStream("resourceFocusedInfo.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        // convert to Serializabe Strings
        for (Property prop : resourceFocusedInfo.keySet()) {
          List<String> l = new ArrayList<String>();
          for (RDFNode n : resourceFocusedInfo.get(prop)) {
            l.add(n.toString());
          }
          ser.put(prop.toString(), l);
        }
        out.writeObject(ser);
        out.close();
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    }
    return resourceFocusedInfo;
  }
示例#27
0
/** Vocabulary definitions from file:vocabularies/owl.owl */
public class OWL {
  /** The RDF model that holds the vocabulary terms */
  private static Model m_model = ModelFactory.createDefaultModel();

  /** The namespace of the vocabulary as a string ({@value}) */
  public static final String NS = "http://www.w3.org/2002/07/owl#";

  /**
   * The namespace of the vocabulary as a string
   *
   * @see #NS
   */
  public static String getURI() {
    return NS;
  }

  /** The namespace of the vocabulary as a resource */
  public static final Resource NAMESPACE = m_model.createResource(NS);

  /** A resource that denotes the OWL-full sublanguage of OWL */
  public static final Resource FULL_LANG = m_model.getResource(getURI());

  /** A resource, not officially sanctioned by WebOnt, that denotes the OWL-DL sublanguage of OWL */
  public static final Resource DL_LANG =
      m_model.getResource("http://www.w3.org/TR/owl-features/#term_OWLDL");

  /**
   * A resource, not officially sanctioned by WebOnt, that denotes the OWL-Lite sublanguage of OWL
   */
  public static final Resource LITE_LANG =
      m_model.getResource("http://www.w3.org/TR/owl-features/#term_OWLLite");

  // Vocabulary properties
  ///////////////////////////

  public static final Property maxCardinality =
      m_model.createProperty("http://www.w3.org/2002/07/owl#maxCardinality");

  public static final Property versionInfo =
      m_model.createProperty("http://www.w3.org/2002/07/owl#versionInfo");

  public static final Property equivalentClass =
      m_model.createProperty("http://www.w3.org/2002/07/owl#equivalentClass");

  public static final Property distinctMembers =
      m_model.createProperty("http://www.w3.org/2002/07/owl#distinctMembers");

  public static final Property oneOf =
      m_model.createProperty("http://www.w3.org/2002/07/owl#oneOf");

  public static final Property sameAs =
      m_model.createProperty("http://www.w3.org/2002/07/owl#sameAs");

  public static final Property incompatibleWith =
      m_model.createProperty("http://www.w3.org/2002/07/owl#incompatibleWith");

  public static final Property minCardinality =
      m_model.createProperty("http://www.w3.org/2002/07/owl#minCardinality");

  public static final Property complementOf =
      m_model.createProperty("http://www.w3.org/2002/07/owl#complementOf");

  public static final Property onProperty =
      m_model.createProperty("http://www.w3.org/2002/07/owl#onProperty");

  public static final Property equivalentProperty =
      m_model.createProperty("http://www.w3.org/2002/07/owl#equivalentProperty");

  public static final Property inverseOf =
      m_model.createProperty("http://www.w3.org/2002/07/owl#inverseOf");

  public static final Property backwardCompatibleWith =
      m_model.createProperty("http://www.w3.org/2002/07/owl#backwardCompatibleWith");

  public static final Property differentFrom =
      m_model.createProperty("http://www.w3.org/2002/07/owl#differentFrom");

  public static final Property priorVersion =
      m_model.createProperty("http://www.w3.org/2002/07/owl#priorVersion");

  public static final Property imports =
      m_model.createProperty("http://www.w3.org/2002/07/owl#imports");

  public static final Property allValuesFrom =
      m_model.createProperty("http://www.w3.org/2002/07/owl#allValuesFrom");

  public static final Property unionOf =
      m_model.createProperty("http://www.w3.org/2002/07/owl#unionOf");

  public static final Property hasValue =
      m_model.createProperty("http://www.w3.org/2002/07/owl#hasValue");

  public static final Property someValuesFrom =
      m_model.createProperty("http://www.w3.org/2002/07/owl#someValuesFrom");

  public static final Property disjointWith =
      m_model.createProperty("http://www.w3.org/2002/07/owl#disjointWith");

  public static final Property cardinality =
      m_model.createProperty("http://www.w3.org/2002/07/owl#cardinality");

  public static final Property intersectionOf =
      m_model.createProperty("http://www.w3.org/2002/07/owl#intersectionOf");

  // Vocabulary classes
  ///////////////////////////

  public static final Resource Thing =
      m_model.createResource("http://www.w3.org/2002/07/owl#Thing");

  public static final Resource DataRange =
      m_model.createResource("http://www.w3.org/2002/07/owl#DataRange");

  public static final Resource Ontology =
      m_model.createResource("http://www.w3.org/2002/07/owl#Ontology");

  public static final Resource DeprecatedClass =
      m_model.createResource("http://www.w3.org/2002/07/owl#DeprecatedClass");

  public static final Resource AllDifferent =
      m_model.createResource("http://www.w3.org/2002/07/owl#AllDifferent");

  public static final Resource DatatypeProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#DatatypeProperty");

  public static final Resource SymmetricProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#SymmetricProperty");

  public static final Resource TransitiveProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#TransitiveProperty");

  public static final Resource DeprecatedProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#DeprecatedProperty");

  public static final Resource AnnotationProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#AnnotationProperty");

  public static final Resource Restriction =
      m_model.createResource("http://www.w3.org/2002/07/owl#Restriction");

  public static final Resource Class =
      m_model.createResource("http://www.w3.org/2002/07/owl#Class");

  public static final Resource OntologyProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#OntologyProperty");

  public static final Resource ObjectProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#ObjectProperty");

  public static final Resource FunctionalProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#FunctionalProperty");

  public static final Resource InverseFunctionalProperty =
      m_model.createResource("http://www.w3.org/2002/07/owl#InverseFunctionalProperty");

  public static final Resource Nothing =
      m_model.createResource("http://www.w3.org/2002/07/owl#Nothing");

  // Vocabulary individuals
  ///////////////////////////

}
示例#28
0
  public static String[] generateConcepts(DocumentAnalyzer analyzer, String id) {

    if (analyzer.Doci.concepts != null && analyzer.Doci.concepts.size() > 0) {
      List<Concept> MainConcepts =
          analyzer.Doci.concepts.subList(
              0, Math.min(analyzer.Doci.concepts.size(), analyzer.MaxNumOfConcepts));
      String out = "";
      String outForConceptPh = ""; // by concept
      String outForMainPh = ""; // for main senatance	
      ArrayList<String> phs = analyzer.Doci.sentences;
      System.out.println(
          "Start generating features for "
              + id
              + " with n of sentences: "
              + phs.size()
              + " and main concepts: "
              + MainConcepts.size());
      if (phs != null)
        for (int i = 0; i < phs.size(); i++) {
          try {
            String oneOutForMainPh = ""; // for one ph for main senatance

            String ph = phs.get(i);

            String[] phTerms = ph.toLowerCase().split(" ");
            String stemedPh = "";
            for (String term : phTerms) {
              stemedPh += " " + analyzer.Doci.SBStemmer.stem(term);
            }

            ArrayList<EnsenDBpediaResource> ress = analyzer.Doci.resourcesInSentenses.get(i);
            // build csv for main sentence
            // id
            oneOutForMainPh += id + "ph" + i;

            // QT
            int qtScore = 0;
            for (String term1 : analyzer.Doci.q.Text.split(" ")) {
              if (term1.length() > 3 && ph.toLowerCase().contains(" " + term1.toLowerCase() + " "))
                qtScore += ph.toLowerCase().split(term1.toLowerCase()).length - 1;
            }
            oneOutForMainPh += "," + qtScore;

            // QR / QMR
            int qrScore = 0;
            int qmrScore = 0;
            int ptScore = 0;
            double csScore = 0.0;
            int cns = 0;
            double asScore = 0.0;
            for (EnsenDBpediaResource res : ress) {
              // cs sum of scores
              int rank = getRank(analyzer.Doci.concepts, res.getFullUri());
              if (rank > -1) csScore += (1 / (1.0 * rank));
              asScore += res.similarityScore;
              // q's resources
              if (DBpediaSpotlightClient.EnsenDBpediaResourceListContains(
                      analyzer.Doci.q.Resources, res.getFullUri())
                  > -1) {
                qrScore++;
                for (int ccIndex = 0; ccIndex < MainConcepts.size(); ccIndex++) {
                  if (MainConcepts.get(ccIndex).URI.contains(res.getFullUri())) {
                    qmrScore++;
                  }
                }
              }

              // groups
              for (Group g : analyzer.groups) {
                for (Entry<String, Double> pred : g.predicates.entrySet()) {
                  String[] predicatWords =
                      pred.getKey().replace("_", " ").split("(?=\\p{Upper})|/| ");
                  for (int j = 0; j < predicatWords.length; j++) {
                    if (predicatWords[j].length() > 3
                        && ph.toLowerCase().contains(predicatWords[j].toLowerCase())) {
                      ptScore++;
                    }
                  }
                }
              }

              // couble of concepts
              Model graph = analyzer.Doci.fullGraph;
              StmtIterator stms =
                  graph.listStatements(
                      new SimpleSelector(
                          graph.getResource(res.getFullUri()), null, (RDFNode) null));
              while (stms.hasNext()) {
                Statement next = stms.next();
                if (next.getObject().isResource()) {
                  if (DBpediaSpotlightClient.EnsenDBpediaResourceListContains(
                          ress, next.getObject().asResource().getURI())
                      > -1) cns++;
                }
              }
            }
            oneOutForMainPh += "," + qrScore;
            oneOutForMainPh += "," + qmrScore;

            // mr
            int MRScore = 0;
            for (int ccIndex = 0; ccIndex < MainConcepts.size(); ccIndex++) {
              if (DBpediaSpotlightClient.EnsenDBpediaResourceListContains(
                      ress, MainConcepts.get(ccIndex).URI)
                  > -1) {
                MRScore++;
              }
            }
            oneOutForMainPh += "," + MRScore;

            // ar
            oneOutForMainPh += "," + ress.size();

            // cns
            oneOutForMainPh += "," + cns;

            int arlScore = 0;
            int mrlScore = 0;
            for (int k = 0; k < analyzer.Doci.concepts.size(); k++) {
              Concept c = analyzer.Doci.concepts.get(k);
              String[] terms = c.name.toLowerCase().split(" ");
              for (String term : terms) {
                term = analyzer.Doci.SBStemmer.stem(term);
                if (stemedPh.contains(" " + term)) {
                  arlScore++;
                  if (k < MainConcepts.size()) // main concept
                  mrlScore++;
                }
              }
            }
            // arl
            oneOutForMainPh += "," + arlScore;

            // mrl
            oneOutForMainPh += "," + mrlScore;

            // pt
            oneOutForMainPh += "," + ptScore;

            ptScore = 0;

            // as
            if (ress.size() > 0) oneOutForMainPh += "," + (asScore / ress.size() * 1.0);
            else oneOutForMainPh += ",0";

            // len
            oneOutForMainPh += "," + ph.length();

            // tt
            int ttscore = 0;
            String[] Tterms = analyzer.Doci.content.getTitle().toLowerCase().split(" ");
            for (String term : Tterms) {
              term = analyzer.Doci.SBStemmer.stem(term);
              if (stemedPh.contains(" " + term)) ttscore++;
            }
            oneOutForMainPh += "," + ttscore;

            // se
            oneOutForMainPh += "," + ph.trim().charAt(ph.trim().length() - 1);

            // ss
            char c = ph.trim().charAt(0);
            if (c >= '0' && c <= '9') oneOutForMainPh += ",n";
            else if (c >= 'a' && c <= 'z') oneOutForMainPh += ",c";
            else if (c >= 'A' && c <= 'A') oneOutForMainPh += ",c";
            else oneOutForMainPh += ",o";

            // hl
            int https = ph.split("http").length - 1;
            https += ph.split("www.").length - 1;
            https += ph.split(".html").length - 1;
            if (https > 0) oneOutForMainPh += ",true";
            else oneOutForMainPh += ",false";

            // sss
            int smallSentence = 0;
            for (String s : ph.split(",")) {
              if (s.split(" ").length < 3) {
                smallSentence++;
              }
            }
            oneOutForMainPh += "," + smallSentence;

            // CS
            if (ress.size() > 0) oneOutForMainPh += "," + csScore / ress.size();
            else oneOutForMainPh += ",0.0";

            // nch
            double nch = 0;
            Pattern p = Pattern.compile("[^A-Za-z ]");
            Matcher matcher = p.matcher(ph);
            int nchi = 0;
            while (matcher.find()) {
              matcher.group();
              nchi++;
            }

            nch = nchi * 100.0 / ph.length();

            oneOutForMainPh += "," + nch;

            // d
            boolean d = false;
            String DATE_PATTERN =
                "([0-9][0-9][0-9][0-9])|([0-9]{2})/([0-9]{2})/([0-9]{4})|([0-9]{2})-([0-9]{2})-([0-9]{4})|([0-9]{4})-([0-9]{2})-([0-9]{2})|([0-9]{4})/([0-9]{2})/([0-9]{2})";
            Pattern pattern = Pattern.compile(DATE_PATTERN);
            matcher = pattern.matcher(ph);
            if (matcher.find()) {
              d = true;
            }
            oneOutForMainPh += "," + d;

            // php
            double php = i * 100 / phs.size();
            oneOutForMainPh += "," + php;

            // fph
            boolean fph = (i == 0);
            oneOutForMainPh += "," + fph;

            // lph
            boolean lph = (i == phs.size() - 1);
            oneOutForMainPh += "," + lph;

            // newline
            oneOutForMainPh += ",\n";

            // build csv for concept-sentence & html files

            if (ress != null) {
              boolean firstTime = true;
              for (int cIndex = 0;
                  cIndex < Math.min(analyzer.Doci.concepts.size(), analyzer.MaxNumOfConcepts);
                  cIndex++) {
                Concept con = analyzer.Doci.concepts.get(cIndex);

                if (DBpediaSpotlightClient.EnsenDBpediaResourceListContains(ress, con.URI) > -1) {
                  // For HTML
                  if (firstTime) {
                    out +=
                        "<fieldset>  <legend>"
                            + i
                            + "- select for the Snippet: yes <input type='radio' name='Q"
                            + analyzer.Doci.q.id
                            + "D"
                            + analyzer.Doci.Rank
                            + "ph"
                            + i
                            + "' value='Q"
                            + analyzer.Doci.q.id
                            + "D"
                            + analyzer.Doci.Rank
                            + "ph"
                            + i
                            + "'>, no<input type='radio' name='Q"
                            + analyzer.Doci.q.id
                            + "D"
                            + analyzer.Doci.Rank
                            + "ph"
                            + i
                            + "' value='' checked>    </legend><p>"
                            + ph
                            + "</p></hr>";
                  }
                  out +=
                      "<input type='checkbox' value='Q"
                          + analyzer.Doci.q.id
                          + "D"
                          + analyzer.Doci.Rank
                          + "C"
                          + cIndex
                          + "ph"
                          + i
                          + "'><span><a target='blank' href='"
                          + con.URI
                          + "'>"
                          + con.name
                          + "</a></span> ";
                  firstTime = false;

                  // for csv

                  // id
                  con.phsFeatures +=
                      "Q" + analyzer.Doci.q.id + "D" + analyzer.Doci.Rank + "C" + cIndex + "ph" + i;

                  // tct
                  int tempScore = 0;
                  int position = -1;
                  double sim = 0.0; // spotlight similarity
                  double count = 0.0; // this concept in this paragraph instances counter
                  for (String term1 : con.name.split(" ")) {
                    if (term1.length() > 3)
                      if (ph.toLowerCase().contains(" " + term1.toLowerCase() + " ")) {
                        if (position == -1) position = ph.indexOf(" " + term1.toLowerCase() + " ");
                        tempScore += 1;
                      }
                  }
                  con.phsFeatures += "," + tempScore;

                  // cb
                  int cbScore = 0;
                  int cnScore = 0;

                  Set<String> brothers = new HashSet<String>();
                  Set<String> neighbors = new HashSet<String>();
                  if (ress != null)
                    for (EnsenDBpediaResource res : ress) {

                      // get position & spotlight similarity
                      if (res.getFullUri().equals(con.URI)) {
                        position = ph.indexOf(res.originalText);
                        sim += res.similarityScore;
                        count++;
                      }
                      // groups
                      for (Group g : con.groups) {
                        if (g.auths != null && g.auths.keySet().contains(res.getFullUri()))
                          brothers.add(res.getFullUri());
                        // g.auths.keySet().toArray().toString().
                        if (g.hubs != null && g.hubs.keySet().contains(res.getFullUri()))
                          brothers.add(res.getFullUri());

                        for (Entry<String, Double> pred : g.predicates.entrySet()) {
                          String[] predicatWords =
                              pred.getKey().replace("_", " ").split("(?=\\p{Upper})|/| ");
                          for (int j = 0; j < predicatWords.length; j++) {
                            if (predicatWords[j].length() > 3
                                && ph.toLowerCase().contains(predicatWords[j].toLowerCase())) {
                              ptScore++;
                            }
                          }
                        }
                      }

                      // graph
                      Model g = analyzer.Doci.fullGraph;
                      StmtIterator res1 =
                          g.listStatements(
                              new SimpleSelector(
                                  g.getResource(res.getFullUri()), null, g.getResource(con.URI)));
                      StmtIterator res2 =
                          g.listStatements(
                              new SimpleSelector(
                                  g.getResource(con.URI), null, g.getResource(res.getFullUri())));
                      while (res1.hasNext()) {
                        Statement next = res1.next();
                        neighbors.add(next.getSubject().getURI());
                      }
                      while (res2.hasNext()) {
                        Statement next = res2.next();
                        neighbors.add(next.getObject().asResource().getURI());
                      }
                    }
                  brothers.remove(con.URI);
                  cbScore = brothers.size();
                  con.phsFeatures += "," + cbScore;

                  // cn
                  neighbors.remove(con.URI);
                  cnScore = neighbors.size();
                  con.phsFeatures += "," + cnScore;

                  // cbl
                  int cblScore = 0;

                  Model m = RDFManager.createRDFModel();

                  for (String oneBrother : brothers) {
                    Resource R = m.createResource(oneBrother);
                    String[] terms = R.getLocalName().toLowerCase().split(" ");
                    for (String term : terms) {
                      term = analyzer.Doci.SBStemmer.stem(term);
                      if (stemedPh.contains(" " + term)) cblScore++;
                    }
                  }

                  con.phsFeatures += "," + cblScore;
                  // cnl
                  int cnlScore = 0;
                  for (String oneNeighbor : neighbors) {
                    Resource R = m.createResource(oneNeighbor);
                    String[] terms = R.getLocalName().toLowerCase().split(" ");
                    for (String term : terms) {
                      term = analyzer.Doci.SBStemmer.stem(term);
                      if (stemedPh.contains(" " + term)) cnlScore++;
                    }
                  }
                  con.phsFeatures += "," + cnlScore;

                  // ca
                  String stemedAbstract = "";
                  for (String term : con.abstractTxt.split(" ")) {
                    term = analyzer.Doci.SBStemmer.stem(term);
                    stemedAbstract += term + " ";
                  }
                  double caScore = jaccardSimilarity(stemedAbstract, stemedPh);

                  con.phsFeatures += "," + caScore;

                  // cp
                  double cpScore = 0;
                  if (position > -1) cpScore = (ph.length() - position) * 1.0 / ph.length();
                  if (cpScore > 1) cpScore = 1.0;
                  con.phsFeatures += "," + cpScore;

                  // qt
                  con.phsFeatures += "," + qtScore;

                  // qr
                  con.phsFeatures += "," + qrScore;

                  // mr

                  con.phsFeatures += "," + MRScore;

                  // ar
                  con.phsFeatures += "," + ress.size();

                  // arl

                  con.phsFeatures += "," + arlScore;

                  // mrl
                  con.phsFeatures += "," + mrlScore;

                  // pt
                  con.phsFeatures += "," + ptScore;

                  // cas
                  con.phsFeatures += "," + sim / count * 1.0;

                  // as
                  if (ress.size() > 0) con.phsFeatures += "," + (asScore / ress.size() * 1.0);
                  else con.phsFeatures += ",0";

                  // len
                  con.phsFeatures += "," + ph.length();

                  // tt

                  con.phsFeatures += "," + ttscore;

                  // se
                  con.phsFeatures += "," + ph.trim().charAt(ph.trim().length() - 1);

                  // ss
                  c = ph.trim().charAt(0);
                  if (c >= '0' && c <= '9') con.phsFeatures += ",n";
                  else if (c >= 'a' && c <= 'z') con.phsFeatures += ",c";
                  else if (c >= 'A' && c <= 'A') con.phsFeatures += ",c";
                  else con.phsFeatures += ",o";

                  // hl
                  if (https > 0) con.phsFeatures += ",true";
                  else con.phsFeatures += ",false";

                  // sss
                  con.phsFeatures += "," + smallSentence;

                  // csc
                  double cscScore = 0.0;
                  int rank = getRank(analyzer.Doci.concepts, con.URI);
                  if (rank > -1) cscScore = (1 / (1.0 * rank));
                  con.phsFeatures += "," + cscScore;

                  // cs
                  if (ress.size() > 0) con.phsFeatures += "," + csScore / ress.size();
                  else con.phsFeatures += ",0.0";

                  con.phsFeatures += "," + nch;

                  // d
                  con.phsFeatures += "," + d;

                  // php
                  con.phsFeatures += "," + php;

                  // fph
                  con.phsFeatures += "," + fph;

                  // lph
                  con.phsFeatures += "," + lph;

                  // newline
                  con.phsFeatures += ",\n";
                }

                if (MRScore == 0) oneOutForMainPh = "";
                outForMainPh += oneOutForMainPh;
                oneOutForMainPh = "";
              }
            }
            out += " </fieldset>";
            // Printer.printToFile("ML/Q" + analyzer.Doci.q.id + "D" + analyzer.Doci.Rank + ".csv",
            // out2);
          } catch (Exception e) {

          }
        }

      for (Concept c : analyzer.Doci.concepts) {
        outForConceptPh += c.phsFeatures;
      }

      outForConceptPh =
          outForConceptPh.replace("\"", "”").replace(",,,", ",\",\",").replace("'", "`");
      outForMainPh = outForMainPh.replace("\"", "”").replace(",,,", ",\",\",").replace("'", "`");

      String[] results = {"", "", ""};
      results[0] = out;
      results[1] = outForConceptPh;
      results[2] = outForMainPh;
      System.out.println("finish generating features for " + id);

      return results;
    }
    return null;
  }
示例#29
0
 @Override
 public Vertex getVertex(final Object id) {
   final Resource resource = model.getResource(id.toString());
   return new JenaVertex(model, resource);
 }