using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
using System.Reflection;

namespace LDA.LdaModel
{
    /// <summary>
    /// 用于移除中文停止词
    /// </summary>
    public class CnStopWordsHandler
    {
		//public static string[] CnstopWordsList = File.ReadAllLines("..\\..\\LdaModel\\Resources\\Cnstopwords.txt");

		private static readonly Hashtable _stopwords = null;

		public static object AddElement(IDictionary collection, Object key, object newValue)
		{
			object element = collection[key];
			collection[key] = newValue;
			return element;
		}

		public static bool IsStopword(string str)
		{

			//int index=Array.BinarySearch(stopWordsList, str)
			return _stopwords.ContainsKey(str.ToLower());
		}


		static CnStopWordsHandler()
		{
			if (_stopwords == null)
			{
				_stopwords = new Hashtable();
				double dummy = 0;
                using (var sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("LDA.LdaModel.Resources.Cnstopwords.txt"), Encoding.UTF8))
                {
					string line = null;
					while ((line = sr.ReadLine()) != null)
                    {
						AddElement(_stopwords, line, dummy);
					}
				}
				//foreach (string word in CnstopWordsList)
				//{
				//	AddElement(_stopwords, word, dummy);
				//}
			}
		}

	}
}
