UnaryRule.java :  » Natural-Language-Processing » Stanford-CoreNLP » edu » stanford » nlp » parser » lexparser » Java Open Source

Java Open Source » Natural Language Processing » Stanford CoreNLP 
Stanford CoreNLP » edu » stanford » nlp » parser » lexparser » UnaryRule.java
package edu.stanford.nlp.parser.lexparser;

import edu.stanford.nlp.util.Numberer;
import edu.stanford.nlp.util.StringUtils;

/**
 * Unary grammar rules (with ints for parent and child).
 *
 * @author Dan Klein
 */
public class UnaryRule extends Rule implements Comparable<UnaryRule> {

  public int child = -1;

  /** Fields are set to: -1, -1, Float.NaN. */
  public UnaryRule() {
  }

  /** The score is set to Float.NaN by default. */
  public UnaryRule(int parent, int child) {
    this.parent = parent;
    this.child = child;
  }

  public UnaryRule(int parent, int child, double score) {
    this.parent = parent;
    this.child = child;
    this.score = (float) score;
  }

  /** Decode a UnaryRule out of a String representation with help from
   *  a Numberer.
   */
  public UnaryRule(String s, Numberer n) {
    String[] fields = StringUtils.splitOnCharWithQuoting(s, ' ', '\"', '\\');
    //    System.out.println("fields:\n" + fields[0] + "\n" + fields[2] + "\n" + fields[3]);
    this.parent = n.number(fields[0]);
    this.child = n.number(fields[2]);
    this.score = Float.parseFloat(fields[3]);
  }

  @Override
  public boolean isUnary() {
    return true;
  }

  @Override
  public int hashCode() {
    return (parent << 16) ^ child;
  }

  /** A UnaryRule is equal to another UnaryRule with the same parent and child.
   *  The score is not included in the equality computation.
   *
   * @param o Object to be compared with
   * @return Whether the object is equal to this
   */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o instanceof UnaryRule) {
      UnaryRule ur = (UnaryRule) o;
      if (parent == ur.parent && child == ur.child) {
        return true;
      }
    }
    return false;
  }

  public int compareTo(UnaryRule ur) {
    if (parent < ur.parent) {
      return -1;
    }
    if (parent > ur.parent) {
      return 1;
    }
    if (child < ur.child) {
      return -1;
    }
    if (child > ur.child) {
      return 1;
    }
    return 0;
  }

  private static final char[] charsToEscape = new char[]{'\"'};

  @Override
  public String toString() {
    Numberer n = Numberer.getGlobalNumberer("states");
    return "\"" + StringUtils.escapeString(n.object(parent).toString(), charsToEscape, '\\') + "\" -> \"" + StringUtils.escapeString(n.object(child).toString(), charsToEscape, '\\') + "\" " + score;
  }

  private transient String cached = null;
  
  public String toStringNoScore() {
    if (cached == null) {
      Numberer n = Numberer.getGlobalNumberer("states");
      cached =  "\"" + StringUtils.escapeString(n.object(parent).toString(), charsToEscape, '\\') + "\" -> \"" + StringUtils.escapeString(n.object(child).toString(), charsToEscape, '\\');
    }
    return cached;
  }

  private static final long serialVersionUID = 1L;

}

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.