Skip to content

Commit f2e141e

Browse files
committed
Updating transliterate doc.
1 parent b6943ce commit f2e141e

File tree

2 files changed

+74
-32
lines changed

2 files changed

+74
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public class SentenceLength
118118

119119
## Create a function to translate text
120120

121-
Within the `Program` class, create a asynchronous function called `TranslateTextRequest`. This function takes four arguments: `subscriptionKey`, `host`, `route`, and `inputText`.
121+
Within the `Program` class, create an asynchronous function called `TranslateTextRequest`. This function takes four arguments: `subscriptionKey`, `host`, `route`, and `inputText`.
122122

123123
```csharp
124124
// This sample requires C# 7.1 or later for async/await.

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

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ ms.author: erhopf
1414

1515
# Quickstart: Use the Translator Text API to transliterate text using C#
1616

17-
In this quickstart, you'll learn how to transliterate (convert) text from one script to another using .NET Core (C#) and the Translator Text REST API. In the sample provided, Japanese is transliterated to use the Latin alphabet.
17+
In this quickstart, you'll learn how to transliterate (convert) text from one script to another using .NET Core (C#), C# 7.1 or later, and the Translator Text REST API. In the sample provided, Japanese is transliterated to use the Latin alphabet.
1818

1919
This quickstart requires an [Azure Cognitive Services account](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account) with a Translator Text resource. If you don't have an account, you can use the [free trial](https://azure.microsoft.com/try/cognitive-services/) to get a subscription key.
2020

2121
## Prerequisites
2222

23+
* C# 7.1 or later
2324
* [.NET SDK](https://www.microsoft.com/net/learn/dotnet/hello-world-tutorial)
2425
* [Json.NET NuGet Package](https://www.nuget.org/packages/Newtonsoft.Json/)
2526
* [Visual Studio](https://visualstudio.microsoft.com/downloads/), [Visual Studio Code](https://code.visualstudio.com/download), or your favorite text editor
@@ -42,6 +43,18 @@ Next, you'll need to install Json.Net. From your project's directory, run:
4243
dotnet add package Newtonsoft.Json --version 11.0.2
4344
```
4445

46+
## Select the C# language version
47+
48+
This quickstart requires C# 7.1 or later. There are a few ways to change the C# version for your project. In this guide, we'll show you how to adjust the `tts-sample.csproj` file. For all available options, such as changing the language in Visual Studio, see [Select the C# language version](https://docs.microsoft.com/dotnet/csharp/language-reference/configure-language-version).
49+
50+
Open your project, then open `tts-sample.csproj`. Make sure that `LangVersion` is set to 7.1 or later. If there isn't a property group for the language version, add these lines:
51+
52+
```xml
53+
<PropertyGroup>
54+
<LangVersion>7.1</LangVersion>
55+
</PropertyGroup>
56+
```
57+
4558
## Add required namespaces to your project
4659

4760
The `dotnet new console` command that you ran earlier created a project, including `Program.cs`. This file is where you'll put your application code. Open `Program.cs`, and replace the existing using statements. These statements ensure that you have access to all the types required to build and run the sample app.
@@ -50,15 +63,32 @@ The `dotnet new console` command that you ran earlier created a project, includi
5063
using System;
5164
using System.Net.Http;
5265
using System.Text;
66+
using System.Threading.Tasks;
67+
// Install Newtonsoft.Json with NuGet
5368
using Newtonsoft.Json;
5469
```
5570

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 TransliterationResult
80+
{
81+
public string Text { get; set; }
82+
public string Script { get; set; }
83+
}
84+
```
85+
5686
## Create a function to transliterate text
5787

58-
Within the `Program` class, create a function called `TransliterateText`. This class encapsulates the code used to call the Transliterate resource and prints the result to console.
88+
Within the `Program` class, create an asynchronous function called `TransliterateTextRequest`. This function takes four arguments: `subscriptionKey`, `host`, `route`, and `inputText`.
5989

6090
```csharp
61-
static void TransliterateText()
91+
static public async Task TransliterateTextRequest(string subscriptionKey, string host, string route, string inputText)
6292
{
6393
/*
6494
* The code for your call to the translation service will be added to this
@@ -67,20 +97,12 @@ static void TransliterateText()
6797
}
6898
```
6999

