- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Collections;
- using System.IO;
- /*
- ' 迅龙中文分类搜索引擎 v0.6
- '
- ' LGPL 许可发行
- '
- ' 宁夏大学 张冬 康彩 zd4004@163.com
- '
- ' 官网 http://blog.163.com/zd4004/
- */
- namespace XunLong.NewXWord
- {
- public class ClassWordX
- {
- ClassTcpClient newST = new ClassTcpClient();
- /// <summary>
- /// 编码
- /// </summary>
- NewNxuEncoding.CNewNxuEncoding mCode = new NewNxuEncoding.CNewNxuEncoding();
- //初始化分词缓存
- public ClassWordX()
- {
- newST.initOKxWord(XunLong.CongifData.Config.xWordCacheData);
- //打开分词缓存
- newST.putXwordCacheFileData_Open(XunLong.CongifData.Config.xWordCacheData, "");
- }
- ~ClassWordX()
- {
- //关闭分词缓存
- newST.putXwordCacheFileData_Close(XunLong.CongifData.Config.xWordCacheData, "");
- }
- /// <summary>
- /// 建立全连接
- /// </summary>
- public void ConnALL()
- {
- int xWordServer_Port = XunLong.CongifData.Config.xWordServer_Port;
- Console.WriteLine("与分词服务连接工作在 "+ xWordServer_Port +" 开始的10个端口 ");
- while (true)
- {
- System.Threading.Thread.Sleep(2000);
- //如果已经连接 则不再进行
- Console.WriteLine(" = = = = = = = = = =");
- Hashtable newUU = (Hashtable)newST.x.Clone();
- ArrayList iuu = new ArrayList();
- iuu.Clear();
- foreach (System.Collections.DictionaryEntry de in newUU)
- {
- try
- {
- Socket OneTT = (Socket)de.Key;
- string a = OneTT.RemoteEndPoint.ToString();
- string[] ss = a.Split(':');
- iuu.Add(Int32.Parse(ss[1]));
- Console.WriteLine("-目前连接-> " + a);
- }
- catch
- { }
- }
- for (int i = 0; i < 10; i++)
- {
- if (iuu.Contains(xWordServer_Port + i) == false)
- {
- newST.Conn(XunLong.CongifData.Config.xWordServer, xWordServer_Port + i);
- }
- }
- System.Threading.Thread.Sleep(10000);
- }
- }
- /// <summary>
- /// 得到1个分词结果
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public string GetOne(string data)
- {
- string f = newST.GetOneEnd(data);
- if (f == null)
- { }
- else
- {
- return f;
- }
- bool X = newST.SendOneData(data);
- if (X == false)
- {
- return null;
- }
- int i = 0;
- System.Threading.Thread.Sleep(50);
- while (i< 20)
- {
- f =newST.GetOneEnd(data);
- if (f == null)
- {
- System.Threading.Thread.Sleep(50);
- i = i + 1;
- }
- else
- {
- // 写入分词缓存
- newST.putXwordCacheFileData_Write(XunLong.CongifData.Config.xWordCacheData,mCode.CN2CODE( data) + "t" + mCode.CN2CODE(f));
- return f;
- }
- }
- //把无法得到结论的数据写入记录
- putmOLDMODELSOURCEFileData("ERR_XL.LOG",data+"rn==============================================================rn");
- return null;
- }
- /// <summary>
- /// 写入数据
- /// </summary>
- /// <param name="okPath"></param>
- /// <param name="data"></param>
- public void putmOLDMODELSOURCEFileData(string okPath, string data)
- {
- Console.WriteLine("ERR_XL+++++++> " + data.Length);
- StreamWriter writer = null;
- try
- {
- writer = new StreamWriter(okPath, true, System.Text.Encoding.GetEncoding("gb2312"));
- // writer.Write(data);
- writer.WriteLine(data);
- writer.Close();
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- finally
- {
- if (writer != null)
- writer.Close();
- }
- }
- }
- }