2012-11-06 43 views
1

目前我正在做一個基於本體的信息檢索項目。我使用本體編輯器Protege創建了基本本體,並獲取了文件,比如說family.owl。基本上我想執行搜索功能。用戶提供搜索詞,然後用本體中的個體進行搜索。如果找到匹配,則打印與該個人相關的評論。我已經使用Jena API來解析本體。到目前爲止,我成功地獲得了與每個資源相關的主題,謂詞和對象,但是我無法獲得該評論與每個資源相關聯。 family.owl的部分看起來像這樣如何使用Jena API從貓頭鷹文件中檢索評論?

<!-- http://www.semanticweb.org/ontologies/2012/9/family.owl#Beth --> 
<owl:Thing rdf:about="#Beth"> 
    <rdfs:comment>Beth is the Daughter of Adam . She is the Sister of Chuck . 
    She is the Mother of Dotty & Edward . She is the Aunt of Fran & Greg. 
    </rdfs:comment> 
    <isChildOf rdf:resource="#Adam"/> 
    <isSiblingOf rdf:resource="#Chuck"/> 
</owl:Thing> 

所以,當我搜索貝絲,我應該得到與它相關ie.Beth是亞當的女​​兒的意見。她是Chuck的妹妹。她是Dotty的母親& Edward。她的弗蘭& Greg.The代碼大媽我用得到的主語,謂語和對象如下

StmtIterator iter=model.listStatements();   
    while(iter.hasNext()) 
    { 
     Statement stmt=iter.nextStatement(); 
     String subject=stmt.getSubject().toString;        
     String predicate=stmt.getPredicate().toString();    
     String object=stmt.getObject().toString(); 
     ... 
    } 

回答

2

rdfs:comment應該在那裏爲你得到一個謂詞(其toString,這建議您不要依賴,將是:http://www.w3.org/2000/01/rdf-schema#comment)。如果它不存在,那麼您的代碼不是您向我們展示的內容,或者數據不是您所引用的內容。由於我找不到所指的本體論,所以我們無法檢查。

更簡單的方法做你想要將使用Jena ontology API什麼。使用本體API,你可以做這樣的事情(我沒有運行這個代碼,但它應該可以工作):

OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); 
FileManager.get().readModel(m, "... your input file here ..."); 

String NS = "http://www.semanticweb.org/ontologies/2012/9/family.owl#"; 
Individual beth = m.getIndividual(NS + "Beth"); 
String comment = beth.getComment(); 
+0

Thanks Ian !!!它工作完美... – user1800722

+0

在這種情況下,請點擊綠色複選標記將答案標記爲已接受。這告訴其他用戶你已經找到了你的問題的答案。 –

+0

in beth.getComment()傳遞了什麼參數 – fanky