예제 #1
0
        public static void ParseRoute(RecongnizeResult recResult, ref NLSMSResult_Route routeResult)
        {
            if (recResult == null || recResult.responseItems == null || recResult.responseItems.GetLength(0) < 1)
            {
                //没有识别结果
                routeResult.ResultCode = NLSMSResultCode.RouteResult_Items_IsNull;
                routeResult.ResultTime = DateTime.Now;
                routeResult.RecType    = NLRecType.RecFail;
                routeResult.ResultDesc = "路由结果项错误-为空";
                routeResult.IsSuccess  = false;
                return;
            }

            #region begin

            //二次解析识别结果
            List <int> bizOrBizCodeIndex = new List <int>();
            for (int index = 0; index < recResult.responseItems.GetLength(0); index++)
            {
                if (recResult.responseItems[index, 0].ToLower().Equals("biz") || recResult.responseItems[index, 0].ToLower().Equals("bizcode"))
                {
                    bizOrBizCodeIndex.Add(index);
                }
            }

            //判断是否有识别结果
            if (bizOrBizCodeIndex.Count == 0)
            {
                //没有识别结果
                routeResult.ResultCode = NLSMSResultCode.RouteResult_Items_IsNull;
                routeResult.ResultTime = DateTime.Now;
                routeResult.RecType    = NLRecType.RecFail;
                routeResult.ResultDesc = "路由结果项错误-为空";
                routeResult.IsSuccess  = false;
                return;
            }
            else
            {
                //设置全局参数
                routeResult.ResultCode = NLSMSResultCode.RoutingEngine_OK;
                routeResult.ResultTime = DateTime.Now;
                routeResult.ResultDesc = "识别成功";
                routeResult.IsSuccess  = true;
            }

            //判断是否是多个识别结果
            if (bizOrBizCodeIndex.Count > 1)
            {
                routeResult.IsMultiRouteResult = true;
            }

            //获取每个识别结果
            routeResult.RouteResultItems = new NLSMSResult_Route.RouteResultItem[bizOrBizCodeIndex.Count];
            for (int index = 0; index < bizOrBizCodeIndex.Count; index++)
            {
                if (index == bizOrBizCodeIndex.Count - 1)
                {
                    //最后一项
                    List <ParamItem> dicParams = new List <ParamItem>();
                    routeResult.RouteResultItems[index] = new NLSMSResult_Route.RouteResultItem();
                    for (int begin = bizOrBizCodeIndex[index]; begin < recResult.responseItems.GetLength(0); begin++)
                    {
                        switch (recResult.responseItems[begin, 0].ToLower())
                        {
                        case "biz":
                        case "bizcode":
                            routeResult.RouteResultItems[index].BizName = recResult.responseItems[begin, 1];
                            break;

                        case "opera":
                            routeResult.RouteResultItems[index].OperationName = recResult.responseItems[begin, 1];
                            break;

                        case "type":
                            //type暂时未使用
                            break;

                        default:
                            ParamItem item = new ParamItem();
                            item.ParamName  = recResult.responseItems[begin, 0];
                            item.ParamValue = recResult.responseItems[begin, 1];
                            dicParams.Add(item);

                            break;
                        }
                    }
                    routeResult.RouteResultItems[index].ParamItems = new NLSMSResult_Route.RouteResultItem.ParamItem[dicParams.Count];
                    int paramIndex = 0;
                    foreach (ParamItem paramItem in dicParams)
                    {
                        routeResult.RouteResultItems[index].ParamItems[paramIndex]            = new NLSMSResult_Route.RouteResultItem.ParamItem();
                        routeResult.RouteResultItems[index].ParamItems[paramIndex].ParamName  = paramItem.ParamName;
                        routeResult.RouteResultItems[index].ParamItems[paramIndex].ParamValue = paramItem.ParamValue;
                        paramIndex++;
                    }
                    //设置参数的个数和得分
                    routeResult.RouteResultItems[index].Score     = 1.0;
                    routeResult.RouteResultItems[index].PramCount = dicParams.Count;
                    //这一句设置无效,主要因为只有bizcode,没有opera的情况下,该项无效
                    //routeResult.RouteResultItems[index].IsValid = true;
                }
                else
                {
                    //参数的个数
                    List <ParamItem> dicParams = new List <ParamItem>();
                    //初始化
                    routeResult.RouteResultItems[index] = new NLSMSResult_Route.RouteResultItem();

                    for (int begin = bizOrBizCodeIndex[index]; begin < bizOrBizCodeIndex[index + 1]; begin++)
                    {
                        switch (recResult.responseItems[begin, 0].ToLower())
                        {
                        case "biz":
                        case "bizcode":
                            routeResult.RouteResultItems[index].BizName = recResult.responseItems[begin, 1];
                            break;

                        case "opera":
                            routeResult.RouteResultItems[index].OperationName = recResult.responseItems[begin, 1];
                            break;

                        case "type":
                            //type暂时未使用
                            break;

                        default:
                            ParamItem item = new ParamItem();
                            item.ParamName  = recResult.responseItems[begin, 0];
                            item.ParamValue = recResult.responseItems[begin, 1];
                            dicParams.Add(item);
                            break;
                        }
                    }
                    routeResult.RouteResultItems[index].ParamItems = new NLSMSResult_Route.RouteResultItem.ParamItem[dicParams.Count];
                    int paramIndex = 0;
                    foreach (ParamItem paramItem in dicParams)
                    {
                        routeResult.RouteResultItems[index].ParamItems[paramIndex]            = new NLSMSResult_Route.RouteResultItem.ParamItem();
                        routeResult.RouteResultItems[index].ParamItems[paramIndex].ParamName  = paramItem.ParamName;
                        routeResult.RouteResultItems[index].ParamItems[paramIndex].ParamValue = paramItem.ParamValue;
                        paramIndex++;
                    }
                    //设置参数的个数和得分
                    routeResult.RouteResultItems[index].Score     = 1.0;
                    routeResult.RouteResultItems[index].PramCount = dicParams.Count;
                }
            }

            #endregion
        }
