LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
hasheddb.cpp
Go to the documentation of this file.
1// Copyright 2002-2019 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6//
7// C++ Implementation: hasheddb
8//
9// Description:
10//
11//
12// Author: Claire Mouton, Guillaume Pitel <claire.mouton@cea.fr, guillaume.pitel@gmail.com>, (C) 2008
13//
14// Copyright: See COPYING file that comes with this distribution
15//
16//
17
18#include <vector>
19#include <algorithm>
20#include <iostream>
21#include <fstream>
22#include <sstream>
23#include "hasheddb.h"
24#include "lshsignature.h"
25#ifdef DMALLOC
26#include "dmalloc.h"
27#endif
28
29using namespace std;
30
31namespace Lima
32{
33namespace LinguisticProcessing
34{
35namespace WordSenseDisambiguation
36{
37
39{
40 if ( i.count == j.count)
41 return i.index < j.index;
42 return ( i.count < j.count );
43}
44
46{
47
48}
49
51{
52 delete nbZero;
53}
54
55HashedDB::HashedDB ( int _signatureLength, int _nSignatures )
56{
57 signatureLength = _signatureLength;
58 nSignatures = _nSignatures;
59 nbZero = new int[signatureLength];
60 for (int s= 0; s<signatureLength; s++)
61 nbZero[s] = 0;
62}
63
65{
66 signatures.push_back ( s );
67}
68
70{
71 signatures.pop_back ( );
72}
73
74void HashedDB::insert ( int ind, LSHSignature & s )
75{
76 cerr << "Insert @ " << ind << endl;
77 s.print(0,10);
78 signatures.at(ind)= s ;
79}
80
81
82
83HashedDB::HashedDB ( string dbFileName )
84{
85 nbZero = new int[signatureLength];
86 int totalSignatureLength=0;
87 ifstream is;
88 is.open(dbFileName.c_str(), std::ifstream::binary);
89 if (!is.is_open())
90 {
91 cerr << "Warning: Error opening file " << dbFileName << endl;
92 }
93 is.seekg(0,ios_base::beg);
94 is.read((char*)&totalSignatureLength, sizeof(totalSignatureLength));
95 is.read((char*)&nSignatures, sizeof(nSignatures));
96 cerr << "Sig Length : " << totalSignatureLength << endl;
97 cerr << "Nb signatures : " << nSignatures << endl;
98
99 signatureLength = totalSignatureLength;
100
101 for (int sigIndex = 0; sigIndex < nSignatures; sigIndex++) {
102 LSHSignature * newLshSig = new LSHSignature(signatureLength);
103 append(*newLshSig);
104 // Read from disk
105 is.read((char*)signatures[sigIndex].getData(), totalSignatureLength/8);
106 }
107 LSHSignature target1 = getSignature ( 0 );
108 LSHSignature target2 = getSignature ( 2 );
109 LSHSignature target3 = getSignature ( 3 );
110 cerr << "Test loadHAshed db : ";
111 target1.print(1,20);
112 target2.print(2,20);
113target3.print(3,20);
114 is.close();
115}
116
117vector< int > & HashedDB::fastKNNSearch1 ( vector<int>& permutations, int beam )
118{
119 vector<int> sortBuffer ( nSignatures );
120 vector<int> *result = new vector<int> ( beam );
121 for ( int i = 0; i < nSignatures; i++ )
122 sortBuffer[i] = i;
123 if (signatureLength > 0)
124 cerr << "before sort " << *sortBuffer.begin() << " " ;
125 getSignature(*(sortBuffer.begin()+1)).print(getSignature(*(sortBuffer.begin()+1)).bitCount(), 20 );
126 getSignature(60996).print(60996, 20);
127 nth_element ( sortBuffer.begin(),
128 sortBuffer.begin() + beam,
129 sortBuffer.end(),
130 (comparePermutedSignatures ( permutations, *this ) ) );
131
132 cerr << "after sort " << *sortBuffer.begin() << " " ;
133 getSignature(*(sortBuffer.begin()+1)).print(getSignature(*(sortBuffer.begin()+1)).bitCount(), 20 );
134
135 copy ( sortBuffer.begin(), sortBuffer.begin() + beam, (*result).begin() );
136 return *result;
137}
138
140{
141 if (signatureLength > 0)
142 for (int i = 0; i < nSignatures; i++) {
143 signatures[i].xorX(s);
144 }
145}
146
148{
149 for (int sig = 0; sig < nSignatures; sig++)
150 for (int s = 0; s < signatureLength; s++)
151 if (!signatures[sig].getBit(s))
152 nbZero[s]++;
153 std::ostringstream oss;
154 oss << "Sigs zeroes/ones count : " << std::endl;
155 for (int s = 0; s < signatureLength; s++)
156 oss << nbZero[s] << "/" << nSignatures - nbZero[s] << ",";
157 oss << std::endl;
158 std::cerr << oss.str();
159}
160
161}
162}
163}
void print(double rank, int limit)
std::vector< int > & fastKNNSearch1(std::vector< int > &permutations, int beam)
Definition hasheddb.cpp:117
bool vectorCountCompare(vectorCount i, vectorCount j)
Definition hasheddb.cpp:38
NAUTITIA.
STL namespace.