TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
BiGrams.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Linq;
21 using System.Text;
22 using TraceLab.Components.Types.Preprocessors.ExecutionTraces;
23 
24 namespace TraceLab.Components.DevelopmentKit.IO
25 {
29  public static class BiGrams
30  {
36  public static void Export(BiGramCollection bigrams, string filename)
37  {
38  TextWriter file = new StreamWriter(filename);
39  foreach (BiGram bigram in bigrams)
40  {
41  file.WriteLine("{0}\t{1}", bigram.Caller, bigram.Callee);
42  }
43  file.Flush();
44  file.Close();
45  }
46 
52  public static BiGramCollection Import(string filename)
53  {
54  TextReader file = new StreamReader(filename);
55  BiGramCollection bigrams = new BiGramCollection();
56  int lineNumber = 0;
57  string line;
58  while ((line = file.ReadLine()) != null)
59  {
60  lineNumber++;
61  if (String.IsNullOrWhiteSpace(line))
62  continue;
63  string caller, callee;
64  try
65  {
66  string[] split = line.Split('\t');
67  caller = split[0];
68  callee = split[1];
69  }
70  catch (IndexOutOfRangeException e)
71  {
72  throw new DevelopmentKitException("Error in bigram file format on line " + lineNumber, e);
73  }
74  bigrams.Add(new BiGram(caller, callee));
75  }
76  return bigrams;
77  }
78  }
79 }