Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44902db

Browse files
committed
add endpoint
1 parent 95e7d5e commit 44902db

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Azure;
2+
using Azure.AI.TextAnalytics;
3+
4+
namespace RichEditOpenAIWebApi.BusinessObjects
5+
{
6+
public class AzureAILanguageHelper
7+
{
8+
private TextAnalyticsClient client;
9+
internal AzureAILanguageHelper(string key, Uri endpoint)
10+
{
11+
AzureKeyCredential azureCredential = new AzureKeyCredential(key);
12+
client = new TextAnalyticsClient(endpoint, azureCredential);
13+
}
14+
internal async Task<string> DetectTextLanguage(string text)
15+
{
16+
DetectedLanguage detectedLanguage = await client.DetectLanguageAsync(text);
17+
return detectedLanguage.Iso6391Name.Replace('_', '-');
18+
}
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Azure;
2+
using Azure.AI.Translation.Text;
3+
4+
namespace RichEditOpenAIWebApi.BusinessObjects
5+
{
6+
public class AzureAITranslationHelper
7+
{
8+
TextTranslationClient client;
9+
internal AzureAITranslationHelper(string key, Uri endpoint, string region = "global")
10+
{
11+
AzureKeyCredential azureCredential = new AzureKeyCredential(key);
12+
client = new TextTranslationClient(azureCredential, endpoint, region);
13+
}
14+
internal async Task<string> TranslateText(string text, string sourceLanguage, string targetLanguage)
15+
{
16+
Response<IReadOnlyList<TranslatedTextItem>> response = await client.TranslateAsync(targetLanguage, text, sourceLanguage);
17+
TranslatedTextItem translatedTextItem = response.Value.First();
18+
return translatedTextItem.Translations[0].Text;
19+
}
20+
}
21+
}

‎CS/Controllers/LanguageController.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using DevExpress.XtraRichEdit;
2+
using DevExpress.XtraRichEdit.API.Native;
3+
using Microsoft.AspNetCore.Mvc;
4+
using RichEditOpenAIWebApi.BusinessObjects;
5+
using Swashbuckle.AspNetCore.Annotations;
6+
using System.Globalization;
7+
using System.Net;
8+
9+
namespace RichEditOpenAIWebApi.Controllers
10+
{
11+
[ApiController]
12+
[Route("[controller]/[action]")]
13+
public class LanguageController : ControllerBase
14+
{
15+
// Insert your Azure key and end point for the Language Service
16+
string languageAzureKey = "";
17+
Uri languageEndPoint = new Uri("");
18+
// Insert your Azure key and end point for the Translation Service
19+
string translationAzureKey = "";
20+
Uri translationEndPoint = new Uri("");
21+
22+
[HttpPost]
23+
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
24+
public async Task<IActionResult> GenerateLanguageSettingsForParagraphs(IFormFile documentWithHyperlinks, [FromQuery] RichEditFormat outputFormat)
25+
{
26+
try
27+
{
28+
var languageHelper = new AzureAILanguageHelper(languageAzureKey, languageEndPoint);
29+
var translationHelper = new AzureAITranslationHelper(translationAzureKey, translationEndPoint, "westeurope");
30+
using (var server = new RichEditDocumentServer())
31+
{
32+
await RichEditHelper.LoadFile(server, documentWithHyperlinks);
33+
34+
server.IterateSubDocuments(async (document) =>
35+
{
36+
foreach (var paragraph in document.Paragraphs)
37+
{
38+
CharacterProperties cp = document.BeginUpdateCharacters(paragraph.Range);
39+
string paragraphText = document.GetText(paragraph.Range);
40+
if (cp.Language.Value.Latin == null && !string.IsNullOrWhiteSpace(paragraphText))
41+
{
42+
CultureInfo? culture = null;
43+
string language = languageHelper.DetectTextLanguage(paragraphText).Result;
44+
try
45+
{
46+
culture = new CultureInfo((language));
47+
}
48+
catch { }
49+
finally
50+
{
51+
if (culture != null)
52+
{
53+
cp.Language = new DevExpress.XtraRichEdit.Model.LangInfo(culture, null, null);
54+
if (language != "en")
55+
{
56+
Comment comment = document.Comments.Create(paragraph.Range, "Translator");
57+
SubDocument commentDoc = comment.BeginUpdate();
58+
string translatedText = translationHelper.TranslateText(paragraphText, language, "en").Result;
59+
commentDoc.InsertText(commentDoc.Range.Start, $"Delected Language: {language}\r\nTranslation (en): {translatedText}");
60+
comment.EndUpdate(commentDoc);
61+
}
62+
}
63+
}
64+
}
65+
document.EndUpdateCharacters(cp);
66+
}
67+
});
68+
Stream result = RichEditHelper.SaveDocument(server, outputFormat);
69+
string contentType = RichEditHelper.GetContentType(outputFormat);
70+
string outputStringFormat = outputFormat.ToString().ToLower();
71+
return File(result, contentType, $"result.{outputStringFormat}");
72+
}
73+
}
74+
catch (Exception e)
75+
{
76+
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
77+
}
78+
}
79+
}
80+
}

‎CS/RichEditOpenAIWebApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.17" />
1414
<PackageReference Include="Azure.AI.TextAnalytics" Version="5.3.0" />
15+
<PackageReference Include="Azure.AI.Translation.Text" Version="1.0.0" />
1516
<PackageReference Include="DevExpress.Document.Processor" Version="23.2.5" />
1617
<PackageReference Include="DevExpress.Drawing.Skia" Version="23.2.5" />
1718
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /