• AI(四): 微信与luis结合(下)


        LUIS(Language Understanding Intelligent Services)是微软新近推出了的的语义理解服务,可以方便用户进行API调用,创建自己场景的语义理解服务,网址为 https://www.luis.ai,   一旦应用程序上线,接收到真实的数据,LUIS使用主动学习来提高自身。在主动学习过程中,LUIS将模棱两可的文本识别出来,并要求你根据意图和实体标记它们。其次,通过专门的样本,LUIS可以快速地学习,短时间内达到特定领域较高的智能水平

    目录:

    • LUIS功能
    • App定义
    • 代码访问

    LUIS功能


    • 本地化支持: LUIS UI始终为英语,但在理解语音时,支持英语,法语,意大利语,西班牙语和中文等多种语言
    • 以编程方式访问LUIS:LUIS提供了一个可编程的REST API,可用于自动化创建和发布特定语意模型

    App定义


    •  创建Application,如下图:
    • 在菜单面板的左上角,可看到一个Intent、Entity区域。 所有应用程序都有一个预定义的Intent,“None”。 这将识别与应用程序无关的用户语句
    • 实体是我们的搜索关键字,就像在我们的例子中实体是"股票“
    • Intent:  指我们的愿望和意图是什么,如下图:我想查股票价格
    • utterances:表达方式,为了让系统理解语意,对文本内容做特定标记,如下图
    • 点击左下角"Train", 然后进行发布,将模型发布成HTTP服务,它将解释我们发送的句子。单击左上角的“发布”按钮,然后在生成的窗口中发布Web服务。几分钟后,你将看到一个网址,使你的模型可用作Web服务。 LUIS将如下面的截图所示
    • 在Query里面可以输入你要表示的主义进行测试,如输入"帮我查一下000977的价格",回车后,测试返回结果如下:

    代码访问


    • 结合微信输入 + luis 语义理解,代码示例实时股票价格查询demo
    • 接收微信服务器转发的请求信息,作业务处理,代码如下:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Text;
      using System.Text.RegularExpressions;
      using System.Threading.Tasks;
      using System.Runtime.Serialization;
      using System.IO;
      
      namespace WXHandler
      {
          /// <summary>
          /// 
          /// </summary>
          public class WeixinExecutor : IWeixinExecutor
          {
              /// <summary>
              /// 
              /// </summary>
              public static WeixinExecutor Default
              {
                  get { return new WeixinExecutor(); }
              }
      
              /// <summary>
              /// 
              /// </summary>
              /// <param name="message"></param>
              /// <returns>已经打包成xml的用于回复用户的消息包</returns>
              public string Execute(WeixinMessage message)
              {
                  string result = string.Empty;
                  var openId = message.Body.FromUserName.Value;
                  var myUserName = message.Body.ToUserName.Value;
                  switch (message.Type)
                  {
                      case WeixinMessageType.Text://文字消息
                          {
                              string userMessage = message.Body.Content.Value;
                              var BotMessage = LuisService.Default.PostMessage(userMessage);
                              result = ReplayPassiveMessageAPI.RepayText(openId, myUserName, BotMessage);
                          }
                          break;
                      case WeixinMessageType.Image://图片消息
                          {
                              string imageUrl = message.Body.PicUrl.Value;//图片地址
      
                              var msg = ImageService.Default.PostMessage(imageUrl);
                              result = ReplayPassiveMessageAPI.RepayText(openId, myUserName, msg);
                          }
                          break;
                      case WeixinMessageType.Voice://语音消息
                          {                        
                              var recognition = Convert.ToString(message.Body.Recognition.Value);
                              var BotMessage = LuisService.Default.PostMessage(recognition);
                              if (BotMessage == LuisInfos.DEFAULT_LUIS)
                                  BotMessage += string.Format(" 您输入的内容为:{0}", recognition);
                              result = ReplayPassiveMessageAPI.RepayText(openId, myUserName, BotMessage);
                          }
                          break;
                      case WeixinMessageType.Location://地理位置消息
                          {
                              var location_X = message.Body.Location_X.Value.ToString();
                              var location_Y = message.Body.Location_Y.Value.ToString();
      
                              var BotMessage = LocationService.Default.PostMessage(location_Y, location_X);
                              result = ReplayPassiveMessageAPI.RepayText(openId, myUserName, BotMessage);
                          }
                          break;
                      case WeixinMessageType.Link://链接消息
                          {
                              var title = message.Body.Title.Value.ToString();
                              var description = message.Body.Description.Value.ToString();
                              var url = message.Body.Url.Value.ToString();
                              var msgId = message.Body.MsgId.Value.ToString();
                          }
                          break;
                      default:
                          result = ReplayPassiveMessageAPI.RepayText(openId, myUserName,"xxxxxx......");
                          break;
                  }
                  return result;
              }
          }
      }
      View Code
    • 将接收到的信息通过luis理解,获取intents 和 entity,在这里我们可以封装一个luis实体对象
      using Newtonsoft.Json;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net.Http;
      using System.Threading.Tasks;
      using System.Web;
      
      namespace WXHandler
      {
          public class LuisAPI
          {
              /// <summary>
              /// 
              /// </summary>
              private static readonly string URI = "https://api.projectoxford.ai/luis/v2.0/apps/eed6361a-dfce-4faf-b69c-b13d1298a560?subscription-key=订阅号&q={0}";
      
              /// <summary>
              /// 
              /// </summary>
              public static LuisAPI Default
              {
                  get { return new LuisAPI(); }
              }
      
              /// <summary>
              /// 
              /// </summary>
              /// <param name="Query"></param>
              /// <returns></returns>
              public LuisMessage GetLuis(string Query)
              {
                  Query = Uri.EscapeDataString(Query);
                  LuisMessage Data = new LuisMessage();
                  using (HttpClient client = new HttpClient())
                  {
                      string RequestURI = string.Format(URI, Query);
                      var task = client.GetAsync(RequestURI);
                      HttpResponseMessage msg = task.Result;
      
                      if (msg.IsSuccessStatusCode)
                      {
                          var task1 = msg.Content.ReadAsStringAsync();
                          var JsonDataResponse = task1.Result;
                          Data = JsonConvert.DeserializeObject<LuisMessage>(JsonDataResponse);
                      }
                  }
                  return Data;
              }
          }
      }
      View Code
    • 根据不同的intents,封装不同的业务逻辑处理

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Threading.Tasks;
      using System.Web;
      
      namespace WXHandler
      {
          public class LuisExecutor
          {
              /// <summary>
              /// 
              /// </summary>
              public static LuisExecutor Default
              {
                  get { return new LuisExecutor(); }
              }
      
              /// <summary>
              /// 
              /// </summary>
              /// <param name="message"></param>
              /// <returns></returns>
              public string PostMessage(string query)
              {            
                  LuisMessage luisMsg = LuisAPI.Default.GetLuis(query);
                  if (luisMsg == null || luisMsg.entities == null || luisMsg.entities.Length <= 0)
                      return LuisInfos.DEFAULT_LUIS;
      
                  string reponse = "";
                  switch (luisMsg.topScoringIntent.intent)
                  {
                      case "查询天气":
                          reponse = LuisWeather.Default.QueryWeather(luisMsg.entities[0].entity);
                          break;
                      case"查询价格":
                          reponse = LuisStock.Default.QueryStock(luisMsg.entities[0].entity);
                          break;
                      case "查询客户信息":
                          reponse = LuisPartner.Default.QueryInfo(luisMsg.entities[0].entity);
                          break;
                      case "查覆盖率":
                          reponse = LuisStaConver.Default.QueryInfo(luisMsg.entities[0].entity);
                          break;
                      default:
                          reponse = LuisInfos.DEFAULT_LUIS;
                          break;
                  }
      
                  return reponse;
              }
          }
      }
      View Code
    • 获取实时股票信息示例 

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net.Http;
      using System.Web;
      
      namespace WXHandler
      {
          /// <summary>
          /// 
          /// </summary>
          public class LuisStock
          {
              private const string dataurl = "http://hq.sinajs.cn/list={0}";
      
              /// <summary>
              /// 
              /// </summary>
              public static LuisStock Default
              {
                  get { return new LuisStock(); }
              }
      
              /// <summary>
              /// 
              /// </summary>
              /// <param name="location"></param>
              /// <returns></returns>
              public string QueryStock(string stockID)
              {
                  string returnString = string.Empty;
                  string id = this.ParseStockID(stockID);
                  if (string.IsNullOrEmpty(id))
                      return string.Format(LuisInfos.DEFAULT_STOCK_REPLY, stockID);
      
                  string dataUrl = string.Format(dataurl, id);
                  HttpClient client = new HttpClient();
                  var task = client.GetStringAsync(dataUrl);
                  string realInfo = task.Result;
                  Utils.WriteLog(realInfo);
                  string[] infos = realInfo.Split('
      ');
                  if (infos.Length <= 0)
                      return string.Format(LuisInfos.DEFAULT_STOCK_REPLY, stockID);
                  
                  StockRealInfo stockInfo = new StockRealInfo(infos[0]);
                  if (stockInfo.IsNull)
                  {
                      return string.Format(LuisInfos.DEFAULT_STOCK_REPLY, stockID);
                  }
      
                  return string.Format("股票 {0} 名称: {1};开盘价: {2}; 当前价: {3}; 今日最高价: {4}; 今日最低价:{5}; 涨跌幅:{6}",
                      id,
                      stockInfo.Name,
                      stockInfo.TodayOpen,
                      stockInfo.Current,
                      stockInfo.High,
                      stockInfo.Low,
                      stockInfo.DiffPrec);
              }
      
              /// <summary>
              /// 
              /// </summary>
              /// <param name="stockid"></param>
              /// <returns></returns>
              private string ParseStockID(string id)
              {
                  if (id.Length == 1 || id.Length < 4)
                      return string.Empty;
      
                  if (id.Substring(0, 2) == "60")//上海是600打头
                  {
                      return string.Format("sh{0},", id);
                  }
                  else if (id.Substring(0, 2) == "51")//上海基金
                  {
                      return string.Format("sh{0},", id);
                  }
                  else  //深圳
                  {
                      return string.Format("sz{0},", id);
                  }
              }
          }
      }
      View Code
    • 运行效果:

  • 相关阅读:
    wustctf2020_name_your_dog
    wdb2018_guess(stack smash,__environ获取栈变量)
    ciscn_2019_en_3
    植物大战僵尸逆向(秒杀僵尸)
    pwnable_hacknote
    pwn-hitcontraining_magicheap(unsorted bin attack)
    pwn-[XMAN]level5(通用gadget,mprotect)
    pwn-pwnable_orw(seccomp&orw)
    《心流》有感
    LC112 路径总和
  • 原文地址:https://www.cnblogs.com/tgzhu/p/6186382.html
Copyright © 2020-2023  润新知