Skip to content

Commit

Permalink
More reviewer-requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Nov 15, 2024
1 parent 2058d4d commit 6ab2faa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
12 changes: 4 additions & 8 deletions src/SIL.Machine/Corpora/CorporaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,14 @@ public static INParallelTextCorpus AlignMany(
return nParallelTextCorpus;
}

public static ITextCorpus ChooseRandom(
this IEnumerable<ITextCorpus> corpora,
IEnumerable<bool> allRows,
int seed
)
public static ITextCorpus ChooseRandom(this IEnumerable<ITextCorpus> corpora, int seed)
{
return new MergedTextCorpus(corpora, allRows.ToArray(), MergeRule.Random, seed);
return new MergedTextCorpus(corpora, MergeRule.Random, seed);
}

public static ITextCorpus ChooseFirst(this IEnumerable<ITextCorpus> corpora, IEnumerable<bool> allRows)
public static ITextCorpus ChooseFirst(this IEnumerable<ITextCorpus> corpora)
{
return new MergedTextCorpus(corpora, allRows.ToArray(), MergeRule.First, 0);
return new MergedTextCorpus(corpora, MergeRule.First);
}

#endregion
Expand Down
14 changes: 6 additions & 8 deletions src/SIL.Machine/Corpora/MergedTextCorpus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ public class MergedTextCorpus : TextCorpusBase

private readonly Random _random;

public MergedTextCorpus(
IEnumerable<ITextCorpus> corpora,
IEnumerable<bool> allRows,
MergeRule mergeRule,
int seed
)
public MergedTextCorpus(IEnumerable<ITextCorpus> corpora, MergeRule mergeRule, int? seed = null)
{
_corpus = new NParallelTextCorpus(corpora) { AllRows = allRows.ToList() };
_corpus = new NParallelTextCorpus(corpora) { AllRows = Enumerable.Repeat(true, corpora.Count()).ToArray() };
_mergeRule = mergeRule;
_random = new Random(seed);
if (seed != null)
_random = new Random(seed.Value);
else
_random = new Random();
}

public override IEnumerable<IText> Texts => _corpus.Corpora.SelectMany(c => c.Texts);
Expand Down
14 changes: 5 additions & 9 deletions tests/SIL.Machine.Tests/Corpora/CorporaExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void MergedCorpus_SelectFirst()
}
)
);
var mergedCorpus = new List<ITextCorpus> { corpus1, corpus2, corpus3 }.ChooseFirst([true, true, true]);
var mergedCorpus = new List<ITextCorpus> { corpus1, corpus2, corpus3 }.ChooseFirst();
var rows = mergedCorpus.ToArray();
Assert.That(rows, Has.Length.EqualTo(3), JsonSerializer.Serialize(rows));
Assert.That(rows[0].Text, Is.EqualTo("source 1 segment 1 ."));
Expand Down Expand Up @@ -138,7 +138,7 @@ public void MergedCorpus_SelectRandom_Seed123456()
}
)
);
var mergedCorpus = new List<ITextCorpus> { corpus1, corpus2, corpus3 }.ChooseRandom([true, true, true], 123456);
var mergedCorpus = new List<ITextCorpus> { corpus1, corpus2, corpus3 }.ChooseRandom(123456);
var rows = mergedCorpus.ToArray();
Assert.That(rows, Has.Length.EqualTo(3), JsonSerializer.Serialize(rows));
Assert.Multiple(() =>
Expand Down Expand Up @@ -185,7 +185,7 @@ public void MergedCorpus_SelectRandom_Seed4501()
}
)
);
var mergedCorpus = new List<ITextCorpus> { corpus1, corpus2, corpus3 }.ChooseRandom([true, true, true], 4501);
var mergedCorpus = new List<ITextCorpus> { corpus1, corpus2, corpus3 }.ChooseRandom(4501);
var rows = mergedCorpus.ToArray();
Assert.That(rows, Has.Length.EqualTo(3), JsonSerializer.Serialize(rows));
Assert.Multiple(() =>
Expand Down Expand Up @@ -233,9 +233,7 @@ public void AlignMergedCorpora()
)
);

ITextCorpus sourceCorpus = new List<ITextCorpus> { sourceCorpus1, sourceCorpus2, sourceCorpus3 }.ChooseFirst(
[true, true, true]
);
ITextCorpus sourceCorpus = new List<ITextCorpus> { sourceCorpus1, sourceCorpus2, sourceCorpus3 }.ChooseFirst();

var targetCorpus1 = new DictionaryTextCorpus(
new MemoryText(
Expand Down Expand Up @@ -271,9 +269,7 @@ public void AlignMergedCorpora()
)
);

ITextCorpus targetCorpus = new List<ITextCorpus> { targetCorpus1, targetCorpus2, targetCorpus3 }.ChooseFirst(
[true, true, true]
);
ITextCorpus targetCorpus = new List<ITextCorpus> { targetCorpus1, targetCorpus2, targetCorpus3 }.ChooseFirst();

IParallelTextCorpus alignedCorpus = sourceCorpus.AlignRows(targetCorpus);
ParallelTextRow[] rows = alignedCorpus.GetRows().ToArray();
Expand Down

0 comments on commit 6ab2faa

Please sign in to comment.