Skip to content

Commit

Permalink
do some code prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
Yijia Liu committed Nov 12, 2013
1 parent 0af6068 commit 7b64cf0
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 87 deletions.
4 changes: 2 additions & 2 deletions examples/multi_cws_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ int main(int argc, char ** argv) {
return -1;
}
int num_threads=atoi(argv[3]);
if(num_threads<0||num_threads>thread::hardware_concurrency()){
num_threads = thread::hardware_concurrency();
if(num_threads < 0 || num_threads > thread::hardware_concurrency()) {
num_threads = thread::hardware_concurrency();
}
std::cerr << "TRACE: Model is loaded" << std::endl;
std::cerr << "TRACE: Running " << num_threads << " thread(s)" << std::endl;
Expand Down
18 changes: 12 additions & 6 deletions src/ner/featurespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class FeatureSpaceIterator {
// should be careful about the empty dicts
}

FeatureSpaceIterator(utility::SmartMap<int> * dicts,int num_dicts) :
// initialize the iterator with dicts and number of dicts
FeatureSpaceIterator(utility::SmartMap<int> * dicts, int num_dicts) :
_dicts(dicts),
_num_dicts(num_dicts),
_num_dicts(num_dicts),
_i(0),
_state(0) {
++ (*this);
Expand All @@ -33,8 +34,13 @@ class FeatureSpaceIterator {
int id() { return (*_j.value()); }
int tid() { return _i; }

bool operator ==(const FeatureSpaceIterator & other) const { return ((_dicts + _i) == other._dicts); }
bool operator !=(const FeatureSpaceIterator & other) const { return ((_dicts + _i) != other._dicts); }
bool operator ==(const FeatureSpaceIterator & other) const {
return ((_dicts + _i) == other._dicts);
}

bool operator !=(const FeatureSpaceIterator & other) const {
return ((_dicts + _i) != other._dicts);
}

FeatureSpaceIterator & operator = (const FeatureSpaceIterator & other) {
if (this != &other) {
Expand Down Expand Up @@ -98,11 +104,11 @@ class FeatureSpace {
bool load(int num_labeles, std::istream & ifs);

FeatureSpaceIterator begin() {
return FeatureSpaceIterator(dicts,_num_dicts);
return FeatureSpaceIterator(dicts, _num_dicts);
}

FeatureSpaceIterator end() {
return FeatureSpaceIterator(dicts + _num_dicts,_num_dicts);
return FeatureSpaceIterator(dicts + _num_dicts, _num_dicts);
}
private:
int _offset;
Expand Down
9 changes: 6 additions & 3 deletions src/parser/collections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ DictionaryCollections::~DictionaryCollections() {
}

Dictionary * DictionaryCollections::getDictionary(int i) {
if(i<dicts.size())
if (i < dicts.size()) {
return dicts[i];
}

return NULL;
}
int DictionaryCollections::retrieve(int tid, const char * key, bool create) {
return dicts[tid]->retrieve(key, create);
}

size_t DictionaryCollections::dim() const{
size_t DictionaryCollections::dim() const {
return idx;
}

int DictionaryCollections::size(){
int DictionaryCollections::size() {
return dicts.size();
}

void DictionaryCollections::dump(ostream & out) {
char chunk[32];
unsigned int sz = dicts.size();
Expand Down
13 changes: 11 additions & 2 deletions src/parser/collections.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,19 @@ class DictionaryCollections {
*/
int retrieve(int tid, const char * key, bool create);

/*Get the ith Dictionary*/
/*
* Get the ith Dictionary
*
* @param[in] i the index of the dictionary
* @return Dictionary * the dictionary
*/
Dictionary * getDictionary(int i);

/*Get size of dicts*/
/*
* Get size of dicts
*
* @return int the size of the dictionary
*/
int size();

public:
Expand Down
19 changes: 10 additions & 9 deletions src/parser/featurespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ int FeatureSpace::index(int gid, int tid, const char * key, int lid) {
return bid * _num_deprels + lid + offsets[gid];
}


void FeatureSpace::build_feature_space_truncate(int num_deprels) {
_num_deprels = num_deprels;
allocate_dictionary_groups();
Expand All @@ -35,23 +34,25 @@ void FeatureSpace::set_offset_truncate() {
_num_features=0;
offsets[DEP]=_offset;
if(feat_opt.use_dependency) {
_num_features+=groups[DEP]->dim();
_offset+=groups[DEP]->dim() * _num_deprels;
_num_features += groups[DEP]->dim();
_offset += groups[DEP]->dim() * _num_deprels;
}

offsets[SIB]=_offset;
if(feat_opt.use_sibling) {
_num_features+=groups[SIB]->dim();
_offset+=groups[SIB]->dim() * _num_deprels;
_num_features += groups[SIB]->dim();
_offset += groups[SIB]->dim() * _num_deprels;
}

offsets[GRD]=_offset;
if(feat_opt.use_grand) {
_num_features+=groups[GRD]->dim();
_offset+=groups[GRD]->dim() * _num_deprels;
_num_features += groups[GRD]->dim();
_offset += groups[GRD]->dim() * _num_deprels;
}
}
int FeatureSpace::build_feature_space(int num_deprels, const std::vector<Instance *> & instances) {

int FeatureSpace::build_feature_space(int num_deprels,
const std::vector<Instance *> & instances) {
_num_deprels = num_deprels;
// allocate dictionary groups according to the options
allocate_dictionary_groups();
Expand Down Expand Up @@ -152,7 +153,7 @@ int FeatureSpace::build_feature_space(int num_deprels, const std::vector<Instanc
offset += groups[GRDSIB]->dim() * _num_deprels;
}*/

return _offset;
return _offset;
}

int FeatureSpace::allocate_dictionary_groups() {
Expand Down
56 changes: 35 additions & 21 deletions src/parser/featurespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,46 @@ class FeatureSpaceIterator {
int tid() { return _i; }

bool end() {
int x=(*_dicts).size();
if((x)==_i) {
// std::cout<<"when i is "<<_i <<" size is "<<x<<std::endl;
int x = _dicts->size();
if((x) == _i) {
return true;
}
}
return false;
}

FeatureSpaceIterator & operator =(const FeatureSpaceIterator & other) {
_dicts=other._dicts;
_i=other._i;
_state=other._state;
_dicts = other._dicts;
_i = other._i;
_state = other._state;

return *this;
}

void operator ++() {
switch (_state) {
case 0:
for (_i=0;;++_i) {
// std::cout<<"size "<<(*_dicts).size()<<" _i"<<_i<<std::endl;
if(!(*_dicts).getDictionary(_i))
return;
if ((*_dicts).getDictionary(_i)->database.begin() == (*_dicts).getDictionary(_i)->database.end()){
_state=1;
for (_i = 0; ; ++_i) {
if(NULL == _dicts->getDictionary(_i)) {
return;
}

if (_dicts->getDictionary(_i)->database.begin() ==
_dicts->getDictionary(_i)->database.end()) {
_state = 1;
return;
}
for (_j = (*_dicts).getDictionary(_i)->database.begin();_j!=(*_dicts).getDictionary(_i)->database.end(); ++_j) {

for (_j = _dicts->getDictionary(_i)->database.begin();
_j != _dicts->getDictionary(_i)->database.end();
++_j) {
_state = 1;
return;
case 1:;
}
}
}
}

int getI() {
return _i;
}
Expand All @@ -75,10 +82,10 @@ class FeatureSpaceIterator {
}

private:
int _i;
int _state;
utility::SmartMap<int>::const_iterator _j;
DictionaryCollections * _dicts;
int _i;
int _state;
utility::SmartMap<int>::const_iterator _j;
DictionaryCollections * _dicts;
};

/*
Expand Down Expand Up @@ -114,13 +121,20 @@ class FeatureSpace {
}

/*
* Build feature space from the instances
* Build feature space from the instances. For dependency parsing,
* there is no negative feature, so the feature space can be maintained
* once the training instance is given.
*
* @param[in] instances the instances
* @param[in] num_deprels
* @param[in] instances the instances
*/
int build_feature_space( int num_deprels, const std::vector<Instance *> & instances);

/*Build feature space for truncate, just like the function above*/
/*
* Build feature space for truncate, just like the function above
*
* @param
*/
void build_feature_space_truncate(int num_deprels);

/*After copy the dic item from the model that not equal to Zero,set the offset */
Expand Down
Loading

0 comments on commit 7b64cf0

Please sign in to comment.