新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Semantic Web(语义Web,语义网或语义万维网, Web 3.0)及相关理论,如:Ontology(本体,本体论), OWL(Web Ontology Langauge,Web本体语言), Description Logic(DL, 描述逻辑),RDFa,Ontology Engineering等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区W3CHINA.ORG讨论区 - Web新技术讨论『 Semantic Web(语义Web)/描述逻辑/本体 』 → Jena文档《An Introduction to RDF and the Jena RDF API》的译文 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 719032 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: Jena文档《An Introduction to RDF and the Jena RDF API》的译文 举报  打印  推荐  IE收藏夹 
       本主题类别: RDF/RDFS | Jena | 中译文档    
     guffey 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:13
      积分:132
      门派:XML.ORG.CN
      注册:2007/7/17

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给guffey发送一个短消息 把guffey加入好友 查看guffey的个人资料 搜索guffey在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看guffey的博客楼主
    发贴心情 

    import java.util.ArrayList;
    import java.util.List;

    import com.hp.hpl.jena.db.*;
    import com.hp.hpl.jena.graph.Node;
    import com.hp.hpl.jena.graph.Triple;
    import com.hp.hpl.jena.query.Query;
    import com.hp.hpl.jena.query.QueryExecution;
    import com.hp.hpl.jena.query.QueryExecutionFactory;
    import com.hp.hpl.jena.query.QueryFactory;
    import com.hp.hpl.jena.query.QuerySolutionMap;
    import com.hp.hpl.jena.query.ResultSet;
    import com.hp.hpl.jena.query.ResultSetFormatter;
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.RDFNode;
    import com.hp.hpl.jena.sparql.algebra.Algebra;
    import com.hp.hpl.jena.sparql.algebra.Op;
    import com.hp.hpl.jena.sparql.algebra.op.OpBGP;
    import com.hp.hpl.jena.sparql.core.BasicPattern;
    import com.hp.hpl.jena.sparql.core.Var;
    import com.hp.hpl.jena.sparql.engine.QueryIterator;
    import com.hp.hpl.jena.sparql.engine.ResultSetStream;
    import com.hp.hpl.jena.sparql.engine.binding.Binding;
    import com.hp.hpl.jena.sparql.util.FmtUtils;
    import com.hp.hpl.jena.sparql.util.IndentedWriter;

    /**
    * Executes an RDQL query to find the hypernym of a given word
    */
    public class FindHypernym {

     /** MySQL driver classname */
     private static final String mysqlDriver = "com.mysql.jdbc.Driver";

     /** URL of database to use */
     private static final String DB_URL = "jdbc:mysql://localhost/jena";
     private static final String DB_TYPE = "MySQL";

     /** User credentials */
     private static final String DB_USER = "user";
     private static final String DB_PASSWORD = "password";

     /** Name of the Jena model to create */
     private static final String MODEL_NAME = "wordnet";

     static public final String NL = System.getProperty("line.separator");

     /**
      * Finds the hypernym of a word given on the commandline
      */
     private static void printUsage() {
      System.out.print("\nUsage:\n");
      System.out.print("\tFindHypernym <hyponym>\n\n");
      System.out.print("\thyponym\t- The word to find hypernyms for\n");
     }
     public static void doSearch(Model m,String s){
      String BASE = "http://www.cogsci.princeton.edu/~wn/schema/" ;
      //Var var_hyponym = Var.alloc("hyponym") ;
            Var var_firstconcept = Var.alloc("firstconcept") ;
            Var var_hypernym = Var.alloc("hypernym");
            Var var_secondconcept = Var.alloc("secondconcept");
            Var var_definition = Var.alloc("definition");
            
            //WorkingVar wvar_hyponym = new WorkingVar();
      //wvar_hyponym.setString(s);
      Var var_hyponym=Var.alloc("hyponym");
      System.out.println(var_hyponym.toString());
            BasicPattern bp = new BasicPattern() ;
            bp.add(new Triple(var_firstconcept, Node.createURI(BASE+"wordForm"), var_hyponym)) ;
            bp.add(new Triple(var_firstconcept, Node.createURI(BASE+"hyponymOf"), var_secondconcept)) ;
            bp.add(new Triple(var_secondconcept, Node.createURI(BASE+"wordForm"), var_hypernym)) ;
            bp.add(new Triple(var_secondconcept, Node.createURI(BASE+"glossaryEntry"), var_definition)) ;
            
            Op op = new OpBGP(bp) ;
            m.write(System.out, "TTL") ;
            System.out.println("--------------") ;
            System.out.print(op) ;
            System.out.println("--------------") ;

            // ---- Execute expression
            QueryIterator qIter = Algebra.exec(op, m.getGraph()) ;
            
            // -------- Either read the query iterator directly ...
            if ( false )
            {
                for ( ; qIter.hasNext() ; )
                {
                    Binding b = qIter.nextBinding() ;
                    Node n = b.get(var_hypernym) ;
                    System.out.println(FmtUtils.stringForNode(n)) ;
                    Node n1 = b.get(var_definition) ;
                    System.out.println(FmtUtils.stringForNode(n1)) ;
                    System.out.println(b) ;
                }
                qIter.close() ;
            }
            else
            {
                // -------- Or make ResultSet from it (but not both - reading an
                //          iterator consumes the current solution)
                List varNames = new ArrayList() ;
                varNames.add("hypernym") ;
                varNames.add("definition") ;
                ResultSet rs = new ResultSetStream(varNames, m, qIter);
                ResultSetFormatter.out(rs) ;
                qIter.close() ;
            }
            //System.exit(0) ;
     }
     public static void main(String args[]) {

      // Check that the user provided a single argument
      if (args.length != 1) {
       printUsage();
       System.exit(-1);
      }

      try {
       // Instantiate database driver
       Class.forName(mysqlDriver);
      } catch (ClassNotFoundException e) {
       System.err.println("MySQL driver class not found");
       System.exit(-1);
      }

      // Get a connection to the db
      DBConnection connection = new DBConnection(DB_URL, DB_USER,
        DB_PASSWORD, DB_TYPE);

      // Get hold of the existing wordnet model
      ModelRDB model = ModelRDB.open(connection, MODEL_NAME);
      
      String prolog = "PREFIX wn: <http://www.cogsci.princeton.edu/~wn/schema/>";
      // Create a query
      
      String queryString = prolog + NL + "SELECT ?hypernym ?definition "
        + "WHERE {?firstconcept wn:wordForm ?hyponym. "
        + "?firstconcept wn:hyponymOf ?secondconcept. "
        + "?secondconcept wn:wordForm ?hypernym. "
        + "?secondconcept wn:glossaryEntry ?definition.} ";
      
      Query query = QueryFactory.create(queryString);
      query.serialize(new IndentedWriter(System.out, true));
      System.out.println();
      
      RDFNode hyponym = model.createLiteral(args[0]);
      //store the node in a QuerySolutionMap
      QuerySolutionMap initialBindings = new QuerySolutionMap();
      initialBindings.add("hyponym", hyponym);
       
      QueryExecution qexec = QueryExecutionFactory.create(query, model,initialBindings);
      ResultSet results = qexec.execSelect();
      // Output the results
      System.out.println("Hypernyms found for '" + args[0] + "':");
      System.out.println();
      ResultSetFormatter.out(System.out, results, query);
      qexec.close();
      System.out.println("========================");
      //doSearch(model,args[0]);
      try {
       // Close the database connection
       connection.close();
      } catch (java.sql.SQLException e) {
      }
     }

     // results.close() ;
     /* Property formword=model.CreateResouces()
     /*
      * ResIterator iter = model.listSubjectsWithProperty(); if (iter.hasNext()) {
      * System.out.println("The database contains vcards for:"); while
      * (iter.hasNext()) { System.out.println(" " + iter.nextStatement()
      * .getProperty(VCARD.FN) .getString()); } } else { System.out.println("No
      * vcards were found in the database"); }
      */
    }

    运行结果:
      1 PREFIX  wn:   <http://www.cogsci.princeton.edu/~wn/schema/>
      2
      3 SELECT  ?hypernym ?definition
      4 WHERE
      5   { ?firstconcept
      6               wn:wordForm       ?hyponym ;
      7               wn:hyponymOf      ?secondconcept .
      8     ?secondconcept
      9               wn:wordForm       ?hypernym ;
    10               wn:glossaryEntry  ?definition .
    11   }

    Hypernyms found for 'tiger':

    --------------------------------------------------------------------------------------------
    | hypernym     | definition                                                                |
    ============================================================================================
    | "big cat"    | "any of several large cats typically able to roar and living in the wild" |
    | "cat"        | "any of several large cats typically able to roar and living in the wild" |
    | "soul"       | "a human being; \"there was too much for one person to do\""              |
    | "human"      | "a human being; \"there was too much for one person to do\""              |
    | "individual" | "a human being; \"there was too much for one person to do\""              |
    | "mortal"     | "a human being; \"there was too much for one person to do\""              |
    | "person"     | "a human being; \"there was too much for one person to do\""              |
    | "somebody"   | "a human being; \"there was too much for one person to do\""              |
    | "someone"    | "a human being; \"there was too much for one person to do\""              |
    --------------------------------------------------------------------------------------------
    嗯,好象是运行成功了,不明白preson,somebody怎么也是上位词了呢。
    还有根据Algebra例子写的一个doSearch方法没完成,请高手指点一下,谢谢!

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/2/7 22:34:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/10 12:27:00

    本主题贴数86,分页: [1] [2] [3] [4]... [9]

     *树形目录 (最近20个回帖) 顶端 
    主题:  Jena文档《An Introduction to RDF ..(29693字) - april1019,2005年7月22日
        回复:  谢谢分享(8字) - 廖娟娟,2015年5月27日
        回复:  感谢!(6字) - cpahuangyg,2012年9月26日
        回复:  崇拜得不行了(12字) - zhenzixiong,2011年3月22日
        回复:  太感谢了(8字) - yinshiwei,2011年3月7日
        回复:  赞!(14字) - zym88,2009年5月7日
        回复:  我想问一下Jena Tutorial的代码从哪里找啊,怎么我在HP的网站上找不到相关的资料那?哪位..(198字) - lipeiqiang1997,2009年3月17日
        回复:  我想问一下Jena Tutorial的代码从哪里找啊,怎么我在HP的网站上找不到相关的资料那?哪位..(198字) - lipeiqiang1997,2009年3月17日
        回复:  我的邮箱是lipeiqiang84@163.com(30字) - lipeiqiang1997,2009年3月10日
        回复:  你能不能把Jena API文档给我发一份。从网上下载不下来,我还是个新手,谢谢..(70字) - lipeiqiang1997,2009年3月10日
        回复:  非常感谢(10字) - hanfei813,2009年2月23日
        回复:  姐姐果然不一般,感谢您为我们初学者提供易于接受的中文译文!(58字) - Humphrey,2008年10月29日
        回复:  多谢!(6字) - lassiefly,2008年10月28日
        回复:  不错 收藏了(11字) - mrcold,2008年7月26日
        回复:  支持外文资料的本土化进程!呵呵!楼主很强!(39字) - jiangyue0011,2008年7月1日
        回复:  真的很好,非常感谢!!!(21字) - ganggao,2008年6月26日
        回复:  谢谢!(6字) - hyue2009,2008年4月27日
        回复:  thanks ...... good poster ~(27字) - dasotkb,2008年4月23日
        回复:  学习中。。。(12字) - xiawared,2008年4月7日
        回复:  import java.util.ArrayList;import java.util.List..(8398字) - guffey,2008年2月7日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    113.281ms