Word.cs
上传用户:huiyue
上传日期:2022-04-08
资源大小:1429k
文件大小:4k
源码类别:

搜索引擎

开发平台:

ASP/ASPX

  1. using System;
  2. using System.IO;
  3. using System.Xml.Serialization;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using cd.net;
  10. namespace Searcharoo.Common
  11. {
  12.     /// <summary>Instance of a word</summary>
  13.     [Serializable]
  14.     public class Word
  15.     {
  16.         #region Private fields: _Text, _FileCollection
  17.         /// <summary>Collection of files the word appears in</summary>
  18.         /// <remarks>Key = File object, Value = Number of times word appears</remarks>
  19.         private System.Collections.Hashtable _FileCollection = new System.Collections.Hashtable();
  20.         private Dictionary<File, List<int>> _FilePositionCollection = new System.Collections.Generic.Dictionary<File, List<int>>();
  21.         /// <summary>The word itself</summary>
  22.         private string _Text;
  23.         #endregion
  24.         /// <summary>
  25.         /// The catalogued word
  26.         /// </summary>
  27.         [XmlElement("t")]
  28.         public string Text
  29.         {
  30.             get { return _Text; }
  31.             set { _Text = value; }
  32.         }
  33.         /// <summary>
  34.         /// Files that this Word appears in
  35.         /// </summary>
  36.         [XmlElement("i")]
  37.         public File[] Files
  38.         {
  39.             get
  40.             {
  41.                 File[] fileArray = new File[_FilePositionCollection.Count];
  42.                 //_FileCollection.Keys.CopyTo(fileArray, 0);
  43.                 _FilePositionCollection.Keys.CopyTo(fileArray, 0);
  44.                 return fileArray;
  45.             }
  46.             set
  47.             {
  48.                 File[] fileArray = value;
  49.                 Hashtable index = new Hashtable();
  50.             }
  51.         }
  52.         /// <summary>
  53.         /// Empty constructor required for serialization
  54.         /// </summary>
  55.         public Word() { }
  56.         /// <summary>Constructor with first file reference</summary>
  57.         public Word(string text, File infile, int position)
  58.         {
  59.             _Text = text;
  60.             //WordInFile thefile = new WordInFile(filename, position);
  61.             _FileCollection.Add(infile, 1);
  62.             // [v7]
  63.             List<int> l = new List<int>();
  64.             l.Add(position);
  65.             _FilePositionCollection.Add(infile, l);
  66.         }
  67.         /// <summary>Add a file referencing this word</summary>
  68.         public void Add(File infile, int position)
  69.         {
  70.             //if (_FileCollection.ContainsKey(infile))
  71.             //{
  72.             //    int wordcount = (int)_FileCollection[infile];
  73.             //    _FileCollection[infile] = wordcount + 1; //thefile.Add (position);
  74.             //}
  75.             //else
  76.             //{
  77.             //    //WordInFile thefile = new WordInFile(filename, position);
  78.             //    _FileCollection.Add(infile, 1);
  79.             //}
  80.             // [v7]
  81.             if (_FilePositionCollection.ContainsKey(infile))
  82.             {
  83.                 _FilePositionCollection[infile].Add(position);
  84.             }
  85.             else
  86.             {
  87.                 List<int> l = new List<int>();
  88.                 l.Add(position);
  89.                 _FilePositionCollection.Add(infile, l);
  90.             }
  91.         }
  92.         /// <summary>Collection of files containing this Word (Value=WordCount)</summary>
  93.         [Obsolete("Use InFilesWithPosition instead")]
  94.         public Hashtable InFiles()
  95.         {
  96.             return _FileCollection;
  97.         }
  98.         /// <summary>Collection of files containing this Word (Value=List of position numbers)</summary>
  99.         public Dictionary<File, List<int>> InFilesWithPosition()
  100.         {
  101.             return _FilePositionCollection; // [v7]
  102.         }
  103.         /// <summary>Debug string</summary>
  104.         public override string ToString()
  105.         {
  106.             string temp = "";
  107.             foreach (object tempFile in _FileCollection.Values) temp += ((File)tempFile).ToString();
  108.             return "tWORD :: " + _Text + "ntt" + temp + "n";
  109.         }
  110.     }  
  111. }