Skip to content

Commit

Permalink
[add] new namespace framework to extract common code, erase executabl…
Browse files Browse the repository at this point in the history
…e in examples.
  • Loading branch information
Oneplus committed Nov 15, 2014
1 parent 47629c0 commit 988c197
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 71 deletions.
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,10 @@ bin/
tools/train/lgdpj
tools/train/lgsrl
tools/train/otcws
tools/train/otcws-customized
tools/train/otpos
tools/train/otner
tools/train/maxent
examples/cws
examples/cws_cmdline
examples/multi_cws_cmdline
examples/pos
examples/pos_cmdline
examples/multi_pos_cmdline
examples/par
examples/ner

###############
# data file #
Expand Down
Binary file removed examples/cws
Binary file not shown.
Binary file removed examples/ner
Binary file not shown.
Binary file removed examples/par
Binary file not shown.
Binary file removed examples/pos
Binary file not shown.
Binary file removed examples/srl
Binary file not shown.
25 changes: 25 additions & 0 deletions src/framework/serializable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __LTP_FRAMEWORK_SERIALIZABLE_H__
#define __LTP_FRAMEWORK_SERIALIZABLE_H__

#include <iostream>

namespace ltp {
namespace framework {

class Serializable {
protected:
void write_uint(std::ostream & out, unsigned int val) {
out.write(reinterpret_cast<const char *>(&val), sizeof(unsigned int));
}

unsigned int read_uint(std::istream & in) {
char p[4];
in.read(reinterpret_cast<char*>(p), sizeof(unsigned int));
return *reinterpret_cast<const unsigned int*>(p);
}
};

} // end for framework
} // end for ltp

#endif // end for __LTP_FRAMEWORK_SERIALIZABLE_H__
30 changes: 14 additions & 16 deletions src/ner/model.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#ifndef __LTP_NER_MODEL_H__
#define __LTP_NER_MODEL_H__

#include "framework/serializable.h"
#include "ner/featurespace.h"
#include "ner/parameter.h"
#include "utils/smartmap.hpp"

namespace ltp {
namespace ner {

using namespace ltp::utility;
namespace utils = ltp::utility;
namespace frame = ltp::framework;

class Model {
class Model : public frame::Serializable {
public:
Model();
~Model();
Expand Down Expand Up @@ -38,21 +40,17 @@ class Model {
*/
bool load(std::istream & ifs);
public:
IndexableSmartMap labels;
FeatureSpace space;
Parameters param;

SmartMap<int> cluster_lexicon;
private:
void write_uint(std::ostream & out, unsigned int val) {
out.write(reinterpret_cast<const char *>(&val), sizeof(unsigned int));
}
//!
utils::IndexableSmartMap labels;

unsigned int read_uint(std::istream & in) {
char p[4];
in.read(reinterpret_cast<char*>(p), sizeof(unsigned int));
return *reinterpret_cast<const unsigned int*>(p);
}
//!
FeatureSpace space;

//!
Parameters param;

//!
utils::SmartMap<int> cluster_lexicon;
};

} // end for namespace ner
Expand Down
16 changes: 4 additions & 12 deletions src/parser/model.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#ifndef __LTP_PARSER_MODEL_H__
#define __LTP_PARSER_MODEL_H__

#include "framework/serializable.h"
#include "parser/featurespace.h"
#include "parser/parameters.h"
#include "parser/options.h"

namespace ltp {
namespace parser {

class Model {
namespace frame = ltp::framework;

class Model: public frame::Serializable {
public:
Model() :
_dim(-1),
Expand Down Expand Up @@ -74,17 +77,6 @@ class Model {
* @return bool true on success, otherwise false
*/
bool load(istream & in);

private:
void write_uint(ostream & out, unsigned int val) {
out.write(reinterpret_cast<const char *>(&val), sizeof(unsigned int));
}

unsigned int read_uint(istream & in) {
char p[4];
in.read(reinterpret_cast<char*>(p), sizeof(unsigned int));
return *reinterpret_cast<const unsigned int*>(p);
}
}; // end for class model

} // end for namespace parser
Expand Down
30 changes: 13 additions & 17 deletions src/postagger/model.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#ifndef __LTP_POSTAGGER_MODEL_H__
#define __LTP_POSTAGGER_MODEL_H__

#include "framework/serializable.h"
#include "postagger/featurespace.h"
#include "postagger/parameter.h"

#include "utils/smartmap.hpp"
#include "utils/tinybitset.hpp"

namespace ltp {
namespace postagger {

using namespace ltp::utility;
namespace utils = ltp::utility;
namespace frame = ltp::framework;

class Model {
class Model: public frame::Serializable {
public:
Model();
~Model();
Expand Down Expand Up @@ -40,22 +41,17 @@ class Model {
*/
bool load(std::istream & ifs);
public:
IndexableSmartMap labels;
FeatureSpace space;
Parameters param;
//SmartMap<Bitset> internal_lexicon;
SmartMap<Bitset> external_lexicon;
//! The labels.
utils::IndexableSmartMap labels;

private:
void write_uint(std::ostream & out, unsigned int val) {
out.write(reinterpret_cast<const char *>(&val), sizeof(unsigned int));
}
//! The feature space.
FeatureSpace space;

unsigned int read_uint(std::istream & in) {
char p[4];
in.read(reinterpret_cast<char*>(p), sizeof(unsigned int));
return *reinterpret_cast<const unsigned int*>(p);
}
//! The parameters.
Parameters param;

//SmartMap<Bitset> internal_lexicon;
utils::SmartMap<utils::Bitset> external_lexicon;
};

} // end for namespace postagger
Expand Down
38 changes: 20 additions & 18 deletions src/segmentor/model.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LTP_SEGMENTOR_MODEL_H__
#define __LTP_SEGMENTOR_MODEL_H__

#include "framework/serializable.h"
#include "segmentor/featurespace.h"
#include "segmentor/parameter.h"
#include "utils/smartmap.hpp"
Expand All @@ -9,8 +10,9 @@ namespace ltp {
namespace segmentor {

namespace utils = ltp::utility;
namespace frame = ltp::framework;

class Model {
class Model: public frame::Serializable {
public:
Model();
~Model();
Expand All @@ -28,7 +30,6 @@ class Model {
* save the model to a output stream
*
* @param[out] ofs the output stream
* @param[in] full use to specify if dump full model.
*/
void save(std::ostream & ofs);

Expand All @@ -40,25 +41,26 @@ class Model {
bool load(std::istream & ifs);

public:
int end_time;
bool full;
FeatureSpace space;
Parameters param;
utils::IndexableSmartMap labels;
//! The timestamp for the last training instance.
int end_time;

utils::SmartMap<bool> internal_lexicon;
utils::SmartMap<bool> external_lexicon;
//! Use to specified if dump the full model.
bool full;

private:
void write_uint(std::ostream & out, unsigned int val) {
out.write(reinterpret_cast<const char *>(&val), sizeof(unsigned int));
}
//! The feature space.
FeatureSpace space;

unsigned int read_uint(std::istream & in) {
char p[4];
in.read(reinterpret_cast<char*>(p), sizeof(unsigned int));
return *reinterpret_cast<const unsigned int*>(p);
}
//! The parameter array.
Parameters param;

//! The labels.
utils::IndexableSmartMap labels;

//! The internal lexicon use to extract lexicon features.
utils::SmartMap<bool> internal_lexicon;

//! The external lexicon use to extract lexicon features.
utils::SmartMap<bool> external_lexicon;
};

} // end for namespace segmentor
Expand Down

0 comments on commit 988c197

Please sign in to comment.