Skip to content

Commit

Permalink
Merge pull request #6 from tilde-nlp/feature/language_directions
Browse files Browse the repository at this point in the history
Add language direction option
  • Loading branch information
gunpuz authored Aug 23, 2024
2 parents c515b3c + e27923c commit 41bf2b3
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Tilde.Translation.Example.Dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

await tester.TranslateTextAsync();
await tester.TranslateDocumentAsync("./Document/ExampleDocument.txt", "./Document/ExampleDocumentResult.txt");
await tester.GetEnginesAsync();
await tester.GetEnginesAsync();await tester.GetLanguageDirectionsAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Tilde.Translation.Example.Framework/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ internal class Program
static async Task Main(string[] args)
{
var apiKey = "";
var tester = new Tilde.Translation.Example.Lib.TranslateTester(apiKey);
var tester = new Lib.TranslateTester(apiKey);

await tester.TranslateTextAsync();
await tester.TranslateDocumentAsync("./Document/ExampleDocument.txt", "./Document/ExampleDocumentResult.txt");
await tester.GetEnginesAsync();
await tester.GetLanguageDirectionsAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Tilde.Translation\Tilde.Translation.csproj" />
</ItemGroup>
Expand Down
18 changes: 17 additions & 1 deletion Tilde.Translation.Example.Lib/TranslateTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public TranslateTester(string apiKey, string serverUrl = "https://translate.tild
AppInfo = new AppInfo()
{
AppName = "my-app",
AppVersion = "1.0.0"
AppVersion = "1.0.0",
AppId = "Tilde|test"
},
ServerUrl = serverUrl
};
Expand All @@ -31,6 +32,7 @@ public TranslateTester(string apiKey, string serverUrl = "https://translate.tild

public async Task TranslateTextAsync()
{
Console.WriteLine("Translating text");
try
{
var translation = await translator.TranslateTextAsync("First sentence", "en", "lv");
Expand All @@ -43,10 +45,14 @@ public async Task TranslateTextAsync()
{
Console.WriteLine(ex.Message);
}

Console.WriteLine("Text translation completed");
}

public async Task TranslateDocumentAsync(string sourceDocumentPath, string targetDocumentPath)
{
Console.WriteLine("Translating document");

// Specify file to translate
var sourceDocument = new FileInfo(sourceDocumentPath);

Expand All @@ -63,6 +69,8 @@ public async Task TranslateDocumentAsync(string sourceDocumentPath, string targe
var fileStream = File.Create(targetDocumentPath);
await translator.TranslateDocumentResultAsync(documentHandle, fileStream);
fileStream.Close();

Console.WriteLine("Document translation completed");
}

public async Task GetEnginesAsync()
Expand All @@ -72,5 +80,13 @@ public async Task GetEnginesAsync()
Console.WriteLine(engines.First().SourceLanguages.First()); // "en"
Console.WriteLine(engines.First().TargetLanguages.First()); // "lv"
}

public async Task GetLanguageDirectionsAsync()
{
await foreach (var languageDirection in translator.GetLanguageDirections())
{
Console.WriteLine($"{languageDirection.SourceLanguage} -> {languageDirection.TargetLanguage} | {languageDirection.EngineId} ({languageDirection.EngineName})");
}
}
}
}
45 changes: 45 additions & 0 deletions Tilde.Translation/Models/Engine/LanguageDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 Tilde SIA (https://tilde.ai/)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
using Tilde.Translation.Enums.Engine;

namespace Tilde.Translation.Models.Engine
{
public class LanguageDirection

Check warning on line 8 in Tilde.Translation/Models/Engine/LanguageDirection.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LanguageDirection'
{
/// <summary>
/// Engine id
/// </summary>
public Guid EngineId { get; internal set; }

/// <summary>
/// Engine name
/// </summary>
public string EngineName { get; internal set; }

Check warning on line 18 in Tilde.Translation/Models/Engine/LanguageDirection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'EngineName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 18 in Tilde.Translation/Models/Engine/LanguageDirection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'EngineName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Source language code
/// </summary>
public string SourceLanguage { get; internal set; }

Check warning on line 23 in Tilde.Translation/Models/Engine/LanguageDirection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'SourceLanguage' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Target language code
/// </summary>
public string TargetLanguage { get; internal set; }

Check warning on line 28 in Tilde.Translation/Models/Engine/LanguageDirection.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TargetLanguage' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Domain
/// </summary>
public string? Domain { get; internal set; }

/// <summary>
/// Supports term collections
/// </summary>
public bool SupportsTermCollections { get; internal set; }

/// <summary>
/// Engine vendor
/// </summary>
public Vendor EngineVendor { get; internal set; }
}
}
4 changes: 2 additions & 2 deletions Tilde.Translation/Tilde.Translation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>Official dotnet library for the Tilde translation API</Description>
<PackageTags>tilde;translation;translator;translate;api;i18n;language;</PackageTags>
<!-- Change only <Version> tag, other tags will be autogenerated on build -->
<Version>1.0.11.0</Version>
<Version>1.1.0.0</Version>
<TargetFrameworks>net6.0;net7.0;net8.0;netstandard2.0;netstandard2.1;net462;net472;net48;net481;</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down Expand Up @@ -44,7 +44,7 @@
<None Include="../icon.png" Pack="true" PackagePath="" />
</ItemGroup>
<Import Project="$(MSBuildProjectDirectory)/BuildTargets/UpdateVersion.targets" />
<Target Name="BeforeBuild2" BeforeTargets="BeforeBuild">
<Target Condition=" '$(Configuration)' != 'Debug' " Name="BeforeBuild2" BeforeTargets="BeforeBuild">
<Message Importance="High" Text="Running Update version script" />
<UpdateVersion />
<Message Importance="High" Text="Version update completed" />
Expand Down
73 changes: 73 additions & 0 deletions Tilde.Translation/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using Tilde.Translation.Exceptions;
using Tilde.Translation.Internal;
using Tilde.Translation.Models;
using Tilde.Translation.Models.Document;
using Tilde.Translation.Models.Engine;

namespace Tilde.Translation
{
Expand Down Expand Up @@ -467,6 +469,77 @@ public async Task TranslateDocumentResultAsync(
return result!.Engines;
}

/// <summary>
/// Get available language directions
/// </summary>
/// <param name="sourceLanguageCode"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async IAsyncEnumerable<LanguageDirection> GetLanguageDirections(
string? sourceLanguageCode = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
var engines = await GetEnginesAsync(cancellationToken);

foreach (var engine in engines)
{
if (engine.Domains != null && engine.Domains.Count > 0)
{
foreach (var domain in engine.Domains)
{
foreach (var sourceLanguage in domain.Value.Languages)
{
// Filter by source language
if (sourceLanguageCode != null && sourceLanguage.Key != sourceLanguageCode)
{
continue;
}

foreach (var targetLanguage in sourceLanguage.Value)
{
yield return new LanguageDirection()
{
Domain = engine.Domain,
EngineVendor = engine.EngineVendor,
EngineId = engine.Id,
EngineName = engine.Name,
SupportsTermCollections = engine.SupportsTermCollections,
SourceLanguage = sourceLanguage.Key,
TargetLanguage = targetLanguage
};
}
}
}
}
else
{
foreach (var sourceLanguage in engine.SourceLanguages)
{
// Filter by source language
if (sourceLanguageCode != null && sourceLanguage != sourceLanguageCode)
{
continue;
}

foreach (var targetLanguage in engine.TargetLanguages)
{
yield return new LanguageDirection()
{
Domain = engine.Domain,
EngineVendor = engine.EngineVendor,
EngineId = engine.Id,
EngineName = engine.Name,
SupportsTermCollections = engine.SupportsTermCollections,
SourceLanguage = sourceLanguage,
TargetLanguage = targetLanguage
};
}
}
}
}
}

/// <summary>
/// Get library version
/// </summary>
Expand Down

0 comments on commit 41bf2b3

Please sign in to comment.