From 703a9d94e27cc4cddb36f4ebcba140069512004b Mon Sep 17 00:00:00 2001 From: Ko van der Sloot Date: Tue, 13 Aug 2019 15:34:41 +0200 Subject: [PATCH] major code refactoring PLUS we now create a libmbtserver --- Makefile.am | 3 +- configure.ac | 1 + include/mbtserver/Makefile.am | 2 -- include/mbtserver/MbtServerBase.h | 4 +++ mbtserver.pc.in | 12 +++++++ src/Makefile.am | 9 ++++- src/MbtJSONServerBase.cxx | 59 +++++++++++++++++++++++++++++-- 7 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 mbtserver.pc.in diff --git a/Makefile.am b/Makefile.am index 4c1aaa9..3cb440e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,9 +2,10 @@ ACLOCAL_AMFLAGS = -I m4 --install SUBDIRS = src include docs example -EXTRA_DIST = bootstrap.sh AUTHORS TODO NEWS README.md +EXTRA_DIST = bootstrap.sh AUTHORS TODO NEWS README.md mbtserver.pc pkgconfigdir = $(libdir)/pkgconfig +pkgconfig_DATA = mbtserver.pc ChangeLog: NEWS git pull; git2cl > ChangeLog diff --git a/configure.ac b/configure.ac index 5180f57..c96d9c3 100644 --- a/configure.ac +++ b/configure.ac @@ -75,6 +75,7 @@ LIBS="$LIBS $mbt_LIBS" AC_CONFIG_FILES([ Makefile + mbtserver.pc src/Makefile include/Makefile include/mbtserver/Makefile diff --git a/include/mbtserver/Makefile.am b/include/mbtserver/Makefile.am index 9d4db77..2c690e8 100644 --- a/include/mbtserver/Makefile.am +++ b/include/mbtserver/Makefile.am @@ -1,4 +1,2 @@ -# $Id$ -# $URL$ pkginclude_HEADERS = MbtServerBase.h diff --git a/include/mbtserver/MbtServerBase.h b/include/mbtserver/MbtServerBase.h index d508aef..c858351 100644 --- a/include/mbtserver/MbtServerBase.h +++ b/include/mbtserver/MbtServerBase.h @@ -60,5 +60,9 @@ namespace MbtServer { void StartServer( TiCC::CL_Options& ); void StartJSONServer( TiCC::CL_Options& ); + nlohmann::json TR_to_json( const Tagger::TaggerClass *, + const std::vector& ); + std::vector json_to_TR( const nlohmann::json& ); + } #endif diff --git a/mbtserver.pc.in b/mbtserver.pc.in new file mode 100644 index 0000000..a5513e6 --- /dev/null +++ b/mbtserver.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: mbt +Version: @VERSION@ +Description: mbt server library. +Requires.private: mbt >= 3.5 +Libs: -L${libdir} -lmbtserver +Libs.private: @LIBS@ +Cflags: -I${includedir} diff --git a/src/Makefile.am b/src/Makefile.am index 41ecab2..846c66d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,13 @@ AM_CPPFLAGS = -I@top_srcdir@/include AM_CXXFLAGS = -std=c++11 +LDADD = libmbtserver.la + bin_PROGRAMS = mbtserver -mbtserver_SOURCES = MbtServer.cxx MbtServerBase.cxx MbtJSONServerBase.cxx +mbtserver_SOURCES = MbtServer.cxx + +lib_LTLIBRARIES = libmbtserver.la +libmbtserver_la_LDFLAGS= -version-info 1:0:0 + +libmbtserver_la_SOURCES = MbtServerBase.cxx MbtJSONServerBase.cxx diff --git a/src/MbtJSONServerBase.cxx b/src/MbtJSONServerBase.cxx index 26b1b32..747db26 100644 --- a/src/MbtJSONServerBase.cxx +++ b/src/MbtJSONServerBase.cxx @@ -50,6 +50,61 @@ using TiCC::operator<<; namespace MbtServer { + nlohmann::json TR_to_json( const TaggerClass *context, + const vector& trs ){ + nlohmann::json result = nlohmann::json::array(); + for ( const auto& tr : trs ){ + // lookup the assigned category + nlohmann::json one_entry; + one_entry["word"] = tr.word(); + one_entry["known"] = tr.is_known(); + if ( context->enriched() ){ + one_entry["enrichment"] = tr.enrichment(); + } + one_entry["tag"] = tr.assigned_tag(); + if ( context->confidence_is_set() ){ + one_entry["confidence"] = tr.confidence(); + } + if ( context->distrib_is_set() ){ + one_entry["distribution"] = tr.distribution(); + } + if ( context->distance_is_set() ){ + one_entry["distance"] = tr.distance(); + } + result.push_back( one_entry ); + } // end of output loop through one sentence + return result; + } + + vector json_to_TR( const nlohmann::json& in ){ + // cerr << "json_to_TR( " << in << ")" << endl; + vector result; + for ( auto& i : in ){ + // cerr << "looping json_to_TR( " << i << ")" << endl; + TagResult tr; + tr.set_word( i["word"] ); + if ( i.find("known") != i.end() ){ + tr.set_known( i["known"] == "true" ); + } + tr.set_tag( i["tag"] ); + if ( i.find("confidence") != i.end() ){ + tr.set_confidence( i["confidence"] );; + } + if ( i.find("distance") != i.end() ){ + tr.set_distance( i["distance"] ); + } + if ( i.find("distribution") != i.end() ){ + tr.set_distribution( i["distribution"] ); + } + if ( i.find("enrichment") != i.end() ){ + tr.set_enrichment( i["enrichment"] ); + } + result.push_back( tr ); + } + return result; + } + + string extract_text( nlohmann::json& my_json ){ string result; if ( my_json.is_array() ){ @@ -169,7 +224,7 @@ namespace MbtServer { SDBG << "ALIVE, got " << num << " tags" << endl; if ( num > 0 ){ nw += num; - nlohmann::json got_json = exp->TR_to_json( v ); + nlohmann::json got_json = TR_to_json( exp, v ); SDBG << "voor WRiTE json! " << got_json << endl; args->os() << got_json << endl; SDBG << "WROTE json!" << endl; @@ -186,7 +241,7 @@ namespace MbtServer { int num = v.size(); if ( num > 0 ){ nw += num; - nlohmann::json got_json = exp->TR_to_json( v ); + nlohmann::json got_json = TR_to_json( exp, v ); SDBG << "voor WRiTE json!: " << got_json << endl; args->os() << got_json << endl; SDBG << "WROTE json!" << endl;