/Crawl/CognitiveServiceTextAnalytics/CognitiveServiceTextAnalytics.cs

https://github.com/Microsoft/mwt-ds · C# · 129 lines · 100 code · 22 blank · 7 comment · 7 complexity · a3242a3bf6765494d2402ecb0b07adac MD5 · raw file

  1. //------------------------------------------------------------------------------
  2. // <copyright company="Microsoft Corporation">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using System.Threading.Tasks;
  7. using Microsoft.Azure.WebJobs.Host;
  8. using System.Net.Http;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.Threading;
  14. namespace Microsoft.DecisionService.Crawl
  15. {
  16. public class CognitiveServiceTextAnalytics
  17. {
  18. private static readonly CognitiveService cogService;
  19. static CognitiveServiceTextAnalytics()
  20. {
  21. cogService = new CognitiveService("CogTextAnalytics", queryParams: "/sentiment");
  22. }
  23. internal static TextAnalyticRequest CreateRequestFromText(string text)
  24. {
  25. // Based on email thread with Arvind Krishnaa Jagannathan <arjagann@microsoft.com>
  26. if (text.Length >= Constants.MaxRequestSizeUtf16)
  27. {
  28. text = text.Substring(0, Constants.MaxRequestSizeUtf16);
  29. }
  30. return new TextAnalyticRequest
  31. {
  32. Documents = new List<TextAnalyticDocument>
  33. {
  34. new TextAnalyticDocument
  35. {
  36. //Language = "english",
  37. Text = text,
  38. Id = "1"
  39. }
  40. }
  41. };
  42. }
  43. public static Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, CancellationToken cancellationToken)
  44. {
  45. return cogService.InvokeAsync(req, log,
  46. reqBody =>
  47. {
  48. var textBuilder = new StringBuilder();
  49. if (!string.IsNullOrEmpty(reqBody.Title))
  50. textBuilder.AppendLine(reqBody.Title);
  51. if (!string.IsNullOrEmpty(reqBody.Article))
  52. textBuilder.AppendLine(reqBody.Article);
  53. if (textBuilder.Length == 0)
  54. return null;
  55. var text = textBuilder.ToString();
  56. return CreateRequestFromText(text);
  57. },
  58. (reqBody, blobContent) =>
  59. {
  60. blobContent.Output = new JObject();
  61. var responseObj = JsonConvert.DeserializeObject<TextAnalyticResponse<DocumentSentiment>>(blobContent.Value);
  62. if (responseObj?.Documents?.Length == 1)
  63. blobContent.Output.Add(new JProperty("XSentiment", responseObj.Documents[0].Score));
  64. },
  65. isPost: true,
  66. cancellationToken: cancellationToken);
  67. }
  68. public class TextAnalyticRequest
  69. {
  70. [JsonProperty("documents")]
  71. public List<TextAnalyticDocument> Documents { get; set; }
  72. }
  73. public class TextAnalyticDocument
  74. {
  75. [JsonProperty("language", NullValueHandling = NullValueHandling.Ignore)]
  76. public string Language { get; set; }
  77. [JsonProperty("id")]
  78. public string Id { get; set; }
  79. [JsonProperty("text")]
  80. public string Text { get; set; }
  81. }
  82. public class TextAnalyticResponse<TDocumentResult> where TDocumentResult : TextAnalyticDocumentResultBase
  83. {
  84. [JsonProperty("documents")]
  85. public TDocumentResult[] Documents { get; set; }
  86. [JsonProperty("errors")]
  87. public TextAnalyticResponseError[] Errors { get; set; }
  88. }
  89. public class TextAnalyticDocumentResultBase
  90. {
  91. [JsonProperty("id")]
  92. public string Id { get; set; }
  93. }
  94. public class DocumentSentiment : TextAnalyticDocumentResultBase
  95. {
  96. [JsonProperty("score")]
  97. public float Score { get; set; }
  98. }
  99. public class TextAnalyticResponseError
  100. {
  101. [JsonProperty("id")]
  102. public string Id { get; set; }
  103. [JsonProperty("message")]
  104. public string Message { get; set; }
  105. }
  106. }
  107. }