1
16 package gate.creole.ml;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21
22 import org.jdom.Element;
23
24 import gate.util.GateException;
25
26
29
30 public class Attribute implements Serializable{
31
32 public Attribute(Element jdomElement) throws GateException {
33 Element anElement = jdomElement.getChild("NAME");
35 if(anElement == null) throw new GateException(
36 "Required element \"NAME\" not present in attribute:\n" +
37 jdomElement.toString() + "!");
38 else name = anElement.getTextTrim();
39
40 anElement = jdomElement.getChild("TYPE");
42 if(anElement == null) throw new GateException(
43 "Required element \"TYPE\" not present in attribute:\n" +
44 jdomElement.toString() + "!");
45 else type = anElement.getTextTrim();
46
47 anElement = jdomElement.getChild("FEATURE");
49 if(anElement != null)feature = anElement.getTextTrim();
50
51 anElement = jdomElement.getChild("POSITION");
53 if(anElement == null) position = 0;
54 else position = Integer.parseInt(anElement.getTextTrim());
55
56 anElement = jdomElement.getChild("WEIGHTING");
58 if (anElement == null) weighting = 1.0;
59 else weighting = Double.parseDouble(anElement.getTextTrim());
60
61 isClass = jdomElement.getChild("CLASS") != null;
63
64 anElement = jdomElement.getChild("VALUES");
66 if(anElement == null) values = null;
67 else{
68 values = new ArrayList();
69 Iterator valuesIter = anElement.getChildren("VALUE").iterator();
70 while(valuesIter.hasNext()){
71 values.add(((Element)valuesIter.next()).getTextTrim());
72 }
73 }
74 }
75
76 public Attribute(){
77 name = null;
78 type =null;
79 feature = null;
80 isClass = false;
81 position = 0;
82 values = null;
83 weighting = 1.0;
84 }
85
86 public String toString(){
87 StringBuffer res = new StringBuffer();
88 res.append("Name: " + name + "\n");
89 res.append("Type: " + type + "\n");
90 res.append("Feature: " + feature + "\n");
91 res.append("Weighting: "+ weighting + "\n");
92 Iterator valIter = values.iterator();
93 while(valIter.hasNext()){
94 res.append(" Value:" + valIter.next().toString() + "\n");
95 }
96 return res.toString();
97 }
98
99
104 public static java.util.List parseSeries(Element jdomElement) throws GateException {
105 Element anElement = jdomElement.getChild("NAME");
107 if(anElement == null) throw new GateException(
108 "Required element \"NAME\" not present in attribute:\n" +
109 jdomElement.toString() + "!");
110
111 String name = anElement.getTextTrim();
112
113 anElement = jdomElement.getChild("TYPE");
115 if(anElement == null) throw new GateException(
116 "Required element \"TYPE\" not present in attribute:\n" +
117 jdomElement.toString() + "!");
118
119 String type = anElement.getTextTrim();
120
121 String feature = null;
122
123 anElement = jdomElement.getChild("FEATURE");
125 if(anElement != null)feature = anElement.getTextTrim();
126
127 int minpos = 0;
128 int maxpos = 0;
129
130 anElement = jdomElement.getChild("RANGE");
132 try {
133 minpos = Integer.parseInt(anElement.getAttributeValue("from").trim());
134 maxpos = Integer.parseInt(anElement.getAttributeValue("to").trim());
135 } catch (Exception e){
136 throw new GateException(
137 "Range element is uncorrect:\n" +
138 jdomElement.toString() + "!");
139 }
140
141 double weighting = 1.0;
142
143 anElement = jdomElement.getChild("WEIGHTING");
145 if (anElement != null) weighting = Double.parseDouble(anElement.getTextTrim());
146
147 boolean isClass = jdomElement.getChild("CLASS") != null;
149 if (isClass){
150 throw new GateException(
151 "Cannot define the class in a serie:\n" +
152 jdomElement.toString() + "!");
153 }
154
155
156 java.util.List values = null;
157 anElement = jdomElement.getChild("VALUES");
159 if(anElement == null) values = null;
160 else{
161 values = new ArrayList();
162 Iterator valuesIter = anElement.getChildren("VALUE").iterator();
163 while(valuesIter.hasNext()){
164 values.add(((Element)valuesIter.next()).getTextTrim());
165 }
166 }
167
168 ArrayList attributes = new ArrayList();
170 for (int position =minpos; position<maxpos+1;position++ ){
171 Attribute attribute = new Attribute();
172 attribute.setClass(false);
173 attribute.setFeature(feature);
174 attribute.setName(name+"_"+position);
175 attribute.setPosition(position);
176 attribute.setType(type);
177 attribute.setWeighting(weighting);
178 attribute.setValues(values);
179 attributes.add(attribute);
180 }
181 return attributes;
182 }
183
184
185 public boolean isClass(){
186 return isClass;
187 }
188
189 public void setName(String name) {
190 this.name = name;
191 }
192
193 public String getName() {
194 return name;
195 }
196
197 public void setType(String type) {
198 this.type = type;
199 }
200
201 public String getType() {
202 return type;
203 }
204
205 public void setFeature(String feature) {
206 this.feature = feature;
207 }
208
209 public String getFeature() {
210 return feature;
211 }
212
213 public void setWeighting(double weighting) {
214 this.weighting = weighting;
215 }
216
217 public double getWeighting() {
218 return weighting;
219 }
220
221 public java.util.List getValues() {
222 return values;
223 }
224
225 public int getPosition() {
226 return position;
227 }
228
229 public void setClass(boolean isClass) {
230 this.isClass = isClass;
231 }
232
233 public void setValues(java.util.List values) {
234 this.values = values;
235 }
236
237 public void setPosition(int position) {
238 this.position = position;
239 }
240
241
246 public int semanticType() {
247 if (feature==null)
251 return BOOLEAN;
252 if (values==null)
253 return NUMERIC;
254 return NOMINAL;
255 }
256
257 public static final int NOMINAL=1;
259 public static final int NUMERIC=2;
260 public static final int BOOLEAN=3;
261
262 boolean isClass = false;
263 private String name;
264 private String type;
265 private String feature;
266 private java.util.List values;
267 private int position;
268 private double weighting;
273 }