Free cookie consent management tool by TermsFeed Policy Generator

Changeset 406


Ignore:
Timestamp:
07/29/08 19:17:36 (16 years ago)
Author:
gkronber
Message:

fixed #209 by changing the import format while keeping compatibility with HL2 exported files

File:
1 edited

Legend:

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

    r405 r406  
    194194        using(StreamReader reader = new StreamReader(importFileName)) {
    195195          tokenizer = new Tokenizer(reader, numberFormat);
    196           tokenizer.Separators = new string[] { " ", ";", "\t" };
    197196          try {
    198197            // parse the file
     
    210209    #region tokenizer
    211210    internal enum TokenTypeEnum {
    212       At, Assign, NewLine, String, Double, Int
     211      At, Assign, NewLine, String, Double, Int, WhiteSpace
    213212    }
    214213
     
    235234      private StreamReader reader;
    236235      private List<Token> tokens;
    237       private string[] separators;
     236      private string[] separators = new string[] { "@", "=", ";", "\t" };
    238237      private NumberFormatInfo numberFormatInfo;
    239238
     
    244243      public static Token AtToken = new Token(TokenTypeEnum.At, "@");
    245244      public static Token AssignmentToken = new Token(TokenTypeEnum.Assign, "=");
    246 
     245      public static Token SeparatorToken = new Token(TokenTypeEnum.WhiteSpace, "");
    247246      public string[] Separators {
    248247        get { return separators; }
     
    262261          CurrentLine = reader.ReadLine();
    263262          Token[] newTokens = Array.ConvertAll(CurrentLine.Split(separators, StringSplitOptions.RemoveEmptyEntries), delegate(string str) {
    264             return MakeToken(str);
     263            return MakeToken(str.Trim());
    265264          });
    266265
    267           tokens.AddRange(newTokens);
     266          foreach(Token tok in newTokens) {
     267            if(tok != SeparatorToken) tokens.Add(tok);
     268          }
    268269          tokens.Add(NewlineToken);
    269270          CurrentLineNumber++;
     
    272273
    273274      private Token MakeToken(string strToken) {
    274         if(strToken == "@")
    275           return AtToken;
    276         else if(strToken == "=")
    277           return AssignmentToken;
    278         else {
    279           Token token = new Token(TokenTypeEnum.String, strToken);
    280 
    281           if(int.TryParse(strToken, NumberStyles.Integer, numberFormatInfo, out token.intValue)) {
    282             token.type = TokenTypeEnum.Int;
    283             return token;
    284           } else if(double.TryParse(strToken, NumberStyles.Float, numberFormatInfo, out token.doubleValue)) {
    285             token.type = TokenTypeEnum.Double;
    286             return token;
    287           }
    288           // couldn't parse the token as an int or float number
     275        Token token = new Token(TokenTypeEnum.String, strToken);
     276
     277        // try to parse as a number first
     278        if(int.TryParse(strToken, NumberStyles.Integer, numberFormatInfo, out token.intValue)) {
     279          token.type = TokenTypeEnum.Int;
    289280          return token;
    290         }
     281        } else if(double.TryParse(strToken, NumberStyles.Float, numberFormatInfo, out token.doubleValue)) {
     282          token.type = TokenTypeEnum.Double;
     283          return token;
     284        }
     285        // couldn't parse the token as an int or float number so return a string token
     286        return token;
    291287      }
    292288
     
    363359
    364360    private void ParseMetaData(bool strict) {
    365       while(tokenizer.Peek() == Tokenizer.AtToken) {
    366         Expect(Tokenizer.AtToken);
    367 
     361      while(tokenizer.Peek().type==TokenTypeEnum.String) {
    368362        Token nameToken = tokenizer.Next();
    369363        if(nameToken.type != TokenTypeEnum.String)
    370364          Error("Expected a variable name.", nameToken.stringValue, tokenizer.CurrentLineNumber);
    371 
    372         Expect(Tokenizer.AssignmentToken);
    373365
    374366        List<Token> tokens = new List<Token>();
Note: See TracChangeset for help on using the changeset viewer.