From 0bb066651cfa7acf0680bf7ea4245a1828835a76 Mon Sep 17 00:00:00 2001 From: RiabushenkoA Date: Fri, 28 Feb 2025 13:13:50 +0200 Subject: [PATCH] Modified action Create dictionary to use tsv file(SYSTRAN standart) --- Apps.Systran/Actions/DictionaryActions.cs | 35 +++++++++++++++++++++-- Apps.Systran/Apps.Systran.csproj | 2 +- Tests.Systran/DictionaryTests.cs | 4 +-- Tests.Systran/TestFiles/Input/tsv.txt | 27 +++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 Tests.Systran/TestFiles/Input/tsv.txt diff --git a/Apps.Systran/Actions/DictionaryActions.cs b/Apps.Systran/Actions/DictionaryActions.cs index d70ec43..67245e3 100644 --- a/Apps.Systran/Actions/DictionaryActions.cs +++ b/Apps.Systran/Actions/DictionaryActions.cs @@ -21,7 +21,7 @@ namespace Apps.Systran.Actions [ActionList] public class DictionaryActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient) : SystranInvocable(invocationContext) { - [Action("Create dictionary", Description = "Create a dictionary and populate it using a TBX file")] + [Action("Create dictionary", Description = "Create a dictionary and populate it using a TBX or TSV(SYSTRAN standart) file")] public async Task CreateDictionary( [ActionParameter] CreateDictionaryParameters parameters, [ActionParameter] TranslateLanguagesOptions options) @@ -44,7 +44,37 @@ public async Task CreateDictionary( var dictionaryId = createResponse.Added.Id; - var systranFormattedContent = await ConvertTbxToSystranFormat(parameters.TbxFile, options.Source, options.Target); + string systranFormattedContent; + await using var originalStream = await fileManagementClient.DownloadAsync(parameters.TbxFile); + using var memoryStream = new MemoryStream(); + await originalStream.CopyToAsync(memoryStream); + + bool isTbx = false; + memoryStream.Position = 0; + try + { + var xDoc = XDocument.Load(memoryStream); + var root = xDoc.Root; + if (root != null && string.Equals(root.Name.LocalName, "tbx", StringComparison.OrdinalIgnoreCase)) + { + isTbx = true; + } + } + catch + { + isTbx = false; + } + + if (isTbx) + { + systranFormattedContent = await ConvertTbxToSystranFormat(parameters.TbxFile, options.Source, options.Target); + } + else + { + memoryStream.Position = 0; + using var reader = new StreamReader(memoryStream); + systranFormattedContent = await reader.ReadToEndAsync(); + } var importEntriesRequest = new SystranRequest("/resources/dictionary/entry/import", Method.Post); importEntriesRequest.AddQueryParameter("dictionaryId", dictionaryId); @@ -60,7 +90,6 @@ public async Task CreateDictionary( } var importResponse = await Client.ExecuteWithErrorHandling(importEntriesRequest); - if (importResponse.Error != null) throw new PluginApplicationException($"Failed to import entries: {importResponse.Error.Message}"); diff --git a/Apps.Systran/Apps.Systran.csproj b/Apps.Systran/Apps.Systran.csproj index 0a65808..611c59e 100644 --- a/Apps.Systran/Apps.Systran.csproj +++ b/Apps.Systran/Apps.Systran.csproj @@ -6,7 +6,7 @@ enable SYSTRAN [Beta] SYSTRAN Translate API is built around a RESTful API and can be used in all types of applications.This API Reference is intended for developers who want to write applications that can interact with the SYSTRAN Translate API. You can integrate our translation technology directly in your internal or external applications to make them multilingual or translate texts and files by using the SYSTRAN Translate API. - 1.0.6 + 1.0.7 Apps.Systran diff --git a/Tests.Systran/DictionaryTests.cs b/Tests.Systran/DictionaryTests.cs index 39a16f7..239fc78 100644 --- a/Tests.Systran/DictionaryTests.cs +++ b/Tests.Systran/DictionaryTests.cs @@ -12,7 +12,7 @@ public class DictionaryTests : TestBase public async Task CreateDictionary_ValidFile_ReturnsResponse() { // Arrange - var fileReference = await FileManager.UploadTestFileAsync("test.tbx"); + var fileReference = await FileManager.UploadTestFileAsync("tsv.txt"); var parameters = new CreateDictionaryParameters { @@ -25,7 +25,7 @@ public async Task CreateDictionary_ValidFile_ReturnsResponse() var options = new TranslateLanguagesOptions { - Source = "en-", + Source = "en", Target = "es" }; diff --git a/Tests.Systran/TestFiles/Input/tsv.txt b/Tests.Systran/TestFiles/Input/tsv.txt new file mode 100644 index 0000000..815befd --- /dev/null +++ b/Tests.Systran/TestFiles/Input/tsv.txt @@ -0,0 +1,27 @@ +#ENCODING=UTF-8 +#SUMMARY=Patricia_UD_ENFR +#DESCRIPTION=Patricia_UD_ENFR +#MULTI +#EN UPOS FR PRIORITY_FR COMMENTS_FR +black cat noun panthère 4 +to feel under the weather noun ne pas se sentir bien 4 +a piece of cake noun Quelque chose de très facile 4 +to cross one’s fingers verb croiser les doigts 4 +take it easy verb détends-toi 4 +go round in circles noun tourner en rond 4 +to sleep on it noun la nuit porte conseil 4 +to know it like the back of one’s hand verb connaître quelque chose sur le bout des doigts 4 +once in a blue moon noun tous les 36 du mois 4 +out of the blue adjective sans crier gare 4 +as easy as a duck soup noun un jeu d’enfant 4 +the cream of the crop noun la crème de la crème 4 +un early bird noun un lève tôt 4 +a cock and bull story noun une histoire à dormir debout 4 +to create a storm in a teacup verb en faire tout un fromage 4 +that rings me a bell noun cela me dit quelque chose 4 +we are in deep water noun on est dans le pétrin 4 +as good as gold noun bien élevé 4 +to spill the beans verb vendre la mèche 4 +to get cold feet verb avoir le trac 4 +to work my fingers to the bone verb travailler d'arrache-pied 4 +all ears noun tout ouïe 4