⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlinstances.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    // add all instances    child = m_Document.createElement(TAG_INSTANCES);    node.appendChild(child);    for (i = 0; i < m_Instances.numInstances(); i++)      addInstance(child, m_Instances.instance(i));  }    /**   * builds up the XML structure based on the given data   *    * @param data	data to generate the XML from   */  public void setInstances(Instances data) {    m_Instances = new Instances(data);    clear();    headerToXML();    dataToXML();  }    /**   * returns the current instances, either the ones that were set or the ones   * that were generated from the XML structure.   *    * @return		the current instances   */  public Instances getInstances() {    return m_Instances;  }  /**   * returns the metadata, if any available underneath this node, otherwise   * just null   *    * @param parent	the attribute node   * @return		the metadata, or null if none found   * @throws Exception	if generation fails   */  protected ProtectedProperties createMetadata(Element parent) throws Exception {    ProtectedProperties	result;    Properties		props;    Vector		list;    Element		node;    Element		metanode;    int			i;        result = null;        // find metadata node directly underneath this attribute, but not in    // deeper nested attributes (e.g., within relational attributes)    metanode = null;    list     = getChildTags(parent, TAG_METADATA);    if (list.size() > 0)      metanode = (Element) list.get(0);        // generate properties    if (metanode != null) {      props = new Properties();      list  = getChildTags(metanode, TAG_PROPERTY);      for (i = 0; i < list.size(); i++) {	node = (Element) list.get(i);	props.setProperty(node.getAttribute(ATT_NAME), getContent(node));      }      result = new ProtectedProperties(props);    }        return result;  }  /**   * returns the labels listed underneath this (nominal) attribute in a    * FastVector   *    * @param parent	the (nominal) attribute node   * @return		the label vector   * @throws Exception	if generation fails   */  protected FastVector createLabels(Element parent) throws Exception {    FastVector		result;    Vector		list;    Element		node;    Element		labelsnode;    int			i;        result = new FastVector();        // find labels node directly underneath this attribute, but not in    // deeper nested attributes (e.g., within relational attributes)    labelsnode = null;    list     = getChildTags(parent, TAG_LABELS);    if (list.size() > 0)      labelsnode = (Element) list.get(0);        // retrieve all labels    if (labelsnode != null) {      list  = getChildTags(labelsnode, TAG_LABEL);      for (i = 0; i < list.size(); i++) {	node = (Element) list.get(i);	result.addElement(getContent(node));      }    }        return result;  }    /**   * creates an Attribute from the given XML node   *    * @param node	the node with the setup   * @return		the configured Attribute   * @throws Exception	if generation fails, e.g., due to unknown attribute type   */  protected Attribute createAttribute(Element node) throws Exception {    String		typeStr;    String		name;    int			type;    Attribute		result;    FastVector		values;    ProtectedProperties	metadata;    Vector		list;    FastVector		atts;        result = null;        // name    name = node.getAttribute(ATT_NAME);    // type    typeStr = node.getAttribute(ATT_TYPE);    if (typeStr.equals(VAL_NUMERIC))      type = Attribute.NUMERIC;    else if (typeStr.equals(VAL_DATE))      type = Attribute.DATE;    else if (typeStr.equals(VAL_NOMINAL))      type = Attribute.NOMINAL;    else if (typeStr.equals(VAL_STRING))      type = Attribute.STRING;    else if (typeStr.equals(VAL_RELATIONAL))      type = Attribute.RELATIONAL;    else      throw new Exception(	  "Attribute type '" + typeStr + "' is not supported!");    // metadata    metadata = createMetadata(node);        switch (type) {      case Attribute.NUMERIC:	if (metadata == null)	  result = new Attribute(name);	else	  result = new Attribute(name, metadata);	break;      case Attribute.DATE:	if (metadata == null)	  result = new Attribute(name, node.getAttribute(ATT_FORMAT));	else	  result = new Attribute(name, node.getAttribute(ATT_FORMAT), metadata);	break;	      case Attribute.NOMINAL:	values = createLabels(node);	if (metadata == null)	  result = new Attribute(name, values);	else	  result = new Attribute(name, values, metadata);	break;	      case Attribute.STRING:	if (metadata == null)	  result = new Attribute(name, (FastVector) null);	else	  result = new Attribute(name, (FastVector) null, metadata);	break;	      case Attribute.RELATIONAL:	list = getChildTags(node, TAG_ATTRIBUTES);	node = (Element) list.get(0);	atts = createAttributes(node, new int[1]);	if (metadata == null)	  result = new Attribute(name, new Instances(name, atts, 0));	else	  result = new Attribute(name, new Instances(name, atts, 0), metadata);	break;    }        return result;  }    /**   * returns a list of generated attributes   *    * @param parent	the attributes node   * @param classIndex	array of length 1 to return the class index, if any   * @return		the vector with the generated attributes   * @throws Exception	if generation fails, e.g., due to unknown attribute type   */  protected FastVector createAttributes(Element parent, int[] classIndex) throws Exception {    Vector	list;    FastVector	result;    int		i;    Element	node;    Attribute	att;    result        = new FastVector();    classIndex[0] = -1;        list = getChildTags(parent, TAG_ATTRIBUTE);    for (i = 0; i < list.size(); i++) {      node = (Element) list.get(i);      att = createAttribute(node);      if (node.getAttribute(ATT_CLASS).equals(VAL_YES))	classIndex[0] = i;      result.addElement(att);    }        return result;  }    /**   * creates an Instance from the given XML node   *    * @param header	the data this instance will belong to   * @param parent	the instance node   * @return		the configured Instance   * @throws Exception	if generation fails, e.g., due to unknown attribute type   */  protected Instance createInstance(Instances header, Element parent) throws Exception {    Instance	result;    Element	node;    Element	child;    boolean	sparse;    int		i;    int		index;    Vector	list;    Vector	subList;    double[]	values;    String	content;    double	weight;    Instances	data;        result = null;    // sparse?    sparse = (parent.getAttribute(ATT_TYPE).equals(VAL_SPARSE));    values = new double[header.numAttributes()];        // weight    if (parent.getAttribute(ATT_WEIGHT).length() != 0)      weight = Double.parseDouble(parent.getAttribute(ATT_WEIGHT));    else      weight = 1.0;        list = getChildTags(parent, TAG_VALUE);    for (i = 0; i < list.size(); i++) {      node = (Element) list.get(i);            // determine index      if (sparse)	index = Integer.parseInt(node.getAttribute(ATT_INDEX)) - 1;      else	index = i;      // set value      if (node.getAttribute(ATT_MISSING).equals(VAL_YES)) {	values[index] = Instance.missingValue();      }      else {	content = getContent(node);	switch (header.attribute(index).type()) {	  case Attribute.NUMERIC:	    values[index] = Double.parseDouble(content);	    break;	    	  case Attribute.DATE:	    values[index] = header.attribute(index).parseDate(content);	    break;	    	  case Attribute.NOMINAL:	    values[index] = header.attribute(index).indexOfValue(content);	    break;	    	  case Attribute.STRING:	    values[index] = header.attribute(index).addStringValue(content);	    break;	    	  case Attribute.RELATIONAL:	    subList       = getChildTags(node, TAG_INSTANCES);	    child         = (Element) subList.get(0);	    data          = createInstances(header.attribute(index).relation(), child);	    values[index] = header.attribute(index).addRelation(data);	    break;	    	  default:	    throw new Exception(		"Attribute type " + header.attribute(index).type() 		+ " is not supported!");  	}      }    }        // create instance    if (sparse)      result = new SparseInstance(weight, values);    else      result = new Instance(weight, values);        return result;  }    /**   * creates Instances from the given XML node   *    * @param header	the header of this data   * @param parent	the instances node   * @return		the generated Instances   * @throws Exception	if generation fails, e.g., due to unknown attribute type   */  protected Instances createInstances(Instances header, Element parent) throws Exception {    Instances	result;    Vector	list;    int		i;        result = new Instances(header, 0);        list = getChildTags(parent, TAG_INSTANCE);    for (i = 0; i < list.size(); i++)      result.add(createInstance(result, (Element) list.get(i)));        return result;  }    /**   * generates the header from the XML document   *    * @return		the generated header   * @throws Exception	if generation fails   */  protected Instances headerFromXML() throws Exception {    Instances	result;    Element	root;    Element	node;    Vector	list;    FastVector	atts;    Version	version;    int[]	classIndex;    root = m_Document.getDocumentElement();        // check version    version = new Version();    if (version.isOlder(root.getAttribute(ATT_VERSION)))      System.out.println(	  "WARNING: loading data of version " + root.getAttribute(ATT_VERSION)	  + " with version " + Version.VERSION);        // attributes    list       = getChildTags(root, TAG_HEADER);    node       = (Element) list.get(0);    list       = getChildTags(node, TAG_ATTRIBUTES);    node       = (Element) list.get(0);    classIndex = new int[1];    atts       = createAttributes(node, classIndex);    // generate header    result = new Instances(root.getAttribute(ATT_NAME), atts, 0);    result.setClassIndex(classIndex[0]);        return result;  }    /**   * generates the complete dataset from the XML document   *    * @param header	the header structure   * @return		the complete dataset   * @throws Exception	if generation fails   */  protected Instances dataFromXML(Instances header) throws Exception {    Instances	result;    Element	node;    Vector	list;    list   = getChildTags(m_Document.getDocumentElement(), TAG_BODY);    node   = (Element) list.get(0);    list   = getChildTags(node, TAG_INSTANCES);    node   = (Element) list.get(0);    result = createInstances(header, node);        return result;  }    /**   * reads the XML structure from the given reader   *    * @param reader	the reader to get the XML from   * @throws Exception	if    */  public void setXML(Reader reader) throws Exception {    read(reader);        // interprete XML structure    m_Instances = dataFromXML(headerFromXML());  }    /**   * takes an XML document as first argument and then outputs the Instances   * statistics   *    * @param args	the commandline options   */  public static void main(String[] args) {    try {      Reader r = null;      if (args.length != 1) {	throw (new Exception("Usage: XMLInstances <filename>"));      }      else {	InputStream in = new FileInputStream(args[0]);	// compressed file?	if (args[0].endsWith(".gz"))	  in = new GZIPInputStream(in);        r = new BufferedReader(new InputStreamReader(in));     }            if (args[0].endsWith(Instances.FILE_EXTENSION)) {	XMLInstances i = new XMLInstances(new Instances(r));	System.out.println(i.toString());      }      else {	Instances i = new XMLInstances(r).getInstances();	System.out.println(i.toSummaryString());      }    }    catch (Exception ex) {      ex.printStackTrace();      System.err.println(ex.getMessage());    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -