All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.ctakes.dependency.parser.util.DependencyUtility Maven / Gradle / Ivy

Go to download

This wraps the ClearNLP dependency parser and semantic role labeler into a UIMA friendly annotator

There is a newer version: 6.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.ctakes.dependency.parser.util;

import org.apache.ctakes.typesystem.type.syntax.ConllDependencyNode;
import org.apache.ctakes.typesystem.type.textspan.Sentence;
import org.apache.log4j.Logger;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.text.AnnotationIndex;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author m081914
 *
 */
public abstract class DependencyUtility {

	public static Logger logger = Logger.getLogger("org.apache.ctakes.dependency.parser.util.DependencyUtility");

	public static void addToIndexes( JCas jcas, ArrayList nodes ) {
		for (int i=0; i=annot2.getEnd() && 
			annot1.getCoveredText().contains(annot2.getCoveredText());
	}
	
	/** Find the sentence in which an Annotation lives **/
	public static Sentence getSentence( JCas jCas, Annotation annot ) {
		FSIterator sentences = jCas.getAnnotationIndex(Sentence.type).iterator();
		while (sentences.hasNext()) {
			Sentence sentence = (Sentence) sentences.next();
			if (doesSubsume(sentence,annot)) {
				return sentence;
			}
		}
		return null;
	}


	/** Returns the first ConllDependencyNode in the CAS w/ same begin and end as the given Annotation **/	
	public static ConllDependencyNode getDependencyNode(JCas jCas, Annotation annot) {

		AnnotationIndex nodeIndex = jCas.getAnnotationIndex(ConllDependencyNode.type);
	    FSIterator nodeIterator = nodeIndex.iterator();
	    while (nodeIterator.hasNext()) {
	        ConllDependencyNode node = (ConllDependencyNode) nodeIterator.next();
	        if (equalCoverage(annot,node)) {
	        	return node;
	        }
	    }
    	return null;
	}
	
	/** Returns the ConllDependencyNodes in the CAS w/ subsumed begins and ends **/	
	public static List getDependencyNodes(JCas jCas, Annotation annot) {
		ArrayList output = new ArrayList();
		AnnotationIndex nodeIndex = jCas.getAnnotationIndex(ConllDependencyNode.type);
	    FSIterator nodeIterator = nodeIndex.iterator();
	    while (nodeIterator.hasNext()) {
	        ConllDependencyNode node = (ConllDependencyNode) nodeIterator.next();
	        if (doesSubsume(annot,node)) {
	        	output.add(node);
	        }
	    }
    	return output;
	}

	/** Returns the first ConllDependencyNode in the CAS w/ same begin and end as the given Annotation **/	
	public static ConllDependencyNode getNominalHeadNode(JCas jCas, Annotation annot) {
		List nodes = getDependencyNodes(jCas, annot);
		if (nodes==null || nodes.size()==0) {
			return null;
		}
		return getNominalHeadNode( nodes );
	}

	/** Finds the head node out of a few ConllDependencyNodes. Biased toward nouns. **/
	public static ConllDependencyNode getNominalHeadNode(
			List nodes) {
		ArrayList anodes = new ArrayList(nodes);
		Boolean[][] matrixofheads = new Boolean[anodes.size()][anodes.size()];
		List outnodes = new ArrayList();

      // Remove root from consideration
		for (int i=0; i nodes, ConllDependencyNode node1, ConllDependencyNode node2) {

		DependencyPath pathUp1 = new DependencyPath();
		DependencyPath pathUp2 = new DependencyPath();
		DependencyPath pathLtoR = new DependencyPath();
		DependencyPath pathRtoL = new DependencyPath();

		if (node1==null || node2==null) {
			System.err.println(" WARNING: looking for path between null nodes.");
			return null;
		}

		// Trace each node up to the ROOT
		pathUp1.add(node1);
		while (node1.getHead()!=null) {
			node1 = node1.getHead();
			pathUp1.add(node1);
		}
		pathUp2.add(node2);
		while (node2.getHead()!=null) {
			node2 = node2.getHead();
			pathUp2.add(node2);
		}
		
		// Give the path from node1 to node2
		pathLtoR.clear();
		boolean foundMatch = false;
		for (int i=0; irel-word>rel-word
					pathLtoR.setCommonNode(nodeUp1);
					
					// add the two halves of the path together
					pathLtoR.addAll(pathRtoL);
					foundMatch = true;
					break;
				} else {
					pathRtoL.addFirst(nodeUp2);
				}
			}

			if (foundMatch) 
				return pathLtoR;
		}

		return null;

	}

