1 2 3 4 5 package net.nutch.ontology; 6 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.regex.Matcher ; 11 import java.util.regex.Pattern ; 12 14 import com.hp.hpl.jena.ontology.OntClass; 15 import com.hp.hpl.jena.ontology.OntModel; 16 import com.hp.hpl.jena.ontology.Individual; 17 import com.hp.hpl.jena.rdf.model.Literal; 18 19 24 public class OwlParser implements Parser { 25 public OwlParser () { 26 } 27 28 31 public void parse(OntModel m) { 32 for (Iterator i = rootClasses( m ); i.hasNext(); ) { 33 OntClass c = (OntClass) i.next(); 34 35 if (c.isAnon()) { 37 continue; 38 } 39 40 parseClass( c, new ArrayList (), 0 ); 41 } 42 } 43 44 protected void parseClass( OntClass cls, List occurs, int depth ) { 45 if (cls.isAnon()) { 47 return; 48 } 49 50 Iterator labelIter = cls.listLabels(null); 53 if (!labelIter.hasNext()) { 55 cls.addLabel(rdfidToLabel(cls.getLocalName()), null); 57 } 58 labelIter = cls.listLabels(null); 60 61 while(labelIter.hasNext()) { 62 Literal l = (Literal) labelIter.next(); 63 OntologyImpl.addSearchTerm(l.toString(), cls); 64 } 65 66 if (cls.canAs( OntClass.class ) && !occurs.contains( cls )) { 68 for (Iterator i = cls.listSubClasses( true ); i.hasNext(); ) { 70 OntClass sub = (OntClass) i.next(); 71 72 occurs.add( cls ); 74 parseClass(sub, occurs, depth+1); 75 occurs.remove( cls ); 76 } 77 78 for (Iterator i=cls.listInstances(); i.hasNext(); ) { 80 82 Individual individual = (Individual) i.next(); 84 for (Iterator j=individual.listLabels(null); j.hasNext();) { 85 Literal l = (Literal) j.next(); 86 OntologyImpl.addSearchTerm(l.toString(), individual); 87 } 88 } 89 } 90 } 91 92 public Iterator rootClasses( OntModel m ) { 93 List roots = new ArrayList (); 94 95 for (Iterator i = m.listClasses(); i.hasNext(); ) { 96 OntClass c = (OntClass) i.next(); 97 98 try { 99 if (c.isAnon()) { 101 continue; 102 } 103 104 if (c.hasSuperClass( m.getProfile().THING(), true ) ) { 105 roots.add( c ); 107 } else if (c.getCardinality( m.getProfile().SUB_CLASS_OF() ) == 0 ) { 108 roots.add( c ); 111 } 112 } catch (Exception e) { 113 System.out.println(e.getMessage()); 115 } 116 } 117 118 return roots.iterator(); 119 } 120 121 public String rdfidToLabel (String idString) { 122 Pattern p = Pattern.compile("([a-z0-9])([A-Z])"); 123 Matcher m = p.matcher(idString); 124 125 String labelString = new String (idString); 126 while(m.find()) { 127 labelString = labelString.replaceAll(m.group(1)+m.group(2), 128 m.group(1)+" "+m.group(2)); 129 } 130 return labelString; 131 } 132 133 } 134 | Popular Tags |