using System; using System.Diagnostics; namespace BotSharp.Algorithm.Bayesian { /// /// Struct ConditionalProbability /// [DebuggerDisplay("P({Class}|{Token})={Probability}")] public struct ConditionalProbability : IEquatable { /// /// The class /// public readonly IClass Class; /// /// The token /// public readonly IToken Token; /// /// The conditional probability /// public readonly double Probability; /// /// The occurrence of the token during the training phase. /// public readonly long Occurrence; /// /// Initializes a new instance of the struct. /// /// The class. /// The token. /// The probability. /// The occurrence. /// @class /// or /// token /// probability;Probability must greater than or equal to zero /// or /// probability;Probability must less than or equal to one public ConditionalProbability(IClass @class, IToken token, double probability, long occurrence) { if (ReferenceEquals(@class, null)) throw new ArgumentNullException("class"); if (ReferenceEquals(token, null)) throw new ArgumentNullException("token"); if (probability < 0) throw new ArgumentOutOfRangeException("probability", probability, "Probability must greater than or equal to zero"); if (probability > 1) throw new ArgumentOutOfRangeException("probability", probability, "Probability must less than or equal to one"); if (probability < 0) throw new ArgumentOutOfRangeException("occurrence", occurrence, "Occurrence must greater than or equal to zero"); Class = @class; Token = token; Probability = probability; Occurrence = occurrence; } /// /// Determines whether the specified is equal to this instance. /// /// Another object to compare to. /// if the specified is equal to this instance; otherwise, . public override bool Equals(object obj) { if (ReferenceEquals(obj, null)) return false; return obj is ConditionalProbability && Equals((ConditionalProbability) obj); } /// /// Indicates whether the current object is equal to another object of the same type. /// /// An object to compare with this object. /// true if the current object is equal to the parameter; otherwise, false. public bool Equals(ConditionalProbability other) { return Class.Equals(other.Class) && Token.Equals(other.Token) && Probability.Equals(other.Probability); } /// /// Returns a hash code for this instance. /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() { var hash = 27; hash = (13 * hash) + Class.GetHashCode(); hash = (13 * hash) + Token.GetHashCode(); hash = (13 * hash) + Probability.GetHashCode(); return hash; } /// /// Returns a that represents this instance. /// /// A that represents this instance. public override string ToString() { return String.Format("P({0}|{1})={2:P}", Class, Token, Probability); } } }