Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics
{
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Rest;

/// <summary>
/// Allows authentication to the API using a basic apiKey mechanism
/// </summary>
public class ApiKeyServiceClientCredentials : ServiceClientCredentials
{
private readonly string subscriptionKey;

/// <summary>
/// Creates a new instance of the ApiKeyServiceClientCredentails class
/// </summary>
/// <param name="subscriptionKey">The subscription key to authenticate and authorize as</param>
public ApiKeyServiceClientCredentials(string subscriptionKey)
{
if (string.IsNullOrWhiteSpace(subscriptionKey))
throw new ArgumentNullException("subscriptionKey");

this.subscriptionKey = subscriptionKey;
}

/// <summary>
/// Add the Basic Authentication Header to each outgoing request
/// </summary>
/// <param name="request">The outgoing request</param>
/// <param name="cancellationToken">A token to cancel the operation</param>
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
throw new ArgumentNullException("request");

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

return Task.FromResult<object>(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using System.Net.Http;

namespace Language.Tests
{
public abstract class BaseTests
{
public static bool IsTestTenant = false;
private static string SubscriptionKey = "";
private static string Region = null;

static BaseTests()
{
// Retrieve the configuration information.

Region = "WestUS";
}
private static string SubscriptionKey = "000";

protected ITextAnalyticsAPI GetClient(DelegatingHandler handler)
{
ITextAnalyticsAPI client;
client = new TextAnalyticsAPI(handlers: handler);
client.SubscriptionKey = SubscriptionKey;

return client;
return new TextAnalyticsAPI(new ApiKeyServiceClientCredentials(SubscriptionKey), handlers: handler)
{
AzureRegion = AzureRegions.Westus
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"Entries": [
{
"RequestUri": "/text/analytics/v2.0/entities",
"EncodedRequestUri": "L3RleHQvYW5hbHl0aWNzL3YyLjAvZW50aXRpZXM=",
"RequestMethod": "POST",
"RequestBody": "{\r\n \"documents\": [\r\n {\r\n \"language\": \"en\",\r\n \"id\": \"id\",\r\n \"text\": \"Microsoft released Windows 10\"\r\n }\r\n ]\r\n}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"123"
],
"Ocp-Apim-Subscription-Key": [
""
],
"User-Agent": [
"FxVersion/4.6.25211.01",
"Microsoft.CognitiveServices.Language.TextAnalytics.TextAnalyticsAPI/1.0.0"
]
},
"ResponseBody": "{ \"documents\": [{\"id\": \"1\", \"entities\": [ { \"name\": \"Windows 10\", \"matches\": [ { \"text\": \"Windows 10\", \"offset\": 19, \"length\": 10 } ], \"wikipediaLanguage\": \"en\", \"wikipediaId\": \"Windows 10\", \"wikipediaUrl\": \"https://en.wikipedia.org/wiki/Windows_10\", \"bingId\": \"5f9fbd03-49c4-39ef-cc95-de83ab897b94\" }, { \"name\": \"Microsoft\", \"matches\": [ { \"text\": \"Microsoft\", \"offset\": 0, \"length\": 9 } ], \"wikipediaLanguage\": \"en\", \"wikipediaId\": \"Microsoft\", \"wikipediaUrl\": \"https://en.wikipedia.org/wiki/Microsoft\", \"bingId\": \"a093e9b9-90f5-a3d5-c4b8-5855e1b01f85\" } ] }\r\n ],\r\n \"errors\": []\r\n}",
"ResponseHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Wed, 30 Aug 2017 02:11:17 GMT"
],
"Transfer-Encoding": [
"chunked"
],
"x-ms-transaction-count": [
"1"
],
"x-aml-ta-request-id": [
"460baaca-7f57-42c1-8cc7-8852957d05b9"
],
"X-Content-Type-Options": [
"nosniff"
],
"apim-request-id": [
"3f09b1c9-fe3b-46bb-badf-98108c599825"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
using Language.Tests;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;

using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;

using System.Collections.Generic;
using System.Threading.Tasks;

using Xunit;

namespace Language.TextAnalytics.Tests
namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Tests
{
public class DetectLanguageTests : BaseTests
{
[Fact]
public void DetectLanguage()
public async Task DetectLanguage()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "DetectLanguage");
ITextAnalyticsAPI client = GetClient(HttpMockServer.CreateInstance());
LanguageBatchResult result = client.DetectLanguage(
LanguageBatchResult result = await client.DetectLanguageAsync(
new BatchInput(
new List<Input>()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Language.Tests;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Tests
{
public class EntitiesTests : BaseTests
{
[Fact]
public async Task Entities()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "Entities");
ITextAnalyticsAPI client = GetClient(HttpMockServer.CreateInstance());
EntitiesBatchResult result = await client.EntitiesAsync(
new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
new MultiLanguageInput()
{
Id ="id",
Text ="Microsoft released Windows 10",
Language ="en"
}
}));

Assert.Equal("Windows 10", result.Documents[0].Entities[0].Name);
Assert.Equal("5f9fbd03-49c4-39ef-cc95-de83ab897b94", result.Documents[0].Entities[0].BingId);
context.Stop();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
using Language.Tests;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;

using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;

using System.Collections.Generic;
using System.Threading.Tasks;

using Xunit;

namespace Language.TextAnalytics.Tests
namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Tests
{
public class KeyPhrasesTests : BaseTests
{
[Fact]
public void KeyPhrases()
public async Task KeyPhrases()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "KeyPhrases");
ITextAnalyticsAPI client = GetClient(HttpMockServer.CreateInstance());
KeyPhraseBatchResult result = client.KeyPhrases(
KeyPhraseBatchResult result = await client.KeyPhrasesAsync(
new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
using Language.Tests;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;

using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;

using System.Collections.Generic;
using System.Threading.Tasks;

using Xunit;

namespace Language.TextAnalytics.Tests
namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Tests
{
public class SentimentTests : BaseTests
{
[Fact]
public void Sentiment()
public async Task Sentiment()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "Sentiment");
ITextAnalyticsAPI client = GetClient(HttpMockServer.CreateInstance());
SentimentBatchResult result = client.Sentiment(
SentimentBatchResult result = await client.SentimentAsync(
new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// <auto-generated>
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
//
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>

namespace Microsoft.Azure.CognitiveServices.Language.TextAnalytics
{
using Microsoft.Azure;
using Microsoft.Azure.CognitiveServices;
using Microsoft.Azure.CognitiveServices.Language;
using Microsoft.Rest;
using Models;
using Newtonsoft.Json;
Expand Down Expand Up @@ -45,26 +44,27 @@ public partial interface ITextAnalyticsAPI : System.IDisposable
/// </summary>
JsonSerializerSettings DeserializationSettings { get; }

/// <summary>
/// Subscription key in header
/// </summary>
string SubscriptionKey { get; set; }

/// <summary>
/// Supported Azure regions for Cognitive Services endpoints. Possible
/// values include: 'westus', 'westeurope', 'southeastasia', 'eastus2',
/// 'westcentralus'
/// 'westcentralus', 'westus2', 'eastus', 'southcentralus',
/// 'northeurope', 'eastasia', 'australiaeast', 'brazilsouth'
/// </summary>
AzureRegions AzureRegion { get; set; }

/// <summary>
/// Subscription credentials which uniquely identify client
/// subscription.
/// </summary>
ServiceClientCredentials Credentials { get; }


/// <summary>
/// The API returns a list of strings denoting the key talking points
/// in the input text.
/// </summary>
/// <remarks>
/// We employ techniques from Microsoft Office's sophisticated Natural
/// Language Processing toolkit. See the &lt;a
/// See the &lt;a
/// href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/overview#supported-languages"&gt;Text
/// Analytics Documentation&lt;/a&gt; for details about the languages
/// that are supported by key phrase extraction.
Expand Down Expand Up @@ -92,28 +92,21 @@ public partial interface ITextAnalyticsAPI : System.IDisposable
/// <param name='input'>
/// Collection of documents to analyze.
/// </param>
/// <param name='numberOfLanguagesToDetect'>
/// (Optional. Deprecated) Number of languages to detect. Set to 1 by
/// default. Irrespective of the value, the language with the highest
/// score is returned.
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<LanguageBatchResult>> DetectLanguageWithHttpMessagesAsync(BatchInput input, int? numberOfLanguagesToDetect = default(int?), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<HttpOperationResponse<LanguageBatchResult>> DetectLanguageWithHttpMessagesAsync(BatchInput input, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// The API returns a numeric score between 0 and 1.
/// </summary>
/// <remarks>
/// Scores close to 1 indicate positive sentiment, while scores close
/// to 0 indicate negative sentiment. Sentiment score is generated
/// using classification techniques. The input features to the
/// classifier include n-grams, features generated from part-of-speech
/// tags, and word embeddings. See the &lt;a
/// to 0 indicate negative sentiment. A score of 0.5 indicates the lack
/// of sentiment (e.g. a factoid statement). See the &lt;a
/// href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/overview#supported-languages"&gt;Text
/// Analytics Documentation&lt;/a&gt; for details about the languages
/// that are supported by sentiment analysis.
Expand All @@ -129,5 +122,27 @@ public partial interface ITextAnalyticsAPI : System.IDisposable
/// </param>
Task<HttpOperationResponse<SentimentBatchResult>> SentimentWithHttpMessagesAsync(MultiLanguageBatchInput input, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// The API returns a list of recognized entities in a given document.
/// </summary>
/// <remarks>
/// To get even more information on each recognized entity we recommend
/// using the Bing Entity Search API by querying for the recognized
/// entities names. See the &lt;a
/// href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/text-analytics-supported-languages"&gt;Supported
/// languages in Text Analytics API&lt;/a&gt; for the list of enabled
/// languages.
/// </remarks>
/// <param name='input'>
/// Collection of documents to analyze.
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<EntitiesBatchResult>> EntitiesWithHttpMessagesAsync(MultiLanguageBatchInput input, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));

}
}
Loading