Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions src/SIL.Machine/Corpora/ParatextProjectTextUpdaterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public string UpdateUsfm(
IEnumerable<IUsfmUpdateBlockHandler> updateBlockHandlers = null,
IEnumerable<(int, string)> remarks = null,
Func<UsfmUpdateBlockHandlerException, bool> errorHandler = null,
bool compareSegments = false
bool compareSegments = false,
bool convertUsfmToUpdateRowVersification = false
)
{
string fileName = _settings.GetBookFileName(bookId);
Expand All @@ -57,7 +58,9 @@ public string UpdateUsfm(
updateBlockHandlers,
remarks,
errorHandler,
compareSegments
compareSegments,
_settings.Versification,
convertUsfmToUpdateRowVersification
);
try
{
Expand Down
109 changes: 99 additions & 10 deletions src/SIL.Machine/Corpora/UpdateUsfmParserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class UpdateUsfmParserHandler : ScriptureRefUsfmParserHandlerBase
private int _verseRowIndex;
private readonly Dictionary<VerseRef, List<RowInfo>> _verseRowsMap;
private readonly ScrVers _updateRowsVersification;
private readonly ScrVers _usfmVersification;
private readonly List<UsfmToken> _tokens;
private readonly List<UsfmToken> _updatedText;
private readonly List<UsfmToken> _embedTokens;
Expand All @@ -65,6 +66,10 @@ public class UpdateUsfmParserHandler : ScriptureRefUsfmParserHandlerBase
private int _tokenIndex;
private readonly Func<UsfmUpdateBlockHandlerException, bool> _errorHandler;
private readonly bool _compareSegments;
private readonly bool _convertUsfmToUpdateRowVersification;
private UsfmToken _currentChapterToken;
private int _currentChapterNum;
private bool _skipNextVerseText;

/// <param name="rows">UpdateUsfmRows must be in order</param>
public UpdateUsfmParserHandler(
Expand All @@ -78,7 +83,9 @@ public UpdateUsfmParserHandler(
IEnumerable<IUsfmUpdateBlockHandler> updateBlockHandlers = null,
IEnumerable<(int, string)> remarks = null,
Func<UsfmUpdateBlockHandlerException, bool> errorHandler = null,
bool compareSegments = false
bool compareSegments = false,
ScrVers usfmVersification = null,
bool convertUsfmToUpdateRowVersification = false
)
{
_rows = rows ?? Array.Empty<UpdateUsfmRow>();
Expand All @@ -89,6 +96,7 @@ public UpdateUsfmParserHandler(
_updateRowsVersification = ScrVers.English;
if (_rows.Count > 0)
_updateRowsVersification = _rows.First(r => r.Refs.Count > 0).Refs[0].Versification;
_usfmVersification = usfmVersification ?? _updateRowsVersification;
_tokens = new List<UsfmToken>();
_updatedText = new List<UsfmToken>();
_updateBlocks = new Stack<UsfmUpdateBlock>();
Expand All @@ -112,13 +120,23 @@ public UpdateUsfmParserHandler(
if (_errorHandler == null)
_errorHandler = (error) => false;
_compareSegments = compareSegments;
_convertUsfmToUpdateRowVersification =
convertUsfmToUpdateRowVersification && _updateRowsVersification != _usfmVersification;
_currentChapterToken = null;
_currentChapterNum = 0;
_skipNextVerseText = false;
}

public IReadOnlyList<UsfmToken> Tokens => _tokens;

public override void EndUsfm(UsfmParserState state)
{
CollectUpdatableTokens(state);
if (_currentChapterToken != null)
{
_tokens.Add(_currentChapterToken);
_currentChapterToken = null;
}
base.EndUsfm(state);
}

Expand Down Expand Up @@ -148,6 +166,12 @@ public override void EndBook(UsfmParserState state, string marker)
UsfmUpdateBlock updateBlock = _updateBlocks.Pop();
_tokens.AddRange(updateBlock.GetTokens());

if (_currentChapterToken != null)
{
_tokens.Add(_currentChapterToken);
_currentChapterToken = null;
}

base.EndBook(state, marker);
}

Expand Down Expand Up @@ -398,7 +422,11 @@ protected override void StartVerseText(UsfmParserState state, IReadOnlyList<Scri

protected override void EndVerseText(UsfmParserState state, IReadOnlyList<ScriptureRef> scriptureRefs)
{
EndUpdateBlock(state, scriptureRefs);
if (!_skipNextVerseText)
{
EndUpdateBlock(state, scriptureRefs);
_skipNextVerseText = false;
}
}

protected override void StartNonVerseText(UsfmParserState state, ScriptureRef scriptureRef)
Expand Down Expand Up @@ -564,9 +592,16 @@ private void CollectUpdatableTokens(UsfmParserState state)
UsfmToken token = state.Tokens[_tokenIndex];
if (token.Type == UsfmTokenType.Verse)
{
string sanitizedVerseData = SanitizeVerseData(token.Data);
token = new UsfmToken(token.Type, token.Marker, token.Text, token.EndMarker, sanitizedVerseData);
VerseRef updatedVerse = UpdateVerseData(state, token);
if (updatedVerse.BookNum != state.VerseRef.BookNum)
{
_tokenIndex++;
_skipNextVerseText = true;
continue;
}
token = new UsfmToken(token.Type, token.Marker, token.Text, token.EndMarker, updatedVerse.Verse);
}

if (CurrentTextType == ScriptureTextType.Embed)
{
_embedTokens.Add(token);
Expand All @@ -582,10 +617,48 @@ private void CollectUpdatableTokens(UsfmParserState state)
{
_tokens.Add(token);
}

_tokenIndex++;
}
}

private VerseRef UpdateVerseData(UsfmParserState state, UsfmToken token)
{
string updatedVerseData = SanitizeVerseData(token.Data);
VerseRef verseRef = state.VerseRef;
verseRef.Verse = updatedVerseData;
if (_convertUsfmToUpdateRowVersification)
{
verseRef = verseRef.ChangeVersificationWithSegments(_updateRowsVersification);
if (verseRef.ChapterNum != _currentChapterNum && state.VerseRef.BookNum == verseRef.BookNum)
{
if (_currentChapterToken != null)
{
_tokens.Add(_currentChapterToken.Copy());
_currentChapterToken = null;
}
else
{
UsfmToken newChapterToken = new UsfmToken(
UsfmTokenType.Chapter,
"c",
"",
"",
verseRef.ChapterNum.ToString()
);
_tokens.Add(newChapterToken);
}
_currentChapterNum = verseRef.ChapterNum;
}
if (verseRef.ChapterNum == verseRef.Versification.GetLastChapter(verseRef.BookNum))
{
_currentChapterToken = null;
}
}

return verseRef;
}

private void CollectReadonlyTokens(UsfmParserState state)
{
while (_tokenIndex <= state.Index + state.SpecialTokenCount)
Expand All @@ -597,7 +670,16 @@ private void CollectReadonlyTokens(UsfmParserState state)
}
else
{
_tokens.Add(token);
if (_convertUsfmToUpdateRowVersification && token.Type == UsfmTokenType.Chapter)
{
if (_currentChapterToken != null)
_tokens.Add(_currentChapterToken);
_currentChapterToken = token;
}
else
{
_tokens.Add(token);
}
}
_tokenIndex++;
}
Expand Down Expand Up @@ -663,10 +745,13 @@ private bool HasNewText()
private void StartUpdateBlock(IReadOnlyList<ScriptureRef> scriptureRefs)
{
(IReadOnlyList<string> rowTexts, Dictionary<string, object> metadata) = AdvanceRows(scriptureRefs);
_updateBlocks.Push(
new UsfmUpdateBlock(scriptureRefs, metadata: metadata ?? new Dictionary<string, object>())
);
PushUpdatedText(rowTexts.Select(t => new UsfmToken(t + " ")));
if (!_skipNextVerseText)
{
_updateBlocks.Push(
new UsfmUpdateBlock(scriptureRefs, metadata: metadata ?? new Dictionary<string, object>())
);
PushUpdatedText(rowTexts.Select(t => new UsfmToken(t + " ")));
}
}

private void EndUpdateBlock(UsfmParserState state, IReadOnlyList<ScriptureRef> scriptureRefs)
Expand Down Expand Up @@ -757,7 +842,11 @@ private bool IsNonverseParagraph(UsfmParserState state, UsfmUpdateBlockElement e
private void UpdateVerseRowsMap()
{
_verseRowsMap.Clear();
while (_rowIndex < _rows.Count && _rows[_rowIndex].Refs[0].ChapterNum == _verseRowsRef.ChapterNum)
while (
_rowIndex < _rows.Count
&& _rows[_rowIndex].Refs[0].ChangeVersification(_verseRowsRef.Versification).ChapterNum
== _verseRowsRef.ChapterNum
)
{
UpdateUsfmRow row = _rows[_rowIndex];
var ri = new RowInfo(_rowIndex);
Expand Down
9 changes: 9 additions & 0 deletions src/SIL.Machine/Corpora/UsfmToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ public void CopyAttributes(UsfmToken sourceToken)
_defaultAttributeName = sourceToken._defaultAttributeName;
}

public UsfmToken Copy()
{
UsfmToken copy = new UsfmToken(Type, Marker, Text, EndMarker, Data);
copy.CopyAttributes(this);
copy.LineNumber = LineNumber;
copy.ColumnNumber = ColumnNumber;
return copy;
}

private static void AppendAttribute(List<UsfmAttribute> attributes, string name, string value)
{
value = value?.Trim(); // don't want to have attribute that is just spaces
Expand Down
Loading
Loading