repaired lemmatizer

This commit is contained in:
Rafał Jaworski 2018-12-31 00:50:24 +01:00
parent 3c575cd596
commit dea4308618
2 changed files with 33 additions and 9 deletions

View File

@ -1,6 +1,9 @@
#include "socket_lemmatizer.hpp" #include "socket_lemmatizer.hpp"
#include <time.h>
#include "config.hpp" #include "config.hpp"
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
SocketLemmatizer::SocketLemmatizer(int port) throw(ConcordiaException) : SocketLemmatizer::SocketLemmatizer(int port) throw(ConcordiaException) :
@ -79,20 +82,38 @@ bool SocketLemmatizer::_send_data(std::string data)
std::string SocketLemmatizer::_receive(int size=512) std::string SocketLemmatizer::_receive(int size=512)
{ {
char buffer[size]; char buffer[size];
std::string reply; std::string reply = "";
//Receive a reply from the server //Receive a reply from the server
if(recv(_sock , buffer , sizeof(buffer) , 0) < 0) { bool dataAvailable = true;
throw ConcordiaException("Receive failed"); while (dataAvailable) {
int amountReceived = recv(_sock , buffer , sizeof(buffer) , 0);
if (amountReceived < 0) {
throw ConcordiaException("Lemmatizer: recv failed");
} else if (amountReceived == 0) {
dataAvailable = false;
} else {
buffer[amountReceived] = '\0';
reply += buffer;
}
} }
reply = buffer;
return reply; return reply;
} }
std::string SocketLemmatizer::lemmatizeSentence(std::string languageCode, std::string sentence) { std::string SocketLemmatizer::lemmatizeSentence(std::string languageCode, std::string sentence) {
_connect(); for (int i=0;i<5;i++) {
_send_data(languageCode+sentence+LEMMATIZER_DELIMITER); try {
std::string reply = _receive(512); _connect();
_disconnect(); _send_data(languageCode+sentence+LEMMATIZER_DELIMITER);
return reply.substr(0,reply.find(LEMMATIZER_DELIMITER)); std::string reply = _receive(512);
_disconnect();
return reply.substr(0,reply.find(LEMMATIZER_DELIMITER));
} catch (std::exception & e) {
_logger.logString("Problem with lemmatization of the sentence", sentence);
_logger.log("Waiting 2 seconds and retrying...");
sleep(2);
}
}
throw ConcordiaException("Can not lemmatize sentence: "+sentence);
} }

View File

@ -9,6 +9,7 @@
#include <concordia/concordia_exception.hpp> #include <concordia/concordia_exception.hpp>
#include "logger.hpp"
class SocketLemmatizer { class SocketLemmatizer {
public: public:
@ -34,6 +35,8 @@ private:
int _sock; int _sock;
struct sockaddr_in _server; struct sockaddr_in _server;
Logger _logger;
}; };
#endif #endif