⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.java

📁 自然语言处理领域的一个开发包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public Parse[] getChildren() {    return (Parse[]) parts.toArray(new Parse[parts.size()]);  }    /**   * Replaces the child at the specified index with a new child with the specified label.    * @param index The index of the child to be replaced.   * @param label The label to be assigned to the new child.   */  public void setChild(int index, String label) {    Parse newChild = (Parse) ((Parse) parts.get(index)).clone();    newChild.setLabel(label);    parts.set(index,newChild);  }    /**   * Returns the number of children for this parse node.   * @return the number of children for this parse node.   */  public int getChildCount() {    return parts.size();  }    /**   * Returns the index of this specified child.   * @param child A child of this parse.   * @return the index of this specified child or -1 if the specified child is not a child of this parse.   */  public int indexOf(Parse child) {    return parts.indexOf(child);  }  /** Returns the head constituent associated with this constituent.   * @return The head constituent associated with this constituent.   */  public Parse getHead() {    return head;  }  /**   * Returns the label assigned to this parse node during parsing    * which specifies how this node will be formed into a constituent.   * @return The outcome label assigned to this node during parsing.     */  public String getLabel() {    return label;  }  /**   * Assigns this parse the specified label.   * @param label A label indicating the constituent this parse node will become part of.   */  public void setLabel(String label) {    this.label = label;  }    private static String getType(String rest) {    if (rest.startsWith("-LCB-")) {      return "-LCB-";    }    else if (rest.startsWith("-RCB-")) {      return "-RCB-";    }    else if (rest.startsWith("-LRB-")) {      return "-LRB-";    }    else if (rest.startsWith("-RRB-")) {      return "-RRB-";    }    else {      Matcher typeMatcher = typePattern.matcher(rest);      if (typeMatcher.find()) {        String type = typeMatcher.group(1);         if (useFunctionTags) {          Matcher funMatcher = funTypePattern.matcher(rest);          if (funMatcher.find()) {            String ftag = funMatcher.group(1);            type = type+"-"+ftag;          }        }        return type;      }    }    return null;  }  private static String getToken(String rest) {    Matcher tokenMatcher = tokenPattern.matcher(rest);    if (tokenMatcher.find()) {      return tokenMatcher.group(1);    }    return null;  }    /**   * Computes the head parses for this parse and its sub-parses and stores this information   * in the parse data structure.    * @param rules The head rules which determine how the head of the parse is computed.   */  public void updateHeads(HeadRules rules) {    if (parts != null && parts.size() != 0) {      for (int pi=0,pn=parts.size();pi<pn;pi++) {        Parse c = (Parse) parts.get(pi);        c.updateHeads(rules);      }      this.head = rules.getHead((Parse[]) parts.toArray(new Parse[parts.size()]), type);      if (head == null) {        head = this;      }    }    else {      this.head = this;    }  }  /**   * Parses the specified tree-bank style parse string and return a Parse structure for that string.    * @param parse A tree-bank style parse string.   * @return a Parse structure for the specified tree-bank style parse string.   */  public static Parse parseParse(String parse) {    StringBuffer text = new StringBuffer();    int offset = 0;    Stack stack = new Stack();    List cons = new LinkedList();    for (int ci = 0, cl = parse.length(); ci < cl; ci++) {      char c = parse.charAt(ci);      if (c == '(') {        String rest = parse.substring(ci + 1);        String type = getType(rest);        if (type == null) {          System.err.println("null type for: " + rest);        }        String token = getToken(rest);        stack.push(new Object[] { type, new Integer(offset)});        if (token != null && !type.equals("-NONE-")) {          cons.add(new Object[] { ParserME.TOK_NODE, new Span(offset, offset + token.length())});          text.append(token).append(" ");          offset += token.length() + 1;        }      }      else if (c == ')') {        Object[] parts = (Object[]) stack.pop();        String type = (String) parts[0];        if (!type.equals("-NONE-")) {          int start = ((Integer) parts[1]).intValue();          cons.add(new Object[] { parts[0], new Span(start, offset - 1)});        }      }    }    String txt = text.toString();    Parse p = new Parse(txt, new Span(0, txt.length()), ParserME.TOP_NODE, 1);    /*    for (int ti=0;ti < tokens.size();ti++) {      Object[] parts = (Object[]) cons.get(ti);      String type = (String) parts[0];      if (!type.equals(ParserME.TOP_NODE)) {        Parse con = new Parse(txt, (Span) parts[1], type, 1);        System.err.println("insert "+type+" "+con.toString());        p.insert(con);      }    }    */    for (int ci=0;ci < cons.size();ci++) {      Object[] parts = (Object[]) cons.get(ci);      String type = (String) parts[0];      if (!type.equals(ParserME.TOP_NODE)) {        Parse con = new Parse(txt, (Span) parts[1], type, 1);        //System.err.println("insert["+ci+"] "+type+" "+con.toString()+" "+con.hashCode());        p.insert(con);        //codeTree(p);      }    }    return p;  }  /**   * Returns the parent parse node of this constituent.   * @return The parent parse node of this constituent.   */  public Parse getParent() {    return parent;  }  /**   * Specifies the parent parse node for this constituent.   * @param parent The parent parse node for this constituent.   */  public void setParent(Parse parent) {    this.parent = parent;  }  /**   * Indicates wether this parse node is a pos-tag.   * @return true if this node is a pos-tag, false otherwise.   */  public boolean isPosTag() {    return (parts.size() == 1 && ((Parse) parts.get(0)).getType().equals(ParserME.TOK_NODE));  }    /**   * Returns the parse nodes which are children of this node and which are pos tags.   * @return the parse nodes which are children of this node and which are pos tags.   */  public Parse[] getTagNodes() {    List tags = new LinkedList();    List nodes = new LinkedList();    nodes.addAll(this.parts);    while(nodes.size() != 0) {      Parse p = (Parse) nodes.remove(0);      if (p.isPosTag()) {        tags.add(p);      }      else {        nodes.addAll(0,p.parts);      }    }    return (Parse[]) tags.toArray(new Parse[tags.size()]);  }    /**   * Returns the deepest shared parent of this node and the specified node.    * If the nodes are identical then their parent is returned.     * If one node is the parent of the other then the parent node is returned.   * @param node The node from which parents are compared to this node's parents.   * @return the deepest shared parent of this node and the specified node.   */  public Parse getCommonParent(Parse node) {    if (this == node) {      return parent;    }    Set parents = new HashSet();    Parse cparent = this;    while(cparent != null) {      parents.add(cparent);      cparent = cparent.getParent();    }    while (node != null) {      if (parents.contains(node)) {        return node;      }      node = node.getParent();    }    return null;  }  public int compareTo(Object o) {    Parse p = (Parse) o;    if (this.getProb() > p.getProb()) {      return -1;    }    else if (this.getProb() < p.getProb()) {      return 1;    }    return 0;  }    /**   * Returns the derivation string for this parse if one has been created.   * @return the derivation string for this parse or null if no derivation string has been created.   */  public StringBuffer getDerivation() {    return derivation;  }    /**   * Specifies the derivation string to be associated with this parse.   * @param derivation The derivation string to be associated with this parse.   */  public void setDerivation(StringBuffer derivation) {    this.derivation = derivation;  }    private void codeTree(Parse p,int[] levels) {    Parse[] kids = p.getChildren();    StringBuffer levelsBuff = new StringBuffer();    levelsBuff.append("[");    int[] nlevels = new int[levels.length+1];    for (int li=0;li<levels.length;li++) {      nlevels[li] = levels[li];      levelsBuff.append(levels[li]).append(".");    }    for (int ki=0;ki<kids.length;ki++) {      nlevels[levels.length] = ki;      System.out.println(levelsBuff.toString()+ki+"] "+kids[ki].getType()+" "+kids[ki].hashCode()+" -> "+kids[ki].getParent().hashCode()+" "+kids[ki].getParent().getType()+" "+kids[ki].toString());      codeTree(kids[ki],nlevels);    }  }    /**   * Prints to standard out a representation of the specified parse which contains hash codes so that    * parent/child relationships can be explicitly seen.   */  public void showCodeTree() {    codeTree(this,new int[0]);  }  /**   * Reads training parses (one-sentence-per-line) and displays parse structure.   * @param args The head rules files.   * @throws java.io.IOException If the head rules file can not be opened and read.   */  public static void main(String[] args) throws java.io.IOException {    if (args.length == 0) {      System.err.println("Usage: Parse -fun head_rules < train_parses");      System.err.println("Reads training parses (one-sentence-per-line) and displays parse structure.");      System.exit(1);    }    int ai=0;    if (args[0].equals("-fun")) {      Parse.useFunctionTags(true);      ai++;    }    HeadRules rules = new opennlp.tools.lang.english.HeadRules(args[ai]);    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));    for (String line = in.readLine(); line != null; line = in.readLine()) {      Parse p = Parse.parseParse(line);      p.updateHeads(rules);      p.show();      //p.showCodeTree();    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -