Skip to content

Commit

Permalink
Modified action Create dictionary to use tsv file(SYSTRAN standart)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiabushenkoA committed Feb 28, 2025
1 parent 023fc87 commit 0bb0666
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
35 changes: 32 additions & 3 deletions Apps.Systran/Actions/DictionaryActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateDictionaryResponse> CreateDictionary(
[ActionParameter] CreateDictionaryParameters parameters,
[ActionParameter] TranslateLanguagesOptions options)
Expand All @@ -44,7 +44,37 @@ public async Task<CreateDictionaryResponse> 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);
Expand All @@ -60,7 +90,6 @@ public async Task<CreateDictionaryResponse> CreateDictionary(
}

var importResponse = await Client.ExecuteWithErrorHandling<ImportResponse>(importEntriesRequest);

if (importResponse.Error != null)
throw new PluginApplicationException($"Failed to import entries: {importResponse.Error.Message}");

Expand Down
2 changes: 1 addition & 1 deletion Apps.Systran/Apps.Systran.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<Product>SYSTRAN [Beta]</Product>
<Description>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.</Description>
<Version>1.0.6</Version>
<Version>1.0.7</Version>

<AssemblyName>Apps.Systran</AssemblyName>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions Tests.Systran/DictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -25,7 +25,7 @@ public async Task CreateDictionary_ValidFile_ReturnsResponse()

var options = new TranslateLanguagesOptions
{
Source = "en-",
Source = "en",
Target = "es"
};

Expand Down
27 changes: 27 additions & 0 deletions Tests.Systran/TestFiles/Input/tsv.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0bb0666

Please sign in to comment.