-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathThotFastAlignWordAlignmentModel.cs
More file actions
59 lines (53 loc) · 2.18 KB
/
Copy pathThotFastAlignWordAlignmentModel.cs
File metadata and controls
59 lines (53 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections.Generic;
using SIL.Machine.Corpora;
namespace SIL.Machine.Translation.Thot
{
public class ThotFastAlignWordAlignmentModel : ThotWordAlignmentModel, IIbm2WordAlignmentModel
{
public ThotFastAlignWordAlignmentModel() { }
public ThotFastAlignWordAlignmentModel(string prefFileName, bool createNew = false)
: base(prefFileName, createNew) { }
public override ThotWordAlignmentModelType Type => ThotWordAlignmentModelType.FastAlign;
public double GetAlignmentProbability(int sourceLen, int sourceIndex, int targetLen, int targetIndex)
{
CheckDisposed();
if (targetIndex == -1)
return 0;
// add 1 to convert the specified indices to Thot position indices, which are 1-based
return Thot.swAlignModel_getFastAlignAlignmentProbability(
Handle,
(uint)(targetIndex + 1),
(uint)sourceLen,
(uint)targetLen,
(uint)(sourceIndex + 1)
);
}
public override void ComputeAlignedWordPairScores(
IReadOnlyList<string> sourceSegment,
IReadOnlyList<string> targetSegment,
IReadOnlyCollection<AlignedWordPair> wordPairs
)
{
foreach (AlignedWordPair wordPair in wordPairs)
{
if (wordPair.TargetIndex == -1)
{
wordPair.TranslationScore = 0;
wordPair.AlignmentScore = 0;
}
else
{
string sourceWord = wordPair.SourceIndex == -1 ? null : sourceSegment[wordPair.SourceIndex];
string targetWord = targetSegment[wordPair.TargetIndex];
wordPair.TranslationScore = GetTranslationProbability(sourceWord, targetWord);
wordPair.AlignmentScore = GetAlignmentProbability(
sourceSegment.Count,
wordPair.SourceIndex,
targetSegment.Count,
wordPair.TargetIndex
);
}
}
}
}
}