001 package edu.upenn.cis.propbank_shen;
002
003 import java.util.LinkedList;
004 import java.util.List;
005 import java.util.Iterator;
006
007 import org.w3c.dom.Node;
008 import org.w3c.dom.NamedNodeMap;
009 import org.w3c.dom.Attr;
010
011 /**
012 A representation of a RoleSet as defined in the propbank lexical guidelines,
013 frameset.dtd.
014
015 <p>
016
017 A roleset defines a sort of coarse grained sense for a verb. When we
018 understand a verb as an object which relates things
019 (such as in "John gave Mary a Penny", "gave" is relating John, Mary, and "a
020 Penny".), we'll quickly come to see that verbs may relate different kinds
021 of things. For example, the verb "call" may be used to relate either
022 a caller with an object and a label (in the labelling sense of call) or
023 a caller with a thing being summoned and a place being summoned to. (in the
024 summon sense of call (eg "John called Mary over").
025
026 <p>
027
028 We then view each of these things
029 <ul>
030 <li> (caller, object, label)
031 <li> (caller, thing summoned, destination)
032 </ul>
033 as a roleset. RoleSet objects provide a handle on these coarse grained
034 senses.
035 @author Scott Cotton
036
037 */
038
039 public class RoleSet {
040
041 /** the identifier of the roleset */
042 protected String id;
043
044 /** the name of the roleset, optional, can be null */
045 protected String name;
046 /** the node from which a roleset is constructed */
047 protected Node node;
048 /** the set of roles associated with the roleset */
049 protected List roles;
050 /** the set of examples associated with the roleset */
051 protected List examples;
052 /** the VerbNet classes to which this roleset belongs */
053 protected String vnclasses[];
054 /**
055 construct a RoleSet object from a roleset node in a frameset
056 xml document.
057 */
058 public RoleSet(Node n) throws CorruptDataException
059 {
060 name = null;
061 vnclasses = new String[0];
062 node = n;
063 NamedNodeMap attrs = n.getAttributes();
064 int len = attrs.getLength();
065 String anm;
066 for(int i=0; i<len; i++) {
067 Attr attr = (Attr) attrs.item(i);
068 anm = attr.getNodeName();
069 if (anm.equals("id")) {
070 id = (String) attr.getNodeValue();
071 } else if(anm.equals("vncls")) {
072 vnclasses = ((String) attr.getNodeValue()).split(" ");
073 } else if (anm.equals("name")) {
074 name = (String) attr.getNodeValue();
075 }
076 }
077 // parse the roles
078 roles = new LinkedList();
079 Node rn = node.getFirstChild();
080 while(rn != null) {
081 if (rn.getNodeName().equals("roles")) {
082 Node nc = rn.getFirstChild();
083 while (nc != null) {
084 if (nc.getNodeName().equals("role")) {
085 roles.add(new Role(nc));
086 }
087 nc = nc.getNextSibling();
088 }
089 }
090 rn = rn.getNextSibling();
091 }
092 // parse the examples
093 examples = new LinkedList();
094 Node ne = node.getFirstChild();
095 while(ne != null) {
096 if (ne.getNodeName().equals("example")) {
097 examples.add(new Example(ne));
098 }
099 ne = ne.getNextSibling();
100 }
101 }
102
103 /**
104 create a RoleSet object from the id, where the id is in the form
105 <verb>.NN, such as "go.01".
106
107 @param id the roleset identifier
108 */
109 public static RoleSet ofId(String id) throws CorruptDataException
110 {
111 int i = id.indexOf('.');
112 if (i == -1) {
113 throw new CorruptDataException("invalid roleset id: " + id);
114 }
115 String verb = id.substring(0, i);
116 FrameSet fs = new FrameSet(verb);
117 List l = fs.getPredicates();
118 Iterator p = l.iterator();
119 while (p.hasNext()) {
120 Predicate pred = (Predicate) p.next();
121 Iterator ri = pred.getRoleSets().iterator();
122 while(ri.hasNext()) {
123 RoleSet rs = (RoleSet) ri.next();
124 if (rs.getId().equals(id)) {
125 return rs;
126 }
127 }
128 }
129 System.err.println("no roleset found with id" + id);
130 return null;
131 }
132
133 /** return the identifier associated with the roleset */
134 public String getId()
135 {
136 return id;
137 }
138
139 /** return the name of this roleset, or null if there is none specified. */
140 public String getName()
141 {
142 return name;
143 }
144 /** return true iff this roleset has an associated name */
145 public boolean hasName()
146 {
147 return name != null;
148 }
149
150 /** return the verbnet class ids associated with this roleset */
151 public String[] getVNClasses()
152 {
153 return vnclasses;
154 }
155
156 /** return the list of roles associated with the roleset
157 @see edu.upenn.cis.propbank_shen.Role
158 */
159 public List getRoles()
160 {
161 return roles;
162 }
163
164 /**
165 return a list of the Example objects associated with this roleset
166 @see edu.upenn.cis.propbank_shen.Example
167 */
168 public List getExamples()
169 {
170 return examples;
171 }
172
173 /**
174 A simple unit test
175 */
176 public static void main(String args[])
177 throws CorruptDataException
178 {
179 if (args.length < 1) {
180 System.err.println("sorry, please give me a roleset id (eg go.01)");
181 System.exit(1);
182 }
183 RoleSet rs = RoleSet.ofId(args[0]);
184 if (rs.hasName()) {
185 System.out.println("roleset " + args[0] + ", name is " + rs.getName());
186 }
187 String vnclasses[] = rs.getVNClasses();
188 for (int j=0; j<vnclasses.length; j++) {
189 System.out.println("\tverbnet class " + vnclasses[j]);
190 }
191 List l = rs.getRoles();
192 Iterator ri = l.iterator();
193 while(ri.hasNext()) {
194 Role r = (Role) ri.next();
195 ArgLabel al = r.getArgLabel();
196 String d = r.getDescription();
197 if (r.hasModLabel()) {
198 ModLabel ml = r.getModLabel();
199 System.out.println(al.toString() + "-" + ml.toString() + ": " + d);
200 } else {
201 System.out.println(al.toString() + ": " + d);
202 }
203 }
204 Iterator ei = rs.getExamples().iterator();
205 while(ei.hasNext()) {
206 Example e = (Example) ei.next();
207 System.out.println(e.getText());
208 }
209 }
210 }