Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/29/08 17:22:10 (16 years ago)
Author:
gkronber
Message:

fixed #158

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAnalysis/DatasetParser.cs

    r173 r272  
    6161        List<Token> nameList = metadata["VARIABLENAMES"];
    6262        string[] names = new string[nameList.Count];
    63         for (int i = 0; i < names.Length; i++) {
     63        for(int i = 0; i < names.Length; i++) {
    6464          names[i] = nameList[i].stringValue;
    6565        }
     
    111111      tokenizer.Separators = new string[] { " ", ";", "\t" };
    112112
    113       // parse the file
    114       Parse(strict);
     113      try {
     114        // parse the file
     115        Parse(strict);
     116      } finally {
     117        reader.Close();
     118      }
    115119
    116120      // translate the list of samples into a DoubleMatrixData item
     
    121125      int i = 0;
    122126      int j = 0;
    123       foreach (List<double> row in samplesList) {
     127      foreach(List<double> row in samplesList) {
    124128        j = 0;
    125         foreach (double element in row) {
     129        foreach(double element in row) {
    126130          samples[i * columns + j] = element;
    127131          j++;
     
    180184
    181185      private void ReadNextTokens() {
    182         if (!reader.EndOfStream) {
     186        if(!reader.EndOfStream) {
    183187          CurrentLine = reader.ReadLine();
    184188          Token[] newTokens = Array.ConvertAll(CurrentLine.Split(separators, StringSplitOptions.RemoveEmptyEntries), delegate(string str) {
     
    193197
    194198      private Token MakeToken(string strToken) {
    195         if (strToken == "@")
     199        if(strToken == "@")
    196200          return AtToken;
    197         else if (strToken == "=")
     201        else if(strToken == "=")
    198202          return AssignmentToken;
    199203        else {
     
    202206          // try invariant culture
    203207          NumberFormatInfo currentNumberFormatInfo = CultureInfo.InvariantCulture.NumberFormat;
    204           if (int.TryParse(strToken, NumberStyles.Integer, currentNumberFormatInfo, out token.intValue)) {
     208          if(int.TryParse(strToken, NumberStyles.Integer, currentNumberFormatInfo, out token.intValue)) {
    205209            token.type = TokenTypeEnum.Int;
    206210            return token;
    207           } else if (double.TryParse(strToken, NumberStyles.Float, currentNumberFormatInfo, out token.doubleValue)) {
     211          } else if(double.TryParse(strToken, NumberStyles.Float, currentNumberFormatInfo, out token.doubleValue)) {
    208212            token.type = TokenTypeEnum.Double;
    209213            return token;
     
    211215          // try german culture
    212216          currentNumberFormatInfo = CultureInfo.GetCultureInfo("de-DE").NumberFormat;
    213           if (int.TryParse(strToken, NumberStyles.Integer, currentNumberFormatInfo, out token.intValue)) {
     217          if(int.TryParse(strToken, NumberStyles.Integer, currentNumberFormatInfo, out token.intValue)) {
    214218            token.type = TokenTypeEnum.Int;
    215219            return token;
    216           } else if (double.TryParse(strToken, NumberStyles.Float, currentNumberFormatInfo, out token.doubleValue)) {
     220          } else if(double.TryParse(strToken, NumberStyles.Float, currentNumberFormatInfo, out token.doubleValue)) {
    217221            token.type = TokenTypeEnum.Double;
    218222            return token;
     
    221225          // try current culture
    222226          currentNumberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
    223           if (int.TryParse(strToken, NumberStyles.Integer, currentNumberFormatInfo, out token.intValue)) {
     227          if(int.TryParse(strToken, NumberStyles.Integer, currentNumberFormatInfo, out token.intValue)) {
    224228            token.type = TokenTypeEnum.Int;
    225229            return token;
    226           } else if (double.TryParse(strToken, NumberStyles.Float, currentNumberFormatInfo, out token.doubleValue)) {
     230          } else if(double.TryParse(strToken, NumberStyles.Float, currentNumberFormatInfo, out token.doubleValue)) {
    227231            token.type = TokenTypeEnum.Double;
    228232            return token;
     
    241245        Token next = tokens[0];
    242246        tokens.RemoveAt(0);
    243         if (tokens.Count == 0) {
     247        if(tokens.Count == 0) {
    244248          ReadNextTokens();
    245249        }
     
    261265    private void ParseSampleData(bool strict) {
    262266      List<double> row = new List<double>();
    263       while (tokenizer.HasNext()) {
     267      while(tokenizer.HasNext()) {
    264268        Token current = tokenizer.Next();
    265         if (current.type == TokenTypeEnum.Double) {
     269        if(current.type == TokenTypeEnum.Double) {
    266270          // just take the value
    267271          row.Add(current.doubleValue);
    268         } else if (current.type == TokenTypeEnum.Int) {
     272        } else if(current.type == TokenTypeEnum.Int) {
    269273          // translate the int value to double
    270274          row.Add((double)current.intValue);
    271         } else if (current == Tokenizer.NewlineToken) {
     275        } else if(current == Tokenizer.NewlineToken) {
    272276          // when parsing strictly all rows have to have the same number of values           
    273           if (strict) {
     277          if(strict) {
    274278            // the first row defines how many samples are needed
    275             if (samplesList.Count > 0 && samplesList[0].Count != row.Count) {
     279            if(samplesList.Count > 0 && samplesList[0].Count != row.Count) {
    276280              Error("The first row of the dataset has " + samplesList[0].Count + " columns." +
    277281                "\nLine " + tokenizer.CurrentLineNumber + " has " + row.Count + " columns.");
    278282            }
    279           } else if (samplesList.Count > 0) {
     283          } else if(samplesList.Count > 0) {
    280284            // when we are not strict then fill or drop elements as needed
    281             if (samplesList[0].Count > row.Count) {
     285            if(samplesList[0].Count > row.Count) {
    282286              // fill with NAN
    283               for (int i = row.Count; i < samplesList[0].Count; i++) {
     287              for(int i = row.Count; i < samplesList[0].Count; i++) {
    284288                row.Add(double.NaN);
    285289              }
    286             } else if (samplesList[0].Count < row.Count) {
     290            } else if(samplesList[0].Count < row.Count) {
    287291              // drop last k elements where k = n - length of first row
    288292              row.RemoveRange(samplesList[0].Count - 1, row.Count - samplesList[0].Count);
     
    296300          // found an unexpected token => return false when parsing strictly
    297301          // when we are parsing non-strictly we also allow unreadable values inserting NAN instead
    298           if (strict) {
     302          if(strict) {
    299303            Error("Unkown value " + current + " in line " + tokenizer.CurrentLineNumber +
    300304              "\n" + tokenizer.CurrentLine);
     
    307311
    308312    private void ParseMetaData(bool strict) {
    309       while (tokenizer.Peek() == Tokenizer.AtToken) {
     313      while(tokenizer.Peek() == Tokenizer.AtToken) {
    310314        Expect(Tokenizer.AtToken);
    311315
    312316        Token nameToken = tokenizer.Next();
    313         if (nameToken.type != TokenTypeEnum.String)
     317        if(nameToken.type != TokenTypeEnum.String)
    314318          throw new Exception("Expected a variable name; got " + nameToken +
    315319            "\nLine " + tokenizer.CurrentLineNumber + ": " + tokenizer.CurrentLine);
     
    319323        List<Token> tokens = new List<Token>();
    320324        Token valueToken = tokenizer.Next();
    321         while (valueToken != Tokenizer.NewlineToken) {
     325        while(valueToken != Tokenizer.NewlineToken) {
    322326          tokens.Add(valueToken);
    323327          valueToken = tokenizer.Next();
     
    330334    private void Expect(Token expectedToken) {
    331335      Token actualToken = tokenizer.Next();
    332       if (actualToken != expectedToken) {
     336      if(actualToken != expectedToken) {
    333337        Error("Expected: " + expectedToken + " got: " + actualToken +
    334338          "\nLine " + tokenizer.CurrentLineNumber + ": " + tokenizer.CurrentLine);
Note: See TracChangeset for help on using the changeset viewer.