-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optional support for doing perfect hashing of feature strings to save…
… lots of memory
- Loading branch information
Chris Dyer
committed
Sep 13, 2011
1 parent
c41704e
commit e7993fb
Showing
9 changed files
with
269 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "config.h" | ||
|
||
#ifdef HAVE_CMPH | ||
|
||
#include "perfect_hash.h" | ||
|
||
#include <cstdio> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
PerfectHashFunction::~PerfectHashFunction() { | ||
cmph_destroy(mphf_); | ||
} | ||
|
||
PerfectHashFunction::PerfectHashFunction(const string& fname) { | ||
FILE* f = fopen(fname.c_str(), "r"); | ||
if (!f) { | ||
cerr << "Failed to open file " << fname << " for reading: cannot load hash function.\n"; | ||
abort(); | ||
} | ||
mphf_ = cmph_load(f); | ||
if (!mphf_) { | ||
cerr << "cmph_load failed on " << fname << "!\n"; | ||
abort(); | ||
} | ||
} | ||
|
||
size_t PerfectHashFunction::operator()(const string& key) const { | ||
return cmph_search(mphf_, &key[0], key.size()); | ||
} | ||
|
||
size_t PerfectHashFunction::number_of_keys() const { | ||
return cmph_size(mphf_); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef _PERFECT_HASH_MAP_H_ | ||
#define _PERFECT_HASH_MAP_H_ | ||
|
||
#include "config.h" | ||
|
||
#ifndef HAVE_CMPH | ||
#error libcmph is required to use PerfectHashFunction | ||
#endif | ||
|
||
#include <vector> | ||
#include <boost/utility.hpp> | ||
#include "cmph.h" | ||
|
||
class PerfectHashFunction : boost::noncopyable { | ||
public: | ||
explicit PerfectHashFunction(const std::string& fname); | ||
~PerfectHashFunction(); | ||
size_t operator()(const std::string& key) const; | ||
size_t number_of_keys() const; | ||
private: | ||
cmph_t *mphf_; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "config.h" | ||
|
||
#ifndef HAVE_CMPH | ||
int main() { | ||
return 0; | ||
} | ||
#else | ||
|
||
#include <iostream> | ||
#include "weights.h" | ||
#include "fdict.h" | ||
|
||
using namespace std; | ||
|
||
int main(int argc, char** argv) { | ||
if (argc != 2) { cerr << "Usage: " << argv[0] << " file.mphf\n"; return 1; } | ||
FD::EnableHash(argv[1]); | ||
cerr << "Number of keys: " << FD::NumFeats() << endl; | ||
cerr << "LexFE = " << FD::Convert("LexFE") << endl; | ||
cerr << "LexEF = " << FD::Convert("LexEF") << endl; | ||
{ | ||
Weights w; | ||
vector<weight_t> v(FD::NumFeats()); | ||
v[FD::Convert("LexFE")] = 1.0; | ||
v[FD::Convert("LexEF")] = 0.5; | ||
w.InitFromVector(v); | ||
cerr << "Writing...\n"; | ||
w.WriteToFile("weights.bin"); | ||
cerr << "Done.\n"; | ||
} | ||
{ | ||
Weights w; | ||
vector<weight_t> v(FD::NumFeats()); | ||
cerr << "Reading...\n"; | ||
w.InitFromFile("weights.bin"); | ||
cerr << "Done.\n"; | ||
w.InitVector(&v); | ||
assert(v[FD::Convert("LexFE")] == 1.0); | ||
assert(v[FD::Convert("LexEF")] == 0.5); | ||
} | ||
} | ||
|
||
#endif | ||
|
Oops, something went wrong.