using System; using System.Collections.Generic; using System.Text; namespace ScrewTurn.Wiki.SearchEngine { /// /// Contains full information about a word in a document. /// public class WordInfo : BasicWordInfo, IEquatable, IComparable { /// /// The word text. /// protected string text; /// /// Initializes a new instance of the class. /// /// The text of the word. /// The index of the first character of the word in the document. /// The index of the word in the document. /// The location of the word in the document. /// If is null. /// If is empty. /// If or are less than zero. public WordInfo(string text, ushort firstCharIndex, ushort wordIndex, WordLocation location) : base(firstCharIndex, wordIndex, location) { if(text == null) throw new ArgumentNullException("text"); if(text.Length == 0) throw new ArgumentException("Invalid text", "text"); this.text = Tools.RemoveDiacriticsAndPunctuation(text, true); //if(this.text.Length == 0) throw new InvalidOperationException(); } /// /// Gets the text of the word. /// public string Text { get { return text; } } /// /// Gets a string representation of the current instance. /// /// The string representation. public override string ToString() { return string.Format("{0}:{1}", text, firstCharIndex); } /// /// Determinates whether the current instance is equal to an instance of an object. /// /// The instance of the object. /// true if the instances are value-equal, false otherwise. public override bool Equals(object other) { if(other is WordInfo) return Equals((WordInfo)other); else return false; } /// /// Determinates whether the current instance is value-equal to another. /// /// The other instance. /// true if the instances are value-equal, false otherwise. public bool Equals(WordInfo other) { if(object.ReferenceEquals(other, null)) return false; return other.FirstCharIndex == firstCharIndex && other.WordIndex == wordIndex && other.Location == location && other.text == text; } /// /// Applies the value-equality operator to two objects. /// /// The first object. /// The second object. /// true if the objects are value-equal, false otherwise. public static bool operator ==(WordInfo x, WordInfo y) { if(object.ReferenceEquals(x, null) && !object.ReferenceEquals(y, null) || !object.ReferenceEquals(x, null) && object.ReferenceEquals(y, null)) return false; if(object.ReferenceEquals(x, null) && object.ReferenceEquals(y, null)) return true; return x.Equals(y); } /// /// Applies the value-equality operator to two objects. /// /// The first object. /// The second object. /// true if the objects are not value-equal, false otherwise. public static bool operator !=(WordInfo x, WordInfo y) { return !(x == y); } /// /// Gets the hash code of the current instance. /// /// The hash code. public override int GetHashCode() { return base.GetHashCode() ^ text.GetHashCode(); } /// /// Compares the current instance with another instance. /// /// The other instance. /// The comparison result. /// The First Char Index does not partecipate to the comparison. public int CompareTo(WordInfo other) { if(other == null) return 1; // text has a distance module of 1 // location has a distance module of 2 // wordIndex has a distance module of 3 int res = location.CompareTo(other.Location) * 2; int res2 = wordIndex.CompareTo(other.WordIndex) * 3; return res + res2 + text.CompareTo(other.Text); } } }