19#include <boost/lexical_cast.hpp>
26namespace LinguisticProcessing
28namespace WordSenseDisambiguation
39 m_totalPermutations(0),
44 LOGINIT(
"WordSenseDisambiguator");
45 if (knnsearchConfig.find(
"hashedDir") != knnsearchConfig.end())
47 m_hashedDir = knnsearchConfig.find(
"hashedDir")->second;
52 LWARN <<
"No hashedDir in knnsearchConfig. Default is set to 'hasheddb'. ";
54 if (knnsearchConfig.find(
"totalPermutations") != knnsearchConfig.end())
56 m_totalPermutations = boost::lexical_cast< int >(knnsearchConfig.find(
"totalPermutations")->second);
61 LWARN <<
"No totalPermutations in knnsearchConfig. Default is set to '10'. ";
63 if (knnsearchConfig.find(
"beam") != knnsearchConfig.end())
65 m_beam = boost::lexical_cast< int >(knnsearchConfig.find(
"beam")->second);
70 LWARN <<
"No beam in knnsearchConfig. Default is set to '5'. ";
72 if (knnsearchConfig.find(
"k") != knnsearchConfig.end())
74 m_k = boost::lexical_cast< int >(knnsearchConfig.find(
"k")->second);
79 LWARN <<
"No k in knnsearchConfig. Default is set to '50'. ";
82 struct stat filestatus;
83 string convFile =
m_hashedDir +
"/semmapconversion.global.bin";
84 stat(convFile.c_str(), &filestatus );
85 uint64_t mmapSize = filestatus.st_size;
86 int nbVects = mmapSize/
sizeof(uint64_t);
87 int ifd = open(convFile.c_str(), O_RDONLY);
89 cerr <<
"Error on opening file " << convFile.c_str() << endl;
91 uint64_t* codemmap = (uint64_t*)mmap(0, mmapSize, PROT_READ, MAP_PRIVATE, ifd, 0);
92 if (codemmap == MAP_FAILED)
95 perror(
"Error mmapping the file");
98 else if (codemmap == NULL)
101 perror(
"Error mmapping the file - codemmap == NULL");
104 for (
int i = 0; i < nbVects; i++)
109 if (munmap(codemmap, mmapSize) == -1) {
110 LWARN <<
"Error un-mmapping the converter file "<< convFile;
119 LOGINIT(
"WordSenseDisambiguator");
121 LWARN <<
"Error un-mmapping the hk file ";
127 SemanticContext::const_iterator itContext,
130 LOGINIT(
"WordSenseDisambiguator");
134 ||
m_data.find(itContext->first)->second == NULL)
136 string hashedDbFile =
m_hashedDir +
"/SemMap." + itContext->first +
".16384.hashed";
137 hashedDb =
new HashedDB(hashedDbFile);
139 m_data[itContext->first] = hashedDb;
143 hashedDb =
m_data.find(itContext->first)->second;
151 uint64_t start = time(NULL);
154 uint64_t end = time(NULL);
155 LDEBUG <<
"Hash context duration " << end-start <<
" s ";
165 LDEBUG <<
"Xoring hashed db" ;
172 trueTarget.
print(0,20);
173 hashedDb->
xorX ( *instanceSignature );
178 LDEBUG <<
"Sorting hashed db";
185 int resultsCount = 50;
188 cerr << finalResult[0].
index << endl;
190 LDEBUG <<
"ResultsCount : " << resultsCount;
191 for (
int i = 0; i < resultsCount; i++)
193 cerr <<
"Reading at : " << i << endl;
201 delete instanceSignature;
210 LOGINIT(
"WordSenseDisambiguator");
225 vector<int> & tmpResult = xoredDb.
fastKNNSearch1 ( permutation ,beam ) ;
227 for ( vector<int>::iterator candidateItr = tmpResult.begin(); candidateItr != tmpResult.end(); candidateItr ++ )
232 result.insert ( result.end(), tmpResult.begin(), tmpResult.end() );
241 LDEBUG <<
"Max results : " << maxResults;
245 for ( vector<int>::iterator candidateItr = result.begin(); candidateItr != result.end(); candidateItr ++ )
246 if ( doneSet.find ( *candidateItr ) == doneSet.end() )
248 semiFinalResult[resultIndex].
index = *candidateItr;
249 LDEBUG <<
"ownerId : "<< *candidateItr;
251 LDEBUG <<
"count : " << semiFinalResult[resultIndex].
count;
252 doneSet.insert ( *candidateItr );
254 cerr <<
"resultindex : " << resultIndex << endl;
256 resultsCount = resultIndex;
258 for (; resultIndex < maxResults; resultIndex++)
260 semiFinalResult[resultIndex].
index = -1;
267 if (
m_k > resultsCount)
271 <<
")> resultsCount ("
273 <<
"). Process will potentially fail.";
276 int limitSort =
m_k > resultsCount
279 LDEBUG <<
"limit sort : " << limitSort;
280 partial_sort ( semiFinalResult, semiFinalResult + limitSort, semiFinalResult + resultsCount,
vectorCountCompare );
282 cerr <<
"maxResults : " << maxResults << endl;
283 cerr <<
"resultsCount : " << resultsCount << endl;
284 cerr <<
"test : " << semiFinalResult[0].
index << endl;
292 LOGINIT(
"WordSenseDisambiguator");
294 int nPairs = itContext->second.size();
297 data = (
char*) malloc (nPairs*(
sizeof(uint)+
sizeof(
float)));
299 for (set<uint64_t>::const_iterator itValues = itContext->second.begin();
300 itValues != itContext->second.end();
304 memcpy(&data[cntCtx], &tmpI,
sizeof(uint));
305 float tmpF = (float)1./(
float)nPairs;
306 memcpy(&data[cntCtx]+
sizeof(
int), &tmpF,
sizeof(
float));
309 assert(nPairs==cntCtx);
311 LDEBUG <<
"magiccosine at " << itContext->first <<
" - m_nbDims = " <<
m_nbDims <<
" - nPairs = " << nPairs ;
312 uint64_t start = time(NULL);
323 instanceSignature->
setBit ( key );
327 instanceSignature->
unsetBit ( key );
332 uint64_t end = time(NULL);
333 LDEBUG <<
"cosine duration : " << end-start<<
" s" ;
335 return instanceSignature;
396 LOGINIT(
"WordSenseDisambiguator");
397 map<uint64_t, int> dists;
398 string hashkeyFile =
m_hashedDir +
"/SemMap.all.16384.hashkey";
399 int hkfd = open(hashkeyFile.c_str(), O_RDONLY);
401 LERROR <<
"Error opening file "<< hashkeyFile <<
" for reading";
405 hkpremap = (
int*)mmap(0, 2*
sizeof(
int), PROT_READ, MAP_PRIVATE, hkfd, 0);
406 if (hkpremap == MAP_FAILED) {
408 LDEBUG <<
"Error mmapping the file " << hashkeyFile ;
416 if (munmap(hkpremap, 2*
sizeof(
int)) == -1)
418 LDEBUG <<
"Error un-mmapping the file " << hashkeyFile ;
428 LDEBUG <<
"Error mmapping the file " << hashkeyFile <<
" at the second time ." ;
434 LDEBUG <<
"Hashkey mapped for " << relation ;
void print(double rank, int limit)
std::vector< int > & fastKNNSearch1(std::vector< int > &permutations, int beam)
LSHSignature & getSignature(int key)
void insert(int ind, LSHSignature &s)
void xorX(LSHSignature &s)
uint64_t sys2Owner(uint64_t systemId)
LSHSignature * buildSignature(SemanticContext::const_iterator itContext)
std::map< uint64_t, uint64_t > m_system2Owner
KnnSearcher(const std::map< std::string, std::string > &knnsearchConfig)
std::map< uint64_t, uint64_t > m_owner2System
void mapHashKey(std::string relation)
int fastKNNSort(HashedDB &xoredDB, vectorCount *finalResult)
int getKNN(uint64_t lemmaId, SemanticContext::const_iterator itContext, NNList &knns)
uint64_t owner2Sys(uint64_t ownerId)
static void logElapsedTime(const std::string &mess, const std::string &taskCategory=std::string(""))
log the number of microseconds since last UpdateCurrentTime
static void updateCurrentTime(const std::string &taskCategory=std::string(""))
store current time for new elapsed time computation
bool vectorCountCompare(vectorCount i, vectorCount j)
std::map< uint64_t, float > NNList