Skip to content
Snippets Groups Projects
Commit 7b6b94fc authored by Felipe Bombardelli's avatar Felipe Bombardelli
Browse files

Implement the mecanism to search by last word in the query

parent 7fdf72cb
No related branches found
No related tags found
No related merge requests found
......@@ -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})
......@@ -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 =============================*/
......
......@@ -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();
}
......
......@@ -39,6 +39,7 @@
class Result{
public:
std::string base;
std::vector<std::string> words;
std::vector<float> fators;
float min, size;
......
/*
* 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);
}
/*---------------------------------------------------------------------------*/
/*
* 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment