Skip to content

Commit 98ef1d6

Browse files
committed
break sentence edits
1 parent d5c8bb4 commit 98ef1d6

File tree

1 file changed

+70
-33
lines changed

1 file changed

+70
-33
lines changed

articles/cognitive-services/Translator/quickstart-csharp-sentences.md

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,38 @@ The `dotnet new console` command that you ran earlier created a project, includi
6363
using System;
6464
using System.Net.Http;
6565
using System.Text;
66+
using System.Threading.Tasks;
67+
// Install Newtonsoft.Json with NuGet
6668
using Newtonsoft.Json;
6769
```
6870

71+
## Create classes for the JSON response
72+
73+
Next, we're going to create a class that's used when deserializing the JSON response returned by the Translator Text API.
74+
75+
```csharp
76+
/// <summary>
77+
/// The C# classes that represents the JSON returned by the Translator Text API.
78+
/// </summary>
79+
public class BreakSentenceResult
80+
{
81+
public int[] SentLen { get; set; }
82+
public DetectedLanguage DetectedLanguage { get; set; }
83+
}
84+
85+
public class DetectedLanguage
86+
{
87+
public string Language { get; set; }
88+
public float Score { get; set; }
89+
}
90+
```
91+
6992
## Create a function to determine sentence length
7093

71-
Within the `Program` class, create a function called `BreakSentence`. This class encapsulates the code used to call the BreakSentence resource and prints the result to console.
94+
Within the `Program` class, create a function called `BreakSentence`. This function takes four arguments: `subscriptionKey`, `host`, `route`, and `inputText`.
7295

7396
```csharp
74-
static void BreakSentence()
97+
static public async Task BreakSentenceRequest(string subscriptionKey, string host, string route, string inputText)
7598
{
7699
/*
77100
* The code for your call to the translation service will be added to this
@@ -80,20 +103,12 @@ static void BreakSentence()
80103
}
81104
```
82105

83-
## Set the subscription key, host name, and path
84-
85-
Add these lines to the `BreakSentence` function. You'll notice that along with the `api-version`, you can define the input language. In this sample it's English.
86-
87-
```csharp
88-
string host = "https://api.cognitive.microsofttranslator.com";
89-
string route = "/breaksentence?api-version=3.0&language=en";
90-
string subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
91-
```
106+
## Serialize the Break Sentence request
92107

93108
Next, you need to create and serialize the JSON object that includes the text. Keep in mind, you can pass more than one object in the `body` array.
94109

95110
```csharp
96-
System.Object[] body = new System.Object[] { new { Text = @"How are you? I am fine. What did you do today?" } };
111+
object[] body = new object[] { new { Text = inputText } };
97112
var requestBody = JsonConvert.SerializeObject(body);
98113
```
99114

@@ -123,36 +138,48 @@ Inside the `HttpRequestMessage` you'll:
123138
Add this code to the `HttpRequestMessage`:
124139

125140
```csharp
141+
// Build the request.
126142
// Set the method to POST
127143
request.Method = HttpMethod.Post;
128-
129-
// Construct the full URI
144+
// Construct the URI and add headers.
130145
request.RequestUri = new Uri(host + route);
131-
132-
// Add the serialized JSON object to your request
133146
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
134-
135-
// Add the authorization header
136147
request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
137148

138-
// Send request, get response
139-
var response = client.SendAsync(request).Result;
140-
var jsonResponse = response.Content.ReadAsStringAsync().Result;
141-
142-
// Print the response
143-
Console.WriteLine(jsonResponse);
144-
Console.WriteLine("Press any key to continue.");
149+
// Send the request and get response.
150+
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
151+
// Read response as a string.
152+
string result = await response.Content.ReadAsStringAsync();
153+
// Deserialize the response using the classes created earlier.
154+
BreakSentenceResult[] deserializedOutput = JsonConvert.DeserializeObject<BreakSentenceResult[]>(result);
155+
foreach (BreakSentenceResult o in deserializedOutput)
156+
{
157+
Console.WriteLine("The detected language is '{0}'. Confidence is: {1}.", o.DetectedLanguage.Language, o.DetectedLanguage.Score);
158+
Console.WriteLine("The first sentence length is: {0}", o.SentLen[0]);
159+
}
145160
```
146161

147162
## Put it all together
148163

149-
The last step is to call `BreakSentence()` in the `Main` function. Locate `static void Main(string[] args)` and add these lines:
164+
The last step is to call `BreakSentenceRequest()` in the `Main` function. Locate `static void Main(string[] args)` and add replace it with this code:
150165

151166
```csharp
152-
BreakSentence();
153-
Console.ReadLine();
167+
static async Task Main(string[] args)
168+
{
169+
// This is our main function.
170+
// Output languages are defined in the route.
171+
// For a complete list of options, see API reference.
172+
string subscriptionKey = "YOUR_TRANSLATOR_TEXT_KEY_GOES_HERE";
173+
string host = "https://api.cognitive.microsofttranslator.com";
174+
string route = "/breaksentence?api-version=3.0";
175+
// Feel free to use any string.
176+
string breakSentenceText = @"How are you doing today? The weather is pretty pleasant. Have you been to the movies lately?";
177+
await BreakSentenceRequest(subscriptionKey, host, route, breakSentenceText);
178+
}
154179
```
155180

181+
You'll notice that in `Main`, you're declaring `subscriptionKey`, `host`, `route`, and the text to evaluate `breakSentenceText`.
182+
156183
## Run the sample app
157184

158185
That's it, you're ready to run your sample app. From the command line (or terminal session), navigate to your project directory and run:
@@ -163,14 +190,24 @@ dotnet run
163190

164191
## Sample response
165192

193+
After you run the sample, you should see the following printed to terminal:
194+
195+
```bash
196+
The detected language is \'en\'. Confidence is: 1.
197+
The first sentence length is: 25
198+
```
199+
200+
This message is built from the raw JSON, which will look like this:
201+
166202
```json
167203
[
168204
{
169-
"sentLen": [
170-
13,
171-
11,
172-
22
173-
]
205+
"detectedLanguage":
206+
{
207+
"language":"en",
208+
"score":1.0
209+
},
210+
"sentLen":[25,32,35]
174211
}
175212
]
176213
```

0 commit comments

Comments
 (0)