Skip to content

Commit 38ac2a6

Browse files
committed
cr3qt: add -s CLI wrapper around lvdocview.exportSentenceInfo(inF,outF)
1 parent ea0da98 commit 38ac2a6

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

cr3qt/src/cr3widget.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,14 @@ bool CR3View::loadDocument( QString fileName )
537537
return res;
538538
}
539539

540+
bool CR3View::exportSentenceInfo( QString inputFileName, QString outputFileName )
541+
{
542+
return _docview->exportSentenceInfo(
543+
qt2cr(inputFileName).c_str(),
544+
qt2cr(outputFileName).c_str()
545+
);
546+
}
547+
540548
void CR3View::wheelEvent( QWheelEvent * event )
541549
{
542550
// Get degrees delta from vertical scrolling

cr3qt/src/cr3widget.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CR3View : public QWidget, public LVDocViewCallback
6363

6464
bool loadDocument( QString fileName );
6565
bool loadLastDocument();
66+
bool exportSentenceInfo( QString inputFileName, QString outputFileName );
6667
void setDocumentText( QString text );
6768

6869
QScrollBar * scrollBar() const;

cr3qt/src/main.cpp

+35-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#else
3030
#include <QtGui/QApplication>
3131
#endif
32+
#include <QTimer>
3233
#include "../crengine/include/crengine.h"
3334
#include "../crengine/include/cr3version.h"
3435
#include "mainwindow.h"
@@ -77,6 +78,18 @@ static void printHelp() {
7778
" -v or --version: print program version\n"
7879
" --loglevel=ERROR|WARN|INFO|DEBUG|TRACE: set logging level\n"
7980
" --logfile=<filename>|stdout|stderr: set log file\n"
81+
"\n"
82+
" --get-sentence-info|-s INPUT_FILE_NAME OUTPUT_FILE_NAME\n"
83+
" analyze INPUT_FILE_NAME and write sentence structure info to OUTPUT_FILE_NAME\n"
84+
" -one sentence per line, formatted: START_POS,TEXT\n"
85+
" -every word appears in exactly one sentence\n"
86+
" -not every character appears; all newlines are omitted, and some whitespace\n"
87+
" -START_POS is a UTF8-encoded string representing a unique position in the DOM of the first word\n"
88+
" -START_POS never contains a comma\n"
89+
" -e.g.: /body/DocFragment[3]/body/div/div[4]/p/a/text()[1].3\n"
90+
" -TEXT is the full UTF8-encoded text of the sentence, without quotes or escaping\n"
91+
" -TEXT never contains newline characters\n"
92+
" -TEXT can contain commas, double quotes, and single quotes\n"
8093
);
8194
}
8295

@@ -95,6 +108,9 @@ int main(int argc, char *argv[])
95108
lString8 loglevel("ERROR");
96109
lString8 logfile("stderr");
97110
#endif
111+
bool exportSentenceInfo = false;
112+
QString exportSentenceInfoInputFileName;
113+
QString exportSentenceInfoOutputFileName;
98114
for ( int i=1; i<argc; i++ ) {
99115
if ( !strcmp("-h", argv[i]) || !strcmp("-?", argv[i]) || !strcmp("/?", argv[i]) || !strcmp("--help", argv[i]) ) {
100116
printHelp();
@@ -119,6 +135,16 @@ int main(int argc, char *argv[])
119135
fclose(out);
120136
return 0;
121137
}
138+
if ( !strcmp("-s", argv[i]) || !strcmp("--get-sentence-info", argv[i])) {
139+
if(i<argc-2){
140+
exportSentenceInfo = true;
141+
exportSentenceInfoInputFileName = QString(argv[++i]);
142+
exportSentenceInfoOutputFileName = QString(argv[++i]);
143+
}else{
144+
printf("ERROR: missing input/output filename args to --get-sentence-info\n");
145+
return 1;
146+
}
147+
}
122148
lString8 s(argv[i]);
123149
if ( s.startsWith(cs8("--loglevel=")) ) {
124150
loglevel = s.substr(11, s.length()-11);
@@ -223,7 +249,15 @@ int main(int argc, char *argv[])
223249
else
224250
CRLog::error("Canot load translation file %s from dir %s", UnicodeToUtf8(qt2cr(trname)).c_str(), UnicodeToUtf8(qt2cr(translations)).c_str() );
225251
MainWindow w;
226-
w.show();
252+
if(exportSentenceInfo){
253+
//run w.exportSentenceInfo() as soon as possible in event loop, and then quit
254+
QTimer::singleShot(0, NULL, [&w, exportSentenceInfoInputFileName, exportSentenceInfoOutputFileName] () {
255+
w.exportSentenceInfo(exportSentenceInfoInputFileName, exportSentenceInfoOutputFileName);
256+
qApp->quit();
257+
});
258+
}else{
259+
w.show();
260+
}
227261
res = a.exec();
228262
}
229263
}

cr3qt/src/mainwindow.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,25 @@ void MainWindow::showEvent ( QShowEvent * event )
504504
}
505505
}
506506

507+
void MainWindow::exportSentenceInfo(QString inputFileName, QString outputFileName) {
508+
if (inputFileName.length() <= 0 ) {
509+
CRLog::error("ERROR: no file to export sentenceinfo\n");
510+
}
511+
512+
bool res = ui->view->exportSentenceInfo(inputFileName, outputFileName);
513+
if ( res ) {
514+
CRLog::info(
515+
"\n\n\nSUCCESS: exported "
516+
+ inputFileName.toUtf8()
517+
+ " to "
518+
+ outputFileName.toUtf8()
519+
+ "\n\n\n"
520+
);
521+
} else {
522+
CRLog::error("\n\n\nERROR: export sentence info failed\n\n\n");
523+
}
524+
}
525+
507526
static bool firstFocus = true;
508527

509528
void MainWindow::focusInEvent ( QFocusEvent * event )

cr3qt/src/mainwindow.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class MainWindow : public QMainWindow, public PropsChangeCallback
5353
virtual void focusInEvent ( QFocusEvent * event );
5454
virtual void closeEvent ( QCloseEvent * event );
5555
public slots:
56+
void exportSentenceInfo(QString inputFileName, QString outputFileName);
5657
void contextMenu( QPoint pos );
5758
void on_actionFindText_triggered();
5859
private slots:

0 commit comments

Comments
 (0)