//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 { /// /// 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.: /// ///

cp_1 cp_2 ... cp_n outcome

///
public class BasicEventReader : ITrainingEventReader { private IContextGenerator mContext; private ITrainingDataReader mDataReader; private TrainingEvent mNextEvent; /// /// Constructor sets up the training event reader based on a stream of training data. /// /// /// Stream of training data. /// public BasicEventReader(ITrainingDataReader dataReader) { mContext = new BasicContextGenerator(); mDataReader = dataReader; if (mDataReader.HasNext()) { mNextEvent = CreateEvent(mDataReader.NextToken()); } } /// /// Returns the next Event object held in this EventReader. Each call to ReadNextEvent advances the EventReader. /// /// /// the Event object which is next in this EventReader /// 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; } /// /// Test whether there are any Events remaining in this EventReader. /// /// /// true if this EventReader has more Events /// 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)))); } } } }