KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > ontology > OntologyImpl


1 /* Copyright (c) 2004 michael j pan. All rights reserved. */
2 /* Use subject to the same conditions as Nutch. */
3 /* see http://www.nutch.org/LICENSE.txt. */
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 JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 import java.util.logging.Logger JavaDoc;
29
30 import java.io.PrintStream JavaDoc;
31
32 /**
33  * this class wraps about a model,
34  * built from a list of ontologies,
35  * uses HP's Jena
36  *
37  * @author michael j pan
38  */

39 public class OntologyImpl implements net.nutch.ontology.Ontology {
40   public static final Logger JavaDoc LOG =
41     LogFormatter.getLogger("net.nutch.ontology.Ontology");
42
43   public final static String JavaDoc DELIMITER_SEARCHTERM = " ";
44
45   private static Hashtable JavaDoc searchTerms = new Hashtable JavaDoc();
46   private static Parser parser;
47
48   //private static Object ontologyModel;
49
private static OntModel ontologyModel;
50
51   private static Ontology ontology = null;
52
53   private static Map JavaDoc m_anonIDs = new HashMap JavaDoc();
54   private static int m_anonCount = 0;
55
56   public OntologyImpl() {
57     //only initialize all the static variables
58
//if first time called to this ontology constructor
59
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         //ModelFactory.createOntologyModel();
69
}
70
71   public static Ontology getInstance () {
72     if (ontology == null) {
73       //ontology = new net.nutch.ontology.Ontology();
74
ontology = new net.nutch.ontology.OntologyImpl();
75     }
76     return ontology;
77   }
78
79   public void load (String JavaDoc[] urls) {
80     for (int i=0; i<urls.length; i++) {
81       String JavaDoc url = urls[i].trim();
82       if (!url.equals(""))
83         load(ontologyModel, url);
84     }
85     parser.parse(ontologyModel);
86   }
87
88   private void load (Object JavaDoc m, String JavaDoc url) {
89     try {
90       LOG.info( "reading "+url);
91       ((OntModel)m).read(url);
92     } catch (Exception JavaDoc e) {
93       LOG.severe("failed on attempting to read ontology "+url);
94       LOG.severe(e.getMessage());
95       StackTraceElement JavaDoc[] 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   // not yet implemented
114
//public void merge (net.nutch.ontology.Ontology o) {
115
//}
116

117   /**
118    * retrieve all subclasses of entity(ies) hashed to searchTerm
119    */

120   public Iterator JavaDoc subclasses (String JavaDoc entitySearchTerm) {
121     Map JavaDoc classMap = retrieve(entitySearchTerm);
122     Map JavaDoc subclasses = new HashMap JavaDoc();
123   
124     Iterator JavaDoc iter = classMap.keySet().iterator();
125     while (iter.hasNext()) {
126       //OntClass resource = (OntClass) iter.next();
127
OntResource resource = (OntResource) iter.next();
128   
129       if (resource instanceof OntClass) {
130         //get subclasses
131
for (Iterator JavaDoc i=((OntClass)resource).listSubClasses(); i.hasNext();) {
132           OntResource subclass = (OntResource) i.next();
133           for (Iterator JavaDoc j=subclass.listLabels(null); j.hasNext();) {
134             Literal l = (Literal) j.next();
135             subclasses.put(l.toString(), "1");
136           }
137         }
138         //get individuals
139
for (Iterator JavaDoc i=((OntClass)resource).listInstances(); i.hasNext();) {
140           OntResource subclass = (OntResource) i.next();
141           for (Iterator JavaDoc 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 JavaDoc i=resource.listSameAs(); i.hasNext();) {
148           OntResource subclass = (OntResource) i.next();
149           for (Iterator JavaDoc 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   /**
161    * retrieves synonyms from wordnet via sweet's web interface
162    */

163   public Iterator JavaDoc synonyms (String JavaDoc queryKeyPhrase) {
164     //need to have a html quote method instead
165
queryKeyPhrase = queryKeyPhrase.replaceAll("\\s+", "\\+");
166
167     Map JavaDoc classMap = retrieve(queryKeyPhrase);
168
169     Map JavaDoc synonyms = new HashMap JavaDoc();
170
171     Iterator JavaDoc iter = classMap.keySet().iterator();
172     while (iter.hasNext()) {
173       OntResource resource = (OntResource) iter.next();
174
175       //listLabels
176
for (Iterator JavaDoc 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       //get all individuals same as this one
183
for (Iterator JavaDoc i=resource.listSameAs(); i.hasNext();) {
184           Individual individual = (Individual) i.next();
185           //add labels
186
for (Iterator JavaDoc 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         //list equivalent classes
193
for (Iterator JavaDoc i=((OntClass)resource).listEquivalentClasses();
194           i.hasNext();) {
195           OntClass equivClass = (OntClass) i.next();
196           //add labels
197
for (Iterator JavaDoc 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 JavaDoc label, OntResource resource) {
209     Map JavaDoc m = retrieve(label);
210     if (m == null) {
211       m=new HashMap JavaDoc();
212     }
213     m.put(resource, "1");
214     searchTerms.put(label.toLowerCase(), m);
215   }
216
217   public static Map JavaDoc retrieve(String JavaDoc label) {
218     Map JavaDoc m = (Map JavaDoc) searchTerms.get(label.toLowerCase());
219     if (m==null) {
220       m = new HashMap JavaDoc();
221     }
222     return m;
223   }
224
225   protected static void renderHierarchy( PrintStream JavaDoc out, OntClass cls,
226             List JavaDoc occurs, int depth ) {
227     renderClassDescription( out, cls, depth );
228     out.println();
229   
230     // recurse to the next level down
231
if (cls.canAs( OntClass.class ) && !occurs.contains( cls )) {
232       for (Iterator JavaDoc i = cls.listSubClasses( true ); i.hasNext(); ) {
233         OntClass sub = (OntClass) i.next();
234
235         // we push this expression on the occurs list before we recurse
236
occurs.add( cls );
237         renderHierarchy( out, sub, occurs, depth + 1 );
238         occurs.remove( cls );
239       }
240       for (Iterator JavaDoc i=cls.listInstances(); i.hasNext(); ) {
241         Individual individual = (Individual) i.next();
242         renderURI(out, individual.getModel(), individual.getURI());
243         out.print(" [");
244         for (Iterator JavaDoc 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 JavaDoc 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         //renderURI( out, c.getModel(), c.getURI() );
263

264         out.print (c.getLocalName());
265
266         out.print( " [" );
267         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc out,
290     PrefixMapping prefixes, String JavaDoc uri ) {
291     out.print( prefixes.usePrefix( uri ) );
292   }
293   
294   protected static void renderAnonymous( PrintStream JavaDoc out,
295     Resource anon, String JavaDoc name ) {
296     String JavaDoc anonID = (String JavaDoc) 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 JavaDoc out, int depth ) {
309     for (int i = 0; i < depth; i++) {
310       out.print( " " );
311     }
312   }
313
314   public static void main( String JavaDoc[] args ) throws Exception JavaDoc {
315
316     Ontology ontology = OntologyFactory.getOntology();
317
318     String JavaDoc 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 JavaDoc i = getParser().rootClasses( getModel() );
327       i.hasNext(); ) {
328     
329       //print class
330
OntClass c = (OntClass) i.next();
331
332       renderHierarchy(System.out, c, new LinkedList JavaDoc(), 0);
333     }
334
335     String JavaDoc[] terms =
336       new String JavaDoc[] { "Season" };
337
338     for (int i=0; i<terms.length; i++) {
339       Iterator JavaDoc iter = ontology.subclasses(terms[i]);
340       while (iter.hasNext()) {
341         System.out.println("subclass >> "+(String JavaDoc)iter.next());
342       }
343     }
344   }
345 }
346
Popular Tags