//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 ComparableEvent.java source file found in the //original java implementation of MaxEnt. That source file contains the following header: // Copyright (C) 2001 Jason Baldridge and Gann Bierner // // 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 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; using System.Text; namespace BotSharp.MachineLearning { /// /// A Maximum Entropy event representation which we can use to sort based on the /// predicates indexes contained in the events. /// /// /// Jason Baldridge /// /// /// Richard J. Northedge /// /// /// based on ComparableEvent.java, $Revision: 1.2 $, $Date: 2001/12/27 19:20:26 $ /// public class ComparableEvent : IComparable { private int mOutcome; private int[] mPredicateIndexes ; private int mSeenCount = 1; /// /// The outcome ID of this event. /// public int Outcome { get { return mOutcome; } set { mOutcome = value; } } /// /// Returns an array containing the indexes of the predicates in this event. /// /// /// Integer array of predicate indexes. /// public int[] GetPredicateIndexes() { return mPredicateIndexes; } /// /// Sets the array containing the indices of the predicates in this event. /// /// /// Integer array of predicate indexes. /// public void SetPredicateIndexes(int[] predicateIndexes) { mPredicateIndexes = predicateIndexes; } /// /// The number of times this event /// has been seen. /// public int SeenCount { get { return mSeenCount; } set { mSeenCount = value; } } /// /// Constructor for the ComparableEvent. /// /// /// The ID of the outcome for this event. /// /// /// Array of indexes for the predicates in this event. /// public ComparableEvent(int outcome, int[] predicateIndexes) { mOutcome = outcome; System.Array.Sort(predicateIndexes); mPredicateIndexes = predicateIndexes; } /// /// Implementation of the IComparable interface. /// /// /// ComparableEvent to compare this event to. /// /// /// A value indicating if the compared object is smaller, greater or the same as this event. /// public virtual int CompareTo(ComparableEvent eventToCompare) { if (mOutcome < eventToCompare.Outcome) { return - 1; } else if (mOutcome > eventToCompare.Outcome) { return 1; } int smallerLength = (mPredicateIndexes .Length > eventToCompare.GetPredicateIndexes().Length ? eventToCompare.GetPredicateIndexes().Length : GetPredicateIndexes().Length); for (int currentIndex = 0; currentIndex < smallerLength; currentIndex++) { if (mPredicateIndexes [currentIndex] < eventToCompare.GetPredicateIndexes()[currentIndex]) { return - 1; } else if (mPredicateIndexes [currentIndex] > eventToCompare.GetPredicateIndexes()[currentIndex]) { return 1; } } if (mPredicateIndexes .Length < eventToCompare.GetPredicateIndexes().Length) { return - 1; } else if (mPredicateIndexes .Length > eventToCompare.GetPredicateIndexes().Length) { return 1; } return 0; } /// /// Tests if this event is equal to another object. /// /// /// Object to test against. /// /// /// True if the objects are equal. /// public override bool Equals (object o) { if (!(o is ComparableEvent)) { return false; } return (this.CompareTo(o as ComparableEvent)== 0); } /// /// Provides a hashcode for storing events in a dictionary or hashtable. /// /// /// A hashcode value. /// public override int GetHashCode() { return this.ToString().GetHashCode(); } /// /// Override to provide a succint summary of the ComparableEvent object. /// /// /// string representation of the ComparableEvent object. /// public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); for (int currentIndex = 0; currentIndex < mPredicateIndexes.Length; currentIndex++) { stringBuilder.Append(" ").Append(mPredicateIndexes [currentIndex]); } return stringBuilder.ToString(); } } }