001    package edu.upenn.cis.propbank_shen;
002    
003    import java.io.File;
004    
005    //import edu.upenn.cis.treebank.TBNode;
006    //import edu.upenn.cis.treebank.InvalidAddressException;
007    //import edu.upenn.cis.treebank.TBFind;
008    
009    
010    /**
011       This class represents the location of a predicate argument structure.  It
012       consists of a filename, a sentence number (starting with 0) and a terminal
013       number in the sentence (starting with 0).
014       
015       @author Scott Cotton
016       @see edu.upenn.cis.propbank_shen.PBConfig
017    
018     */
019    public class PASLoc {
020    
021        /** the filename of the associated PAS. */
022        String path;
023        /** the sentence number of the sentence, starting with 0. */
024        int sentno;
025        /** the terminal number in the sentence, starting with 0. */
026        int termno;
027        
028        /** the treebank node, or null if we haven't looked it up yet */
029    //    protected TBNode tbnode;
030        
031        /**
032           A constructor -- you supply the pieces, we supply the Object.
033         */
034        public PASLoc(String p, int sno, int tno) {
035            path = p;
036            sentno = sno;
037            termno = tno;
038      //      tbnode = null;
039        }
040    
041    
042        /**
043           return the full path of the file to which this location refers.
044         */
045        public File getPath()
046        {
047            return new File(PBConfig.TreeBankDir(), path);
048        }
049    
050    
051        
052    //    
053    //    /**
054    //       get the treebank node associated with this predicate argument
055    //       structure location.
056    //
057    //       @see edu.upenn.cis.treebank.TBNode
058    //     */
059    //    public TBNode getTBNode() throws InvalidAddressException
060    //    {
061    //        if (tbnode != null) {
062    //            return tbnode;
063    //        }
064    //        tbnode = TBFind.nodeAt(getPath().toString(), sentno, termno);
065    //        return tbnode;
066    //    }
067    
068        /**
069           construct a canonical string from the object.
070         */
071        public String toString() {
072            return path + " " + (new Integer(sentno)).toString() + " " 
073                + (new Integer(termno)).toString();
074        }
075    
076        /**
077           given a canonical string representing a location of 
078           a predicate, return a corresponding PASLoc object.
079    
080           @param s the canonical string representation of a PAS location
081         */
082        public static PASLoc ofString(String s) throws CorruptDataException {
083            String parts[] = s.split(" ");
084            if (parts.length != 3) { 
085                throw new CorruptDataException("Invalid location of a predicate argument structure: " + s);
086            }
087            try {
088                int sn = Integer.decode(parts[1]).intValue();
089                int tn = Integer.decode(parts[2]).intValue();
090                return new PASLoc(parts[0], sn, tn);
091            } catch (NumberFormatException e) { // XXX what's this exception?
092                throw new CorruptDataException("Invalid location of a predicate argument structure: " + s);
093            }
094        }
095    
096        /**
097         *  return true iff o is the same PASLoc as this object
098         */
099        public boolean equals (Object o) {
100            if (!(o instanceof PASLoc))
101                return false;
102            PASLoc p = (PASLoc)o;
103            return path.equals(p.path) && sentno == p.sentno && termno == p.termno;
104        }
105    
106        /**
107         * Produce a hash code for this instance
108         */
109        public int hashCode () {
110            return this.toString().hashCode();
111    // TODO apply this neat trick elsewhere
112        }
113    }
114