1 2 3 4 5 package net.nutch.ontology; 6 7 import net.nutch.util.NutchConf; 8 import net.nutch.util.LogFormatter; 9 10 import com.hp.hpl.jena.ontology.Individual; 11 import com.hp.hpl.jena.ontology.OntClass; 12 import com.hp.hpl.jena.ontology.OntModel; 13 import com.hp.hpl.jena.ontology.OntModelSpec; 14 import com.hp.hpl.jena.ontology.OntResource; 15 import com.hp.hpl.jena.ontology.Restriction; 16 import com.hp.hpl.jena.rdf.model.Literal; 17 import com.hp.hpl.jena.rdf.model.Resource; 18 import com.hp.hpl.jena.rdf.model.ModelFactory; 19 import com.hp.hpl.jena.shared.PrefixMapping; 20 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.LinkedList ; 27 28 import java.util.logging.Logger ; 29 30 import java.io.PrintStream ; 31 32 39 public class OntologyImpl implements net.nutch.ontology.Ontology { 40 public static final Logger LOG = 41 LogFormatter.getLogger("net.nutch.ontology.Ontology"); 42 43 public final static String DELIMITER_SEARCHTERM = " "; 44 45 private static Hashtable searchTerms = new Hashtable (); 46 private static Parser parser; 47 48 private static OntModel ontologyModel; 50 51 private static Ontology ontology = null; 52 53 private static Map m_anonIDs = new HashMap (); 54 private static int m_anonCount = 0; 55 56 public OntologyImpl() { 57 if (ontology == null) { 60 LOG.info( "creating new ontology"); 61 parser = new OwlParser(); 62 ontology = this; 63 } 64 65 if (ontologyModel == null) 66 ontologyModel = 67 ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null); 68 } 70 71 public static Ontology getInstance () { 72 if (ontology == null) { 73 ontology = new net.nutch.ontology.OntologyImpl(); 75 } 76 return ontology; 77 } 78 79 public void load (String [] urls) { 80 for (int i=0; i<urls.length; i++) { 81 String url = urls[i].trim(); 82 if (!url.equals("")) 83 load(ontologyModel, url); 84 } 85 parser.parse(ontologyModel); 86 } 87 88 private void load (Object m, String url) { 89 try { 90 LOG.info( "reading "+url); 91 ((OntModel)m).read(url); 92 } catch (Exception e) { 93 LOG.severe("failed on attempting to read ontology "+url); 94 LOG.severe(e.getMessage()); 95 StackTraceElement [] traces = e.getStackTrace(); 96 for (int i=0; i<traces.length; i++) { 97 LOG.severe(traces[i].toString()); 98 } 99 } 100 } 101 102 public static Parser getParser() { 103 if (parser == null) { 104 parser = new OwlParser(); 105 } 106 return parser; 107 } 108 109 public static OntModel getModel() { 110 return (OntModel)ontologyModel; 111 } 112 113 117 120 public Iterator subclasses (String entitySearchTerm) { 121 Map classMap = retrieve(entitySearchTerm); 122 Map subclasses = new HashMap (); 123 124 Iterator iter = classMap.keySet().iterator(); 125 while (iter.hasNext()) { 126 OntResource resource = (OntResource) iter.next(); 128 129 if (resource instanceof OntClass) { 130 for (Iterator i=((OntClass)resource).listSubClasses(); i.hasNext();) { 132 OntResource subclass = (OntResource) i.next(); 133 for (Iterator j=subclass.listLabels(null); j.hasNext();) { 134 Literal l = (Literal) j.next(); 135 subclasses.put(l.toString(), "1"); 136 } 137 } 138 for (Iterator i=((OntClass)resource).listInstances(); i.hasNext();) { 140 OntResource subclass = (OntResource) i.next(); 141 for (Iterator j=subclass.listLabels(null); j.hasNext();) { 142 Literal l = (Literal) j.next(); 143 subclasses.put(l.toString(), "1"); 144 } 145 } 146 } else if (resource instanceof Individual) { 147 for (Iterator i=resource.listSameAs(); i.hasNext();) { 148 OntResource subclass = (OntResource) i.next(); 149 for (Iterator j=subclass.listLabels(null); j.hasNext();) { 150 Literal l = (Literal) j.next(); 151 subclasses.put(l.toString(), "1"); 152 } 153 } 154 } 155 } 156 157 return subclasses.keySet().iterator(); 158 } 159 160 163 public Iterator synonyms (String queryKeyPhrase) { 164 queryKeyPhrase = queryKeyPhrase.replaceAll("\\s+", "\\+"); 166 167 Map classMap = retrieve(queryKeyPhrase); 168 169 Map synonyms = new HashMap (); 170 171 Iterator iter = classMap.keySet().iterator(); 172 while (iter.hasNext()) { 173 OntResource resource = (OntResource) iter.next(); 174 175 for (Iterator i=resource.listLabels(null); i.hasNext();) { 177 Literal l = (Literal) i.next(); 178 synonyms.put(l.toString(), "1"); 179 } 180 181 if (resource instanceof Individual) { 182 for (Iterator i=resource.listSameAs(); i.hasNext();) { 184 Individual individual = (Individual) i.next(); 185 for (Iterator j =individual.listLabels(null); j.hasNext();) { 187 Literal l = (Literal) i.next(); 188 synonyms.put(l.toString(), "1"); 189 } 190 } 191 } else if (resource instanceof OntClass) { 192 for (Iterator i=((OntClass)resource).listEquivalentClasses(); 194 i.hasNext();) { 195 OntClass equivClass = (OntClass) i.next(); 196 for (Iterator j=equivClass.listLabels(null); j.hasNext();) { 198 Literal l = (Literal) j.next(); 199 synonyms.put(l.toString(), "1"); 200 } 201 } 202 } 203 } 204 205 return synonyms.keySet().iterator(); 206 } 207 208 public static void addSearchTerm(String label, OntResource resource) { 209 Map m = retrieve(label); 210 if (m == null) { 211 m=new HashMap (); 212 } 213 m.put(resource, "1"); 214 searchTerms.put(label.toLowerCase(), m); 215 } 216 217 public static Map retrieve(String label) { 218 Map m = (Map ) searchTerms.get(label.toLowerCase()); 219 if (m==null) { 220 m = new HashMap (); 221 } 222 return m; 223 } 224 225 protected static void renderHierarchy( PrintStream out, OntClass cls, 226 List occurs, int depth ) { 227 renderClassDescription( out, cls, depth ); 228 out.println(); 229 230 if (cls.canAs( OntClass.class ) && !occurs.contains( cls )) { 232 for (Iterator i = cls.listSubClasses( true ); i.hasNext(); ) { 233 OntClass sub = (OntClass) i.next(); 234 235 occurs.add( cls ); 237 renderHierarchy( out, sub, occurs, depth + 1 ); 238 occurs.remove( cls ); 239 } 240 for (Iterator i=cls.listInstances(); i.hasNext(); ) { 241 Individual individual = (Individual) i.next(); 242 renderURI(out, individual.getModel(), individual.getURI()); 243 out.print(" ["); 244 for (Iterator j=individual.listLabels(null); j.hasNext();) { 245 out.print(((Literal)j.next()).getString()+", "); 246 } 247 out.print("] "); 248 out.println(); 249 } 250 } 251 } 252 253 public static void renderClassDescription( PrintStream out, 254 OntClass c, int depth ) { 255 indent( out, depth ); 256 257 if (c.isRestriction()) { 258 renderRestriction( out, (Restriction) c.as( Restriction.class ) ); 259 } else { 260 if (!c.isAnon()) { 261 out.print( "Class " ); 262 264 out.print (c.getLocalName()); 265 266 out.print( " [" ); 267 for (Iterator i=c.listLabels(null); i.hasNext(); ) { 268 out.print(((Literal)i.next()).getString()+", "); 269 } 270 out.print( "] "); 271 } else { 272 renderAnonymous( out, c, "class" ); 273 } 274 } 275 } 276 277 protected static void renderRestriction( PrintStream out, Restriction r ) { 278 if (!r.isAnon()) { 279 out.print( "Restriction " ); 280 renderURI( out, r.getModel(), r.getURI() ); 281 } else { 282 renderAnonymous( out, r, "restriction" ); 283 } 284 285 out.print( " on property " ); 286 renderURI( out, r.getModel(), r.getOnProperty().getURI() ); 287 } 288 289 protected static void renderURI( PrintStream out, 290 PrefixMapping prefixes, String uri ) { 291 out.print( prefixes.usePrefix( uri ) ); 292 } 293 294 protected static void renderAnonymous( PrintStream out, 295 Resource anon, String name ) { 296 String anonID = (String ) m_anonIDs.get( anon.getId() ); 297 if (anonID == null) { 298 anonID = "a-" + m_anonCount++; 299 m_anonIDs.put( anon.getId(), anonID ); 300 } 301 302 out.print( "Anonymous "); 303 out.print( name ); 304 out.print( " with ID " ); 305 out.print( anonID ); 306 } 307 308 protected static void indent( PrintStream out, int depth ) { 309 for (int i = 0; i < depth; i++) { 310 out.print( " " ); 311 } 312 } 313 314 public static void main( String [] args ) throws Exception { 315 316 Ontology ontology = OntologyFactory.getOntology(); 317 318 String urls = NutchConf.get("extension.ontology.urls"); 319 if (urls==null || urls.trim().equals("")) { 320 LOG.severe("No ontology url found."); 321 return; 322 } 323 ontology.load(urls.split("\\s+")); 324 LOG.info( "created new ontology"); 325 326 for (Iterator i = getParser().rootClasses( getModel() ); 327 i.hasNext(); ) { 328 329 OntClass c = (OntClass) i.next(); 331 332 renderHierarchy(System.out, c, new LinkedList (), 0); 333 } 334 335 String [] terms = 336 new String [] { "Season" }; 337 338 for (int i=0; i<terms.length; i++) { 339 Iterator iter = ontology.subclasses(terms[i]); 340 while (iter.hasNext()) { 341 System.out.println("subclass >> "+(String )iter.next()); 342 } 343 } 344 } 345 } 346 | Popular Tags |