예제 #2
0
        /// <summary>
        /// 发送请求gettrs
        /// </summary>
        /// <param name="routeResult"></param>
        private static void Route_Engine(ref NLSMSResult_Route routeResult, string usercontent, TRSConfig config)
        {
            routeResult = new NLSMSResult_Route();
            IPEndPoint endPoint = null;
            Socket     socket   = null;

            try
            {
                //Encoding encode = Encoding.Default;
                //默认是gb2312编码的
                endPoint = new IPEndPoint(IPAddress.Parse(config.TRSIP), config.TRSPort);
                //构建socket,并连接服务器端
                socket                = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.SendTimeout    = config.SendTimeout;
                socket.ReceiveTimeout = config.ReceiveTimeout;
                socket.Connect(endPoint);

                //构建要发送的消息
                StringBuilder buff = new StringBuilder();
                //构建request-line
                buff.Append("POST ").Append("/trs_recognize").Append(" HTTP/1.0\r\n");

                //构建请求头

                Byte[] bodyBytes = new byte[0];
                //检查是否需要传递包体

                buff.Append(string.Format("content-length: {0}\r\n", 0));
                buff.Append(string.Format("usrname: {0}\r\n", config.TRSUserName));
                buff.Append(string.Format("password: {0}\r\n", config.TRSPassword));
                buff.Append(string.Format("usercontent: {0}\r\n", usercontent));

                //blank-line
                buff.Append("\r\n");

                //没有request-body
                //这里的编码方式取决于服务器端的编码方式//Encoding.GetEncoding("gb2312") 即 Encoding.Default
                Byte[] sendBytes = Encoding.Default.GetBytes(buff.ToString());
                socket.Send(sendBytes);
                //存放服务器端发回的字符
                byte[] recvBytes = new byte[1024];
                //实际接收到的字符数
                int ibytes;
                //接收到的字符串
                string recvStr = string.Empty;
                do
                {
                    ibytes = socket.Receive(recvBytes, recvBytes.Length, SocketFlags.None);
                    //这里的编码方式取决于服务器端的编码方式//Encoding.GetEncoding("gb2312")
                    recvStr += Encoding.Default.GetString(recvBytes, 0, ibytes);
                }while (ibytes != 0);
                //解析发回的字符串
                RecongnizeResult result = RecongnizeResult.ParseResponse(recvStr);
                routeResult.RecType = RecongnizeResult.GetRecType(result.HeadProcesstype);
                RecongnizeResult.ParseRoute(result, ref routeResult);
            }
            catch (Exception ex)
            {
                throw new Exception("调用trs引擎异常", ex);
            }
            finally
            {
                if (socket != null)
                {
                    //关闭连接
                    socket.Close();
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 将识别结果按照原样解析
        /// </summary>
        /// <param name="source">待解析的字符串</param>
        /// <returns>解析后的对象</returns>
        public static RecongnizeResult ParseResponse(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(null);
            }
            RecongnizeResult recognizeResult = new RecongnizeResult();

            string[] splitStr    = new string[] { "\r\n" };
            string[] targetArray = source.Split(splitStr, StringSplitOptions.RemoveEmptyEntries);
            if (targetArray != null && targetArray.Length > 0)
            {
                recognizeResult.requestLine = targetArray[0];

                #region 解析包头
                foreach (string str in targetArray)
                {
                    string[] strTarget = str.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    if (strTarget.Length == 2)
                    {
                        switch (strTarget[0].ToLower().Trim())
                        {
                        case "response":
                            recognizeResult.headResponse = strTarget[1].Trim();
                            break;

                        case "content-length":
                            recognizeResult.headContentlength = strTarget[1].Trim();
                            break;

                        case "retcode":
                            recognizeResult.headRetcode = strTarget[1].Trim();
                            break;

                        case "sessionid":
                            recognizeResult.headSessionid = strTarget[1].Trim();
                            break;

                        case "processtype":
                            recognizeResult.headProcesstype = strTarget[1].Trim();
                            break;
                        }
                    }
                }
                #endregion

                #region 解析包体

                if (!string.IsNullOrEmpty(recognizeResult.headRetcode) && recognizeResult.headRetcode.Equals("0"))
                {
                    recognizeResult.responseBody = targetArray[targetArray.Length - 1];
                    //解析包体
                    string[] strTarget = targetArray[targetArray.Length - 1].Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                    if (strTarget != null && strTarget.Length % 2 == 0)
                    {
                        recognizeResult.responseItems = new string[(strTarget.Length / 2), 2];
                        for (int index = 0; index < strTarget.Length; index += 2)
                        {
                            recognizeResult.responseItems[index / 2, 0] = strTarget[index].Trim();
                            recognizeResult.responseItems[index / 2, 1] = strTarget[index + 1].Trim();
                        }
                    }
                }

                #endregion
            }

            return(recognizeResult);
        }