-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Working NParallelTextCorpus * Change mergedtextcorpus parameter * Move same ref rows out of range info * Make seed optional; remove unneeded code
- Loading branch information
Showing
13 changed files
with
1,741 additions
and
599 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace SIL.Machine.Corpora | ||
{ | ||
public interface INParallelTextCorpus : ICorpus<NParallelTextRow> | ||
{ | ||
int Count(bool includeEmpty = true, IEnumerable<string> textIds = null); | ||
|
||
IEnumerable<NParallelTextRow> GetRows(IEnumerable<string> textIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace SIL.Machine.Corpora | ||
{ | ||
public enum MergeRule | ||
{ | ||
First, | ||
Random | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using SIL.Scripture; | ||
|
||
namespace SIL.Machine.Corpora | ||
{ | ||
public class MergedTextCorpus : TextCorpusBase | ||
{ | ||
private readonly NParallelTextCorpus _corpus; | ||
|
||
private readonly MergeRule _mergeRule; | ||
|
||
private readonly Random _random; | ||
|
||
public MergedTextCorpus(IEnumerable<ITextCorpus> corpora, MergeRule mergeRule, int? seed = null) | ||
{ | ||
_corpus = new NParallelTextCorpus(corpora) { AllRows = Enumerable.Repeat(true, corpora.Count()).ToArray() }; | ||
_mergeRule = mergeRule; | ||
if (seed != null) | ||
_random = new Random(seed.Value); | ||
else | ||
_random = new Random(); | ||
} | ||
|
||
public override IEnumerable<IText> Texts => _corpus.Corpora.SelectMany(c => c.Texts); | ||
|
||
public override bool IsTokenized => Enumerable.Range(0, _corpus.N).All(i => _corpus.IsTokenized(i)); | ||
|
||
public override ScrVers Versification => _corpus.N > 0 ? _corpus.Corpora[0].Versification : null; | ||
|
||
public override IEnumerable<TextRow> GetRows(IEnumerable<string> textIds) | ||
{ | ||
int indexOfInRangeRow = -1; | ||
foreach (NParallelTextRow nRow in _corpus.GetRows(textIds)) | ||
{ | ||
IReadOnlyList<int> nonEmptyIndices = nRow | ||
.NSegments.Select((s, i) => (s, i)) | ||
.Where(pair => pair.s.Count > 0 || nRow.IsInRange(pair.i)) | ||
.Select(pair => pair.i) | ||
.ToList(); | ||
IReadOnlyList<int> indices = | ||
nonEmptyIndices.Count > 0 ? nonEmptyIndices : Enumerable.Range(0, nRow.N).ToList(); | ||
if (indexOfInRangeRow == -1) | ||
{ | ||
indices = indices.Where(i => nRow.IsRangeStart(i) || !nRow.IsInRange(i)).ToList(); | ||
} | ||
if (indices.Count == 0) | ||
continue; | ||
int indexOfSelectedRow = -1; | ||
switch (_mergeRule) | ||
{ | ||
case MergeRule.First: | ||
indexOfSelectedRow = indices.First(); | ||
break; | ||
case MergeRule.Random: | ||
indexOfSelectedRow = indices[_random.Next(0, indices.Count)]; | ||
break; | ||
} | ||
indexOfSelectedRow = indexOfInRangeRow != -1 ? indexOfInRangeRow : indexOfSelectedRow; | ||
if (!nRow.IsInRange(indexOfSelectedRow)) | ||
{ | ||
indexOfInRangeRow = -1; | ||
} | ||
if (nRow.IsRangeStart(indexOfSelectedRow)) | ||
{ | ||
indexOfInRangeRow = indexOfSelectedRow; | ||
} | ||
yield return new TextRow(nRow.TextId, nRow.Ref) | ||
{ | ||
Segment = nRow.NSegments[indexOfSelectedRow], | ||
Flags = nRow.NFlags[indexOfSelectedRow] | ||
}; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.