1   /*
2    *  Copyright (c) 1998-2004, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 19/11/2002
10   *  semantic type added by Mike Dowman 31-03-2004
11   *  Weightings added by Mike Dowman 24-5-2004
12   *
13   *  $Id: Attribute.java,v 1.7 2004/07/21 17:10:05 akshay Exp $
14   *
15   */
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  /**
27   * Describes an attribute associated to a ML instance.
28   */
29  
30  public class Attribute implements Serializable{
31  
32    public Attribute(Element jdomElement) throws GateException {
33      //find the name
34      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      //find the type
41      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      //find the feature if present
48      anElement = jdomElement.getChild("FEATURE");
49      if(anElement != null)feature = anElement.getTextTrim();
50  
51      //find the position if present
52      anElement = jdomElement.getChild("POSITION");
53      if(anElement == null) position = 0;
54      else position = Integer.parseInt(anElement.getTextTrim());
55  
56      // find the weighting if present
57      anElement = jdomElement.getChild("WEIGHTING");
58      if (anElement == null) weighting = 1.0;
59      else weighting = Double.parseDouble(anElement.getTextTrim());
60  
61      //find the class if present
62      isClass = jdomElement.getChild("CLASS") != null;
63  
64      //find the allowed values if present
65      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    public boolean isClass(){
100     return isClass;
101   }
102 
103   public void setName(String name) {
104     this.name = name;
105   }
106 
107   public String getName() {
108     return name;
109   }
110 
111   public void setType(String type) {
112     this.type = type;
113   }
114 
115   public String getType() {
116     return type;
117   }
118 
119   public void setFeature(String feature) {
120     this.feature = feature;
121   }
122 
123   public String getFeature() {
124     return feature;
125   }
126 
127   public void setWeighting(double weighting) {
128     this.weighting = weighting;
129   }
130 
131   public double getWeighting() {
132     return weighting;
133   }
134 
135   public java.util.List getValues() {
136     return values;
137   }
138 
139   public int getPosition() {
140     return position;
141   }
142 
143   public void setClass(boolean isClass) {
144     this.isClass = isClass;
145   }
146 
147   public void setValues(java.util.List values) {
148     this.values = values;
149   }
150 
151   public void setPosition(int position) {
152     this.position = position;
153   }
154 
155   /**
156    * This method reports whether the attribute is nominal, numeric or boolean.
157    *
158    * @return Attribute.NOMINAL, Attribute.NUMERIC or Attribute.BOOLEAN
159    */
160   public int semanticType() {
161     // Only nominal attributes specify values, and only numeric and nominal
162     // attributes specify feature, so this code is sufficient to distinguish
163     // the three kinds of attribute.
164     if (feature==null)
165       return BOOLEAN;
166     if (values==null)
167       return NUMERIC;
168     return NOMINAL;
169   }
170 
171   // These constants are used only for returning values from semanticType
172   public static final int NOMINAL=1;
173   public static final int NUMERIC=2;
174   public static final int BOOLEAN=3;
175 
176   boolean isClass = false;
177   private String name;
178   private String type;
179   private String feature;
180   private java.util.List values;
181   private int position;
182   // The SVMLightWrapper allows weighting for attributes to be specified in
183   // the configuration file, and those weightings are stored in this member.
184   // Weightings are (at time of writing) ignored by the Weka and Maxent
185   // wrappers.
186   private double weighting;
187 }