예제 #1
0
        /// <summary>
        /// Reads events from <tt>eventReader</tt> into a List&lt;TrainingEvent&gt;.  The
        /// predicates associated with each event are counted and any which
        /// occur at least <tt>cutoff</tt> times are added to the
        /// <tt>predicatesInOut</tt> dictionary along with a unique integer index.
        /// </summary>
        /// <param name="eventReader">
        /// an <code>ITrainingEventReader</code> value
        /// </param>
        /// <param name="predicatesInOut">
        /// a <code>Dictionary</code> value
        /// </param>
        /// <param name="cutoff">
        /// an <code>int</code> value
        /// </param>
        /// <returns>
        /// an <code>List of TrainingEvents</code> value
        /// </returns>
        private List <TrainingEvent> ComputeEventCounts(ITrainingEventReader eventReader, Dictionary <string, int> predicatesInOut, int cutoff)
        {
            var counter        = new Dictionary <string, int>();
            var events         = new List <TrainingEvent>();
            int predicateIndex = 0;

            while (eventReader.HasNext())
            {
                TrainingEvent trainingEvent = eventReader.ReadNextEvent();
                events.Add(trainingEvent);
                string[] eventContext = trainingEvent.Context;
                for (int currentEventContext = 0; currentEventContext < eventContext.Length; currentEventContext++)
                {
                    if (!predicatesInOut.ContainsKey(eventContext[currentEventContext]))
                    {
                        if (counter.ContainsKey(eventContext[currentEventContext]))
                        {
                            counter[eventContext[currentEventContext]]++;
                        }
                        else
                        {
                            counter.Add(eventContext[currentEventContext], 1);
                        }
                        if (counter[eventContext[currentEventContext]] >= cutoff)
                        {
                            predicatesInOut.Add(eventContext[currentEventContext], predicateIndex++);
                            counter.Remove(eventContext[currentEventContext]);
                        }
                    }
                }
            }
            return(events);
        }
예제 #2
0
        private List <ComparableEvent> Index(int eventCount, ITrainingEventReader eventReader, Dictionary <string, int> predicateIndex)
        {
            var outcomeMap      = new Dictionary <string, int>();
            int outcomeCount    = 0;
            var eventsToCompare = new List <ComparableEvent>(eventCount);
            var indexedContext  = new List <int>();

            while (eventReader.HasNext())
            {
                TrainingEvent   currentTrainingEvent = eventReader.ReadNextEvent();
                string[]        eventContext         = currentTrainingEvent.Context;
                ComparableEvent comparableEvent;

                int    outcomeId;
                string outcome = currentTrainingEvent.Outcome;

                if (outcomeMap.ContainsKey(outcome))
                {
                    outcomeId = outcomeMap[outcome];
                }
                else
                {
                    outcomeId = outcomeCount++;
                    outcomeMap.Add(outcome, outcomeId);
                }

                for (int currentPredicate = 0; currentPredicate < eventContext.Length; currentPredicate++)
                {
                    string predicate = eventContext[currentPredicate];
                    if (predicateIndex.ContainsKey(predicate))
                    {
                        indexedContext.Add(predicateIndex[predicate]);
                    }
                }

                // drop events with no active features
                if (indexedContext.Count > 0)
                {
                    comparableEvent = new ComparableEvent(outcomeId, indexedContext.ToArray());
                    eventsToCompare.Add(comparableEvent);
                }
                else
                {
                    //"Dropped event " + currentTrainingEvent.Outcome + ":" + currentTrainingEvent.Context);
                }
                // recycle the list
                indexedContext.Clear();
            }
            SetOutcomeLabels(ToIndexedStringArray(outcomeMap));
            SetPredicateLabels(ToIndexedStringArray(predicateIndex));
            return(eventsToCompare);
        }
