private List <VerseSplitLocation> GetVerseSplitLocations(PortionScript script, int bookNum) { var splitLocations = new List <VerseSplitLocation>(); Block prevBlock = null; foreach (var block in script.GetScriptBlocks()) { if (prevBlock != null && block.StartsAtVerseStart) { splitLocations.Add(new VerseSplitLocation(bookNum, prevBlock, block, Versification)); } prevBlock = block; } return(splitLocations); }
public BlockMatchup(BookScript vernacularBook, int iBlock, Action <PortionScript> splitBlocks, Func <VerseRef, bool> isOkayToBreakAtVerse, IReferenceLanguageInfo heSaidProvider) { m_vernacularBook = vernacularBook; int bookNum = BCVRef.BookToNumber(m_vernacularBook.BookId); m_referenceLanguageInfo = heSaidProvider; var blocks = m_vernacularBook.GetScriptBlocks(); var originalAnchorBlock = blocks[iBlock]; var blocksForVersesCoveredByBlock = vernacularBook.GetBlocksForVerse(originalAnchorBlock.ChapterNumber, originalAnchorBlock.InitialStartVerseNumber).ToList(); m_iStartBlock = iBlock - blocksForVersesCoveredByBlock.IndexOf(originalAnchorBlock); while (!blocksForVersesCoveredByBlock.First().StartsAtVerseStart&& blocksForVersesCoveredByBlock.First().InitialStartVerseNumber < originalAnchorBlock.InitialStartVerseNumber) { var prepend = vernacularBook.GetBlocksForVerse(originalAnchorBlock.ChapterNumber, blocksForVersesCoveredByBlock.First().InitialStartVerseNumber).ToList(); prepend.RemoveAt(prepend.Count - 1); m_iStartBlock -= prepend.Count; blocksForVersesCoveredByBlock.InsertRange(0, prepend); } int iLastBlock = m_iStartBlock + blocksForVersesCoveredByBlock.Count - 1; int i = iLastBlock; AdvanceToCleanVerseBreak(blocks, verseNum => { return(isOkayToBreakAtVerse(new VerseRef(bookNum, originalAnchorBlock.ChapterNumber, verseNum))); }, ref i); if (i > iLastBlock) { blocksForVersesCoveredByBlock.AddRange(blocks.Skip(iLastBlock + 1).Take(i - iLastBlock)); } while (CharacterVerseData.IsCharacterOfType(blocksForVersesCoveredByBlock.Last().CharacterId, CharacterVerseData.StandardCharacter.ExtraBiblical)) { blocksForVersesCoveredByBlock.RemoveAt(blocksForVersesCoveredByBlock.Count - 1); } m_portion = new PortionScript(vernacularBook.BookId, blocksForVersesCoveredByBlock.Select(b => b.Clone())); CorrelatedAnchorBlock = m_portion.GetScriptBlocks()[iBlock - m_iStartBlock]; if (splitBlocks != null) { int origCount = m_portion.GetScriptBlocks().Count; splitBlocks(m_portion); m_numberOfBlocksAddedBySplitting = m_portion.GetScriptBlocks().Count - origCount; } }
/// <summary> /// Split blocks in the given book to match verse split locations /// </summary> /// <returns>A value indicating whether any splits were made</returns> private static bool MakesSplits(PortionScript blocksToSplit, int bookNum, ScrVers versification, List <VerseSplitLocation> verseSplitLocations, string descriptionOfProjectBeingSplit, string descriptionOfProjectUsedToDetermineSplitLocations) { if (!verseSplitLocations.Any()) { return(false); } bool splitsMade = false; var iSplit = 0; VerseRef verseToSplitAfter = verseSplitLocations[iSplit]; var blocks = blocksToSplit.GetScriptBlocks(); for (int index = 0; index < blocks.Count; index++) { var block = blocks[index]; var initStartVerse = new VerseRef(bookNum, block.ChapterNumber, block.InitialStartVerseNumber, versification); VerseRef initEndVerse; if (block.InitialEndVerseNumber != 0) { initEndVerse = new VerseRef(bookNum, block.ChapterNumber, block.InitialEndVerseNumber, versification); } else { initEndVerse = initStartVerse; } while (initEndVerse > verseToSplitAfter) { if (iSplit == verseSplitLocations.Count - 1) { return(splitsMade); } verseToSplitAfter = verseSplitLocations[++iSplit]; } var lastVerse = new VerseRef(bookNum, block.ChapterNumber, block.LastVerseNum, versification); if (lastVerse < verseToSplitAfter) { continue; } if (initEndVerse.CompareTo(lastVerse) != 0 && lastVerse >= verseSplitLocations[iSplit].Before) { versification.ChangeVersification(verseToSplitAfter); if (blocksToSplit.TrySplitBlockAtEndOfVerse(block, verseToSplitAfter.VerseNum)) { splitsMade = true; } else { #if DEBUG if (!BlockContainsVerseEndInMiddleOfVerseBridge(block, verseToSplitAfter.VerseNum)) { ErrorReport.NotifyUserOfProblem( "Attempt to split {0} block to match breaks in the {1} text failed. Book: {2}; Chapter: {3}; Verse: {4}; Block: {5}", descriptionOfProjectBeingSplit, descriptionOfProjectUsedToDetermineSplitLocations, blocksToSplit.Id, block.ChapterNumber, verseToSplitAfter.VerseNum, block.GetText(true)); } #endif if (iSplit == verseSplitLocations.Count - 1) { break; } verseToSplitAfter = verseSplitLocations[++iSplit]; index--; } } } return(splitsMade); }
public PortionScript(PortionScript parent, IEnumerable <Block> blocks) : this(parent.BookId, blocks, parent.Versification) { }
public BlockMatchup(BookScript vernacularBook, int iBlock, Action <PortionScript> splitBlocks, Func <VerseRef, bool> isOkayToBreakAtVerse, IReferenceLanguageInfo heSaidProvider, uint predeterminedBlockCount = 0) { m_vernacularBook = vernacularBook; int bookNum = BCVRef.BookToNumber(m_vernacularBook.BookId); m_referenceLanguageInfo = heSaidProvider; var blocks = m_vernacularBook.GetScriptBlocks(); var originalAnchorBlock = blocks[iBlock]; if (predeterminedBlockCount == 0) { var blocksForVersesCoveredByBlock = vernacularBook.GetBlocksForVerse(originalAnchorBlock.ChapterNumber, originalAnchorBlock.InitialStartVerseNumber).ToList(); var indexOfAnchorBlockInVerse = blocksForVersesCoveredByBlock.IndexOf(originalAnchorBlock); if (indexOfAnchorBlockInVerse < 0) { Logger.WriteEvent($"Anchor block not found in verse: {m_vernacularBook.BookId} {originalAnchorBlock.ChapterNumber}:" + $"{originalAnchorBlock.InitialStartVerseNumber} Verse apparently occurs more than once in the Scripture text."); // REVIEW: This logic assumes that the repeated verse is wholly contained in this onwe block. blocksForVersesCoveredByBlock = new List <Block>() { originalAnchorBlock }; indexOfAnchorBlockInVerse = 0; } m_iStartBlock = iBlock - indexOfAnchorBlockInVerse; while (m_iStartBlock > 0) { if (blocksForVersesCoveredByBlock.First().InitialStartVerseNumber < originalAnchorBlock.InitialStartVerseNumber && !blocksForVersesCoveredByBlock.First().StartsAtVerseStart) { var prepend = vernacularBook.GetBlocksForVerse(originalAnchorBlock.ChapterNumber, blocksForVersesCoveredByBlock.First().InitialStartVerseNumber).ToList(); prepend.RemoveAt(prepend.Count - 1); m_iStartBlock -= prepend.Count; blocksForVersesCoveredByBlock.InsertRange(0, prepend); } if (m_iStartBlock == 0 || isOkayToBreakAtVerse(new VerseRef(bookNum, originalAnchorBlock.ChapterNumber, blocksForVersesCoveredByBlock.First().InitialStartVerseNumber))) { break; } m_iStartBlock--; blocksForVersesCoveredByBlock.Insert(0, blocks[m_iStartBlock]); } int iLastBlock = m_iStartBlock + blocksForVersesCoveredByBlock.Count - 1; int i = iLastBlock; AdvanceToCleanVerseBreak(blocks, verseNum => isOkayToBreakAtVerse(new VerseRef(bookNum, originalAnchorBlock.ChapterNumber, verseNum)), ref i); if (i > iLastBlock) { blocksForVersesCoveredByBlock.AddRange(blocks.Skip(iLastBlock + 1).Take(i - iLastBlock)); } while (CharacterVerseData.IsCharacterOfType(blocksForVersesCoveredByBlock.Last().CharacterId, CharacterVerseData.StandardCharacter.ExtraBiblical)) { blocksForVersesCoveredByBlock.RemoveAt(blocksForVersesCoveredByBlock.Count - 1); } m_portion = new PortionScript(vernacularBook.BookId, blocksForVersesCoveredByBlock.Select(b => b.Clone())); try { CorrelatedAnchorBlock = m_portion.GetScriptBlocks()[iBlock - m_iStartBlock]; } catch (Exception ex) { Logger.WriteEvent(ex.Message); Logger.WriteEvent($"iBlock = {iBlock}; m_iStartBlock = {m_iStartBlock}"); foreach (var block in m_portion.GetScriptBlocks()) { Logger.WriteEvent($"block = {block}"); } throw; } } else { m_iStartBlock = iBlock; m_portion = new PortionScript(vernacularBook.BookId, vernacularBook.GetScriptBlocks().Skip(iBlock).Take((int)predeterminedBlockCount).Select(b => b.Clone())); CorrelatedAnchorBlock = m_portion.GetScriptBlocks().First(); } if (splitBlocks != null) { int origCount = m_portion.GetScriptBlocks().Count; splitBlocks(m_portion); m_numberOfBlocksAddedBySplitting = m_portion.GetScriptBlocks().Count - origCount; } }