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

edu.emory.mathcs.nlp.common.propbank.PBArgument Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2015, Emory University
 * 
 * Licensed 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 edu.emory.mathcs.nlp.common.propbank;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import edu.emory.mathcs.nlp.common.constant.StringConst;
import edu.emory.mathcs.nlp.common.constituent.CTTree;
import edu.emory.mathcs.nlp.common.util.DSUtils;

/**
 * @author Jinho D. Choi ({@code [email protected]})
 */
public class PBArgument implements Serializable, Comparable
{
	private static final long serialVersionUID = -5706844524751492352L;
	/** The delimiter between location and label ("-"). */
	static public final String DELIM = StringConst.HYPHEN;
	
	private List l_locations;
	private String s_label;
	
	public PBArgument()
	{
		l_locations = new ArrayList<>();
	}
	
	/** @param str "()*-label". */
	public PBArgument(String str)
	{
		l_locations = new ArrayList<>();
		int idx = str.indexOf(DELIM);
		String type;
		
		if (idx == -1)
			throw new IllegalArgumentException(str);
		
		StringTokenizer tok = new StringTokenizer(str.substring(0, idx), "*&,;", true);
		s_label = str.substring(idx+1);
		
		if (!tok.hasMoreTokens())
			throw new IllegalArgumentException(str);
		
		addLocation(new PBLocation(tok.nextToken(), StringConst.EMPTY));
		
		while (tok.hasMoreTokens())
		{
			type = tok.nextToken();
		
			if (!tok.hasMoreTokens())
				throw new IllegalArgumentException(str);
			
			addLocation(new PBLocation(tok.nextToken(), type));
		}
	}
	
	public String getLabel()
	{
		return s_label;
	}
	
	/** @return the index'th location of this argument if exists; otherwise, {@code null}. */
	public PBLocation getLocation(int index)
	{
		return DSUtils.isRange(l_locations, index) ? l_locations.get(index) : null;
	}
	
	/** @return the first location matching the specific terminal ID and height in this argument. */
	public PBLocation getLocation(int terminalID, int height)
	{
		for (PBLocation loc : l_locations)
		{
			if (loc.matches(terminalID, height))
				return loc;
		}
		
		return null;
	}
	
	/** @return a list of locations of this argument. */
	public List getLocationList()
	{
		return l_locations;
	}
	
	/** @return the number of locations in this argument. */
	public int getLocationSize()
	{
		return l_locations.size();
	}
	
	/**
	 * Returns a set of terminal IDs belonging to this argument given the specific tree.
	 * @param tree the constituent tree.
	 * @return a set of terminal IDs belonging to this argument.
	 */
	public Set getTerminalIDSet(CTTree tree)
	{
		Set set = new HashSet<>();
		
		for (PBLocation loc : l_locations)
			set.addAll(tree.getNode(loc).getTerminalIDSet());
		
		return set;
	}
		
	/** Adds the specific location to this argument. */
	public void addLocation(PBLocation location)
	{
		l_locations.add(location);
	}
	
	/** Adds the specific collection of locations to this argument. */
	public void addLocations(Collection locations)
	{
		l_locations.addAll(locations);
	}
	
	public void setLabel(String label)
	{
		s_label = label;
	}
	
	public void setLocations(List locations)
	{
		l_locations = locations;
	}
	
	/** Removes the first location matching the specific terminal ID and height from this argument. */
	public void removeLocation(int terminalId, int height)
	{
		int i, size = l_locations.size();
		
		for (i=0; i locations)
	{
		l_locations.removeAll(locations);
		if (!l_locations.isEmpty())	l_locations.get(0).setType(StringConst.EMPTY);
	}
	
	/**
	 * Sorts the locations of this argument by their terminal IDs and heights.
	 * @see PBLocation#compareTo(PBLocation)
	 */
	public void sortLocations()
	{
		if (l_locations.isEmpty())	return;
		
		Collections.sort(l_locations);
		PBLocation fst = l_locations.get(0), loc;
		
		if (!fst.isType(StringConst.EMPTY))
		{
			for (int i=1; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy