diff -Nru -x CVS src-orig/edu/stanford/nlp/trees/GrammaticalRelation.java src/edu/stanford/nlp/trees/GrammaticalRelation.java --- src-orig/edu/stanford/nlp/trees/GrammaticalRelation.java 2008-10-27 08:31:17.000000000 +0100 +++ src/edu/stanford/nlp/trees/GrammaticalRelation.java 2008-10-23 07:06:57.000000000 +0200 @@ -1,16 +1,12 @@ package edu.stanford.nlp.trees; -import java.util.ArrayList; +import java.io.Serializable; import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -import java.io.Serializable; import edu.stanford.nlp.ling.CoreAnnotation; -import edu.stanford.nlp.trees.tregex.TregexMatcher; import edu.stanford.nlp.trees.tregex.TregexPattern; import edu.stanford.nlp.util.Generics; import edu.stanford.nlp.util.StringUtils; @@ -85,7 +81,7 @@ * @author Galen Andrew (refactoring English-specific stuff) * @author Ilya Sherman (refactoring annotation-relation pairing) */ -public class GrammaticalRelation implements Comparable, Serializable { +public final class GrammaticalRelation extends ProtoGrammaticalRelation implements Comparable, Serializable { private static final long serialVersionUID = 892618003417550128L; @@ -193,18 +189,6 @@ return longName == null; } - - - /* Non-static stuff */ - private final String shortName; - private final String longName; - private final GrammaticalRelation parent; - private final List children = new ArrayList(); - // a regexp for node values at which this relation can hold - private final Pattern sourcePattern; - private final List targetPatterns = new ArrayList(); - private final String specific; // to hold the specific prep or conjunction associated with the grammatical relation - // TODO document constructor public GrammaticalRelation(String shortName, String longName, Class annotation, @@ -212,14 +196,7 @@ String sourcePattern, String[] targetPatterns, String specificString) { - this.shortName = shortName; - this.longName = longName; - this.parent = parent; - this.specific = specificString; // this can be null! - - if (parent != null) { - parent.addChild(this); - } + super(shortName, longName, parent, sourcePattern, targetPatterns, specificString); if (annotation != null) { if (GrammaticalRelation.annotationsToRelations.put(annotation, this) != null) { @@ -230,25 +207,6 @@ } } - if (sourcePattern != null) { - try { - this.sourcePattern = Pattern.compile(sourcePattern); - } catch (java.util.regex.PatternSyntaxException e) { - throw new RuntimeException("Bad pattern: " + sourcePattern); - } - } else { - this.sourcePattern = null; - } - - for (String pattern : targetPatterns) { - try { - TregexPattern p = TregexPattern.compile(pattern); - this.targetPatterns.add(p); - } catch (edu.stanford.nlp.trees.tregex.ParseException pe) { - throw new RuntimeException("Bad pattern: " + pattern, pe); - } - } - GrammaticalRelation previous = GrammaticalRelation.stringsToRelations.put(toString(), this); if (previous != null) { if (!previous.isFromString()) { @@ -286,68 +244,8 @@ StringUtils.EMPTY_STRING_ARRAY, specificString); } - private void addChild(GrammaticalRelation child) { - children.add(child); - } - - /** Given a Tree node t, attempts to - * return a list of nodes to which node t has this - * grammatical relation. - * - * @param t Target for finding governors of t related by this GR - * @param root The root of the Tree - * @return Governor nodes to which t bears this GR - */ - public Collection getRelatedNodes(Tree t, Tree root) { - Set nodeList = new LinkedHashSet(); - for (TregexPattern p : targetPatterns) { // cdm: I deleted: && nodeList.isEmpty() - if (root.value() == null) { - root.setValue("ROOT"); - } - TregexMatcher m = p.matcher(root); - while (m.find()) { - if (m.getMatch() == t) { - nodeList.add(m.getNode("target")); - // System.out.println("found " + this + "(" + t + ", " + m.getNode("target") + ")"); - } - } - } - return nodeList; - } - - /** Returns true iff the value of Tree - * node t matches the sourcePattern for - * this GrammaticalRelation, indicating that this - * GrammaticalRelation is one that could hold between - * Tree node t and some other node. - */ - public boolean isApplicable(Tree t) { - return (t.value() != null) && (sourcePattern != null) && - sourcePattern.matcher(t.value()).matches(); - } - public boolean isAncestor(GrammaticalRelation gr) { - while (gr != null) { - if (this == gr) { return true; } - gr = gr.parent; - } - return false; - } - - /** - * Returns short name (abbreviation) for this - * GrammaticalRelation. - * - * Implementation note: Note that this method must be synced with - * the equals() and valueOf(String) methods - */ - @Override - public final String toString() { - if (specific == null) { - return shortName; - } else { - return shortName + '_' + specific; - } + return super.isAncestor(gr); } /** @@ -378,8 +276,9 @@ buf.append(" "); } buf.append(shortName).append(": ").append(targetPatterns); - for (GrammaticalRelation child : children) { + for (ProtoGrammaticalRelation protochild : getChildren()) { buf.append('\n'); + GrammaticalRelation child = (GrammaticalRelation)protochild; child.toPrettyString(indentLevel + 1, buf); } } @@ -418,30 +317,17 @@ } } - public int compareTo(GrammaticalRelation o) { String thisN = this.toString(); String oN = o.toString(); return thisN.compareTo(oN); } - public String getLongName() { - return longName; - } - - public String getShortName() { - return shortName; - } - - public String getSpecific() { - return specific; - } - /** * Returns the parent of this GrammaticalRelation. */ public GrammaticalRelation getParent() { - return parent; + return (GrammaticalRelation) super.getParent(); } public static void main(String[] args) { @@ -454,5 +340,4 @@ System.out.println("\tSpecific name: " + reln.getSpecific()); } } - -} +} \ Pas de fin de ligne à la fin du fichier. diff -Nru -x CVS src-orig/edu/stanford/nlp/trees/ProtoGrammaticalRelation.java src/edu/stanford/nlp/trees/ProtoGrammaticalRelation.java --- src-orig/edu/stanford/nlp/trees/ProtoGrammaticalRelation.java 1970-01-01 01:00:00.000000000 +0100 +++ src/edu/stanford/nlp/trees/ProtoGrammaticalRelation.java 2008-10-27 16:07:55.000000000 +0100 @@ -0,0 +1,250 @@ +package edu.stanford.nlp.trees; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; + +import edu.stanford.nlp.trees.tregex.TregexMatcher; +import edu.stanford.nlp.trees.tregex.TregexPattern; + + +/** + * ProtoGrammaticalRelation is used to define a + * standardized, hierarchical set of grammatical relations, + * together with patterns for identifying them in + * parse trees.

+ * + * Each GrammaticalRelation has: + *

    + *
  • A String short name, which should be a lowercase + * abbreviation of some kind.
  • + *
  • A String long name, which should be descriptive.
  • + *
  • A parent in the GrammaticalRelation hierarchy.
  • + *
  • A {@link Pattern Pattern} called + * sourcePattern which matches (parent) nodes from which + * this GrammaticalRelation could hold. (Note: this is done + * with the Java regex Pattern matches() predicate: the pattern + * must match the + * whole node name, and ^ or $ aren't needed.)
  • + *
  • A list of zero or more {@link TregexPattern + * TregexPatterns} called targetPatterns, + * which describe the local tree structure which must hold between + * the source node and a target node for the + * GrammaticalRelation to apply. (Note tregex + * regular expressions match with the find() method - though + * literal string label descriptions that are not regular expressions must + * be equals().)
  • + *
+ * + * The targetPatterns associated + * with a GrammaticalRelation are designed as follows. + * In order to recognize a grammatical relation X holding between + * nodes A and B in a parse tree, we want to associate with + * GrammaticalRelation X a {@link TregexPattern + * TregexPattern} such that: + *
    + *
  • the root of the pattern matches A, and
  • + *
  • the pattern includes a special node label, "target", which matches B.
  • + *
+ * For example, for the grammatical relation PREDICATE + * which holds between a clause and its primary verb phrase, we might + * want to use the pattern "S < VP=target", in which the + * root will match a clause and the node labeled "target" + * will match the verb phrase.

+ * + * For a given grammatical relation, the method {@link + * ProtoGrammaticalRelation#getRelatedNodes getRelatedNodes()} + * takes a Tree node as an argument and attempts to + * return other nodes which have this grammatical relation to the + * argument node. By default, this method operates as follows: it + * steps through the patterns in the pattern list, trying to match + * each pattern against the argument node, until it finds some + * matches. If a pattern matches, all matching nodes (that is, each + * node which corresponds to node label "target" in some match) are + * returned as a list; otherwise the next pattern is tried.

+ * + * @see GrammaticalRelation + * + * @author Bill MacCartney + * @author Galen Andrew (refactoring English-specific stuff) + * @author Ilya Sherman (refactoring annotation-relation pairing) + * @author Bernard (this class factored out of GrammaticalRelation) + */ +public class ProtoGrammaticalRelation implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * This function is used to determine whether the GrammaticalRelation in + * question is one that was created to be a thin wrapper around a String + * representation by valueOf(String), or whether it is a full-fledged + * GrammaticalRelation created by direct invocation of the constructor. + * @return Whether this relation is just a wrapper created by valueOf(String) + */ + public boolean isFromString() { + return longName == null; + } + + /* Non-static stuff */ + // + protected String shortName; + protected String longName; + protected ProtoGrammaticalRelation parent; + protected List children = new ArrayList(); + protected Pattern sourcePattern; // a regexp for node values at which this relation can hold + protected List targetPatterns = new ArrayList(); + protected String specific; // to hold the specific prep or conjunction associated with the grammatical relation + // + + // constructor + public ProtoGrammaticalRelation(String shortName, String longName, + ProtoGrammaticalRelation parent, + String sourcePattern, + String[] targetPatterns, + String specificString) { + + // members + this.shortName = shortName; + this.longName = longName; + this.specific = specificString; // this can be null! + this.parent = parent; + + // hierarchy + if (parent != null) { + parent.addChild(this); + } + + // source patterns + if (sourcePattern != null) { + try { + this.sourcePattern = Pattern.compile(sourcePattern); + } catch (java.util.regex.PatternSyntaxException e) { + throw new RuntimeException("Bad pattern: " + sourcePattern); + } + } else { + this.sourcePattern = null; + } + + // target patterns + if(targetPatterns !=null) + for (String pattern : targetPatterns) { + try { + TregexPattern p = TregexPattern.compile(pattern); + this.targetPatterns.add(p); + } catch (edu.stanford.nlp.trees.tregex.ParseException pe) { + throw new RuntimeException("Bad pattern: " + pattern, pe); + } + } + } + + // + protected void addChild(ProtoGrammaticalRelation child) { + children.add(child); + } + // + + /** Given a Tree node t, attempts to + * return a list of nodes to which node t has this + * grammatical relation. + * + * @param t Target for finding governors of t related by this GR + * @param root The root of the Tree + * @return Governor nodes to which t bears this GR + */ + public Collection getRelatedNodes(Tree t, Tree root) { + Set nodeList = new LinkedHashSet(); + for (TregexPattern p : targetPatterns) { // cdm: I deleted: && nodeList.isEmpty() + if (root.value() == null) { + root.setValue("ROOT"); + } + TregexMatcher m = p.matcher(root); + while (m.find()) { + if (m.getMatch() == t) { + nodeList.add(m.getNode("target")); + // System.out.println("found " + this + "(" + t + ", " + m.getNode("target") + ")"); + } + } + } + return nodeList; + } + + /** Returns true iff the value of Tree + * node t matches the sourcePattern for + * this GrammaticalRelation, indicating that this + * GrammaticalRelation is one that could hold between + * Tree node t and some other node. + */ + public boolean isApplicable(Tree t) { + return (t.value() != null) && (sourcePattern != null) && + sourcePattern.matcher(t.value()).matches(); + } + + public boolean isAncestor(ProtoGrammaticalRelation gr) { + while (gr != null) { + if (this == gr) { return true; } + gr = gr.parent; + } + return false; + } + + /** + * Returns short name (abbreviation) for this + * GrammaticalRelation. + */ + @Override + public final String toString() { + if (specific == null) { + return shortName; + } else { + return shortName + '_' + specific; + } + } + + public String getLongName() { + return longName; + } + + public String getShortName() { + return shortName; + } + + public String getSpecific() { + return specific; + } + + /** + * Returns the parent of this GrammaticalRelation. + */ + public ProtoGrammaticalRelation getParent() { + return parent; + } + + // + /** + * Returns the children of this GrammaticalRelation. + */ + public List getChildren() { + return children; + } + // + + // + /** + * Returns the source patterns of this GrammaticalRelation. + */ + public Pattern getSourcePattern() { + return sourcePattern; + } + + /** + * Returns the target patterns of this GrammaticalRelation. + */ + public List getTargetPatterns() { + return targetPatterns; + } + // +} \ Pas de fin de ligne à la fin du fichier.