LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
limaSimpleClientDelegate.h
Go to the documentation of this file.
1// Copyright (C) 2017 by CEA - LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef LIMASIMPLECLIENTDELEGATE_H
7#define LIMASIMPLECLIENTDELEGATE_H
8
12#include <iostream>
13#include <QObject>
14#include <QCoreApplication>
15#include <QThread>
16#include <thread>
17#include <boost/thread.hpp>
18
19Q_DECLARE_METATYPE(std::string)
20
21namespace Lima {
22
23// the LimaWorker does the job (launches lima analysis on a text)
24// the LimaController tells LimaWorker to do the job
25// the LimaSimpleClientDelegate holds the thread in which the lima worker and controller run + the QCoreApplication
26// LimaController and LimaWorker communicate using Qt signals and slots (only way to be taken into account in the
27// main event loop of the QCoreApplication): both are QObject. In order to avoid the WARNING (QApplication was not created in the main() thread),
28// all QObjects must be created in the same thread as the QCoreApplication (hence the need of a controller in the thread, different
29// from the delegate).
30
31class LimaWorker: public QObject
32{
33Q_OBJECT
34public:
35 LimaWorker(QObject* parent = nullptr);
36 virtual ~LimaWorker();
37
38 const std::string& getResult() const { return m_result; }
39
40public Q_SLOTS:
41 void initialize(const std::string& language, const std::string& pipeline);
42 void analyze(const std::string& text);
43 void quit();
44
45Q_SIGNALS:
48
49public:
50 std::string m_language;
51 std::string m_pipeline;
52 std::shared_ptr< Lima::LinguisticProcessing::AbstractLinguisticProcessingClient > m_client;
54 std::map<std::string, Lima::AbstractAnalysisHandler*> m_handlers; // the list of handlers (contains only m_handler, but keep it to make it easy)
55
56 // if first initialization, has to initialize common data and configure factory, otherwise, only
57 // generates the client
59
60private:
61 std::string m_result;
62};
63
64class LimaController: public QObject
65{
66Q_OBJECT
67public:
68 LimaController(QObject* parent=nullptr);
69 virtual ~LimaController();
70
71 bool hasFinishedInit() { return m_finishedInit; }
72 bool hasFinishedAnalyze() { return m_finishedAnalyze; }
73
74 void initialize(const std::string& language, const std::string& pipeline);
75 void analyze(const std::string& text);
76
77 void stop();
78
79public Q_SLOTS:
80 void endInit() { m_finishedInit=true; }
81 void endAnalyze() { m_finishedAnalyze=true; }
82
83Q_SIGNALS:
84 void doInitialize(const std::string& language, const std::string& pipeline);
85 void doAnalyze(const std::string& text);
86 void closeApp(); // to stop the app
87 void closeWorker(); // to stop the worker properly
88
89private:
90 bool m_finishedInit;
91 bool m_finishedAnalyze;
92};
93
95{
96public:
99
100 void initialize(const std::string& language, const std::string& pipeline);
101 std::string analyze(const std::string& text);
102
103private:
104 // static function called in the thread initialization
105 static void onStarted();
106
107 // LimaController is static: shared by all lima client
108 static std::shared_ptr<LimaController> m_controller;
109 // LimaWorker is static
110 static std::shared_ptr<LimaWorker> m_worker;
111
112 // internal thread to run QCoreApplication
113 static int argc;
114 static char* argv[2];
115 static QCoreApplication* app;
116 // must use a std::thread (or boost), not a QThread, to run the QApplication (otherwise, have a
117 // warning: QApplication was not created in the main() thread.)
118 static boost::thread* thread;
119};
120
121} // end namespace
122
123#endif
void doAnalyze(const std::string &text)
void analyze(const std::string &text)
void initialize(const std::string &language, const std::string &pipeline)
void doInitialize(const std::string &language, const std::string &pipeline)
void initialize(const std::string &language, const std::string &pipeline)
std::string analyze(const std::string &text)
void finishedAnalyze()
void initialize(const std::string &language, const std::string &pipeline)
std::map< std::string, Lima::AbstractAnalysisHandler * > m_handlers
const std::string & getResult() const
void analyze(const std::string &text)
std::shared_ptr< Lima::LinguisticProcessing::AbstractLinguisticProcessingClient > m_client
Lima::LinguisticProcessing::SimpleStreamHandler * m_handler
NAUTITIA.