nlentityrenderer.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 513 行 · 第 1/2 页
JAVA
513 行
if (!done) { /* We need to give at least an empty definition */ println( className + " is an undefined class." ); } } protected void renderIndividual(OWLIndividual ind) throws OWLException { OWLIndividualImpl test = (OWLIndividualImpl) ind; OWLDataFactoryImpl factory = (OWLDataFactoryImpl) test.getOWLDataFactory(); boolean done = false; String indName = this.getShortForm(ind.getURI()); // ** global reset tree ** //((NLVisitor) visitor).resetNLTree(indName, NLVisitor.LINK_EQUIVALENT); if(!ind.getAnnotations(reasoner.getOntology()).isEmpty()) { println("Annotations:"); renderAnnotations(ind); println("\n"); done = true; } // ** global reset tree ** ((NLVisitor) visitor).resetNLTree(indName, NLVisitor.LINK_SUBCLASS, 3); println("Details:"); for ( Iterator it = ind.getTypes( reasoner.getOntologies() ).iterator(); it.hasNext(); ) { OWLDescription eq = (OWLDescription) it.next(); eq.accept(visitor); // ** local reset tree ** ((NLVisitor) visitor).setLinkContext(NLVisitor.LINK_SUBCLASS); ((NLVisitor) visitor).resetParent(); } // ** global reset tree ** //((NLVisitor) visitor).resetNLTree(indName, NLVisitor.LINK_SOMEVALUES, 3); ((NLVisitor) visitor).setLinkContext(NLVisitor.LINK_SUBCLASS); ((NLVisitor) visitor).resetParent(); Map oPropVals = ind.getObjectPropertyValues( reasoner.getOntologies() ); for ( Iterator it = oPropVals.keySet().iterator(); it.hasNext(); ) { OWLObjectProperty currKey = (OWLObjectProperty) it.next(); Set targetSet = (Set) oPropVals.get( currKey ); System.out.println( targetSet.getClass() ); for ( Iterator t = targetSet.iterator(); t.hasNext(); ) { OWLIndividual target = (OWLIndividual) t.next(); System.out.println( target + " ++ " + target.getClass() ); OWLObjectPropertyInstance propInst = new OWLObjectPropertyInstanceImpl( factory, ind, currKey, target ); propInst.accept( visitor ); } // ** local reset tree ** ((NLVisitor) visitor).setLinkContext(NLVisitor.LINK_SUBCLASS); ((NLVisitor) visitor).resetParent(); } visitor.reset(); ((NLVisitor) visitor).printTree(); print(postProcess(visitor.result())); done = true; if (!done) { /* We need to give at least an empty definition */ println( indName + " is an undefined individual." ); } return; } protected void renderAnnotationProperty(OWLAnnotationProperty prop) throws OWLException { println(" <b>AnnotationProperty</b>(" + getShortForm(prop.getURI()) + ")"); } protected void renderObjectProperty(OWLObjectProperty prop) throws OWLException { // TODO: Not implemented yet pw.print("Natural Language rendering of properties not implemented yet"); return; } protected void renderDataProperty(OWLDataProperty prop) throws OWLException { // TODO: Not implemented yet pw.print("Natural Language rendering of properties not implemented yet"); return; } protected void renderDataType(OWLDataType datatype) throws OWLException { println("<b>Datatype</b>(" + getShortForm(datatype.getURI()) + ")"); } protected void println() { pw.println(); } protected void renderEntity() throws OWLException { // turn qnames off in Natural Language mode //if (swoopModel.showQNames()) swoopModel.setShowQNames(false); print("<html><body><code><pre><FONT FACE=\""+swoopModel.getFontFace()+"\" SIZE="+fontSize+">"); super.renderEntity(); print("</FONT></pre></code></body></html>"); } protected void renderPropertyAxiom(OWLPropertyAxiom axiom) throws OWLException { } protected String sanitize(String str) { str = str.replaceAll("&", "&"); str = str.replaceAll("<", "<"); str = str.replaceAll(">", ">"); return str; } protected void renderIndividualAxiom(OWLIndividualAxiom axiom) throws OWLException { OWLIndividual ind = (OWLIndividual) entity; pw.println("<rdf:Description rdf:about=\"" + ind.getURI() + "\">"); Set individuals = axiom.getIndividuals(); for (Iterator it = individuals.iterator(); it.hasNext(); ) { OWLIndividual oi = (OWLIndividual) it.next(); if(oi.equals(ind)) continue; pw.println(INDENT + "<" + (axiom instanceof OWLDifferentIndividualsAxiom ? "owl:differentFrom" : "owl:sameAs") + " rdf:resource=\"" // + getShortForm(oi.getURI()) + oi.getURI() + "\"/>"); } } public String getShortForm(URI uri) throws OWLException { String sf = shortForm(uri); if (sf.indexOf(":")>=0) sf = sf.substring(sf.indexOf(":")+1, sf.length()); return sf; } protected void renderForeignEntity(OWLEntity ent) throws OWLException { } // eliminate the duplicate words, example has has // split the string into words, eliminate the ones that are duplicate and rebuild it // right now it only works for 1 word , but it can be extended if needed to handle n-grams // it only works when the repetition occurs immediately after, as its purpose is to clean up // the has has stuff protected String eliminateDuplicateWords(String str) { String[] tokens = str.split("( )+"); String strTokens = ""; for (int i=0; i<tokens.length-1;i++) { if (!tokens[i].equalsIgnoreCase(tokens[i+1])) strTokens += (tokens[i] + " "); } strTokens += tokens[tokens.length-1]; return strTokens; } protected boolean startsWithVowel(String str) { if (str.toLowerCase().charAt(0) == 'a' || str.charAt(0) == 'e' || str.charAt(0) == 'i' || str.charAt(0) == 'o' || str.charAt(0) == 'u' ) return true; else return false; } protected String fixAorAN(String str) { String[] tokens = str.split("( )+"); String strTokens = ""; for (int i=0; i<tokens.length-1;i++) { if (tokens[i].equals("a") && startsWithVowel(tokens[i+1])) strTokens += ("an "); else strTokens += (tokens[i] + " "); } strTokens += tokens[tokens.length-1]; return strTokens; } protected String removeThingClause(String str) { str = str.replaceAll("and is a thing", ""); str = str.replaceAll("and is not a thing", ""); return str; } protected String insertHyperlinks(String tmp) { //TODO// tmp = " " + tmp +" ";// Hashtable hLinks = ((NLVisitor) visitor).hyperlinkMap;// for (Iterator iter = hLinks.keySet().iterator(); iter.hasNext(); ) {// String key = iter.next().toString();// String value = hLinks.get(key).toString();// tmp = tmp.replaceAll(" "+key+" ", value);// } return tmp; } protected String removeHasIsClause(String str) { str = str.replaceAll("has is", "is"); return str; } protected String insertBreaks(String str) { int pos = 0; String newStr = ""; int breakPos = 80; while (str.length()>pos+breakPos) { newStr += str.substring(pos, pos+breakPos); if (!newStr.endsWith(" ")) newStr += "-"; newStr += "<br>"; str = str.substring(pos+breakPos, str.length()); pos += breakPos; } newStr += str; return newStr; } protected String postProcess(String str) { String tmp = eliminateDuplicateWords(str); tmp = fixAorAN(tmp); tmp = removeThingClause(tmp); // tmp = removeHasIsClause(tmp); // tmp = insertBreaks(tmp); //TODO: Need to improve the hyperlinking code tmp = insertHyperlinks(tmp); // do this right at the end of post processing return tmp; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?