-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51db2f5
commit aabf2ed
Showing
32 changed files
with
834 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Mime; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Apps.App.Api; | ||
using Apps.App.Invocables; | ||
using Apps.Systran.Models.Request; | ||
using Apps.Systran.Models.Response; | ||
using Blackbird.Applications.Sdk.Common; | ||
using Blackbird.Applications.Sdk.Common.Actions; | ||
using Blackbird.Applications.Sdk.Common.Exceptions; | ||
using Blackbird.Applications.Sdk.Common.Files; | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces; | ||
using RestSharp; | ||
|
||
namespace Apps.Systran.Actions | ||
{ | ||
[ActionList] | ||
public class CorpusActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient) : SystranInvocable(invocationContext) | ||
{ | ||
|
||
[Action("Export corpus", Description = "Export a corpus file by its ID")] | ||
public async Task<FileReference> ExportCorpus([ActionParameter] ExportCorpusParameters parameters) | ||
{ | ||
var request = new SystranRequest($"/resources/corpus/export", RestSharp.Method.Get); | ||
request.AddQueryParameter("corpusId", parameters.CorpusId); | ||
|
||
var response = await Client.ExecuteAsync(request, cancellationToken: default); | ||
if (!response.IsSuccessful || response.RawBytes == null) | ||
throw new Exception($"Failed to export corpus. Status: {response.StatusCode}, Error: {response.ErrorMessage}"); | ||
|
||
|
||
await using var memoryStream = new MemoryStream(response.RawBytes); | ||
|
||
var fileReference = await fileManagementClient.UploadAsync( | ||
memoryStream, | ||
response.ContentType, | ||
parameters.CorpusId | ||
); | ||
|
||
return fileReference; | ||
} | ||
|
||
[Action("Import corpus from TMX file", Description = "Add a new corpus from an existing corpus.")] | ||
public async Task<ImportCorpusResponse> ImportCorpus([ActionParameter] ImportCorpusParameters parameters) | ||
{ | ||
var request = new SystranRequest($"/resources/corpus/import", RestSharp.Method.Post); | ||
|
||
request.AddQueryParameter("name", parameters.Name); | ||
request.AddQueryParameter("format", parameters.Format); | ||
|
||
if (parameters.Tag != null && parameters.Tag.Any()) | ||
{ | ||
foreach (var tag in parameters.Tag) | ||
{ | ||
request.AddQueryParameter("tag", tag); | ||
} | ||
} | ||
|
||
if (!string.IsNullOrEmpty(parameters.Input)) | ||
{ | ||
request.AddParameter("input", parameters.Input); | ||
} | ||
else if (parameters.InputFile != null) | ||
{ | ||
var fileBytes = await fileManagementClient.DownloadAsync(parameters.InputFile); | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
await fileBytes.CopyToAsync(memoryStream); | ||
request.AddFile("inputFile", memoryStream.ToArray(), parameters.InputFile.Name, MediaTypeNames.Application.Octet); | ||
} | ||
} | ||
else | ||
{ | ||
throw new PluginMisconfigurationException("Either input or inputFile must be provided"); | ||
} | ||
|
||
var response = await Client.ExecuteAsync<ImportCorpusResponse>(request); | ||
|
||
return response.Data; | ||
} | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Apps.App.Api; | ||
using Apps.App.Invocables; | ||
using Apps.Systran.Models.Request; | ||
using Apps.Systran.Models.Response; | ||
using Blackbird.Applications.Sdk.Common; | ||
using Blackbird.Applications.Sdk.Common.Actions; | ||
using Blackbird.Applications.Sdk.Common.Files; | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
using Blackbird.Applications.Sdk.Glossaries.Utils.Converters; | ||
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces; | ||
using RestSharp; | ||
|
||
namespace Apps.Systran.Actions | ||
{ | ||
[ActionList] | ||
public class DictionaryActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient) : SystranInvocable(invocationContext) | ||
{ | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Apps.App.Api; | ||
using Apps.App.Invocables; | ||
using Blackbird.Applications.Sdk.Common.Actions; | ||
using Blackbird.Applications.Sdk.Common; | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
using RestSharp; | ||
using Apps.Systran.Models.Request; | ||
using Apps.Systran.Models.Response; | ||
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces; | ||
|
||
namespace Apps.Systran.Actions | ||
{ | ||
[ActionList] | ||
public class TranslateFileActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient) : SystranInvocable(invocationContext) | ||
{ | ||
|
||
[Action("Translate file", Description = "Translate a file from source language to target language")] | ||
public async Task<TranslateFileResponse> TranslateFile([ActionParameter] TranslateFileRequest input) | ||
{ | ||
var baseUrl = InvocationContext.AuthenticationCredentialsProviders | ||
.First(p => p.KeyName == "url").Value; | ||
|
||
var request = new SystranRequest("/translation/file/translate", Method.Post) | ||
{ | ||
AlwaysMultipartFormData = true | ||
}; | ||
|
||
if (!string.IsNullOrEmpty(input.Source)) | ||
request.AddQueryParameter("source", input.Source); | ||
if (!string.IsNullOrEmpty(input.Target)) | ||
request.AddQueryParameter("target", input.Target); | ||
if (!string.IsNullOrEmpty(input.Format)) | ||
request.AddQueryParameter("format", input.Format); | ||
if (!string.IsNullOrEmpty(input.Profile)) | ||
request.AddQueryParameter("profile", input.Profile); | ||
if (input.WithInfo == true) | ||
request.AddQueryParameter("withInfo", "true"); | ||
if (input.WithSource == true) | ||
request.AddQueryParameter("withSource", "true"); | ||
if (input.WithAnnotations == true) | ||
request.AddQueryParameter("withAnnotations", "true"); | ||
if (input.Async == true) | ||
request.AddQueryParameter("async", "true"); | ||
if (!string.IsNullOrEmpty(input.Owner)) | ||
request.AddQueryParameter("owner", input.Owner); | ||
if (!string.IsNullOrEmpty(input.Domain)) | ||
request.AddQueryParameter("domain", input.Domain); | ||
if (!string.IsNullOrEmpty(input.Size)) | ||
request.AddQueryParameter("size", input.Size); | ||
|
||
|
||
|
||
var response = await Client.ExecuteWithErrorHandling<TranslateFileResponse>(request); | ||
|
||
return response; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Apps.App.Api; | ||
using Apps.App.Invocables; | ||
using Apps.Systran.Models.Response; | ||
using Blackbird.Applications.Sdk.Common.Dynamic; | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
|
||
namespace Apps.Systran.DataSourceHandlers | ||
{ | ||
public class CorporaDataHandler : SystranInvocable, IAsyncDataSourceItemHandler | ||
{ | ||
public CorporaDataHandler(InvocationContext invocationContext) : base(invocationContext) { } | ||
|
||
public async Task<IEnumerable<DataSourceItem>> GetDataAsync(DataSourceContext context, CancellationToken cancellationToken) | ||
{ | ||
var request = new SystranRequest("/resources/corpus/list", RestSharp.Method.Get); | ||
var response = await Client.ExecuteWithErrorHandling<CorporaListResponse>(request); | ||
|
||
return response.Files | ||
.Where(corpus => string.IsNullOrWhiteSpace(context.SearchString) || | ||
corpus.Name.Contains(context.SearchString, StringComparison.OrdinalIgnoreCase)) | ||
.Take(50) | ||
.Select(corpus => new DataSourceItem( | ||
corpus.Id, | ||
$"{corpus.Name} ({corpus.SourceLang} -> {string.Join(", ", corpus.TargetLangs ?? Enumerable.Empty<string>())})" | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Apps.App.Api; | ||
using Apps.App.Invocables; | ||
using Apps.Systran.Models.Response; | ||
using Blackbird.Applications.Sdk.Common.Dynamic; | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
|
||
namespace Apps.Systran.DataSourceHandlers | ||
{ | ||
public class DictionaryDataHandler : SystranInvocable, IAsyncDataSourceItemHandler | ||
{ | ||
public DictionaryDataHandler(InvocationContext invocationContext):base(invocationContext) { } | ||
public async Task<IEnumerable<DataSourceItem>> GetDataAsync(DataSourceContext context, CancellationToken cancellationToken) | ||
{ | ||
var request = new SystranRequest("/resources/dictionary/list", RestSharp.Method.Post); | ||
var response = await Client.ExecuteWithErrorHandling<DictionaryDataHandlerResponse>(request); | ||
|
||
return response.Dictionaries | ||
.Where(dictionary => string.IsNullOrWhiteSpace(context.SearchString) || | ||
dictionary.Name.Contains(context.SearchString, StringComparison.OrdinalIgnoreCase)) | ||
.Take(50) | ||
.Select(dictionary => new DataSourceItem( | ||
dictionary.Id, | ||
$"{dictionary.Name} ({dictionary.SourceLang} -> {dictionary.TargetLangs})" | ||
)); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Apps.App/DataSourceHandlers/EnumDataHandlers/CorpusFormatDataHandler .cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Blackbird.Applications.Sdk.Common.Dictionaries; | ||
using Blackbird.Applications.Sdk.Common.Dynamic; | ||
|
||
namespace Apps.Systran.DataSourceHandlers.EnumDataHandlers | ||
{ | ||
public class CorpusFormatDataHandler : IStaticDataSourceItemHandler | ||
{ | ||
protected Dictionary<string, string> EnumValues => new() | ||
{ | ||
{ "application/x-tmx+xml", "TMX XML" }, | ||
{ "text/bitext", "Bitext" } | ||
}; | ||
|
||
public IEnumerable<DataSourceItem> GetData() | ||
{ | ||
return EnumValues.Select(item => new DataSourceItem | ||
{ | ||
Value = item.Key, | ||
DisplayName = item.Value | ||
}); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Apps.App/DataSourceHandlers/EnumDataHandlers/DictionaryTypeDataHandler .cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Blackbird.Applications.Sdk.Common.Dictionaries; | ||
using Blackbird.Applications.Sdk.Common.Dynamic; | ||
|
||
namespace Apps.Systran.DataSourceHandlers.EnumDataHandlers | ||
{ | ||
public class DictionaryTypeDataHandler : IStaticDataSourceItemHandler | ||
{ | ||
protected Dictionary<string, string> EnumValues => new() | ||
{ | ||
{"UD", "User Dictionary"}, | ||
{"NORM", "Normalization Dictionary"} | ||
}; | ||
|
||
public IEnumerable<DataSourceItem> GetData() | ||
{ | ||
return EnumValues.Select(item => new DataSourceItem | ||
{ | ||
Value = item.Key, | ||
DisplayName = item.Value | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.