001    package edu.upenn.cis.propbank_shen;
002    
003    import java.util.*;
004    
005    /**
006     *
007     * A class representing an instance of predicate argument structure.
008     *
009     * @author Scott Cotton
010     * @see edu.upenn.cis.propbank_shen.Argument
011    */
012    public class PAStruct {
013        
014        /** 
015         * the arguments associated with the structure, including the verb itself 
016         */
017        protected List arguments;
018        /** the predicate itself, i.e., the root form of the verb in question */
019        protected String lemma;
020       
021        /** construct a PAStruct object from the lemma */
022        public PAStruct(String lem) {
023            lemma = lem;
024            arguments = new ArrayList();
025        }
026        
027        // getters
028        /**
029         * return the lemma, eg the root form of the associated verb.
030         */
031        public String getLemma() { return lemma; }
032        /** 
033         *  return a list of arguments, including the verb itself
034         * @see edu.upenn.cis.propbank_shen.Argument
035         */
036        public List getArgs() { return arguments; }
037        
038        /* 
039           some wrapper functions around the list functionality
040           
041           XXX should we go ahead and implement the enumeration 
042           interface?
043        */
044    
045        /**
046         *  Add a single argument to the arguments associated with this PA structures.
047         */
048        public void addArg(Argument a) {
049            arguments.add((Object) a);
050            Collections.sort(arguments);
051        }
052        
053        /**
054         * get the nth argument
055         *
056         * @param n the n'th argument
057         */
058        public Argument nthArg(int n) {
059            return (Argument) arguments.get(n);
060        }
061       
062        /**
063         * remove the argument at position i.
064         */
065        public void removeArg(int i) {
066            arguments.remove(i);
067        }
068       
069        /**
070         * remove the argument that is the same as a
071         * @param a  argument to be removed
072         */
073        public void removeArg(Argument a) {
074            arguments.remove(a);
075        }
076        
077        /**
078         * The number of arguments
079         */
080        public int size() {
081            return arguments.size();
082        }
083        
084        /*
085          to/from storage
086         */
087        /**
088         * convert the predicate argument structure to a canonical string.
089         */
090        public String toString() {
091            String res = "";
092            if (arguments.size() > 0)
093                res += arguments.get(0).toString();
094            for(int i=1; i<arguments.size(); i++) {
095                res += " " + arguments.get(i);
096            }
097            return res;
098        }
099        
100        /**
101         * create a predicate argument structure from a canonical string
102         *
103         * @param s the canonical string of the argument.
104         * @throws CorruptDataException
105         */
106        public static PAStruct ofString(String s) throws CorruptDataException {
107            StringTokenizer stok = new StringTokenizer(s);
108            int nelts = stok.countTokens();
109            if (nelts < 2) {
110                throw new CorruptDataException("invalid PAStruct string: " + s);
111            }
112            String lem = stok.nextToken();
113            PAStruct pas = new PAStruct(lem);
114            while (stok.hasMoreTokens()) {
115                Argument arg = Argument.ofString(stok.nextToken());
116                pas.addArg(arg);
117            }
118            return pas;
119        }
120        /**
121         * Checks whether the PAstructs are equivalent.  Ignores the order in which the arguments are stored.
122         */
123        public boolean equals (Object o) {
124            if (!(o instanceof PAStruct))
125                return false;
126            PAStruct p = (PAStruct)o;
127    
128            return this.lemma.equals(p.lemma) 
129                && (this.arguments).equals(p.arguments);
130        }
131    
132        public int hashCode () {
133            return lemma.hashCode() + 13 * arguments.hashCode();
134        }
135    }