001    package edu.upenn.cis.propbank_shen;
002    import java.util.*;
003    
004    /**
005       This class represents an "argument" in a "predicate argument
006       structure".  As such, on a logical/mathematical level, one
007       may construe an "argument" as a projection of tuple, where
008       the tuple is an element of a relation "defined" by the verb
009       in question.  For example, the sentence
010    <p><center>
011            John gave Sue a Penny.  
012        </center><p>
013    
014       may be viewed as denoting, amongst other
015       things, a predicate "gave" with three "arguments", as in
016       gave(John,Sue,Penny).
017        <p>
018       In the propbank, each of these arguments is constituted by 
019       up to three things (two of which arg optional):
020       
021       <ol>
022       <li> an argument label, as defined by the class ArgLabel
023       <li> an optional modifying label, as defined by the class ModLabel
024       <li>an optional argument location, as defined by the class ArgLoc
025       </ol>
026       
027       @author Scott Cotton
028       @see edu.upenn.cis.propbank_shen.ArgLabel
029       @see edu.upenn.cis.propbank_shen.ModLabel
030       @see edu.upenn.cis.propbank_shen.ArgLoc
031       
032     */
033    public class Argument implements Comparable{
034    
035        /** the primary argument label associated with this Argument */
036        public ArgLabel arg_label;
037        /** the secondary label associated with this argument, or null */
038        public ModLabel mod_label; // can be null
039        /** the edu.upenn.cis.treebank location associated with this argument, or null */
040        public ArgLoc   location;  // can be null
041    
042        /** the separator for the parts for one-line at a time representation */
043        public static final String sep="-";
044    
045        /** create an Argument from just a label */
046        public Argument(ArgLabel albl) {
047            arg_label = albl;
048            location = null;
049            mod_label = null;
050        }
051    
052        /** create an Argument from an argument label and a modifiying label */
053        public Argument(ArgLabel albl, ModLabel mlbl) {
054            arg_label = albl;
055            mod_label = mlbl;
056            location = null;
057        }
058    
059        /** create an Argument from an argument label and a location */
060        public Argument(ArgLabel albl, ArgLoc aloc) {
061            arg_label = albl;
062            mod_label = null;
063            location = aloc;
064        }
065    
066        /** create an Argument from an argument label, a modifying label, and a location */
067        public Argument(ArgLabel albl, ModLabel mlbl, ArgLoc aloc) {
068            arg_label = albl;
069            mod_label = mlbl;
070            location = aloc;
071        }
072        
073        public int compareTo(Object o) {
074            Argument arg = (Argument) o;
075            int locComparison = this.location.compareTo(arg.location);
076            if (locComparison != 0) return locComparison;
077            String labelString = arg_label.toString();
078            if (mod_label != null) labelString += mod_label.toString();
079            String labelString2 = arg.arg_label.toString();
080            if (arg.mod_label != null) labelString2 += arg.mod_label.toString();
081            return labelString.compareTo(labelString2);
082        }
083    
084        /** set the argument's location to loc
085            @param loc the new argument location
086        */
087        public void setLocation(ArgLoc loc) { location = loc;}
088        
089        
090        public ArgLoc getLocation() { return this.location; }
091        
092        /**
093           set the arguments modifying label.
094           @param ml  the new modifying label
095        */
096        public void setMod(ModLabel ml) { mod_label = ml; }
097        
098        /**
099           set the argument's primary label.
100           @param al the new primary label
101         */
102        public void setLabel(ArgLabel al) { arg_label = al; }
103        
104        /** produce a canonical string from the Argument */
105        public String toString() {
106            String locstr;
107            if (location == null) { locstr = "?:?"; } 
108            else { locstr = location.toString();  }
109            
110            String modstr;
111            if (mod_label == null) { modstr = "";}
112            else { modstr = sep + mod_label.toString(); }
113            
114            String astr = arg_label.toString();
115            
116            return locstr + sep + astr + modstr;
117        }
118    
119        public boolean equals (Object o) {
120            if (!(o instanceof Argument))
121                return false;
122            Argument a = (Argument)o;
123            return 
124                ((a.arg_label == arg_label) ||
125                 (a.arg_label != null && a.arg_label.equals(arg_label))) &&
126                ((a.mod_label == mod_label) ||
127                 (a.mod_label != null && a.mod_label.equals(mod_label))) &&
128                ((a.location == location) ||
129                 (a.location != null && a.location.equals(location)));
130        }
131    
132    //     public int hashCode () {
133    //         return arg_label.hashCode() + 19 * mod_label.hashCode() +
134    //            23 * location.hashCode();
135    //     }
136    
137        /**
138           convert a string into an "argument".
139           
140           @param s  the string to be converted.
141         */
142        public static Argument ofString(String s) throws CorruptDataException {
143            StringTokenizer tok = new StringTokenizer(s, sep);
144            int nelts = tok.countTokens();
145            if (nelts < 2 || nelts > 3) {
146                throw new CorruptDataException("invalid argument string, too few or too many parts: " + s);
147            }
148            String loctok  = tok.nextToken();
149            ArgLoc aloc = null;
150            if (loctok == "?:?") {
151                aloc = null;
152            } else {
153                aloc = ArgLoc.ofString(loctok);
154            }
155            ArgLabel albl = ArgLabel.ofString(tok.nextToken());
156            if (nelts == 3) {
157                ModLabel mlbl = ModLabel.ofString(tok.nextToken());
158                return new Argument(albl, mlbl, aloc);
159            } else {
160                return new Argument(albl, aloc);
161            }
162        }
163    }
164