working addSentence
This commit is contained in:
parent
fa3f4db781
commit
de5d1f4a63
@ -10,6 +10,8 @@ add_executable(concordia_server_process
|
|||||||
string_param.cpp
|
string_param.cpp
|
||||||
int_param.cpp
|
int_param.cpp
|
||||||
logger.cpp
|
logger.cpp
|
||||||
|
int_array_param.cpp
|
||||||
|
simple_search_result.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(concordia_server_process fcgi fcgi++ pq concordia config++ log4cpp ${Boost_LIBRARIES} divsufsort utf8case)
|
target_link_libraries(concordia_server_process fcgi fcgi++ pq concordia config++ log4cpp ${Boost_LIBRARIES} divsufsort utf8case)
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
@ -57,7 +57,9 @@ void DBconnection::endTransaction() throw(ConcordiaException) {
|
|||||||
PGresult * DBconnection::execute(std::string query) throw(ConcordiaException) {
|
PGresult * DBconnection::execute(std::string query) throw(ConcordiaException) {
|
||||||
if (_connection != NULL) {
|
if (_connection != NULL) {
|
||||||
PGresult * result = PQexec(_connection, query.c_str());
|
PGresult * result = PQexec(_connection, query.c_str());
|
||||||
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
|
if (PQresultStatus(result) != PGRES_COMMAND_OK &&
|
||||||
|
PQresultStatus(result) != PGRES_TUPLES_OK) {
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "query execution failed with message: ";
|
ss << "query execution failed with message: ";
|
||||||
ss << PQresultErrorMessage(result) << std::endl;
|
ss << PQresultErrorMessage(result) << std::endl;
|
||||||
@ -96,7 +98,9 @@ PGresult * DBconnection::execute(std::string query,
|
|||||||
paramFormats,
|
paramFormats,
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
|
if (PQresultStatus(result) != PGRES_COMMAND_OK &&
|
||||||
|
PQresultStatus(result) != PGRES_TUPLES_OK) {
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "parametrized query execution failed with message: ";
|
ss << "parametrized query execution failed with message: ";
|
||||||
ss << PQresultErrorMessage(result) << std::endl;
|
ss << PQresultErrorMessage(result) << std::endl;
|
||||||
@ -111,4 +115,13 @@ PGresult * DBconnection::execute(std::string query,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DBconnection::clearResult(PGresult * result) {
|
||||||
|
PQclear(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
int DBconnection::getIntValue(PGresult * result, int row, int col) {
|
||||||
|
char * valueStr = PQgetvalue(result,row,col);
|
||||||
|
return strtol(valueStr, NULL, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,6 +26,11 @@ public:
|
|||||||
|
|
||||||
PGresult * execute(std::string query,
|
PGresult * execute(std::string query,
|
||||||
std::vector<QueryParam*> params) throw(ConcordiaException);
|
std::vector<QueryParam*> params) throw(ConcordiaException);
|
||||||
|
|
||||||
|
void clearResult(PGresult * result);
|
||||||
|
|
||||||
|
int getIntValue(PGresult * result, int row, int col);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
|
@ -1,96 +0,0 @@
|
|||||||
#include <libpq-fe.h>
|
|
||||||
|
|
||||||
static void
|
|
||||||
exit_nicely(PGconn *conn)
|
|
||||||
{
|
|
||||||
PQfinish(conn);
|
|
||||||
throw ConcordiaException("Closing connection and shutting down");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const char *conninfo;
|
|
||||||
PGconn *conn;
|
|
||||||
PGresult *res;
|
|
||||||
int nFields;
|
|
||||||
int i,
|
|
||||||
j;
|
|
||||||
|
|
||||||
conninfo = "dbname=concordia_server user=concordia";
|
|
||||||
|
|
||||||
/* Make a connection to the database */
|
|
||||||
conn = PQconnectdb(conninfo);
|
|
||||||
|
|
||||||
/* Check to see that the backend connection was successfully made */
|
|
||||||
if (PQstatus(conn) != CONNECTION_OK)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Connection to database failed: %s",
|
|
||||||
PQerrorMessage(conn));
|
|
||||||
exit_nicely(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Our test case here involves using a cursor, for which we must be inside
|
|
||||||
* a transaction block. We could do the whole thing with a single
|
|
||||||
* PQexec() of "select * from pg_database", but that's too trivial to make
|
|
||||||
* a good example.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Start a transaction block */
|
|
||||||
res = PQexec(conn, "BEGIN");
|
|
||||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
|
|
||||||
PQclear(res);
|
|
||||||
exit_nicely(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Should PQclear PGresult whenever it is no longer needed to avoid memory
|
|
||||||
* leaks
|
|
||||||
*/
|
|
||||||
PQclear(res);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Fetch rows from pg_database, the system catalog of databases
|
|
||||||
*/
|
|
||||||
res = PQexec(conn, "SELECT * FROM language");
|
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
|
|
||||||
PQclear(res);
|
|
||||||
exit_nicely(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonWriter.String("attributes");
|
|
||||||
jsonWriter.StartArray();
|
|
||||||
/* first, print out the attribute names */
|
|
||||||
nFields = PQnfields(res);
|
|
||||||
for (i = 0; i < nFields; i++)
|
|
||||||
jsonWriter.String(PQfname(res, i));
|
|
||||||
|
|
||||||
jsonWriter.EndArray();
|
|
||||||
|
|
||||||
|
|
||||||
jsonWriter.String("values");
|
|
||||||
jsonWriter.StartArray();
|
|
||||||
/* next, print out the rows */
|
|
||||||
for (i = 0; i < PQntuples(res); i++)
|
|
||||||
{
|
|
||||||
jsonWriter.StartArray();
|
|
||||||
for (j = 0; j < nFields; j++)
|
|
||||||
jsonWriter.String(PQgetvalue(res, i, j));
|
|
||||||
jsonWriter.EndArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonWriter.EndArray();
|
|
||||||
|
|
||||||
PQclear(res);
|
|
||||||
|
|
||||||
/* end the transaction */
|
|
||||||
res = PQexec(conn, "END");
|
|
||||||
PQclear(res);
|
|
||||||
|
|
||||||
/* close the connection to the database and cleanup */
|
|
||||||
PQfinish(conn);
|
|
||||||
|
|
||||||
|
|
34
concordia-server/int_array_param.cpp
Normal file
34
concordia-server/int_array_param.cpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include "int_array_param.hpp"
|
||||||
|
|
||||||
|
#include <boost/foreach.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
IntArrayParam::IntArrayParam(std::vector<int> array) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "{";
|
||||||
|
int index = 0;
|
||||||
|
BOOST_FOREACH(int & number, array) {
|
||||||
|
ss << number;
|
||||||
|
if (index < array.size() - 1) {
|
||||||
|
ss << ",";
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
ss << "}";
|
||||||
|
_arrayString = ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
IntArrayParam::~IntArrayParam() {
|
||||||
|
}
|
||||||
|
|
||||||
|
const char * IntArrayParam::getValue() {
|
||||||
|
return _arrayString.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
const int IntArrayParam::getLength() {
|
||||||
|
return _arrayString.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const int IntArrayParam::isBinary() {
|
||||||
|
return 0;
|
||||||
|
}
|
27
concordia-server/int_array_param.hpp
Normal file
27
concordia-server/int_array_param.hpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef INT_ARRAY_PARAM_HDR
|
||||||
|
#define INT_ARRAY_PARAM_HDR
|
||||||
|
|
||||||
|
#include "query_param.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class IntArrayParam : public QueryParam {
|
||||||
|
public:
|
||||||
|
/*! Constructor.
|
||||||
|
*/
|
||||||
|
IntArrayParam(std::vector<int> array);
|
||||||
|
/*! Destructor.
|
||||||
|
*/
|
||||||
|
virtual ~IntArrayParam();
|
||||||
|
|
||||||
|
const char * getValue();
|
||||||
|
|
||||||
|
const int getLength();
|
||||||
|
|
||||||
|
const int isBinary();
|
||||||
|
private:
|
||||||
|
std::string _arrayString;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,5 @@
|
|||||||
#include "searcher_controller.hpp"
|
#include "searcher_controller.hpp"
|
||||||
|
|
||||||
#include <concordia/substring_occurence.hpp>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
SearcherController::SearcherController(boost::shared_ptr<Concordia> concordia)
|
SearcherController::SearcherController(boost::shared_ptr<Concordia> concordia)
|
||||||
@ -13,17 +12,15 @@ SearcherController::~SearcherController() {
|
|||||||
|
|
||||||
|
|
||||||
void SearcherController::simpleSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & pattern) {
|
void SearcherController::simpleSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & pattern) {
|
||||||
std::vector<SubstringOccurence> result = _concordia->simpleSearch(pattern);
|
std::vector<SubstringOccurence> results = _concordia->simpleSearch(pattern);
|
||||||
|
|
||||||
jsonWriter.StartObject();
|
jsonWriter.StartObject();
|
||||||
jsonWriter.String("status");
|
jsonWriter.String("status");
|
||||||
jsonWriter.String("success");
|
jsonWriter.String("success");
|
||||||
jsonWriter.String("count");
|
jsonWriter.String("results");
|
||||||
jsonWriter.Int(result.size());
|
jsonWriter.StartArray();
|
||||||
jsonWriter.String("firstId");
|
|
||||||
jsonWriter.Int(result.at(0).getId());
|
jsonWriter.EndArray();
|
||||||
jsonWriter.String("firstOffset");
|
|
||||||
jsonWriter.Int(result.at(0).getOffset());
|
|
||||||
jsonWriter.EndObject();
|
jsonWriter.EndObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
8
concordia-server/simple_search_result.cpp
Normal file
8
concordia-server/simple_search_result.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include "simple_search_result.hpp"
|
||||||
|
|
||||||
|
SimpleSearchResult::SimpleSearchResult() {
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleSearchResult::~SimpleSearchResult() {
|
||||||
|
}
|
||||||
|
|
25
concordia-server/simple_search_result.hpp
Normal file
25
concordia-server/simple_search_result.hpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef SIMPLE_SEARCH_RESULT_HDR
|
||||||
|
#define SIMPLE_SEARCH_RESULT_HDR
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class SimpleSearchResult {
|
||||||
|
public:
|
||||||
|
/*! Constructor.
|
||||||
|
*/
|
||||||
|
SimpleSearchResult();
|
||||||
|
/*! Destructor.
|
||||||
|
*/
|
||||||
|
virtual ~SimpleSearchResult();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int id;
|
||||||
|
|
||||||
|
std::string _matchedFragment;
|
||||||
|
|
||||||
|
std::string _sourceSegment;
|
||||||
|
|
||||||
|
std::string _targetSegment;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -4,10 +4,12 @@
|
|||||||
#include "query_param.hpp"
|
#include "query_param.hpp"
|
||||||
#include "string_param.hpp"
|
#include "string_param.hpp"
|
||||||
#include "int_param.hpp"
|
#include "int_param.hpp"
|
||||||
|
#include "int_array_param.hpp"
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <libpq-fe.h>
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
|
#include <concordia/token_annotation.hpp>
|
||||||
|
|
||||||
UnitDAO::UnitDAO() {
|
UnitDAO::UnitDAO() {
|
||||||
}
|
}
|
||||||
@ -22,21 +24,33 @@ int UnitDAO::addSentence(
|
|||||||
|
|
||||||
DBconnection connection;
|
DBconnection connection;
|
||||||
connection.startTransaction();
|
connection.startTransaction();
|
||||||
std::string query = "INSERT INTO unit(source_segment, target_segment, tm_id) values($1::text,$2::text,$3::integer)";
|
std::string query = "INSERT INTO unit(source_segment, target_segment, tm_id, source_tokens) values($1::text,$2::text,$3::integer,$4) RETURNING id";
|
||||||
std::vector<QueryParam*> params;
|
std::vector<QueryParam*> params;
|
||||||
params.push_back(new StringParam(sourceSentence->getSentence()));
|
params.push_back(new StringParam(sourceSentence->getSentence()));
|
||||||
params.push_back(new StringParam(targetSentence));
|
params.push_back(new StringParam(targetSentence));
|
||||||
params.push_back(new IntParam(tmId));
|
params.push_back(new IntParam(tmId));
|
||||||
|
params.push_back(new IntArrayParam(_getTokenPositions(sourceSentence)));
|
||||||
|
|
||||||
connection.execute(query, params);
|
PGresult * result = connection.execute(query, params);
|
||||||
|
int newId = connection.getIntValue(result, 0, 0);
|
||||||
|
connection.clearResult(result);
|
||||||
connection.endTransaction();
|
connection.endTransaction();
|
||||||
|
|
||||||
BOOST_FOREACH (QueryParam * param, params) {
|
BOOST_FOREACH (QueryParam * param, params) {
|
||||||
delete param;
|
delete param;
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo return new unit id
|
return newId;
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<int> UnitDAO::_getTokenPositions(boost::shared_ptr<TokenizedSentence> ts) {
|
||||||
|
std::vector<int> result;
|
||||||
|
BOOST_FOREACH(const TokenAnnotation & token, ts->getTokens()) {
|
||||||
|
result.push_back(token.getStart());
|
||||||
|
result.push_back(token.getEnd());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define UNIT_DAO_HDR
|
#define UNIT_DAO_HDR
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <concordia/tokenized_sentence.hpp>
|
#include <concordia/tokenized_sentence.hpp>
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
@ -20,7 +21,7 @@ public:
|
|||||||
std::string & targetSentence,
|
std::string & targetSentence,
|
||||||
int tmId);
|
int tmId);
|
||||||
private:
|
private:
|
||||||
|
std::vector<int> _getTokenPositions(boost::shared_ptr<TokenizedSentence> ts);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -18,6 +18,7 @@ CREATE TABLE unit (
|
|||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
tm_id integer,
|
tm_id integer,
|
||||||
source_segment text,
|
source_segment text,
|
||||||
target_segment text
|
target_segment text,
|
||||||
|
source_tokens integer[]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
1
db/query.txt
Normal file
1
db/query.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
select substring(source_segment,source_tokens[start_token*2+1]+1,source_tokens[end_token*2+2]-source_tokens[start_token*2+1]) from unit where id = 3;
|
@ -1,6 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
curl -H "Content-Type: application/json" -X POST -d '{"operation":"addSentence", "sourceSentence":"zupełnie nowe zdanie", "targetSentence":"zażółć gęślą jaźńZAŻÓŁĆ GĘŚLĄ JAŹŃ", "tmId":1234782314}' http://localhost
|
curl -H "Content-Type: application/json" -X POST -d '{"operation":"addSentence", "sourceSentence":"zu\"pełnie nowe zdanie", "targetSentence":"zażółć gęślą jaźńZAŻÓŁĆ GĘŚLĄ JAŹŃ", "tmId":1234782314}' http://localhost
|
||||||
|
|
||||||
|
|
||||||
#curl -H "Content-Type: application/json" -X POST -d '{"operation":"simpleSearch", "sentence":"zupełnie nowe"}' http://localhost
|
#curl -H "Content-Type: application/json" -X POST -d '{"operation":"simpleSearch", "sentence":"zupełnie nowe"}' http://localhost
|
||||||
|
Loading…
Reference in New Issue
Block a user