using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
//using System.IO;
using System.Text;
using System.Collections.Generic;

//声明命名空间
//using Lucene.Net;
//using Lucene.Net.Analysis;
//using Lucene.Net.Analysis.Standard;
//using Lucene.Net.Documents;
//using Lucene.Net.Index;
//using Lucene.Net.QueryParsers;
//using Lucene.Net.Search;
//using Lucene.Net.Store;
//using Lucene.Net.Util;
//using Lucene.Net.Highlight;

//using KTDictSeg;　　//分词
using System.Diagnostics;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Lucene.Net.Analysis.Analyzer objCA = new Lucene.Net.Analysis.China.ChineseAnalyzer();

        //Lucene.Net.Analysis.Standard.StandardAnalyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
        //IndexWriter iw = new IndexWriter(Server.MapPath("Index"), a, true);   //E:1index  为索引文件存放地址
        //string conn = "Server=DONET\\DONET2005;DataBase=db_SIS;Uid=sa;pwd=;";
        //using (SqlConnection con = new SqlConnection(conn))
        //{
        //    SqlDataAdapter dap = new SqlDataAdapter("select * from tb_sou", con);
        //    DataSet ds = new DataSet();
        //    dap.Fill(ds);

        //    foreach (DataRow dr in ds.Tables[0].Rows)
        //    {
        //        IndexBook(dr["title"].ToString(), dr["info"].ToString(), iw); // 这是关键
        //        //IndexBook(dr["title"].ToString(), dr["info"].ToString(), iw); // 这是关键
        //    }
        //}
        //iw.Optimize();  //优化索引
        //iw.Close();     //关闭索引
    }

    //private void IndexBook(string title, string info, IndexWriter writer)
    //{
        //try
        //{
        //    Document doc = new Document();
        //    doc.Add(Field.Keyword("title", title));
        //    doc.Add(Field.Text("info", info));

        //    doc.Add(Field.Text("title", title));

        //    writer.AddDocument(doc);
        //}
        //catch (Exception ex)
        //{
        //}
   // }

    protected void Button2_Click(object sender, EventArgs e)
    {
        //DateTime dt = DateTime.Now;//开始记时

        //Lucene.Net.Search.IndexSearcher search = new Lucene.Net.Search.IndexSearcher(Server.MapPath("Index")); //把刚才建立的索引取出来
        //Lucene.Net.Search.Query q = Lucene.Net.QueryParsers.QueryParser.Parse(txtKey.Text, "info", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
        //Lucene.Net.Search.Hits hit = search.Search(q);

        //TimeSpan ts = System.DateTime.Now.Subtract(dt); //计算用时

        //Response.Write("<font color=red>共找到" + hit.Length() + "条记录</font>.. -----" + ts.ToString() + " 毫秒  ..<br>");
        //Response.Write("<br><br><br>");

        //for (int i = 0; i <= hit.Length() - 1; i++)
        //{
        //    Response.Write("<<" + hit.Doc(i).GetField("title").StringValue() + ">> ............   内容：" + hit.Doc(i).GetField("info").StringValue() + "<br><br><br><br>");
        //}
        //search.Close();
    }

    //多字段查询
    protected void Button3_Click(object sender, EventArgs e)
    {
        //DateTime dt = DateTime.Now;//开始记时

        //IndexSearcher search = new IndexSearcher(Server.MapPath("Index")); //把刚才建立的索引取出来
        ////Query q = QueryParser.Parse(txtKey.Text, "info", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
        //Query qq = MultiFieldQueryParser.Parse(strKey, new string[] { "title", "info" }, new Lucene.Net.Analysis.Standard.StandardAnalyzer());
        //Hits hit = search.Search(qq);

        //TimeSpan ts = System.DateTime.Now.Subtract(dt); //计算用时

        //Response.Write("<font color=red>共找到" + hit.Length() + "条记录</font>.. -----" + ts.ToString() + " 毫秒  ..<br>");
        //Response.Write("<br><br><br>");

        //for (int i = 0; i <= hit.Length() - 1; i++)
        //{
        //    Response.Write("<<" + hit.Doc(i).GetField("title").StringValue() + ">> ............   内容：" + hit.Doc(i).GetField("info").StringValue() + "<br>");
        //}
        //search.Close();
    }

    //多条件查询

    DataTable Results = new DataTable();

    private void setTable()
    {
        //创建结果记录集
        //定义字段
        this.Results.Columns.Add("title", typeof(string));
        this.Results.Columns.Add("info", typeof(string));
        //this.Results.Columns.Add("className", typeof(string));
        //this.Results.Columns.Add("titleImg", typeof(string));
        //this.Results.Columns.Add("updateTime", typeof(DateTime));
        //this.Results.Columns.Add("source", typeof(string));
        //this.Results.Columns.Add("title", typeof(string));
        //this.Results.Columns.Add("summary", typeof(string));
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        //this.setTable();
        //DateTime dt = DateTime.Now;//开始记时

        //IndexSearcher search = new IndexSearcher(Server.MapPath("Index")); //把刚才建立的索引取出来
        ////Query q1 = new TermQuery(new Term("info", txtKey.Text));
        ////Query q2 = new TermQuery(new Term("info", "招聘"));
        //string[] sField = new string[strKey.Length];
        //sField.SetValue("title", 0);
        //for (int s = 1; s < strKey.Length; s++)
        //{
        //    sField.SetValue("info", s);
        //}

        //Query qq = MultiFieldQueryParser.Parse(strKey, sField, new Lucene.Net.Analysis.Standard.StandardAnalyzer());
        //BooleanQuery q = new BooleanQuery();
        //q.Add(qq, true, false);
        ////q.Add(qq, BooleanClause.Occur.MUST);

        //Hits hit = search.Search(q);

        //TimeSpan ts = System.DateTime.Now.Subtract(dt); //计算用时

        //for (int i = 0; i <= hit.Length() - 1; i++)
        //{
        //    Document doc = hit.Doc(i);
        //    DataRow row = Results.NewRow();
        //    row["title"] = doc.Get("title");
        //    row["info"] = doc.Get("info");
        //    this.Results.Rows.Add(row);
        //}
        //search.Close();
        //GridView1.DataSource = Results;
        //GridView1.DataBind();
        //Response.Write("<font color=red>共找到" + hit.Length() + "条记录</font>.. -----" + ts.ToString() + " 毫秒  ..<br>");
        //Response.Write("<br><br><br>");
    }


    //分词
   // public static CSimpleDictSeg m_SimpleDictSeg;
    //private static string[] strKey;
    protected void Button5_Click(object sender, EventArgs e)
    {
        //if (m_SimpleDictSeg == null)
        //{
        //    try
        //    {
        //        m_SimpleDictSeg = new CSimpleDictSeg();
        //        //m_SimpleDictSeg.DictPath = Path.Combine(Environment.CurrentDirectory, "Data") + Path.DirectorySeparatorChar; ;
        //        m_SimpleDictSeg.DictPath = Server.MapPath("Data/");
        //        m_SimpleDictSeg.LoadDict();
        //    }
        //    catch (Exception e1)
        //    {
        //        m_SimpleDictSeg = null;
        //        Response.Write(String.Format("Load Dict Fail! ErrMsg:{0}", e1.Message));
        //        return;
        //    }
        //}

        //m_SimpleDictSeg.FilterStopWords = false;　　//过滤停用词
        //m_SimpleDictSeg.MatchName = true; //识别中文人名

        //Stopwatch watch = new Stopwatch();
        //watch.Start();


        ////过滤字符和标点符号
        //string key = txtKey.Text.Trim();
        //key = key.Replace('，', ' ');       //将标点符号替换为空格
        //key = key.Replace('。', ' ');
        //key = key.Replace('！', ' ');
        //key = key.Replace('-', ' ');
        //key = key.Replace('－', ' ');
        //key = key.Replace('：', ' ');
        //key = key.Replace('；', ' ');
        //key = key.Replace(':', ' ');
        //key = key.Replace(';', ' ');
        //key = key.Replace('*', ' ');
        //key = key.Replace('.', ' ');
        //key = key.Replace(',', ' ');
        //key = key.Replace('‘', ' ');
        //key = key.Replace('’', ' ');
        //key = key.Replace('“', ' ');
        //key = key.Replace('”', ' ');
        //key = key.Replace('？', ' ');
        //key = key.Replace('?', ' ');
        //key = key.Replace("'", " ");
        //key = key.Replace('"', ' ');
        //key = key.Replace('(', ' ');
        //key = key.Replace(')', ' ');
        //key = key.Replace('）', ' ');
        //key = key.Replace('（', ' ');
        //key = key.Replace('_', ' ');
        //key = key.Replace('、', ' ');
        //key = key.Replace(" ", "");         //最后再替换掉空格


        //ArrayList words = m_SimpleDictSeg.Segment(key);   //输入分词

        //watch.Stop();

        ////labelSrcLength.Text = textBoxSource.Text.Length.ToString();   //源字符串长度

        ////labelSegTime.Text = watch.Elapsed.ToString();　　//分词时间
        ////if (watch.ElapsedMilliseconds == 0)
        ////{
        ////    labelRegRate.Text = "无穷大";
        ////}　
        ////else　
        ////{
        ////    labelRegRate.Text = ((double)(textBoxSource.Text.Length / watch.ElapsedMilliseconds) * 1000).ToString();
        ////}

        //StringBuilder wordsString = new StringBuilder();
        //foreach (String str in words)
        //{
        //    wordsString.AppendFormat("{0}/", str);
        //}
        ////textBoxSegwords.Text = wordsString.ToString();


        ////去掉空格
        //key = wordsString.ToString();
        //key = key.Substring(0, key.Length - 1);

        //strKey = key.Split(new Char[] { '/', ' ' });
        //Response.Write("分词结果：------" + wordsString.ToString());
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //GridView1.PageIndex = e.NewPageIndex;
        //this.Button4_Click(sender, e);
    }
}
