diff --git a/autocomplete/CMakeLists.txt b/autocomplete/CMakeLists.txt index 12551186036cc8d4335e1ff0f79b68e795218b6a..53854e71cf01778fdf28296591c24d9c4db25651 100644 --- a/autocomplete/CMakeLists.txt +++ b/autocomplete/CMakeLists.txt @@ -8,7 +8,7 @@ pkg_check_modules(MICROHTTPD libmicrohttpd) set (EXECUTABLE_NAME "autocomplete_server") -add_executable(${EXECUTABLE_NAME} src/main.cpp src/server.cpp src/task.cpp) +add_executable(${EXECUTABLE_NAME} src/main.cpp src/server.cpp src/task.cpp src/util.cpp) target_link_libraries(${EXECUTABLE_NAME} ${MICROHTTPD_LIBRARIES}) diff --git a/autocomplete/src/server.cpp b/autocomplete/src/server.cpp index becc02e92843d882f12b79899804976d557ab6a7..0cc542e2ab0fb2126bd1a3cc20bc2a994c0a0d7f 100644 --- a/autocomplete/src/server.cpp +++ b/autocomplete/src/server.cpp @@ -24,6 +24,7 @@ #include <stdio.h> #include <sys/stat.h> #include <string.h> +#include "util.hpp" using namespace std; @@ -31,29 +32,6 @@ using namespace std; -/*================================= UTIL ==================================*/ - -void explode(vector<string>& out, char qry, string text){ - out.clear(); - string buffer; - for (int i=0; i<text.size(); i++){ - char c = text[i]; - if ( c == qry ){ - if ( buffer.size() != 0 ){ - out.push_back(buffer); - buffer = ""; - } - } else { - buffer += c; - } - } - if ( buffer.size() != 0 ) - out.push_back(buffer); -} - -/*---------------------------------------------------------------------------*/ - - /*============================ EMBEDDEDSERVER =============================*/ diff --git a/autocomplete/src/task.cpp b/autocomplete/src/task.cpp index 6fa8b768f52e58e0ae62afeb25f73e03b20f0cb5..b1fc909be140af5da55bfa24b93ba0e264d2639e 100644 --- a/autocomplete/src/task.cpp +++ b/autocomplete/src/task.cpp @@ -23,6 +23,7 @@ #include "task.hpp" #include <stdio.h> #include <stdlib.h> +#include "util.hpp" using namespace std; @@ -58,9 +59,9 @@ std::string Result::json(){ res += "["; if ( words.size() > 0 ){ for (int i=words.size()-1; i>0; i--){ - res += "{\"data\":\""+words[i]+"\",\"value\":\""+words[i]+"\"},"; + res += "{\"data\":\""+base+words[i]+"\",\"value\":\""+base+words[i]+"\"},"; } - res += "{\"data\":\""+words[0]+"\",\"value\":\""+words[0]+"\"}"; + res += "{\"data\":\""+base+words[0]+"\",\"value\":\""+base+words[0]+"\"}"; } res += "]"; return res; @@ -84,8 +85,28 @@ void Task::load(std::string filename){ printf("Qtde: %d\n",size); } -std::string Task::find(std::string query){ +std::string Task::find(std::string _query){ Result result; + + string query; + vector<string> query_words; + explode(query_words,' ',_query); + int last; + for (last=query_words.size()-1; last>=0; last--){ + if ( query_words[last] != "" ){ + query = query_words[last]; + break; + } + } + + string base; + for (int i=0; i<last; i++){ + base += query_words[i]; + base += " "; + } + result.base = base; + + for (int i=0; i<words.size(); i++){ string& word = words[i]; float fator = 0.0; @@ -99,6 +120,8 @@ std::string Task::find(std::string query){ } result.push(word, fator); } + + return result.json(); } diff --git a/autocomplete/src/task.hpp b/autocomplete/src/task.hpp index 006c6408fae8940ed484e4c13164ba15a567bb7a..4343e290c860377ce5f0abbc76219fa44c0003dd 100644 --- a/autocomplete/src/task.hpp +++ b/autocomplete/src/task.hpp @@ -39,6 +39,7 @@ class Result{ public: + std::string base; std::vector<std::string> words; std::vector<float> fators; float min, size; diff --git a/autocomplete/src/util.cpp b/autocomplete/src/util.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6573160faf16b579cbee204d6979b7dd7dda814c --- /dev/null +++ b/autocomplete/src/util.cpp @@ -0,0 +1,51 @@ +/* + * This file is part of Autocomplete-Server + * + * Autocomplete-Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <http://www.gnu.org/licenses/>. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + + +/*=============================== HEADER ==================================*/ + +#include "util.hpp" + + +using namespace std; + +/*---------------------------------------------------------------------------*/ + + + +/*================================= UTIL ==================================*/ + +void explode(std::vector<std::string>& out, char qry, std::string text){ + out.clear(); + string buffer; + for (int i=0; i<text.size(); i++){ + char c = text[i]; + if ( c == qry ){ + if ( buffer.size() != 0 ){ + out.push_back(buffer); + buffer = ""; + } + } else { + buffer += c; + } + } + if ( buffer.size() != 0 ) + out.push_back(buffer); +} + +/*---------------------------------------------------------------------------*/ diff --git a/autocomplete/src/util.hpp b/autocomplete/src/util.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f1f13cac24582f6bc78c21c76ec72f1e3968da3c --- /dev/null +++ b/autocomplete/src/util.hpp @@ -0,0 +1,28 @@ +/* + * This file is part of Autocomplete-Server + * + * Autocomplete-Server is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Foobar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <http://www.gnu.org/licenses/>. + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef UTIL_HPP +#define UTIL_HPP + +#include <vector> +#include <string> + +void explode(std::vector<std::string>& out, char qry, std::string text); + + +#endif