예제 #3
0
        /// <summary>
        /// Reads events from <tt>eventStream</tt> into a dictionary.  The
        /// predicates associated with each event are counted and any which
        /// occur at least <tt>cutoff</tt> times are added to the
        /// <tt>predicatesInOut</tt> map along with a unique integer index.
        /// </summary>
        /// <param name="eventReader">
        /// an <code>ITrainingEventReader</code> value
        /// </param>
        /// <param name="eventStoreFile">
        /// a file name to which the events are written to for later processing.
        /// </param>
        /// <param name="predicatesInOut">
        /// a <code>Dictionary</code> value
        /// </param>
        /// <param name="cutoff">
        /// an <code>int</code> value
        /// </param>
        private int ComputeEventCounts(ITrainingEventReader eventReader, string eventStoreFile, Dictionary <string, int> predicatesInOut, int cutoff)
        {
            var counter        = new Dictionary <string, int>();
            int predicateIndex = 0;
            int eventCount     = 0;

#if DNF
            using (var eventStoreWriter = new StreamWriter(eventStoreFile))
#else
            using (var stream = new FileStream(eventStoreFile, FileMode.Open))
                using (var eventStoreWriter = new StreamWriter(stream))
#endif
            {
                while (eventReader.HasNext())
                {
                    TrainingEvent currentTrainingEvent = eventReader.ReadNextEvent();
                    eventCount++;
                    eventStoreWriter.Write(FileEventReader.ToLine(currentTrainingEvent));
                    string[] eventContext = currentTrainingEvent.Context;
                    for (int currentPredicate = 0; currentPredicate < eventContext.Length; currentPredicate++)
                    {
                        if (!predicatesInOut.ContainsKey(eventContext[currentPredicate]))
                        {
                            if (counter.ContainsKey(eventContext[currentPredicate]))
                            {
                                counter[eventContext[currentPredicate]]++;
                            }
                            else
                            {
                                counter.Add(eventContext[currentPredicate], 1);
                            }
                            if (counter[eventContext[currentPredicate]] >= cutoff)
                            {
                                predicatesInOut.Add(eventContext[currentPredicate], predicateIndex++);
                                counter.Remove(eventContext[currentPredicate]);
                            }
                        }
                    }
                }
            }
            return(eventCount);
        }
예제 #4
0
        private List<ComparableEvent> Index(int eventCount, ITrainingEventReader eventReader, Dictionary<string, int> predicateIndex)
		{
            var outcomeMap = new Dictionary<string, int>();
			int outcomeCount = 0;
            var eventsToCompare = new List<ComparableEvent>(eventCount);
            var indexedContext = new List<int>();
			while (eventReader.HasNext())
			{
				TrainingEvent currentTrainingEvent = eventReader.ReadNextEvent();
				string[] eventContext = currentTrainingEvent.Context;
				ComparableEvent comparableEvent;
				
				int	outcomeId;
				string outcome = currentTrainingEvent.Outcome;
				
				if (outcomeMap.ContainsKey(outcome))
				{
					outcomeId = outcomeMap[outcome];
				}
				else
				{
					outcomeId = outcomeCount++;
					outcomeMap.Add(outcome, outcomeId);
				}
				
				for (int currentPredicate = 0; currentPredicate < eventContext.Length; currentPredicate++)
				{
					string predicate = eventContext[currentPredicate];
					if (predicateIndex.ContainsKey(predicate))
					{
						indexedContext.Add(predicateIndex[predicate]);
					}
				}
				
				// drop events with no active features
				if (indexedContext.Count > 0)
				{
					comparableEvent = new ComparableEvent(outcomeId, indexedContext.ToArray());
					eventsToCompare.Add(comparableEvent);
				}
				else
				{
					//"Dropped event " + currentTrainingEvent.Outcome + ":" + currentTrainingEvent.Context);
				}
				// recycle the list
				indexedContext.Clear();
			}
			SetOutcomeLabels(ToIndexedStringArray(outcomeMap));
			SetPredicateLabels(ToIndexedStringArray(predicateIndex));
			return eventsToCompare;
		}