	/** Given a CAS, find the path between two nodes in a sentence **/
	public static DependencyPath getPath( JCas jCas, ConllDependencyNode node1, ConllDependencyNode node2) {
		
		Sentence sent1 = getSentence( jCas, node1);
		Sentence sent2 = getSentence( jCas, node2);
		if (sent1.equals(sent2)) {
			return getPath( getDependencyNodes(jCas, sent1), node1, node2);
		} else {
			
			// 6/28/13 shalgrim
			// nodes can be null so check before calling getCoveredText
			String node1txt, node2txt;
			
			if (node1 == null) {
				node1txt = "null";
			} else {
				node1txt = node1.getCoveredText();
			}
			
			if (node2 == null)
			{
				node2txt = "null";
			} else {
				node2txt = node2.getCoveredText();
			}
			
			logger.debug(String.format("Cannot find path between nodes in different sentences. Node1: %s  Node2: %s",
					node1txt, node2txt));			
		}
		return null;
	}

	
	public static DependencyPath getPathToTop(JCas jCas, ConllDependencyNode node1) {

		DependencyPath pathUp1 = new DependencyPath();

		if (node1==null) {
			System.err.println(" WARNING: looking for path between null nodes.");
			return null;
		}

		pathUp1.add(node1);
		while (node1.getHead()!=null) {
			node1 = node1.getHead();
			pathUp1.add(node1);
		}
		return pathUp1;
	}

	public static List getRightSibs( ConllDependencyNode refNode, List tree ) {
		
		ConllDependencyNode parent = refNode.getHead();
		List out = new ArrayList();
		
		for ( ConllDependencyNode node : tree.subList( tree.indexOf(refNode)+1, tree.size() ) ) {
			if ( node.getHead().equals(parent) ) {
				out.add(node);
			}
		}
		return out;
	}
	
	public static List getLeftSibs( ConllDependencyNode refNode, List tree ) {
		
		ConllDependencyNode parent = refNode.getHead();
		List out = new ArrayList();
		
		List lSide = tree.subList(0,tree.indexOf(refNode));
		for ( int i=tree.indexOf(refNode)-1; i>=0; i-- ) {
			ConllDependencyNode node = lSide.get(i);
			if ( node.getHead().equals(parent) ) {
				out.add(node);
			}
		}
		return out;
	}
	
	public static List getProgeny( ConllDependencyNode refNode, List tree) {

		List out = new ArrayList();
		
		// Find the path to root for every node
		for ( ConllDependencyNode node : tree ) {
			
			// Progeny includes the reference node itself
			if ( node.equals(refNode) ) {
				out.add(node);
			} else {
				
				// Anything with refNode on its path to root is progeny.  Requires acyclicity
				ConllDependencyNode upNode = node;
				while (upNode.getHead()!=null) {
					upNode = upNode.getHead();
					if (upNode.equals(refNode)) {
						out.add(node);
						break;
					}
				}
				
			}
			
		}
		
		return out;
	}
	
	public static List getProgeny( List refNodes, List tree) {

		List out = new ArrayList();
		
		// Find the path to root for every node
		for ( ConllDependencyNode node : tree ) {
			
			// Progeny includes the reference nodes themselves
			if ( refNodes.contains(node) ) {
				out.add(node);
			} else {
				
				// Anything with refNode on its path to root is progeny.  Requires acyclicity
				ConllDependencyNode upNode = node;
				while (upNode.getHead()!=null) {
					upNode = upNode.getHead();
					
					if (refNodes.contains(upNode)) {
						out.add(node);
						break;
					}
				}
				
			}
			
		}
		
		return out;
	}

	public static List getRightSibProgeny( ConllDependencyNode refNode, List tree) {
		return getProgeny( getRightSibs(refNode,tree), tree );
	}
	
	public static List getLeftSibProgeny( ConllDependencyNode refNode, List tree) {
		return getProgeny( getLeftSibs(refNode,tree), tree );
	}


	public static String dumpDependencyGraph(Annotation annotation) {
		StringBuilder builder = new StringBuilder();
		for (ConllDependencyNode depNode : JCasUtil.selectCovered(ConllDependencyNode.class, annotation)) {
			ConllDependencyNode head = depNode.getHead();
			String headStr = (head != null) ? head.getCoveredText() : "TOP";
			builder.append(String.format("%s(%s, %s)\n", depNode.getDeprel(), depNode.getCoveredText(), headStr));
		}
		return builder.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy