/// <summary> Appends all elements of the supplied vector to this vector. /// /// </summary> /// <param name="toAppend">the FastVector containing elements to append. /// </param> public void appendXmlElements(FastVector toAppend) { Capacity = size() + toAppend.size(); Array.Copy(toAppend.m_Objects, 0, m_Objects, size(), toAppend.size()); m_Size = m_Objects.Length; }
/// <summary> Split up a string containing options into an array of strings, /// one for each option. /// /// </summary> /// <param name="optionString">the string containing the options /// </param> /// <returns> the array of options /// </returns> public static System.String[] splitOptions(System.String quotedOptionString) { FastVector optionsVec = new FastVector(); System.String str = new System.Text.StringBuilder(quotedOptionString).ToString(); int i; while (true) { //trimLeft i = 0; while ((i < str.Length) && (System.Char.IsWhiteSpace(str[i]))) i++; str = str.Substring(i); //stop when str is empty if (str.Length == 0) break; //if str start with a double quote if (str[0] == '"') { //find the first not anti-slached double quote i = 1; while (i < str.Length) { if (str[i] == str[0]) break; if (str[i] == '\\') { i += 1; if (i >= str.Length) throw new System.Exception("String should not finish with \\"); if (str[i] != '\\' && str[i] != '"') throw new System.Exception("Unknow character \\" + str[i]); } i += 1; } if (i >= str.Length) throw new System.Exception("Quote parse error."); //add the founded string to the option vector (without quotes) System.String optStr = str.Substring(1, (i) - (1)); optStr = unbackQuoteChars(optStr); optionsVec.addElement(optStr); str = str.Substring(i + 1); } else { //find first whiteSpace i = 0; while ((i < str.Length) && (!System.Char.IsWhiteSpace(str[i]))) i++; //add the founded string to the option vector System.String optStr = str.Substring(0, (i) - (0)); optionsVec.addElement(optStr); str = str.Substring(i); } } //convert optionsVec to an array of String System.String[] options = new System.String[optionsVec.size()]; for (i = 0; i < optionsVec.size(); i++) { options[i] = ((System.String) optionsVec.elementAt(i)); } return options; }
/// <summary> Reads and stores header of an ARFF file. /// /// </summary> /// <param name="tokenizer">the stream tokenizer /// </param> /// <exception cref="IOException">if the information is not read /// successfully /// </exception> protected internal virtual void readHeader(StreamTokenizer tokenizer) { System.String attributeName; FastVector attributeValues; //int i; Token token=null; // Get name of relation. getFirstToken(tokenizer, out token); //if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOF) if ((token != null) && (token is EofToken)) { errms(tokenizer, "premature end of file"); } if (ARFF_RELATION.ToUpper().Equals(token.StringValue.ToUpper())) { getNextToken(tokenizer,out token); m_RelationName = token.StringValue; getLastToken(tokenizer,out token, false); } else { errms(tokenizer, "keyword " + ARFF_RELATION + " expected"); } // Create vectors to hold information temporarily. m_Attributes = new FastVector(); // Get attribute declarations. getFirstToken(tokenizer, out token); //if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOF) if ((token != null) && (token is EofToken)) { errms(tokenizer, "premature end of file"); } while (Attribute.ARFF_ATTRIBUTE.ToUpper().Equals(token.StringValue.ToUpper())) { // Get attribute name. getNextToken(tokenizer,out token); attributeName = token.StringValue; getNextToken(tokenizer,out token); // Check if attribute is nominal. //if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_WORD) if ((token != null) && (token is WordToken)) { // Attribute is real, integer, or string. if (token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_REAL.ToUpper()) || token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_INTEGER.ToUpper()) || token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_NUMERIC.ToUpper())) { m_Attributes.addElement(new Attribute(attributeName, numAttributes())); readTillEOL(tokenizer); } else if (token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_STRING.ToUpper())) { m_Attributes.addElement(new Attribute(attributeName, (FastVector) null, numAttributes())); readTillEOL(tokenizer); } else if (token.StringValue.ToUpper().Equals(Attribute.ARFF_ATTRIBUTE_DATE.ToUpper())) { System.String format = null; tokenizer.NextToken(out token); //if (tokenizer.NextToken() != SupportClass.StreamTokenizerSupport.TT_EOL) if ((token != null) && (!(token is EofToken))) { //if ((tokenizer.ttype != SupportClass.StreamTokenizerSupport.TT_WORD) && (tokenizer.ttype != '\'') && (tokenizer.ttype != '\"')) if ((token != null) && (!(token is WordToken)) && (token.StringValue!="'") && (token.StringValue!="\"") ) { errms(tokenizer, "not a valid date format"); } format = token.StringValue; readTillEOL(tokenizer); } else { tokenizer.PushBack(token); } m_Attributes.addElement(new Attribute(attributeName, format, numAttributes())); } else { errms(tokenizer, "no valid attribute type or invalid " + "enumeration"); } } else { // Attribute is nominal. attributeValues = new FastVector(); tokenizer.PushBack(token); // Get values for nominal attribute. tokenizer.NextToken(out token); if ( token.StringValue != "{") { errms(tokenizer, "{ expected at beginning of enumeration"); } tokenizer.NextToken(out token); while ( token.StringValue != "}") { //if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOL) if (token is EolToken) { errms(tokenizer, "} expected at end of enumeration"); } else { attributeValues.addElement(token.StringValue); } tokenizer.NextToken(out token); } if (attributeValues.size() == 0) { errms(tokenizer, "no nominal values found"); } m_Attributes.addElement(new Attribute(attributeName, attributeValues, numAttributes())); } getLastToken(tokenizer,out token, false); getFirstToken(tokenizer,out token); //if (tokenizer.ttype == SupportClass.StreamTokenizerSupport.TT_EOF) if (token is EofToken) errms(tokenizer, "premature end of file"); } // Check if data part follows. We can't easily check for EOL. if (!ARFF_DATA.ToUpper().Equals(token.StringValue.ToUpper())) { errms(tokenizer, "keyword " + ARFF_DATA + " expected"); } // Check if any attributes have been declared. if (m_Attributes.size() == 0) { errms(tokenizer, "no attributes declared"); } // Allocate buffers in case sparse instances have to be read m_ValueBuffer = new double[numAttributes()]; m_IndicesBuffer = new int[numAttributes()]; }
/// <summary> Help function needed for stratification of set. /// /// </summary> /// <param name="numFolds">the number of folds for the stratification /// </param> protected internal virtual void stratStep(int numFolds) { FastVector newVec = new FastVector(m_Instances.capacity()); int start = 0, j; // create stratified batch while (newVec.size() < numInstances()) { j = start; while (j < numInstances()) { newVec.addElement(instance(j)); j = j + numFolds; } start++; } m_Instances = newVec; }
/// <summary> Constructor for nominal attributes and string attributes, where /// metadata is supplied. If a null vector of attribute values is passed /// to the method, the attribute is assumed to be a string. /// /// </summary> /// <param name="attributeName">the name for the attribute /// </param> /// <param name="attributeValues">a vector of strings denoting the /// attribute values. Null if the attribute is a string attribute. /// </param> /// <param name="metadata">the attribute's properties /// </param> //@ requires attributeName != null; //@ requires metadata != null; /*@ ensures m_Name == attributeName; ensures m_Index == -1; ensures attributeValues == null && m_Type == STRING || attributeValues != null && m_Type == NOMINAL && m_Values.size() == attributeValues.size(); signals (IllegalArgumentException ex) (* if duplicate strings in attributeValues *); */ public Attribute(System.String attributeName, FastVector attributeValues, ProtectedProperties metadata) { m_Name = attributeName; m_Index = - 1; if (attributeValues == null) { m_Values = new FastVector(); m_Hashtable = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable()); m_Type = STRING; } else { m_Values = new FastVector(attributeValues.size()); //m_Hashtable = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable(attributeValues.size())); m_Hashtable = new System.Collections.Hashtable(attributeValues.size()); for (int i = 0; i < attributeValues.size(); i++) { // No serialization System.Object store = attributeValues.elementAt(i); //if (((System.String) store).Length > STRING_COMPRESS_THRESHOLD) //{ // try // { // store = new SerializedObject(attributeValues.elementAt(i), true); // } // catch (System.Exception ex) // { // System.Console.Error.WriteLine("Couldn't compress nominal attribute value -" + " storing uncompressed."); // } //} if (m_Hashtable.ContainsKey(store)) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" throw new System.ArgumentException("A nominal attribute (" + attributeName + ") cannot" + " have duplicate labels (" + store + ")."); } m_Values.addElement(store); m_Hashtable.Add(store, (System.Int32) i); } m_Type = NOMINAL; } setMetadata(metadata); }