Click here to Skip to main content
15,897,704 members
Articles / Web Development / ASP.NET

ASP.NET C# Search Engine (Highlighting, JSON, jQuery & Silverlight)

Rate me:
Please Sign up or sign in to vote.
4.60/5 (38 votes)
8 Mar 2009CPOL10 min read 379.8K   13.2K   184  
More professional ASP.NET C# search with proper document summary, query highlighting and RIA display options
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using cd.net;

namespace Searcharoo.Common
{
    /// <summary>Instance of a word</summary>
    [Serializable]
    public class Word
    {
        #region Private fields: _Text, _FileCollection
        /// <summary>Collection of files the word appears in</summary>
        /// <remarks>Key = File object, Value = Number of times word appears</remarks>
        private System.Collections.Hashtable _FileCollection = new System.Collections.Hashtable();

        private Dictionary<File, List<int>> _FilePositionCollection = new System.Collections.Generic.Dictionary<File, List<int>>();
        /// <summary>The word itself</summary>
        private string _Text;
        #endregion

        /// <summary>
        /// The catalogued word
        /// </summary>
        [XmlElement("t")]
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }
        /// <summary>
        /// Files that this Word appears in
        /// </summary>
        [XmlElement("i")]
        public File[] Files
        {
            get
            {
                File[] fileArray = new File[_FilePositionCollection.Count];
                //_FileCollection.Keys.CopyTo(fileArray, 0);
                _FilePositionCollection.Keys.CopyTo(fileArray, 0);
                return fileArray;
            }
            set
            {
                File[] fileArray = value;
                Hashtable index = new Hashtable();
            }
        }

        /// <summary>
        /// Empty constructor required for serialization
        /// </summary>
        public Word() { }

        /// <summary>Constructor with first file reference</summary>
        public Word(string text, File infile, int position)
        {
            _Text = text;
            //WordInFile thefile = new WordInFile(filename, position);
            _FileCollection.Add(infile, 1);

            // [v7]
            List<int> l = new List<int>();
            l.Add(position);
            _FilePositionCollection.Add(infile, l);
        }

        /// <summary>Add a file referencing this word</summary>
        public void Add(File infile, int position)
        {
            //if (_FileCollection.ContainsKey(infile))
            //{
            //    int wordcount = (int)_FileCollection[infile];
            //    _FileCollection[infile] = wordcount + 1; //thefile.Add (position);
            //}
            //else
            //{
            //    //WordInFile thefile = new WordInFile(filename, position);
            //    _FileCollection.Add(infile, 1);
            //}

            // [v7]
            if (_FilePositionCollection.ContainsKey(infile))
            {
                _FilePositionCollection[infile].Add(position);
            }
            else
            {
                List<int> l = new List<int>();
                l.Add(position);
                _FilePositionCollection.Add(infile, l);
            }
        }

        /// <summary>Collection of files containing this Word (Value=WordCount)</summary>
        [Obsolete("Use InFilesWithPosition instead")]
        public Hashtable InFiles()
        {
            return _FileCollection;
        }

        /// <summary>Collection of files containing this Word (Value=List of position numbers)</summary>
        public Dictionary<File, List<int>> InFilesWithPosition()
        {
            return _FilePositionCollection; // [v7]
        }

        /// <summary>Debug string</summary>
        public override string ToString()
        {
            string temp = "";
            foreach (object tempFile in _FileCollection.Values) temp += ((File)tempFile).ToString();
            return "\tWORD :: " + _Text + "\n\t\t" + temp + "\n";
        }
    }  
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
-- ooo ---
www.conceptdevelopment.net
conceptdev.blogspot.com
www.searcharoo.net
www.recipenow.net
www.racereplay.net
www.silverlightearth.com

Comments and Discussions