Skip to content

Commit

Permalink
1st commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffWang0325 committed Sep 29, 2021
0 parents commit f497343
Show file tree
Hide file tree
Showing 149 changed files with 24,282 additions and 0 deletions.
Binary file added DLL/clsLanguage.dll
Binary file not shown.
25 changes: 25 additions & 0 deletions MicrosoftCognitiveServices.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicrosoftCognitiveServices", "MicrosoftCognitiveServices\MicrosoftCognitiveServices.csproj", "{5AF24A9B-BA9D-47D3-A493-2F3117DBC692}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5AF24A9B-BA9D-47D3-A493-2F3117DBC692}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5AF24A9B-BA9D-47D3-A493-2F3117DBC692}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AF24A9B-BA9D-47D3-A493-2F3117DBC692}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AF24A9B-BA9D-47D3-A493-2F3117DBC692}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F922983C-42A4-4598-B87C-D4627C512D79}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Text;

using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using System.Web;

namespace LUIS
{
public class LUISApp
{
/// <summary>
/// Call LUIS API
/// </summary>
/// <param name="luisappid"></param>
/// <param name="subscriptionkey"></param>
/// <param name="usertalk"></param>
/// <param name="location"></param>
/// <returns></returns>
public static async Task<LuisResult> MakeRequest(string luisappid, string subscriptionkey, string usertalk, string location = "westus")
{
var result = new LuisResult();

try
{
var endpoint = "https://" + location + ".api.cognitive.microsoft.com/luis/v2.0/apps/" + luisappid + "?"; // For v2.0
var client = new HttpClient();
using (var request = new HttpRequestMessage())
{
// Request parameters
// Method 1: 在Console應用程式,輸入中文時,會編碼成Unicode ---> 問題已解決!!!
// HttpUtility.ParseQueryString 還原字串中文編碼問題: https://blog.darkthread.net/blog/httpvaluecollection-tostring-urlencode/
var querystr = HttpUtility.ParseQueryString(string.Empty); // 輸入中文時,會編碼成Unicode
querystr["verbose"] = "true"; //需要回傳的所有 Intent ,將 verbose 設定為 true
querystr["q"] = usertalk;
querystr["spellCheck"] = "false";
querystr["staging"] = "false";
querystr["log"] = "true";

// Method 2
//string querystr2 = "verbose=true&q=" + usertalk + "&spellCheck=false&staging=false&log=true"; // For v2.0

// Method 3
//var querystr = HttpUtility.ParseQueryString(string.Empty); // 輸入中文時,會編碼成Unicode
//querystr["verbose"] = "true"; //需要回傳的所有 Intent ,將 verbose 設定為 true
//querystr["spellCheck"] = "false";
//querystr["staging"] = "false";
//querystr["log"] = "true";
//var uri = endpoint + "q=" + HttpUtility.UrlEncode(usertalk) + "&" + querystr;

request.Method = HttpMethod.Get;
request.RequestUri = new Uri(endpoint + querystr); // Method 1
//request.RequestUri = new Uri(endpoint + querystr2); // Method 2
//request.RequestUri = new Uri(uri); // Method 3

request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionkey);

var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<LuisResult>(responseBody);

if (result.Query == null)
result = null;
}
}
catch (Exception ex)
{
result = null;
}

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Newtonsoft.Json;

namespace LUIS
{
/// <summary>
/// For v2.0
/// </summary>
public class LuisResult
{
[JsonProperty("query")]
public string Query { get; set; }

[JsonProperty("topScoringIntent")]
public Topscoringintent TopScoringIntent { get; set; }

/// <summary>
/// 實體
/// </summary>
[JsonProperty("entities")]
public Entity[] Entities { get; set; }

[JsonProperty("sentimentAnalysis")]
public Sentimentanalysis SentimentAnalysis { get; set; }
}

public class Topscoringintent
{
/// <summary>
/// 意圖
/// </summary>
[JsonProperty("intent")]
public string Intent { get; set; }

[JsonProperty("score")]
public float Score { get; set; }
}

public class Sentimentanalysis
{
[JsonProperty("label")]
public string Label { get; set; }

[JsonProperty("score")]
public float Score { get; set; }
}

public class Entity
{
[JsonProperty("entity")]
public string EntityItem { get; set; }

[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("startIndex")]
public int StartIndex { get; set; }

[JsonProperty("endIndex")]
public int EndIndex { get; set; }

[JsonProperty("resolution")]
public Resolution Resolution { get; set; }

[JsonProperty("score")]
public float Score { get; set; }
}

public class Resolution
{
[JsonProperty("value")]
public string Value { get; set; }

[JsonProperty("unit")]
public string Unit { get; set; }

[JsonProperty("values")]
public string[] Values { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace QnAMaker
{
public class AnswerResult
{
[JsonProperty("answers")]
public List<Answer> Answers { get; set; }
}

public class Answer
{
[JsonProperty("answer")]
public string AnswerStr { get; set; }

[JsonProperty("questions")]
public List<string> Questions { get; set; }

[JsonProperty("score")]
public float Score { get; set; }

[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("source")]
public string Source { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Text;

using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Web;

namespace QnAMaker
{
public class QnAMakerApp
{
/// <summary>
/// Call QnA Maker API
/// </summary>
/// <param name="url"></param>
/// <param name="endpoint_key"></param>
/// <param name="question"></param>
/// <returns></returns>
public static async Task<AnswerResult> MakeRequest(string url, string endpoint_key, Question question)
{
var result = new AnswerResult();

try
{
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(url);
request.Headers.Add("Authorization", "EndpointKey "
+ endpoint_key);

string content = JsonConvert.SerializeObject(question);
request.Content = new StringContent(content, Encoding.UTF8, "application/json");

var response = await client.SendAsync(request);
var val = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<AnswerResult>(val);

if (result.Answers == null)
result = null;
}
}
}
catch (Exception ex)
{
result = null;
}

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace QnAMaker
{
public class Question
{
/// <summary>
/// 要詢問的問題
/// </summary>
[JsonProperty("question")]
public string QuestionStr { get; set; } = "";

/// <summary>
/// 取回分數最高的答案筆數
/// </summary>
[JsonProperty("top")]
public int Top { get; set; } = 2;
}
}
Loading

0 comments on commit f497343

Please sign in to comment.