working simple search

This commit is contained in:
rjawor 2019-08-29 23:17:16 +02:00
parent 10641ecb6c
commit 63c44d55ea
6 changed files with 53 additions and 10 deletions

View File

@ -99,6 +99,10 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
jsonWriter.String(tm.getSourceLanguageCode().c_str());
jsonWriter.String("targetLanguageCode");
jsonWriter.String(tm.getTargetLanguageCode().c_str());
jsonWriter.String("lemmatized");
jsonWriter.Bool(tm.isLemmatized());
jsonWriter.String("pairedTmId");
jsonWriter.Int(tm.getPairedTmId());
jsonWriter.EndObject();
}
jsonWriter.EndArray();
@ -250,6 +254,26 @@ std::string ConcordiaServer::handleRequest(std::string & requestString) {
jsonWriter.String("newTmId");
jsonWriter.Int(newId);
jsonWriter.EndObject();
} else if (operation == ADD_PAIRED_TMS_OP) {
int sourceLangId = _getIntParameter(d, SOURCE_LANG_PARAM);
int targetLangId = _getIntParameter(d, TARGET_LANG_PARAM);
std::string name = _getStringParameter(d, NAME_PARAM);
std::string nameLemmatized = name;
nameLemmatized.append("_lemmatized");
int lemmatizedTmId = _tmDAO.addTm(sourceLangId, targetLangId, name, true);
_addTm(lemmatizedTmId);
int nonLemmatizedTmId = _tmDAO.addTm(sourceLangId, targetLangId, name, false, lemmatizedTmId);
_addTm(nonLemmatizedTmId);
jsonWriter.StartObject();
jsonWriter.String("status");
jsonWriter.String("success");
jsonWriter.String("lemmatizedTmId");
jsonWriter.Int(lemmatizedTmId);
jsonWriter.String("nonLemmatizedTmId");
jsonWriter.Int(nonLemmatizedTmId);
jsonWriter.EndObject();
} else if (operation == ADD_SOURCE_OP) {
int externalId = _getIntParameter(d, EXTERNAL_ID_PARAM);
std::string name = _getStringParameter(d, NAME_PARAM);

View File

@ -46,6 +46,7 @@
#define CONCORDIA_SEARCH_OP "concordiaSearch"
#define CONCORDIA_PHRASE_SEARCH_OP "concordiaPhraseSearch"
#define ADD_TM_OP "addTm"
#define ADD_PAIRED_TMS_OP "addPairedTms"
#define ADD_SOURCE_OP "addSource"
#define ADD_SOURCES_OP "addSources"

View File

@ -28,21 +28,30 @@ void SearcherController::simpleSearch(rapidjson::Writer<rapidjson::StringBuffer>
// tmId should point to non-lemmatized memory
boost::ptr_map<int,Concordia>::iterator it = _concordiasMap->find(tmId);
if (it != _concordiasMap->end()) {
Logger::logString("simple search for pattern", pattern);
TokenizedSentence tokenizedPattern = it->second->tokenize(pattern, false, false);
Logger::logInt("queried tm", tmId);
Tm tm = _tmDAO.getTm(tmId);
Logger::logInt("paired tm", tm.getPairedTmId());
LemmatizerResult lemmatizerResult = _lemmatizerFacade->lemmatizeSentence(
tm.getSourceLanguageCode(), tokenizedPattern.getTokenizedSentence());
std::string pattern = tokenizedPattern.getTokenizedSentence();
Logger::logString("tokenized pattern", tokenizedPattern.getTokenizedSentence());
Logger::logString("lemmatized pattern", lemmatizerResult.getLemmatizedSentence());
if (lemmatizerResult.isFirstLemmatized()) {
Logger::log("searching in paired memory");
// search in lemmatized memory
it = _concordiasMap->find(tm.getPairedTmId());
pattern = lemmatizerResult.getLemmatizedSentence();
Logger::logString("Changing pattern to lemmatized", pattern);
if (it == _concordiasMap->end()) {
JsonGenerator::signalError(jsonWriter, "paired tm not found!");
return;
}
}
Logger::logString("Final pattern", pattern);
SimpleSearchResult result = _unitDAO.getSimpleSearchResult(
it->second->simpleSearch(lemmatizerResult.getLemmatizedSentence(), true));
it->second->simpleSearch(pattern, true));
jsonWriter.StartObject();
jsonWriter.String("status");
jsonWriter.String("success");

View File

@ -4,10 +4,12 @@ Tm::Tm(const int id,
const std::string & name,
const std::string & sourceLanguageCode,
const std::string & targetLanguageCode,
const bool lemmatized,
const int pairedTmId) :
_id(id),_name(name),
_sourceLanguageCode(sourceLanguageCode),
_targetLanguageCode(targetLanguageCode),
_lemmatized(lemmatized),
_pairedTmId(pairedTmId) {
}

View File

@ -12,6 +12,7 @@ public:
const std::string & name,
const std::string & sourceLanguageCode,
const std::string & targetLanguageCode,
const bool lemmatized,
const int pairedTmId);
/*! Destructor.
@ -34,6 +35,10 @@ public:
return _targetLanguageCode;
}
bool isLemmatized() const {
return _lemmatized;
}
int getPairedTmId() const {
return _pairedTmId;
}
@ -47,6 +52,8 @@ private:
std::string _targetLanguageCode;
bool _lemmatized;
int _pairedTmId;
};

View File

@ -5,7 +5,6 @@
#include "int_param.hpp"
#include "bool_param.hpp"
#include "int_array_param.hpp"
#include "logger.hpp"
#include <boost/foreach.hpp>
#include <libpq-fe.h>
@ -36,15 +35,16 @@ std::vector<Tm> TmDAO::getTms() {
std::vector<Tm> result;
DBconnection connection;
connection.startTransaction();
std::string query = "select tm.id, tm.name, src_lang.code as src_code, trg_lang.code as trg_code, paired_tm_id from tm inner join language as src_lang on src_lang.id = tm.source_lang_id inner join language as trg_lang on trg_lang.id = tm.target_lang_id;";
std::string query = "select tm.id, tm.name, src_lang.code as src_code, trg_lang.code as trg_code, lemmatized, paired_tm_id from tm inner join language as src_lang on src_lang.id = tm.source_lang_id inner join language as trg_lang on trg_lang.id = tm.target_lang_id;";
PGresult * dbResult = connection.execute(query);
for (int i=0;i<connection.getRowCount(dbResult);i++) {
int id = connection.getIntValue(dbResult, i, 0);
std::string name = connection.getStringValue(dbResult, i, 1);
std::string sourceLanguageCode = connection.getStringValue(dbResult, i, 2);
std::string targetLanguageCode = connection.getStringValue(dbResult, i, 3);
int pairedTmId = connection.getIntValue(dbResult, i, 4);
result.push_back(Tm(id, name, sourceLanguageCode, targetLanguageCode, pairedTmId));
bool lemmatized = connection.getBoolValue(dbResult, i, 4);
int pairedTmId = connection.getIntValue(dbResult, i, 5);
result.push_back(Tm(id, name, sourceLanguageCode, targetLanguageCode, lemmatized, pairedTmId));
}
connection.clearResult(dbResult);
connection.endTransaction();
@ -58,11 +58,11 @@ std::vector<Tm> TmDAO::getTms() {
int TmDAO::addTm(const int sourceLangId, const int targetLangId, const std::string name) {
addTm(sourceLangId, targetLangId, name, false, -1);
return addTm(sourceLangId, targetLangId, name, false, -1);
}
int TmDAO::addTm(const int sourceLangId, const int targetLangId, const std::string name, bool lemmatized) {
addTm(sourceLangId, targetLangId, name, lemmatized, -1);
return addTm(sourceLangId, targetLangId, name, lemmatized, -1);
}
int TmDAO::addTm(const int sourceLangId, const int targetLangId, const std::string name, bool lemmatized, int pairedTmId) {
@ -105,6 +105,6 @@ Tm TmDAO::getTm(int tmId) {
connection.clearResult(dbResult);
connection.endTransaction();
return Tm(id, name, sourceLanguageCode, targetLanguageCode, pairedTmId);
return Tm(id, name, sourceLanguageCode, targetLanguageCode, lemmatized, pairedTmId);
}