using System.Collections.Generic;
using System.IO;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.PanGu;
using PanGu;
using Lucene.Net.Analysis.Tokenattributes;

namespace LuceneNet.Filters
{
    public class SplitContent
    {

        /// 需要添加PanGu.dll的引用
        /// <summary>
        /// 盘古分词
        /// </summary>
        /// <param name="content"> 输入内容字 </param>
        /// <returns> 分词结果 </returns>
        public static string[] SplitWords(string content)
        {
            List<string> strList = new List<string>();
            StringReader reader = new StringReader(content);
            Analyzer analyzer = new PanGuAnalyzer();//指定使用盘古 PanGuAnalyzer 分词算法
            TokenStream tokenStream = analyzer.TokenStream("", reader);
            Token token = null;
            while ((token = tokenStream.Next()) != null)
            { //Next继续分词 直至返回null
                strList.Add(token.TermText()); //得到分词后结果
            }
            return strList.ToArray();
        }

        /// 需要添加PanGu.HighLight.dll的引用
        /// <summary>
        /// 搜索结果高亮显示
        /// </summary>
        /// <param name="keyword"> 关键字 </param>
        /// <param name="content"> 搜索结果 </param>
        /// <param name="wordlength"> 摘要段的字符数 </param>
        /// <returns> 高亮后结果 </returns>
        public static string HightLight(string keyword, string content,int wordlength = 80)
        {
            //创建HTMLFormatter,参数为高亮单词的前后缀
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
                new PanGu.HighLight.SimpleHTMLFormatter("<font style=\"font-style:normal;color:#cc0000;\"><b>", "</b></font>");
            //创建 Highlighter ，输入HTMLFormatter 和 盘古分词对象Semgent
            PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
            //设置每个摘要段的字符数
            highlighter.FragmentSize = wordlength;
            //获取最匹配的摘要段
            return highlighter.GetBestFragment(keyword, content);
        }
    }
}