From bdd2182953f5c01ec33f075d053f154c9b005f7c Mon Sep 17 00:00:00 2001 From: Peter Chapman Date: Thu, 21 May 2026 13:40:42 +1200 Subject: [PATCH] Make corpus alignment exceptions more generic --- src/SIL.Machine/Corpora/CorporaExtensions.cs | 11 +++++++++++ .../Corpora/CorpusAlignmentException.cs | 19 ------------------- .../Corpora/NParallelTextCorpus.cs | 8 +++----- src/SIL.Machine/Corpora/ParallelTextCorpus.cs | 2 +- 4 files changed, 15 insertions(+), 25 deletions(-) delete mode 100644 src/SIL.Machine/Corpora/CorpusAlignmentException.cs diff --git a/src/SIL.Machine/Corpora/CorporaExtensions.cs b/src/SIL.Machine/Corpora/CorporaExtensions.cs index 2cf7ca98..7f7433d5 100644 --- a/src/SIL.Machine/Corpora/CorporaExtensions.cs +++ b/src/SIL.Machine/Corpora/CorporaExtensions.cs @@ -52,6 +52,17 @@ public static (IEnumerable<(T, bool)>, int, int) InterleavedSplit( return (splitCorpus, corpusSize - splitIndices.Count, splitIndices.Count); } + public static ArgumentException AlignmentException( + this ICorpus _, + string[] refs, + Exception innerException = null + ) + where T : IRow => + new ArgumentException( + $"Invalid format in {string.Join(", ", refs)}. Mismatched key formats. There may be an extraneous tab, missing ref, or inconsistent use of user-defined refs.", + innerException + ); + #endregion #region ITextCorpus operations diff --git a/src/SIL.Machine/Corpora/CorpusAlignmentException.cs b/src/SIL.Machine/Corpora/CorpusAlignmentException.cs deleted file mode 100644 index dae43706..00000000 --- a/src/SIL.Machine/Corpora/CorpusAlignmentException.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace SIL.Machine.Corpora -{ - public class CorpusAlignmentException : Exception - { - public CorpusAlignmentException(string sourceRef, string targetRef, Exception innerException = null) - : base( - $"Invalid format in {sourceRef} and {targetRef}. Mismatched key formats \"{sourceRef}\" and \"{targetRef}\". There may be an extraneous tab, missing ref, or inconsistent use of user-defined refs.", - innerException - ) { } - - public CorpusAlignmentException(string[] refs, Exception innerException = null) - : base( - $"Invalid format in {string.Join(", ", refs)}. Mismatched key formats. There may be an extraneous tab, missing ref, or inconsistent use of user-defined refs.", - innerException - ) { } - } -} diff --git a/src/SIL.Machine/Corpora/NParallelTextCorpus.cs b/src/SIL.Machine/Corpora/NParallelTextCorpus.cs index 18189dc8..769217ac 100644 --- a/src/SIL.Machine/Corpora/NParallelTextCorpus.cs +++ b/src/SIL.Machine/Corpora/NParallelTextCorpus.cs @@ -142,7 +142,7 @@ private IEnumerable GetRows(IList> enumer } catch (ArgumentException e) { - throw new CorpusAlignmentException( + throw this.AlignmentException( currentRows.Where(r => r != null).Select(r => r.Ref.ToString()).ToArray(), e ); @@ -264,9 +264,7 @@ NParallelTextRow row in CreateRows( } else { - throw new CorpusAlignmentException( - minRefIndexes.Select(i => currentRows[i].Ref.ToString()).ToArray() - ); + throw this.AlignmentException(minRefIndexes.Select(i => currentRows[i].Ref.ToString()).ToArray()); } } @@ -400,7 +398,7 @@ private bool CheckSameRefRows(IList sameRefRows, TextRow otherRow) } catch (ArgumentException e) { - throw new CorpusAlignmentException(sameRefRows[0].Ref.ToString(), otherRow.Ref.ToString(), e); + throw this.AlignmentException(new[] { sameRefRows[0].Ref.ToString(), otherRow.Ref.ToString() }, e); } return sameRefRows.Count > 0; } diff --git a/src/SIL.Machine/Corpora/ParallelTextCorpus.cs b/src/SIL.Machine/Corpora/ParallelTextCorpus.cs index 382d8ff2..f4df4771 100644 --- a/src/SIL.Machine/Corpora/ParallelTextCorpus.cs +++ b/src/SIL.Machine/Corpora/ParallelTextCorpus.cs @@ -54,7 +54,7 @@ public override IEnumerable GetRows(IEnumerable textIds } catch (ArgumentException e) { - throw new CorpusAlignmentException(nRow.NRefs.Select(r => r.ToString()).ToArray(), e); + throw this.AlignmentException(nRow.NRefs.Select(r => r.ToString()).ToArray(), e); } } while (compareAlignmentCorpus < 0); }