Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle corrupt SMT models #138

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/SIL.Machine.Tool/CorpusCommandSpecBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using McMaster.Extensions.CommandLineUtils;
using SIL.Machine.Corpora;

Expand Down
3 changes: 1 addition & 2 deletions src/SIL.Machine.Tool/ParallelCorpusCommandSpec.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.IO;
using McMaster.Extensions.CommandLineUtils;
using SIL.Machine.Corpora;
using SIL.Machine.Tokenization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Import Project="../AssemblyInfo.props" />

<ItemGroup>
<PackageReference Include="Thot" Version="3.4.1" />
<PackageReference Include="Thot" Version="3.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 8 additions & 3 deletions src/SIL.Machine.Translation.Thot/Thot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,10 @@ IReadOnlyList<string> targetSegment
public static IntPtr LoadSmtModel(ThotWordAlignmentModelType alignmentModelType, ThotSmtParameters parameters)
{
IntPtr handle = smtModel_create(GetAlignmentModelType(alignmentModelType, incremental: true));
smtModel_loadTranslationModel(handle, parameters.TranslationModelFileNamePrefix);
smtModel_loadLanguageModel(handle, parameters.LanguageModelFileNamePrefix);
if (!smtModel_loadTranslationModel(handle, parameters.TranslationModelFileNamePrefix))
throw new InvalidOperationException("Unable to load translation model.");
if (!smtModel_loadLanguageModel(handle, parameters.LanguageModelFileNamePrefix))
throw new InvalidOperationException("Unable to load language model.");
smtModel_setNonMonotonicity(handle, parameters.ModelNonMonotonicity);
smtModel_setW(handle, parameters.ModelW);
smtModel_setA(handle, parameters.ModelA);
Expand Down Expand Up @@ -650,7 +652,10 @@ public static IntPtr CreateAlignmentModel(ThotWordAlignmentModelType type, IntPt

public static IntPtr OpenAlignmentModel(ThotWordAlignmentModelType type, string prefFileName)
{
return swAlignModel_open(GetAlignmentModelType(type, incremental: false), prefFileName);
IntPtr handle = swAlignModel_open(GetAlignmentModelType(type, incremental: false), prefFileName);
if (handle == IntPtr.Zero)
throw new InvalidOperationException("Unable to load word alignment model.");
return handle;
}

public static AlignmentModelType GetAlignmentModelType(ThotWordAlignmentModelType type, bool incremental)
Expand Down
10 changes: 9 additions & 1 deletion src/SIL.Machine.Translation.Thot/ThotSmtModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -31,6 +32,12 @@ public ThotSmtModel(ThotWordAlignmentModelType wordAlignmentModelType, string cf

public ThotSmtModel(ThotWordAlignmentModelType wordAlignmentModelType, ThotSmtParameters parameters)
{
if (!File.Exists(parameters.TranslationModelFileNamePrefix + ".ttable"))
throw new FileNotFoundException("The translation model could not be found.");

if (!File.Exists(parameters.LanguageModelFileNamePrefix))
throw new FileNotFoundException("The language model could not be found.");

_decoderPool = new ObjectPool<ThotSmtDecoder>(MaxDecoderPoolSize, () => new ThotSmtDecoder(this));

Parameters = parameters;
Expand Down Expand Up @@ -605,7 +612,8 @@ protected override void DisposeManagedResources()

protected override void DisposeUnmanagedResources()
{
Thot.smtModel_close(_handle);
if (_handle != IntPtr.Zero)
Thot.smtModel_close(_handle);
}

private double GetWordAlignmentScore(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using SIL.Machine.Utils;

namespace SIL.Machine.Translation.Thot
{
Expand Down Expand Up @@ -165,5 +166,14 @@ public void GetAvgTranslationScore_Symmetrized()
double score = model.GetAvgTranslationScore(sourceSegment, targetSegment, alignment);
Assert.That(score, Is.EqualTo(0.36).Within(0.01));
}

[Test]
public void Constructor_ModelCorrupted()
{
using var tempDir = new TempDirectory("ThotFastAlignWordAlignmentModelTests");
string modelPrefix = Path.Combine(tempDir.Path, "src_trg_invswm");
File.WriteAllText(modelPrefix + ".src", "corrupted");
Assert.Throws<InvalidOperationException>(() => new ThotFastAlignWordAlignmentModel(modelPrefix));
}
}
}
40 changes: 40 additions & 0 deletions tests/SIL.Machine.Translation.Thot.Tests/ThotSmtModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using SIL.Machine.Utils;

namespace SIL.Machine.Translation.Thot
{
Expand Down Expand Up @@ -189,6 +190,45 @@ public async Task GetWordGraphAsync_EmptySegment_FastAlign()
Assert.That(wordGraph.IsEmpty, Is.True);
}

[Test]
public void Constructor_ModelDoesNotExist()
{
Assert.Throws<FileNotFoundException>(
() =>
new ThotSmtModel(
ThotWordAlignmentModelType.Hmm,
new ThotSmtParameters
{
TranslationModelFileNamePrefix = "does-not-exist",
LanguageModelFileNamePrefix = "does-not-exist"
}
)
);
}

[Test]
public void Constructor_ModelCorrupted()
{
using var tempDir = new TempDirectory("ThotSmtModelTests");
string tmDir = Path.Combine(tempDir.Path, "tm");
Directory.CreateDirectory(tmDir);
File.WriteAllText(Path.Combine(tmDir, "src_trg.ttable"), "corrupted");
string lmDir = Path.Combine(tempDir.Path, "lm");
Directory.CreateDirectory(lmDir);
File.WriteAllText(Path.Combine(lmDir, "trg.lm"), "corrupted");
Assert.Throws<InvalidOperationException>(
() =>
new ThotSmtModel(
ThotWordAlignmentModelType.Hmm,
new ThotSmtParameters
{
TranslationModelFileNamePrefix = Path.Combine(tmDir, "src_trg"),
LanguageModelFileNamePrefix = Path.Combine(lmDir, "trg.lm")
}
)
);
}

private static ThotSmtModel CreateHmmModel()
{
return new ThotSmtModel(ThotWordAlignmentModelType.Hmm, TestHelpers.ToyCorpusHmmConfigFileName);
Expand Down
Loading