Exemple #1
0
        static KeyWordsHelper()
        {
            string str = ConfigurationManager.AppSettings["KeyWordsDictionary"];

            if (string.IsNullOrEmpty(str))
            {
                str = "KeyWordsDictionary.txt";
            }
            Uri    uri           = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase);
            string directoryName = Path.GetDirectoryName(uri.LocalPath);
            string path          = Path.Combine(directoryName, str);

            if (!File.Exists(path))
            {
                path = Path.Combine(Directory.GetParent(directoryName).FullName, str);
            }
            if (!File.Exists(path))
            {
                HttpContext current = HttpContext.Current;
                if (current != null)
                {
                    path = current.Server.MapPath("~/" + str);
                }
                else
                {
                    path = HostingEnvironment.MapPath("~/" + str);
                }
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }
            _filter = new FilterKeyWords(File.ReadAllLines(path));
        }
Exemple #2
0
 public KeyWordFilter(IList <string> keyWordList)
 {
     if (keyWordList == null)
     {
         throw new Exception("传入的keyWordList对象为空");
     }
     string[] keyWords = keyWordList.ToArray <string>();
     for (int i = 0; i < keyWords.Length; i++)
     {
         if (!string.IsNullOrEmpty(keyWords[i]))
         {
             keyWords[i] = keyWords[i].Trim();
         }
     }
     this._filter = new FilterKeyWords(keyWords);
 }
Exemple #3
0
 public Dictionary <int, int> Find(IList <string> keyWordList, string sourceContent)
 {
     if (keyWordList == null)
     {
         throw new Exception("传入的keyWordList对象为空");
     }
     string[] keyWords = keyWordList.ToArray <string>();
     for (int i = 0; i < keyWords.Length; i++)
     {
         if (!string.IsNullOrEmpty(keyWords[i]))
         {
             keyWords[i] = keyWords[i].Trim();
         }
     }
     this._filter = new FilterKeyWords(keyWords);
     return(this._filter.Find(sourceContent));
 }