コード例 #1
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;
  }
コード例 #2
0
  /**
   * Lookup a concept of a given type. If found it creates a Model that contains: - Found resource
   * and provided descriptors (rdf:type, skos:subject, dc:description) - Links the found resource
   * with the resource provided in the parameter using the given Property - Assigns a rdf:type to
   * the found resource
   *
   * @param pLookUpText - name of the concept to find
   * @param pLinkSubjectUri - the resource to be linked to the result
   * @param pLinkPredicate - the property to link the found resource to the pLinkSubjectUri
   * @param pRdfType - if not null, the this type is given to the found resource. If this parameter
   *     is not set, then a similarity comparison will take place to return the most similar which
   *     similarity is higher than the minimum score.
   * @param pRangeTypes - if not null it filters results to the given types of this property
   * @retutn Model - If the concept was found, it returns a Jena Model object with the resuls and
   *     the created links.
   */
  public Model lookUp(
      String pLookUpText,
      String pLinkSubjectUri,
      Property pLinkPredicate,
      Resource pRdfType,
      String[] pRangeTypes) {

    this.LinkUri = pLinkSubjectUri;

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

    // KeywordSearch?QueryString=string&QueryClass=string&MaxHits=string";

    try {
      // logger.debug(this.urlService+"?QueryString=" + URLEncoder.encode(pLookUpText, "UTF-8")+
      // "&MaxHits="+this.MaxHits+"&QueryClass=");
      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();

      // System.out.println("ENCODING:"  + lConnection.getContentEncoding());
      reader = new BufferedReader(new InputStreamReader(lConnection.getInputStream(), "UTF-8"));
      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()) {
        // logger.debug("	matcher.groupCount() " + matcher.groupCount());

        String groupStr = matcher.group(0);

        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());
        double maxScore = 0.0;
        Result resSimilaruty = null;
        if (results.size() > 0) {
          Iterator<Result> it;
          // compare results with types provided in the parameters
          it = results.iterator();

          while (it.hasNext()) {
            Result r = it.next();

            Iterator<SemanticClass> itClass = r.getSemanticClasses().iterator();

            if (pRangeTypes != null && itClass.hasNext()) {
              logger.debug(
                  "		Comapre Types " + r.getUri() + " test ranges " + Arrays.toString(pRangeTypes));
              //	compare if the classes are the same type as requested in the parameters
              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.getUri().toLowerCase()
                          + " - "
                          + pRangeTypes[i].toLowerCase());
                  if (mclass.getUri().toLowerCase().contains(pRangeTypes[i].toLowerCase())) {
                    logger.debug(
                        "		FOUND TYPE "
                            + pLookUpText
                            + "	"
                            + r.getUri()
                            + " CLASS "
                            + mclass.getMlabel());
                    model =
                        DBPediaResult.createModelFromLookUp(r, LinkUri, pLinkPredicate, pRdfType);
                    resSimilaruty = null;
                    break;
                  }
                }
                if (model != null) break;
              }

              if (model != null) break;
            } else {
              logger.debug("		Comapre Similarity " + r.getMlabel() + " with " + pLookUpText);
              // 	gets the result that is more similar
              double s = similartityAlgorithm.getSimilarity(pLookUpText, r.getMlabel());
              if (s > maxScore) {
                resSimilaruty = r;
                maxScore = s;
              }
            }
          }

          if (resSimilaruty != null) {
            // compare similarity

            if (maxScore >= similarityMinScore) {
              logger.debug(
                  "		FOUND SIMILARITY "
                      + pLookUpText
                      + "	"
                      + resSimilaruty.getUri()
                      + " SCORE "
                      + maxScore);
              model =
                  DBPediaResult.createModelFromLookUp(
                      resSimilaruty, LinkUri, pLinkPredicate, pRdfType);
            } else logger.debug("		REJECTED SIMILARITY " + maxScore);
          }

        } else logger.debug("		Result size 0 ");
      } else logger.debug("		ArrayOfResult empty ");
    } else logger.debug("		result empty ");

    return model;
  }