001    package edu.upenn.cis.propbank_shen;
002    
003    
004    // these are done sortof like enumerations, ick.
005    /** 
006        An Argument Label represents the information ascribed to an argument in 
007       a predicate-argument structure.  A Basic Argument label just ascribes
008       a number, such as Arg0, Arg1, .... There is also ArgM for a modifying
009       argument, and ArgA for a causative agent.
010    
011       @author Scott Cotton
012       @see edu.upenn.cis.propbank_shen.PAStruct
013    */
014    public final class ArgLabel {
015        
016        private String name;
017        private int number;
018        public static String prefix = "ARG";
019    
020        /**
021           This private constructor is creates numbered arguments
022    
023           @param nm the name of the label, eg "Arg"
024           @param num the number of the label.
025        */
026        private ArgLabel(int num)  {
027            name = prefix;
028            number=num;
029        }
030    
031        /**
032           This private constructor creates non-numbered arguments
033           
034           @param nm the name of the label, eg "ArgM"
035         */
036        private ArgLabel(String nm) {
037            name = prefix + nm;
038            number = -1;
039        }
040        
041    
042    
043        /**
044           create a string representation of the argument label.
045         */
046        public String toString() {  
047            String result;
048            if (name.endsWith("REL")) {
049                return "rel";
050            }
051            if (name.endsWith("TBERR")) {
052                return "TBERR";
053            }
054            if (number < 0) {
055                return name;
056            } else {
057                Integer i = new Integer(number);
058                return name + i.toString();
059            }
060        }
061    
062        /**
063           create an argument from a string of the form
064           "Arg<bf>X</bf>" where <bf>X</bf> is either "M", "A", or an integer.
065           
066           @param s the string from which the resulting arg label is made
067         */
068        public static ArgLabel ofString(String s) throws CorruptDataException {
069            if (s.toUpperCase().equals("REL")) {
070                return ArgLabel.REL;
071            }
072            if (s.toUpperCase().equals("TBERR")) {
073                return ArgLabel.TBERR;
074            }
075            if (!s.toUpperCase().startsWith("ARG")) {
076                throw new CorruptDataException("invalid argument label: " + s);
077            }
078            if (s.length() == 4) {
079                if (s.toUpperCase().endsWith("A")) {
080                    return ArgLabel.ARGA;
081                }
082                else if (s.toUpperCase().endsWith("M")) {
083                    return ArgLabel.ARGM;
084                }
085            }
086            try {
087                Integer n = Integer.decode(s.substring(3, s.length()));
088                return numberedLabels[n.intValue()];
089            } catch (Exception e) {
090                throw new CorruptDataException("invalid argument label: " + s);
091            }
092        }
093        
094        public boolean isRel() {
095            return this.equals(REL);
096        }
097        
098        public boolean isArgM() {
099            return this.equals(ARGM);
100        }
101        
102        public boolean isArgA() {
103            return this.equals(ARGA);
104        }
105        
106        public boolean isNumbered() {
107            return number != -1;
108        }
109        
110        public boolean isArgument() {
111            return this.isNumbered() || this.isArgA();
112        }    
113        
114    
115        
116        /** get the numbner associated with the label, if
117            the number is meaningless, return -1 */
118        public int getNum() { return number; }
119    
120    
121        /**
122           get the name associated with the label
123           all labels have names.
124        */
125        public String getName() { return name; }
126    
127        /** Modifying argument -- an adjunct in the argument/adjuct "distinction"*/
128        public static final ArgLabel ARGM=new ArgLabel("M");
129        /** Causative agent.  A Causative agent is an extra agent, such as
130         * the general in the sentence 
131         *  <center>
132         *  The general marches the soldiers around the barracks.
133         *  </center>
134         * */
135        public static final ArgLabel ARGA=new ArgLabel("A");
136    
137        /**
138           this label is used for the predicating verb.  It is present
139           not so much as an argument as to provide a slot in which
140           to identify the location of the verb, which can be tricky
141           when there are phrasals and what not.
142         */
143        public static final ArgLabel REL=new ArgLabel("REL");
144    
145        /**
146           This label is used by our annotators whenever they feel the
147           predicate argument structure cannot be annotated correctly
148           due to an error in the treebank.
149         */
150        public static final ArgLabel TBERR = new ArgLabel("TBERR");
151    
152        /**
153         * Numbered arguments
154         * */
155        public static final ArgLabel ARG0=new ArgLabel(0);
156        public static final ArgLabel ARG1=new ArgLabel(1);
157        public static final ArgLabel ARG2=new ArgLabel(2);
158        public static final ArgLabel ARG3=new ArgLabel(3);
159        public static final ArgLabel ARG4=new ArgLabel(4);
160        public static final ArgLabel ARG5=new ArgLabel(5);
161        public static final ArgLabel ARG6=new ArgLabel(6);
162        public static final ArgLabel ARG7=new ArgLabel(7);
163        public static final ArgLabel ARG8=new ArgLabel(8);
164        public static final ArgLabel ARG9=new ArgLabel(9);
165    
166        private static ArgLabel[] numberedLabels = {ARG0, ARG1, ARG2, ARG3, ARG4, 
167                                                    ARG5, ARG6, ARG7, ARG8, ARG9};
168    
169        public boolean equals (Object o) {
170            if (!(o instanceof ArgLabel))
171                return false;
172            ArgLabel al = (ArgLabel)o;
173            return al.number == number && al.name.equals(name);
174        }
175        
176    //     public int hashCode () {
177    //         return number + name.hashCode();
178    //     }
179    }