예제 #5
0
		/// <summary>
		/// Reads events from <tt>eventStream</tt> into a dictionary.  The
		/// predicates associated with each event are counted and any which
		/// occur at least <tt>cutoff</tt> times are added to the
		/// <tt>predicatesInOut</tt> map along with a unique integer index.
		/// </summary>
		/// <param name="eventReader">
		/// an <code>ITrainingEventReader</code> value
		/// </param>
		/// <param name="eventStoreFile">
		/// a file name to which the events are written to for later processing.
		/// </param>
		/// <param name="predicatesInOut">
		/// a <code>Dictionary</code> value
		/// </param>
		/// <param name="cutoff">
		/// an <code>int</code> value
		/// </param>
        private int ComputeEventCounts(ITrainingEventReader eventReader, string eventStoreFile, Dictionary<string, int> predicatesInOut, int cutoff)
		{
            var counter = new Dictionary<string, int>();
			int predicateIndex = 0;
			int eventCount = 0;

			using (var eventStoreWriter = new StreamWriter(eventStoreFile))
			{
				while (eventReader.HasNext())
				{
					TrainingEvent currentTrainingEvent = eventReader.ReadNextEvent();
					eventCount++;
					eventStoreWriter.Write(FileEventReader.ToLine(currentTrainingEvent));
					string[] eventContext = currentTrainingEvent.Context;
					for (int currentPredicate = 0; currentPredicate < eventContext.Length; currentPredicate++)
					{
						if (!predicatesInOut.ContainsKey(eventContext[currentPredicate]))
						{
							if (counter.ContainsKey(eventContext[currentPredicate]))
							{
								counter[eventContext[currentPredicate]]++;
							}
							else
							{
								counter.Add(eventContext[currentPredicate], 1);
							}
							if (counter[eventContext[currentPredicate]] >= cutoff)
							{
								predicatesInOut.Add(eventContext[currentPredicate], predicateIndex++);
								counter.Remove(eventContext[currentPredicate]);
							}
						}
					}
				}
			}
			return eventCount;
		}
예제 #6
0
		/// <summary>
        /// Reads events from <tt>eventReader</tt> into a List&lt;TrainingEvent&gt;.  The
		/// predicates associated with each event are counted and any which
		/// occur at least <tt>cutoff</tt> times are added to the
		/// <tt>predicatesInOut</tt> dictionary along with a unique integer index.
		/// </summary>
		/// <param name="eventReader">
		/// an <code>ITrainingEventReader</code> value
		/// </param>
		/// <param name="predicatesInOut">
		/// a <code>Dictionary</code> value
		/// </param>
		/// <param name="cutoff">
		/// an <code>int</code> value
		/// </param>
		/// <returns>
        /// an <code>List of TrainingEvents</code> value
		/// </returns>
        private List<TrainingEvent> ComputeEventCounts(ITrainingEventReader eventReader, Dictionary<string, int> predicatesInOut, int cutoff)
		{
            var counter = new Dictionary<string, int>();
            var events = new List<TrainingEvent>();
			int predicateIndex = 0;
			while (eventReader.HasNext())
			{
				TrainingEvent trainingEvent = eventReader.ReadNextEvent();
				events.Add(trainingEvent);
				string[] eventContext = trainingEvent.Context;
				for (int currentEventContext = 0; currentEventContext < eventContext.Length; currentEventContext++)
				{
					if (!predicatesInOut.ContainsKey(eventContext[currentEventContext]))
					{
						if (counter.ContainsKey(eventContext[currentEventContext]))
						{
							counter[eventContext[currentEventContext]]++;
						}
						else
						{
							counter.Add(eventContext[currentEventContext], 1);
						}
						if (counter[eventContext[currentEventContext]] >= cutoff)
						{
							predicatesInOut.Add(eventContext[currentEventContext], predicateIndex++);
							counter.Remove(eventContext[currentEventContext]);
						}
					}
				}
			}
			return events;
		}