From 3d5db7e2db7e195d597ce53dc7163eb5b7002859 Mon Sep 17 00:00:00 2001 From: Elliot Wolk Date: Fri, 21 Apr 2023 12:05:08 -0400 Subject: [PATCH] sentenceinfo: implement exportSentenceInfo(infile, outfile) in lvdocview --- crengine/include/lvdocview.h | 3 +++ crengine/src/lvdocview.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/crengine/include/lvdocview.h b/crengine/include/lvdocview.h index d0d16ce89..d4b12ebd4 100644 --- a/crengine/include/lvdocview.h +++ b/crengine/include/lvdocview.h @@ -955,6 +955,9 @@ class LVDocView : public CacheLoadingCallback /// load document from stream bool LoadDocument( LVStreamRef stream, const lChar32 * contentPath, bool metadataOnly = false ); + /// load document and export sentence info + bool exportSentenceInfo(const lChar32 * inputFileName, const lChar32 * outputFileName); + /// save last file position void savePosition(); /// restore last file position diff --git a/crengine/src/lvdocview.cpp b/crengine/src/lvdocview.cpp index 2de2a6292..910d744d4 100644 --- a/crengine/src/lvdocview.cpp +++ b/crengine/src/lvdocview.cpp @@ -4043,6 +4043,41 @@ static bool needToConvertBookmarks(CRFileHistRecord* historyRecord, lUInt32 domV return convertBookmarks; } +bool LVDocView::exportSentenceInfo(const lChar32 * inputFileName, const lChar32 * outputFileName) { + if (!LoadDocument(inputFileName, false)) { + return false; + } + + LVStreamRef out = LVOpenFileStream(outputFileName, LVOM_WRITE); + if ( out.isNull() ) { + return false; + } + + checkRender(); + + ldomXPointerEx ptrStart( m_doc->getRootNode(), m_doc->getRootNode()->getChildCount()); + if ( !ptrStart.thisSentenceStart() ) { + ptrStart.nextSentenceStart(); + } + + if ( !ptrStart.thisSentenceStart() ) { + return false; + } + + while ( 1 ) { + ldomXPointerEx ptrEnd(ptrStart); + ptrEnd.thisSentenceEnd(); + + ldomXRange range(ptrStart, ptrEnd); + lString32 sentenceText = range.getRangeText(); + *out << UnicodeToUtf8(ptrStart.toString()) << "," << UnicodeToUtf8(sentenceText) << "\n"; + if ( !ptrStart.nextSentenceStart() ) { + break; + } + } + return true; +} + /// load document from file bool LVDocView::LoadDocument(const lChar32 * fname, bool metadataOnly) { if (!fname || !fname[0])