📄 findinferredhypernyms.java
字号:
import com.hp.hpl.jena.db.*;import com.hp.hpl.jena.rdf.model.*;import com.hp.hpl.jena.reasoner.ReasonerRegistry;import com.hp.hpl.jena.reasoner.Reasoner;import com.hp.hpl.jena.vocabulary.RDF;import com.hp.hpl.jena.vocabulary.OWL;import com.hp.hpl.jena.ontology.OntModel;//import com.hp.hpl.jena.rdql.*;
import com.hp.hpl.jena.sparql.lang.rdql.*;import java.io.PrintWriter;import java.util.Iterator;/** * Uses Jena's OWL reasoner to find all hyponyms of a given word */public class FindInferredHypernyms { /** 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 = "username"; private static final String DB_PASSWORD = "password"; /** Name of the Jena model to create */ private static final String MODEL_NAME = "wordnet-plants"; /** * Finds the hypernyms of a wordform given on the commandline */ 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); // Grab a ref to the existing model ModelRDB model = ModelRDB.open(connection,MODEL_NAME); // Make a new model to act as an OWL ontology for WordNet OntModel wnOntology = ModelFactory.createOntologyModel(); // Use OntModel's convenience method to describe WordNet's hyponymOf property as transitive wnOntology.createTransitiveProperty(WordnetVocab.hyponymOf.getURI()); // Create a reasoner bound to the ontology Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner(); Reasoner wnReasoner = owlReasoner.bindSchema(wnOntology); // Use the reasoner to create a inference model InfModel infModel = ModelFactory.createInfModel(wnReasoner, model); // Create a query String queryString = "SELECT ?hypernym, ?definition " + "WHERE (?firstconcept, <wn:wordForm>, ?hyponym), " + "(?firstconcept, <wn:hyponymOf>, ?secondconcept), " + "(?secondconcept, <wn:wordForm>, ?hypernym), " + "(?secondconcept, <wn:glossaryEntry>, ?definition) " + "USING wn FOR <http://www.cogsci.princeton.edu/~wn/schema/>"; Query query = new Query(queryString); // Need to set the source since the query does not. query.setSource(infModel); // Bind the "hyponym" term in the query to the user's input ResultBinding initialBinding = new ResultBinding() ; WorkingVar hyponym = new WorkingVar(); hyponym.setString(args[0]); initialBinding.add("hyponym", hyponym); QueryExecution qe = new QueryEngine(query); // Execute the query with the binding we defined QueryResults results = qe.exec(initialBinding); // Output the results System.out.println("Hypernyms found for '"+args[0]+"':"); System.out.println(); while (results.hasNext()) { ResultBinding binding = (ResultBinding)results.next(); Object wordform = binding.get("hypernym"); Object definition = binding.get("definition"); System.out.println(wordform+":\t"+definition); } results.close() ; try { // Close the database connection connection.close(); } catch (java.sql.SQLException e) {} } private static void printUsage() { System.out.print("\nUsage:\n"); System.out.print("\tFindInferredHypernyms <hyponym>\n\n"); System.out.print("\thyponym\t- The word to find hypernyms for\n"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -