Code Examples
A repository of 155 code examples for BeepBeep
SequenceReader.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package mining;
19 
20 import java.io.InputStream;
21 import java.util.HashSet;
22 import java.util.Scanner;
23 import java.util.Set;
24 
25 import ca.uqac.lif.cep.peg.Sequence;
26 
27 /**
28  * Utility class that creates a set of sequences from a file. This
29  * object is used in the code throughout this section in order to
30  * avoid programmatically creating input traces for every example.
31  *
32  * @author Sylvain HallĂ©
33  */
34 public class SequenceReader
35 {
36  public static Set<Sequence<Number>> readNumericalSequences(String filename)
37  {
38  Set<Sequence<Number>> seqs = new HashSet<Sequence<Number>>();
39  InputStream is = SequenceReader.class.getResourceAsStream(filename);
40  if (is != null)
41  {
42  Scanner scanner = new Scanner(is);
43  while (scanner.hasNextLine())
44  {
45  String line = scanner.nextLine().trim();
46  if (line.isEmpty() || line.startsWith("#"))
47  {
48  continue;
49  }
50  Sequence<Number> seq = readNumericalSequence(line);
51  if (seq != null)
52  {
53  seqs.add(seq);
54  }
55  }
56  scanner.close();
57  }
58  return seqs;
59  }
60 
61  public static Set<Sequence<String>> readStringSequences(String filename)
62  {
63  Set<Sequence<String>> seqs = new HashSet<Sequence<String>>();
64  InputStream is = SequenceReader.class.getResourceAsStream(filename);
65  if (is != null)
66  {
67  Scanner scanner = new Scanner(is);
68  while (scanner.hasNextLine())
69  {
70  String line = scanner.nextLine().trim();
71  if (line.isEmpty() || line.startsWith("#"))
72  {
73  continue;
74  }
75  Sequence<String> seq = readStringSequence(line);
76  if (seq != null)
77  {
78  seqs.add(seq);
79  }
80  }
81  scanner.close();
82  }
83  return seqs;
84  }
85 
86  public static Sequence<Number> readNumericalSequence(String line)
87  {
88  String[] parts = line.split(",");
89  Sequence<Number> seq = new Sequence<Number>();
90  for (String p : parts)
91  {
92  Float f = Float.parseFloat(p.trim());
93  seq.add(f);
94  }
95  return seq;
96  }
97 
98  public static Sequence<String> readStringSequence(String line)
99  {
100  String[] parts = line.split(",");
101  Sequence<String> seq = new Sequence<String>();
102  for (String p : parts)
103  {
104  seq.add(p);
105  }
106  return seq;
107  }
108 }
Utility class that creates a set of sequences from a file.