Skip to content

Commit

Permalink
cr3qt: add -s CLI wrapper around lvdocview.exportSentenceInfo(inF,outF)
Browse files Browse the repository at this point in the history
  • Loading branch information
teleshoes committed Apr 21, 2023
1 parent 3d5db7e commit f6eb6a5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cr3qt/src/cr3widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ bool CR3View::loadDocument( QString fileName )
return res;
}

bool CR3View::exportSentenceInfo( QString inputFileName, QString outputFileName )
{
return _docview->exportSentenceInfo(
qt2cr(inputFileName).c_str(),
qt2cr(outputFileName).c_str()
);
}

void CR3View::wheelEvent( QWheelEvent * event )
{
// Get degrees delta from vertical scrolling
Expand Down
1 change: 1 addition & 0 deletions cr3qt/src/cr3widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CR3View : public QWidget, public LVDocViewCallback

bool loadDocument( QString fileName );
bool loadLastDocument();
bool exportSentenceInfo( QString inputFileName, QString outputFileName );
void setDocumentText( QString text );

QScrollBar * scrollBar() const;
Expand Down
36 changes: 35 additions & 1 deletion cr3qt/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#else
#include <QtGui/QApplication>
#endif
#include <QTimer>
#include "../crengine/include/crengine.h"
#include "../crengine/include/cr3version.h"
#include "mainwindow.h"
Expand Down Expand Up @@ -77,6 +78,18 @@ static void printHelp() {
" -v or --version: print program version\n"
" --loglevel=ERROR|WARN|INFO|DEBUG|TRACE: set logging level\n"
" --logfile=<filename>|stdout|stderr: set log file\n"
"\n"
" --get-sentence-info|-s INPUT_FILE_NAME OUTPUT_FILE_NAME\n"
" analyze INPUT_FILE_NAME and write sentence structure info to OUTPUT_FILE_NAME\n"
" -one sentence per line, formatted: START_POS,TEXT\n"
" -every word appears in exactly one sentence\n"
" -not every character appears; all newlines are omitted, and some whitespace\n"
" -START_POS is a UTF8-encoded string representing a unique position in the DOM of the first word\n"
" -START_POS never contains a comma\n"
" -e.g.: /body/DocFragment[3]/body/div/div[4]/p/a/text()[1].3\n"
" -TEXT is the full UTF8-encoded text of the sentence, without quotes or escaping\n"
" -TEXT never contains newline characters\n"
" -TEXT can contain commas, double quotes, and single quotes\n"
);
}

Expand All @@ -95,6 +108,9 @@ int main(int argc, char *argv[])
lString8 loglevel("ERROR");
lString8 logfile("stderr");
#endif
bool exportSentenceInfo = false;
QString exportSentenceInfoInputFileName;
QString exportSentenceInfoOutputFileName;
for ( int i=1; i<argc; i++ ) {
if ( !strcmp("-h", argv[i]) || !strcmp("-?", argv[i]) || !strcmp("/?", argv[i]) || !strcmp("--help", argv[i]) ) {
printHelp();
Expand All @@ -119,6 +135,16 @@ int main(int argc, char *argv[])
fclose(out);
return 0;
}
if ( !strcmp("-s", argv[i]) || !strcmp("--get-sentence-info", argv[i])) {
if(i<argc-2){
exportSentenceInfo = true;
exportSentenceInfoInputFileName = QString(argv[++i]);
exportSentenceInfoOutputFileName = QString(argv[++i]);
}else{
printf("ERROR: missing input/output filename args to --get-sentence-info\n");
return 1;
}
}
lString8 s(argv[i]);
if ( s.startsWith(cs8("--loglevel=")) ) {
loglevel = s.substr(11, s.length()-11);
Expand Down Expand Up @@ -223,7 +249,15 @@ int main(int argc, char *argv[])
else
CRLog::error("Canot load translation file %s from dir %s", UnicodeToUtf8(qt2cr(trname)).c_str(), UnicodeToUtf8(qt2cr(translations)).c_str() );
MainWindow w;
w.show();
if(exportSentenceInfo){
//run w.exportSentenceInfo() as soon as possible in event loop, and then quit
QTimer::singleShot(0, NULL, [&w, exportSentenceInfoInputFileName, exportSentenceInfoOutputFileName] () {
w.exportSentenceInfo(exportSentenceInfoInputFileName, exportSentenceInfoOutputFileName);
qApp->quit();
});
}else{
w.show();
}
res = a.exec();
}
}
Expand Down
19 changes: 19 additions & 0 deletions cr3qt/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,25 @@ void MainWindow::showEvent ( QShowEvent * event )
}
}

void MainWindow::exportSentenceInfo(QString inputFileName, QString outputFileName) {
if (inputFileName.length() <= 0 ) {
CRLog::error("ERROR: no file to export sentenceinfo\n");
}

bool res = ui->view->exportSentenceInfo(inputFileName, outputFileName);
if ( res ) {
CRLog::info(
"\n\n\nSUCCESS: exported "
+ inputFileName.toUtf8()
+ " to "
+ outputFileName.toUtf8()
+ "\n\n\n"
);
} else {
CRLog::error("\n\n\nERROR: export sentence info failed\n\n\n");
}
}

static bool firstFocus = true;

void MainWindow::focusInEvent ( QFocusEvent * event )
Expand Down
1 change: 1 addition & 0 deletions cr3qt/src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MainWindow : public QMainWindow, public PropsChangeCallback
virtual void focusInEvent ( QFocusEvent * event );
virtual void closeEvent ( QCloseEvent * event );
public slots:
void exportSentenceInfo(QString inputFileName, QString outputFileName);
void contextMenu( QPoint pos );
void on_actionFindText_triggered();
private slots:
Expand Down

0 comments on commit f6eb6a5

Please sign in to comment.