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

edu.berkeley.nlp.syntax.SpanTree Maven / Gradle / Ivy

Go to download

The Berkeley parser analyzes the grammatical structure of natural language using probabilistic context-free grammars (PCFGs).

The newest version!
package edu.berkeley.nlp.syntax;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SpanTree{
	int start, end;
  L label;
  List> children;
  
	// inheritance should take care of that but i am too stupid... slav
  public List> getChildren() {
    return children;
  } 
  
  public boolean isLeaf() {
    return getChildren().isEmpty();
  }

  public boolean isPreTerminal() {
    return getChildren().size() == 1 && getChildren().get(0).isLeaf();
  }
  
  public void setChildren(List> c) {
    this.children= c;
  }
  
  public SpanTree(L label) {
    this.label = label;
    this.children = Collections.emptyList();
		this.start = this.end = -1; 
  }
  
  public L getLabel() {
    return label;
  }
  
	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	/*public SpanTree(L label, List> children, int start, int end) {
		super(label, children);
		// TODO Auto-generated constructor stub
		this.start = start;
		this.end = end;
	}
	*/
	
	public void setSpans(){
		List yield = new ArrayList();
		setSpansHelper(this, yield);
	}
	
	public void setSpansHelper(SpanTree tree, List yield){
		if (tree.isLeaf()) {
      int pos = yield.size();
			yield.add(tree.getLabel());
      tree.setStart(pos);
      tree.setEnd(pos+1);
      return;
    }
		List> children = tree.getChildren();
		for (SpanTree child : children){
			setSpansHelper(child, yield);
		}
  	SpanTree child1 = children.get(0);
		SpanTree child2 = children.get(children.size()-1);

		tree.setStart(child1.getStart());
		tree.setEnd(child2.getEnd());
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy