Hi!

I'm new to the Web Semantic technologies and wanted to use the Dbpedia
Lookup Web Service with JAVA. Searching around the Internet, I found a code
that sends a word to Lookup, and gets an XML returned. It uses SAX to get
the *label*,* abstract* and *URI* from the XML. The results isn't precise
though, sometimes it gets the wrong properties.

Someone told me this isn't the correct way to use Lookup. The code is
bellow, only for you to know it, but what's the correct way to use Lookup?

public class DbpediaLookupClient extends DefaultHandler {
  public DbpediaLookupClient(String query, String queryClass) throws
Exception {
    this.query = query;
    HttpClient client = new HttpClient();
    HttpMethod method;

    String query2 = query.replaceAll(" ", "+");

    if( queryClass.equalsIgnoreCase("Person") ) {
     method = new GetMethod("
http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?"; +
                    "MaxHits=5&QueryClass=" + queryClass + "&QueryString="
+ query2);
    } else {
    method = new GetMethod("
http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?"; +
                       "MaxHits=5&QueryString=" + query2);

    }

    try {
      client.executeMethod(method);
      System.out.println(method);
      InputStream ins = method.getResponseBodyAsStream();
      //System.out.println(method.getResponseBodyAsString());
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser sax = factory.newSAXParser();

      sax.parse(ins, this);
    } catch (HttpException he) {
      System.err.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
      System.err.println("Unable to connect to lookup.dbpedia.org");
    }
    method.releaseConnection();
  }

  private List<Map<String, String>> variableBindings = new
ArrayList<Map<String, String>>();
  private Map<String, String> tempBinding = null;
  private String lastElementName = null;

  public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
    if (qName.equalsIgnoreCase("result")) {
      tempBinding = new HashMap<String, String>();
    }
    lastElementName = qName;
  }

  public void endElement(String uri, String localName, String qName) throws
SAXException {
    if (qName.equalsIgnoreCase("result")) {
      if (!variableBindings.contains(tempBinding) &&
containsSearchTerms(tempBinding))
        variableBindings.add(tempBinding);
    }
  }

  public void characters(char[] ch, int start, int length) throws
SAXException {
    String s = new String(ch, start, length).trim();
    if (s.length() > 0) {
      if ("Description".equals(lastElementName)) {
        if (tempBinding.get("Description") == null) {
          tempBinding.put("Description", s);
        }
        tempBinding.put("Description", "" + tempBinding.get("Description")
+ " " + s);
      }
      if ("URI".equals(lastElementName) && s.indexOf("Category")== -1 &&
      tempBinding.get("URI") == null) {
          tempBinding.put("URI", s);
      }
      if ("Label".equals(lastElementName) &&
           tempBinding.get("Label") == null) tempBinding.put("Label", s);
    }
  }

  public List<Map<String, String>> variableBindings() {
    return variableBindings;
  }
  private boolean containsSearchTerms(Map<String, String> bindings) {
    StringBuilder sb = new StringBuilder();
    for (String value : bindings.values()) sb.append(value); // do not need
white space
    String text = sb.toString().toLowerCase();
    StringTokenizer st = new StringTokenizer(this.query);
    while (st.hasMoreTokens()) {
      if (text.indexOf(st.nextToken().toLowerCase()) == -1) {
        return false;
      }
    }
    return true;
  }
  private String query = "";
}
------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349351&iu=/4140/ostg.clktrk
_______________________________________________
Dbpedia-discussion mailing list
Dbpedia-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dbpedia-discussion

Reply via email to