From 7b6b94fc5f5ddb2706bda5a428371cd525ed875f Mon Sep 17 00:00:00 2001
From: Felipe Bombardelli <felipebombardelli@gmail.com>
Date: Mon, 26 Oct 2015 10:22:28 -0200
Subject: [PATCH] Implement the mecanism to search by last word in the query

---
 autocomplete/CMakeLists.txt |  2 +-
 autocomplete/src/server.cpp | 24 +----------------
 autocomplete/src/task.cpp   | 29 ++++++++++++++++++---
 autocomplete/src/task.hpp   |  1 +
 autocomplete/src/util.cpp   | 51 +++++++++++++++++++++++++++++++++++++
 autocomplete/src/util.hpp   | 28 ++++++++++++++++++++
 6 files changed, 108 insertions(+), 27 deletions(-)
 create mode 100644 autocomplete/src/util.cpp
 create mode 100644 autocomplete/src/util.hpp

diff --git a/autocomplete/CMakeLists.txt b/autocomplete/CMakeLists.txt
index 125511860..53854e71c 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 becc02e92..0cc542e2a 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 6fa8b768f..b1fc909be 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 006c6408f..4343e290c 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 000000000..6573160fa
--- /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 000000000..f1f13cac2
--- /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
-- 
GitLab