Repository navigation
Expand file tree
/
Copy pathBasicEventReader.cs
More file actions
125 lines (114 loc) · 3.96 KB
/
Copy pathBasicEventReader.cs
File metadata and controls
125 lines (114 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//Copyright (C) 2005 Richard J. Northedge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//This file is based on the BasicEventStream.java source file found in the
//original java implementation of MaxEnt. That source file contains the following header:
// Copyright (C) 2001 Jason Baldridge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
namespace BotSharp.Models
{
/// <summary>
/// An object which can deliver a stream of training events assuming
/// that each event is represented as a space separated list containing
/// all the contextual predicates, with the last item being the
/// outcome, e.g.:
///
/// <p> cp_1 cp_2 ... cp_n outcome</p>
/// </summary>
public class BasicEventReader : ITrainingEventReader
{
private IContextGenerator<string> mContext;
private ITrainingDataReader<string> mDataReader;
private TrainingEvent mNextEvent;
/// <summary>
/// Constructor sets up the training event reader based on a stream of training data.
/// </summary>
/// <param name="dataReader">
/// Stream of training data.
/// </param>
public BasicEventReader(ITrainingDataReader<string> dataReader)
{
mContext = new BasicContextGenerator();
mDataReader = dataReader;
if (mDataReader.HasNext())
{
mNextEvent = CreateEvent(mDataReader.NextToken());
}
}
/// <summary>
/// Returns the next Event object held in this EventReader. Each call to ReadNextEvent advances the EventReader.
/// </summary>
/// <returns>
/// the Event object which is next in this EventReader
/// </returns>
public virtual TrainingEvent ReadNextEvent()
{
while (mNextEvent == null && mDataReader.HasNext())
{
mNextEvent = CreateEvent(mDataReader.NextToken());
}
TrainingEvent currentEvent = mNextEvent;
if (mDataReader.HasNext())
{
mNextEvent = CreateEvent(mDataReader.NextToken());
}
else
{
mNextEvent = null;
}
return currentEvent;
}
/// <summary>
/// Test whether there are any Events remaining in this EventReader.
/// </summary>
/// <returns>
/// true if this EventReader has more Events
/// </returns>
public virtual bool HasNext()
{
while (mNextEvent == null && mDataReader.HasNext())
{
mNextEvent = CreateEvent(mDataReader.NextToken());
}
return mNextEvent != null;
}
private TrainingEvent CreateEvent(string observation)
{
int lastSpace = observation.LastIndexOf((char)' ');
if (lastSpace == -1)
{
return null;
}
else
{
return new TrainingEvent(observation.Substring(lastSpace + 1), mContext.GetContext(observation.Substring(0, (lastSpace) - (0))));
}
}
}
}