nlvisitor.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,843 行 · 第 1/4 页

JAVA
1,843
字号
	}	public void visit( OWLDataAllRestriction restriction ) throws OWLException {		prefixDomain(restriction.getDataProperty());		linkContext.setLinkType(LINK_ALLVALUES);		restriction.getDataProperty().accept( this );				restriction.getDataType().accept( this );			}	public void visit( OWLObjectCardinalityRestriction restriction ) throws OWLException {				prefixDomain(restriction.getObjectProperty());		int ncard = -1;		if ( restriction.isExactly() ) {			linkContext.setLinkType(LINK_CARD);			ncard = restriction.getAtLeast();					} 		else if ( restriction.isAtMost() ) {			linkContext.setLinkType(LINK_MAXCARD);			ncard = restriction.getAtMost();					} 		else if ( restriction.isAtLeast() ) {			linkContext.setLinkType(LINK_MINCARD);			ncard = restriction.getAtLeast();					}  		restriction.getObjectProperty().accept( this );				NLNode cardNode = new NLNode(String.valueOf(ncard), 2);		parent.addLink(linkContext, cardNode); 			}	public void visit( OWLDataCardinalityRestriction restriction ) throws OWLException {				prefixDomain(restriction.getProperty());		int ncard = -1;		if ( restriction.isExactly() ) {			linkContext.setLinkType(LINK_CARD);			ncard = restriction.getAtLeast();					} 		else if ( restriction.isAtMost() ) {			linkContext.setLinkType(LINK_MAXCARD);			ncard = restriction.getAtMost();					} 		else if ( restriction.isAtLeast() ) {			linkContext.setLinkType(LINK_MINCARD);			ncard = restriction.getAtLeast();					}  		restriction.getDataProperty().accept( this );				NLNode cardNode = new NLNode(String.valueOf(ncard), 2);		parent.addLink(linkContext, cardNode);	}	public void visit( OWLDataValueRestriction restriction ) throws OWLException {		prefixDomain(restriction.getDataProperty());		linkContext.setLinkType(LINK_HASVALUE);		restriction.getDataProperty().accept( this );		restriction.getValue().accept( this );			}	public void visit( OWLEquivalentClassesAxiom axiom ) throws OWLException {		pw.print("EquivalentClasses(");		for ( Iterator it = axiom.getEquivalentClasses().iterator();		it.hasNext(); ) {			OWLDescription desc = (OWLDescription) it.next();			desc.accept( this );			if (it.hasNext()) {				pw.print(" ");			}		}		pw.print(")");	}	public void visit( OWLDisjointClassesAxiom axiom ) throws OWLException {		pw.print("(");		for ( Iterator it = axiom.getDisjointClasses().iterator();		it.hasNext(); ) {			OWLDescription desc = (OWLDescription) it.next();			desc.accept( this );			if (it.hasNext()) {				pw.print(" " + ConciseFormat.DISJOINT + " ");			}		}		pw.print(")");	}	public void visit( OWLSubClassAxiom axiom ) throws OWLException {		pw.print("(");		axiom.getSubClass().accept( this );		pw.print(" " + ConciseFormat.SUBSET + " ");		axiom.getSuperClass().accept( this );		pw.print(")");	}	public void visit( OWLEquivalentPropertiesAxiom axiom ) throws OWLException {		pw.print("(");		for ( Iterator it = axiom.getProperties().iterator();		it.hasNext(); ) {			OWLProperty prop = (OWLProperty) it.next();			prop.accept( this );			if (it.hasNext()) {				pw.print(" = ");			}		}		pw.print(")");	}	public void visit( OWLSubPropertyAxiom axiom ) throws OWLException {		pw.print("(");		axiom.getSubProperty().accept( this );		pw.print(" " + ConciseFormat.SUBSET + " ");		axiom.getSuperProperty().accept( this );		pw.print(")");	}	public void visit( OWLDifferentIndividualsAxiom ax) throws OWLException {		pw.print("(");		for ( Iterator it = ax.getIndividuals().iterator();		it.hasNext(); ) {			OWLIndividual desc = (OWLIndividual) it.next();			desc.accept( this );			if (it.hasNext()) {				pw.print(" " + ConciseFormat.DISJOINT + " ");			}		}		pw.print(")");	}	public void visit( OWLSameIndividualsAxiom ax) throws OWLException {		pw.print("SameIndividual(");		for ( Iterator it = ax.getIndividuals().iterator();		it.hasNext(); ) {			OWLIndividual desc = (OWLIndividual) it.next();			desc.accept( this );			if (it.hasNext()) {				pw.print(" = ");			}		}		pw.print(")");	}	public void visit( OWLDataType ocdt ) throws OWLException {		//pw.print( "<a href=\"" + ocdt.getURI() + "\">" + getShortForm( ocdt.getURI() ) + "</a>" );				//pw.print( getShortForm( ocdt.getURI() ) );	}	public void visit( OWLDataEnumeration enumeration ) throws OWLException {		pw.print("{");		for ( Iterator it = enumeration.getValues().iterator();		it.hasNext(); ) {			linkContext.setLinkType(LINK_ONEOF);			OWLDataValue desc = (OWLDataValue) it.next();			desc.accept( this );			if (it.hasNext()) {				pw.print(", ");			}		}		pw.print("}");	}		public void prefixDomain(OWLProperty prop) {			    System.out.print( "PROP: " + prop );	    		String domainName = "";		URI uri = null;		try {			for (Iterator it = swoopModel.getReasoner().domainsOf(prop).iterator(); it.hasNext();) {				OWLDescription dom = (OWLDescription) it.next();				if (dom instanceof OWLClass) {					uri = ((OWLClass) dom).getURI();					domainName = getShortForm(((OWLClass) dom).getURI());					break;				}			}		}		catch (Exception e) {			e.printStackTrace();		}		finally {			if (domainName.equals("")) domainName = "Thing"; // THIS WAS CHANGED FROM thing						String tokens = getEntityTokens(domainName);	        // add hyperlink			hyperlinkMap.put(tokens, "<a href=\"" + uri + "\">" + tokens + "</a>");			NLNode target = new NLNode(tokens, 0);						//NLLink isaLink = new NLLink("", this.LINK_SUBCLASS);			parent = parent.addLink(linkContext, target);		}						System.out.println( " DOM: " + domainName );	}		// this is what does the splitting of prop names into tokens, here is where we add 	// delimiter support in the future	public String getEntityTokens(String entityName) {				String[] tokens = new String[10];		String strTokens = "";		int tLen = 0;		int prev = 0;				for (int i=1; i<entityName.length(); i++) {		    if ( Character.isUpperCase( entityName.charAt( i ) ) ) {			//if (entityName.charAt(i) >= 'A' && entityName.charAt(i) <= 'Z') {				if (i-prev>0) {					strTokens += (entityName.substring(prev,i) + " ");					tokens[tLen++] = entityName.substring(prev,i);					prev = i;				}			}						if (i==(entityName.length()-1)) {				// System.out.println(i+":"+prev+":"+entityName+":"+entityName.substring(prev));				tokens[tLen] = entityName.substring(prev);								strTokens += entityName.substring(prev);			}		}				return strTokens;			}		public String getShortForm(URI uri) throws OWLException {		String sf = shortForms.shortForm(uri);		if (sf.indexOf(":")>=0) sf = sf.substring(sf.indexOf(":")+1, sf.length());		return sf;	}		public void printTree() {	    		NLNode root = tree.getRoot();		cleanUpThingNodes();		printNode(root, true, 0);			}		private void putLinks(List sorted, HashMap map) {		for (Iterator i = map.keySet().iterator(); i.hasNext(); ) {			NLLink link = (NLLink) i.next();			NLNode node = (NLNode) map.get(link);			sorted.add(new NLLinkNode(link, node));		}	}		// onlyPrefix == false means return the rest, not the whole thing	private String getPart(String sentence, boolean onlyPrefix) {				// remove hyperlink stuff if any		if (sentence.indexOf("<a href")>=0) {			sentence = sentence.substring(sentence.indexOf("\">")+2, sentence.indexOf("</a>"));		}				if (sentence.indexOf(" ")==-1) return sentence;		if (onlyPrefix) {			String prefix = sentence.substring(0, sentence.indexOf(" "));			return prefix;		}		else {			String rest = sentence.substring(sentence.indexOf(" ")+1, sentence.length());			return rest;		}	}		/* New NLP Functions - Daniel */		// 0 = complex np (just multiple nouns)			phone number	// 1 = np and p 								child of	// 2 = vp and np								produces wine	// 3 = vp and p 								located in	// 4 = vp and pp (p and np)						made from grape	// 5 = is NP of									is specific function of	// -1 = other (unrecognized)					prop12		private int classifyComplexProp( String[] tags, String[] tokens ) {	    boolean containsVerb = false;	    boolean containsPrep = false;	    boolean containsNoun = false;	    boolean containsOf = false;	    boolean containsIs = false;	    	    for ( int i = 0; i < tags.length; i++ ) {	        String curr = tags[i];	    	        System.err.println( tokens[i] + " " + tags[i] );	        	        if ( curr.startsWith( "V" ) ) {	            containsVerb = true;	        } else if ( curr.startsWith( "N" ) ) {	            containsNoun = true;	            	            if ( i == tags.length - 1 ) {	                if ( containsVerb && containsPrep ) {	                    return 4;	                } else if ( containsVerb && !containsPrep ) {	                    return 2;	                } 	            }	        } else if ( curr.startsWith( "IN" ) ) {	            containsPrep = true;	            	            System.out.println( "PREP: " + tokens[i] );	            	            if ( tokens[i].equals( "of" ) ) 	                containsOf = true;	            	            if ( i == tags.length - 1 ) {	                if ( containsOf ) { 		// I assume that anything with of will always have an NP	                    if ( containsIs ) { 	// is brother of	                        System.out.println( "RETURNING 5" );	                        return 5;	                    } else {				// brother of	                        return 1;	                    }	                }	                	                if ( containsVerb ) {	                    return 3;	                } else if ( containsNoun ) {	                    return 1; 	                }	                //	                if ( containsNoun && !containsVerb ) {//	                    return 1;//	                } else if ( containsVerb && !containsNoun ) {//	                    return 3;//	                } 	            }		    } else if ( curr.startsWith( "B" ) ) {		        containsIs = true;		    }	    }	    	    if ( containsOf && containsIs ) 	        return 5;	    	    if ( containsNoun && !containsVerb && !containsPrep ) {	        return 0;	    }	    	    return -1;	}		private void cleanUpThingNodes() {	    NLNode root = tree.getRoot();	    	    System.out.println( "BEGINNING THING REMOVAL" );	    	    printDebugTree();	    	    reconcileThingNodes( root );	    	    printDebugTree();	    	    //reconcileNegativeThingNodes( root );	    	    //printDebugTree();	    	    reconcileNonIsaThingNodes( root );	    	    printDebugTree();	    	    System.out.println( "END THING REMOVAL" );	}		public void printDebugTree() {	    printDebugNode( tree.getRoot(), 0 );	}		private void printDebugNode( NLNode node, int indent ) {	    System.out.println( node.keyword );	    	    HashMap links = node.getLinks();	    Set keys = links.keySet();		for (Iterator iter = keys.iterator(); iter.hasNext(); ) {			// cycle through each link			NLLink link = (NLLink) iter.next();			NLNode target = (NLNode) links.get(link);						for ( int i = 0; i < indent; i++ ) 			    System.out.print( "\t" );						System.out.print( "(" + link.linkType + ") " + link.keyword + " " );						printDebugNode( target, indent + 1 );		}	}		// TODO make this fix the problem of multiple all values on same property	private void mergeAllValues( NLNode node ) {	    	}		// PROPERTIES	private void reconcileNonIsaThingNodes( NLNode node ) {	    HashMap links = node.getLinks();	    Set keys = links.keySet();	    NLLink isaLink = new NLLink("", NLVisitor.LINK_SUBCLASS);	    boolean thingFound = false;	    Set isaLinks = new HashSet();	    //Set nonIsaLinks = new HashSet();	    Set nonIsaThingLinks = new HashSet(); // may be more than one, unlike is-a		for (Iterator iter = new HashSet(keys).iterator(); iter.hasNext(); ) {			// cycle through each link			NLLink link = (NLLink) iter.next();			NLNode target = (NLNode) links.get(link);						System.out.println( "LINK: " + link.keyword + " (" + link.linkType + "), TARGET: " + target.keyword );						if ( !link.equals( isaLink ) ) {			    //nonIsaLinks.add( link );			    			    if ( target.getKeyword().equals( "Thing" ) || target.getKeyword().equals( "thing" ) ) {			        System.out.println( "NON-ISA thing found" );			        nonIsaThingLinks.add( link );			    } 			}		} 				// pulling-up named classes for non-isa thing links		for ( Iterator nt = nonIsaThingLinks.iterator(); nt.hasNext(); ) {		    NLLink curr = (NLLink) nt.next();		    		    NLNode thingNode = (NLNode) links.get( curr );		    HashMap tLinks = thingNode.getLinks();		    		    Set tNonThingTargets = new HashSet();		    Set tKeys = tLinks.keySet();		    for ( Iterator i = tKeys.iterator(); i.hasNext(); ) {		        NLLink tLink = (NLLink) i.next();				NLNode tTarget = (NLNode) tLinks.get(tLink);								if ( tLink.equals( isaLink ) ) {				    if ( !tTarget.getKeyword().equals( "Thing" ) && !tTarget.getKeyword().equals( "thing" ) ) {				        System.out.println( "pull-up" );				        				        System.out.println( links );				        				        links.remove( curr );				        				        links.put( curr, tTarget );				        				        tTarget.links.putAll( thingNode.links );				        tTarget.links.remove( tLink );				    } 				}		    }    		}	    		// Recurse		keys = links.keySet();		for (Iterator iter = keys.iterator(); iter.hasNext(); ) {			NLLink link = (NLLink) iter.next();			NLNode target = (NLNode) links.get(link);						System.out.println( "NEW LINK: " + link.keyword + " (" + link.linkType + "), TARGET: " + target.keyword );			reconcileNonIsaThingNodes( target );		}	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?