Dependencies.java :  » Natural-Language-Processing » Stanford-Parser » edu » stanford » nlp » trees » Java Open Source

Java Open Source » Natural Language Processing » Stanford Parser 
Stanford Parser » edu » stanford » nlp » trees » Dependencies.java
package edu.stanford.nlp.trees;

import java.util.Comparator;

import edu.stanford.nlp.ling.HasTag;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Label;
import edu.stanford.nlp.ling.CoreAnnotations.IndexAnnotation;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.Filter;

/** Utilities for Dependency objects.
 *
 *  @author Christopher Manning
 */
public class Dependencies {

  private Dependencies() {} // only static methods


  public static class DependentPuncTagRejectFilter<G extends Label,D extends Label,N> implements Filter<Dependency<G, D, N>> {

    private Filter<String> tagRejectFilter;

    public DependentPuncTagRejectFilter(Filter<String> trf) {
      tagRejectFilter = trf;
    }

    public boolean accept(Dependency<G, D, N> d) {
      if (d == null) {
        return false;
      }
      if ( ! (d.dependent() instanceof HasTag)) {
        return false;
      }
      String tag = ((HasTag) d.dependent()).tag();
      return tagRejectFilter.accept(tag);
    }

    private static final long serialVersionUID = -7732189363171164852L;

  } // end class DependentPuncTagRejectFilter


  public static class DependentPuncWordRejectFilter<G extends Label,D extends Label,N> implements Filter<Dependency<G, D, N>> {

    /**
     *
     */
    private static final long serialVersionUID = 1166489968248785287L;
    private final Filter<String> wordRejectFilter;

    /** @param wrf A filter that rejects punctuation words.
     */
    public DependentPuncWordRejectFilter(Filter<String> wrf) {
      // System.err.println("wrf is " + wrf);
      wordRejectFilter = wrf;
    }

    public boolean accept(Dependency<G, D, N> d) {
      /*
      System.err.println("DRF: Checking " + d + ": hasWord?: " +
                         (d.dependent() instanceof HasWord) + "; value: " +
                         ((d.dependent() instanceof HasWord)? ((HasWord) d.dependent()).word(): d.dependent().value()));
      */
      if (d == null) {
        return false;
      }
      String word = null;
      if (d.dependent() instanceof HasWord) {
        word = ((HasWord) d.dependent()).word();
      }
      if (word == null) {
        word = d.dependent().value();
      }
      // System.err.println("Dep: kid is " + ((MapLabel) d.dependent()).toString("value{map}"));
      return wordRejectFilter.accept(word);
    }

  } // end class DependentPuncWordRejectFilter


  // extra class guarantees correct lazy loading (Bloch p.194)
  private static class ComparatorHolder {

    private ComparatorHolder() {}

    private static class DependencyIdxComparator implements Comparator<Dependency> {

      public int compare(Dependency dep1, Dependency dep2) {
        CoreMap dep1lab = (CoreMap) dep1.dependent();
        CoreMap dep2lab = (CoreMap) dep2.dependent();
        int dep1idx = dep1lab.get(IndexAnnotation.class);
        int dep2idx = dep2lab.get(IndexAnnotation.class);
        return dep1idx - dep2idx;
      }

    }

    private static final Comparator<Dependency> dc = new DependencyIdxComparator();

  }

  /** A Comparator for Dependencies based on their dependent annotation.
   *  It will only work if the Labels at the ends of Dependencies have
   *  an index().
   *
   *  @return A Comparator for Dependencies
   */
  public static Comparator<Dependency> dependencyIndexComparator() {
    return ComparatorHolder.dc;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.