ClassWordX.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:5k
源码类别:

搜索引擎

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Collections;
  7. using System.IO;
  8. /*
  9.       '       迅龙中文分类搜索引擎  v0.6
  10.       '
  11.       '        LGPL  许可发行
  12.       '
  13.       '       宁夏大学  张冬 康彩  zd4004@163.com
  14.       ' 
  15.       '        官网 http://blog.163.com/zd4004/
  16.  */
  17. namespace XunLong.NewXWord
  18. {
  19.     public  class ClassWordX
  20.     { 
  21.         
  22.         ClassTcpClient newST = new ClassTcpClient();
  23.         /// <summary>
  24.         /// 编码 
  25.         /// </summary>
  26.         NewNxuEncoding.CNewNxuEncoding mCode = new NewNxuEncoding.CNewNxuEncoding();
  27.         //初始化分词缓存
  28.         public ClassWordX()
  29.         {
  30.             newST.initOKxWord(XunLong.CongifData.Config.xWordCacheData);
  31.             //打开分词缓存
  32.             newST.putXwordCacheFileData_Open(XunLong.CongifData.Config.xWordCacheData, "");
  33.         }
  34.        ~ClassWordX()
  35.         {
  36.             
  37.             //关闭分词缓存
  38.             newST.putXwordCacheFileData_Close(XunLong.CongifData.Config.xWordCacheData, "");
  39.         }
  40.         /// <summary>
  41.         /// 建立全连接
  42.         /// </summary>
  43.         public void ConnALL()
  44.         {
  45.             int xWordServer_Port = XunLong.CongifData.Config.xWordServer_Port;
  46.             Console.WriteLine("与分词服务连接工作在 "+ xWordServer_Port +"  开始的10个端口 ");
  47.             while (true)
  48.             {
  49.                 System.Threading.Thread.Sleep(2000);
  50.                 //如果已经连接 则不再进行
  51.                 Console.WriteLine(" = = = = = = = = = =");
  52.                 Hashtable newUU = (Hashtable)newST.x.Clone();
  53.                 ArrayList iuu = new ArrayList();
  54.                 iuu.Clear();
  55.                 foreach (System.Collections.DictionaryEntry de in newUU)
  56.                 {
  57.                     try
  58.                     {
  59.                         Socket OneTT = (Socket)de.Key;
  60.                         string a = OneTT.RemoteEndPoint.ToString();
  61.                         string[] ss = a.Split(':');
  62.                         iuu.Add(Int32.Parse(ss[1]));
  63.                         Console.WriteLine("-目前连接-> " + a);
  64.                     }
  65.                     catch
  66.                     { }
  67.                 }
  68.                 for (int i = 0; i < 10; i++)
  69.                 {
  70.                     if (iuu.Contains(xWordServer_Port + i) == false)
  71.                     {
  72.                         newST.Conn(XunLong.CongifData.Config.xWordServer, xWordServer_Port + i);
  73.                     }
  74.                 }
  75.                 System.Threading.Thread.Sleep(10000);
  76.             }
  77.         }
  78.         /// <summary>
  79.         /// 得到1个分词结果
  80.         /// </summary>
  81.         /// <param name="data"></param>
  82.         /// <returns></returns>
  83.         public string GetOne(string data)
  84.         {
  85.             string f = newST.GetOneEnd(data);
  86.             if (f == null)
  87.             { }
  88.             else
  89.             {
  90.                 return f;
  91.             }
  92.            bool X =  newST.SendOneData(data);
  93.            if (X == false)
  94.            {
  95.                return null;
  96.            }
  97.             int i = 0;
  98.             System.Threading.Thread.Sleep(50);
  99.             while (i< 20)
  100.             { 
  101.                  f =newST.GetOneEnd(data);
  102.                 if (f == null)
  103.                 {
  104.                     System.Threading.Thread.Sleep(50);
  105.                     i = i + 1;
  106.                 }
  107.                 else
  108.                 {
  109.                     // 写入分词缓存 
  110.                    newST.putXwordCacheFileData_Write(XunLong.CongifData.Config.xWordCacheData,mCode.CN2CODE( data) + "t" + mCode.CN2CODE(f));
  111.                    return f;
  112.                 }
  113.             }
  114.             //把无法得到结论的数据写入记录
  115.             putmOLDMODELSOURCEFileData("ERR_XL.LOG",data+"rn==============================================================rn");
  116.             return null;
  117.         
  118.         }
  119.         /// <summary>
  120.         /// 写入数据
  121.         /// </summary>
  122.         /// <param name="okPath"></param>
  123.         /// <param name="data"></param>
  124.         public void putmOLDMODELSOURCEFileData(string okPath, string data)
  125.         {
  126.             Console.WriteLine("ERR_XL+++++++>  " +  data.Length);
  127.             StreamWriter writer = null;
  128.             try
  129.             {
  130.                 writer = new StreamWriter(okPath, true, System.Text.Encoding.GetEncoding("gb2312"));
  131.                 //  writer.Write(data);
  132.                 writer.WriteLine(data);
  133.                 writer.Close();
  134.             }
  135.             catch (IOException e)
  136.             {
  137.                 Console.WriteLine(e.Message);
  138.             }
  139.             finally
  140.             {
  141.                 if (writer != null)
  142.                     writer.Close();
  143.             }
  144.         }
  145.     }
  146. }