Skip to content

Commit

Permalink
sentenceinfo: implement exportSentenceInfo(infile, outfile) in lvdocview
Browse files Browse the repository at this point in the history
  • Loading branch information
teleshoes committed Apr 21, 2023
1 parent 3ad11b3 commit 3d5db7e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crengine/include/lvdocview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions crengine/src/lvdocview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 3d5db7e

Please sign in to comment.