-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made minhash base class and LSHBanding a derivate class
- Loading branch information
Showing
6 changed files
with
79 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef LSHBANDING_H | ||
#define LSHBANDING_H | ||
#include "MinHash.h" | ||
#include <set> | ||
|
||
class LSHBanding : MinHash { | ||
public: | ||
LSHBanding(vector<vector<string>> shinglesMatrix, int r); | ||
vector <pair<int,int>> getSimilarDocuments(double threshold); | ||
private: | ||
vector<pair<int, int> > getCandidatesLSH(int bandWidth); | ||
int calculateBandWidth(double threshold); | ||
int hashVector(int docIdx, int init, int fin, int value, int mod); | ||
}; | ||
|
||
#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
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,51 @@ | ||
#include "LSHBanding.h" | ||
|
||
LSHBanding::LSHBanding(vector<vector<string>> shinglesMatrix, int r) : MinHash(shinglesMatrix,r) {} | ||
|
||
int LSHBanding::calculateBandWidth(double threshold){ | ||
double margin = 0.1; | ||
if (threshold < margin) { | ||
return -1; | ||
} | ||
for (int bandWidth = 1; bandWidth < numPerm; bandWidth++){ | ||
double value = pow(1.0/double(numPerm/bandWidth),1.0/double(bandWidth)); | ||
if (value > threshold - margin) return bandWidth; | ||
} | ||
return 1; | ||
} | ||
|
||
int LSHBanding::hashVector(int docIdx, int init, int fin, int value, int mod){ | ||
vector<int> v(fin-init); | ||
for (int i = init; i < fin; i++) v[i-init] = signatureMatrix[i][docIdx]; | ||
PolyHash P = PolyHash(v,mod); | ||
return P.evaluate(value); | ||
} | ||
|
||
vector<pair<int,int>> LSHBanding::getCandidatesLSH(int bandWidth){ | ||
set<pair<int,int>> candidates1; | ||
int numBands = numPerm/bandWidth; | ||
for (int i = 0; i < numBands; ++i){ | ||
vector <vector<int>> v (7919); | ||
for (int j = 0; j < numDoc; ++j){ | ||
int hash = hashVector(j,i*bandWidth, (i+1)*bandWidth, 107, 7919); | ||
for (int t = 0; t<(int)v[hash].size(); ++t){ | ||
candidates1.insert({min(v[hash][t], j), max(v[hash][t], j)}); | ||
} | ||
v[hash].push_back(j); | ||
} | ||
} | ||
vector <pair<int,int>> candidates; | ||
for (auto t : candidates1) candidates.push_back(t); | ||
return candidates; | ||
} | ||
|
||
vector <pair<int,int>> LSHBanding::getSimilarDocuments(double threshold){ | ||
int bandWidth = calculateBandWidth(threshold); | ||
auto candidates = getCandidatesLSH(bandWidth); | ||
vector <pair<int,int>> similar; | ||
for (auto s : candidates){ | ||
if (getJaccard(s.first, s.second)) similar.push_back(s); | ||
} | ||
return similar; | ||
} | ||
|
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