70-
## Set the subscription key, host name, and path
100+
## Serialize the translation request
71101

72-
Add these lines to the `TransliterateText` function. You'll notice that along with the `api-version`, two additional parameters have been appended to the `route`. These parameters are used to set the input language, and the scripts for transliteration. In this sample, it's set to Japanese (`jpan`) and Latin (`latn`). Make sure you update the subscription key value.
102+
Next, we need to create and serialize the JSON object that includes the text you want to translate. Keep in mind, you can pass more than one object in the `body`.
73103

74104
```csharp
75-
string host = "https://api.cognitive.microsofttranslator.com";
76-
string route = "/transliterate?api-version=3.0&language=ja&fromScript=jpan&toScript=latn";
77-
string subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
78-
```
79-
80-
Next, we need to create and serialize the JSON object that includes the text you want to transliterate. Keep in mind, you can pass more than one object in the `body` array.
81-
82-
```csharp
83-
System.Object[] body = new System.Object[] { new { Text = @"こんにちは" } };
105+
object[] body = new object[] { new { Text = inputText } };
84106
var requestBody = JsonConvert.SerializeObject(body);
85107
```
86108

@@ -110,36 +132,48 @@ Inside the `HttpRequestMessage` you'll:
110132
Add this code to the `HttpRequestMessage`:
111133

112134
```csharp
113-
// Set the method to POST
135+
// Build the request.
136+
// Set the method to Post.
114137
request.Method = HttpMethod.Post;
115-
116-
// Construct the full URI
138+
// Construct the URI and add headers.
117139
request.RequestUri = new Uri(host + route);
118-
119-
// Add the serialized JSON object to your request
120140
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
121-
122-
// Add the authorization header
123141
request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
124142

125-
// Send request, get response
126-
var response = client.SendAsync(request).Result;
127-
var jsonResponse = response.Content.ReadAsStringAsync().Result;
128-
129-
// Print the response
130-
Console.WriteLine(jsonResponse);
131-
Console.WriteLine("Press any key to continue.");
143+
// Send the request and get response.
144+
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
145+
// Read response as a string.
146+
string result = await response.Content.ReadAsStringAsync();
147+
// Deserialize the response using the classes created earlier.
148+
TransliterationResult[] deserializedOutput = JsonConvert.DeserializeObject<TransliterationResult[]>(result);
149+
// Iterate over the deserialized results.
150+
foreach (TransliterationResult o in deserializedOutput)
151+
{
152+
Console.WriteLine("Transliterated to {0} script: {1}", o.Script, o.Text);
153+
}
132154
```
133155

134156
## Put it all together
135157

136-
The last step is to call `TransliterateText()` in the `Main` function. Locate `static void Main(string[] args)` and add these lines:
158+
The last step is to call `TransliterateTextRequest()` in the `Main` function. In this sample, we're transliterating from Japanese to latin script. Locate `static void Main(string[] args)` and add replace it with this code:
137159

138160
```csharp
139-
TransliterateText();
140-
Console.ReadLine();
161+
static async Task Main(string[] args)
162+
{
163+
// This is our main function.
164+
// Output languages are defined in the route.
165+
// For a complete list of options, see API reference.
166+
// https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-transliterate
167+
string subscriptionKey = "YOUR_TRANSLATOR_TEXT_KEY_GOES_HERE";
168+
string host = "https://api.cognitive.microsofttranslator.com";
169+
string route = "/transliterate?api-version=3.0&language=ja&fromScript=jpan&toScript=latn";
170+
string textToTransliterate = @"こんにちは";
171+
await TransliterateTextRequest(subscriptionKey, host, route, textToTransliterate);
172+
}
141173
```
142174

175+
You'll notice that in `Main`, you're declaring `subscriptionKey`, `host`, `route`, and the script to transliterate `textToTransliterate`.
176+
143177
## Run the sample app
144178

145179
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:
@@ -150,6 +184,14 @@ dotnet run
150184

151185
## Sample response
152186

187+
After you run the sample, you should see the following printed to terminal:
188+
189+
```bash
190+
Transliterated to latn script: Kon\'nichiwa
191+
```
192+
193+
This message is built from the raw JSON, which will look like this:
194+
153195
```json
154196
[
155197
{

0 commit comments

Comments
 (0)