From dea43086183b7cf8a4744cf44ee9420a080d0081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Jaworski?= Date: Mon, 31 Dec 2018 00:50:24 +0100 Subject: [PATCH] repaired lemmatizer --- concordia-server/socket_lemmatizer.cpp | 39 ++++++++++++++++++++------ concordia-server/socket_lemmatizer.hpp | 3 ++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/concordia-server/socket_lemmatizer.cpp b/concordia-server/socket_lemmatizer.cpp index 0cd6aee..137f4ab 100644 --- a/concordia-server/socket_lemmatizer.cpp +++ b/concordia-server/socket_lemmatizer.cpp @@ -1,6 +1,9 @@ #include "socket_lemmatizer.hpp" +#include + #include "config.hpp" + #include SocketLemmatizer::SocketLemmatizer(int port) throw(ConcordiaException) : @@ -79,20 +82,38 @@ bool SocketLemmatizer::_send_data(std::string data) std::string SocketLemmatizer::_receive(int size=512) { char buffer[size]; - std::string reply; + std::string reply = ""; //Receive a reply from the server - if(recv(_sock , buffer , sizeof(buffer) , 0) < 0) { - throw ConcordiaException("Receive failed"); + bool dataAvailable = true; + 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; } std::string SocketLemmatizer::lemmatizeSentence(std::string languageCode, std::string sentence) { - _connect(); - _send_data(languageCode+sentence+LEMMATIZER_DELIMITER); - std::string reply = _receive(512); - _disconnect(); - return reply.substr(0,reply.find(LEMMATIZER_DELIMITER)); + for (int i=0;i<5;i++) { + try { + _connect(); + _send_data(languageCode+sentence+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); } diff --git a/concordia-server/socket_lemmatizer.hpp b/concordia-server/socket_lemmatizer.hpp index 4f5e9e9..13d5949 100644 --- a/concordia-server/socket_lemmatizer.hpp +++ b/concordia-server/socket_lemmatizer.hpp @@ -9,6 +9,7 @@ #include +#include "logger.hpp" class SocketLemmatizer { public: @@ -34,6 +35,8 @@ private: int _sock; struct sockaddr_in _server; + + Logger _logger; }; #endif