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

namespace JiebaNet.Analyser
{
    public class IdfLoader
    {
        internal string IdfFilePath { get; set; }
        internal IDictionary<string, double> IdfFreq { get; set; }
        internal double MedianIdf { get; set; }

        public IdfLoader(string idfPath = null)
        {
            IdfFilePath = string.Empty;
            IdfFreq = new Dictionary<string, double>();
            MedianIdf = 0.0;
            SetNewPath(idfPath);
            //if (!string.IsNullOrWhiteSpace(idfPath))
            //{
            //    SetNewPath(idfPath);
            //}
        }

        public void SetNewPath(string newIdfPath)
        {
            using (var sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Jieba.Net.Segmenter.Resources.idf.txt"), Encoding.UTF8))
            {
                IdfFreq = new Dictionary<string, double>();
                string line = null;
                while ((line = sr.ReadLine()) != null)
                {
                    var parts = line.Trim().Split(' ');
                    var word = parts[0];
                    var freq = double.Parse(parts[1]);
                    IdfFreq[word] = freq;
                }
                MedianIdf = IdfFreq.Values.OrderBy(v => v).ToList()[IdfFreq.Count / 2];
            }
            //var idfPath = Path.GetFullPath(newIdfPath);
            //if (IdfFilePath != idfPath)
            //{
            //    IdfFilePath = idfPath;
            //    var lines = File.ReadAllLines(idfPath, Encoding.UTF8);
            //    IdfFreq = new Dictionary<string, double>();
            //    foreach (var line in lines)
            //    {
            //        var parts = line.Trim().Split(' ');
            //        var word = parts[0];
            //        var freq = double.Parse(parts[1]);
            //        IdfFreq[word] = freq;
            //    }

            //    MedianIdf = IdfFreq.Values.OrderBy(v => v).ToList()[IdfFreq.Count / 2];
            //}
        }
    }
}