forked from eugenp/tutorials
-
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.
* BAEL-980 lsh code * BAEL-980 rename test
- Loading branch information
1 parent
4ca15c9
commit 0144471
Showing
2 changed files
with
55 additions
and
0 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
48 changes: 48 additions & 0 deletions
48
libraries/src/test/java/com/baeldung/lsh/LocalSensitiveHashingUnitTest.java
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,48 @@ | ||
package com.baeldung.lsh; | ||
|
||
import info.debatty.java.lsh.LSHMinHash; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
public class LocalSensitiveHashingUnitTest { | ||
|
||
@Ignore("for simplicity of the example number of input vectors is very low, that's why LSH may yield non deterministic results") | ||
@Test() | ||
public void givenNVectors_whenPerformLSH_thenShouldCalculateSameHashForSimilarVectors() { | ||
//given | ||
boolean[] vector1 = new boolean[]{true, true, true, true, true}; | ||
boolean[] vector2 = new boolean[]{false, false, false, true, false}; | ||
boolean[] vector3 = new boolean[]{false, false, true, true, false}; | ||
|
||
int sizeOfVectors = 5; | ||
int numberOfBuckets = 10; | ||
int stages = 4; | ||
|
||
LSHMinHash lsh = new LSHMinHash(stages, numberOfBuckets, sizeOfVectors); | ||
|
||
//when | ||
int[] firstHash = lsh.hash(vector1); | ||
int[] secondHash = lsh.hash(vector2); | ||
int[] thirdHash = lsh.hash(vector3); | ||
|
||
System.out.println(Arrays.toString(firstHash)); | ||
System.out.println(Arrays.toString(secondHash)); | ||
System.out.println(Arrays.toString(thirdHash)); | ||
|
||
//then | ||
int lastIndexOfResult = stages - 1; | ||
assertThat(firstHash[lastIndexOfResult]).isNotEqualTo(secondHash[lastIndexOfResult]); | ||
assertThat(firstHash[lastIndexOfResult]).isNotEqualTo(thirdHash[lastIndexOfResult]); | ||
assertThat(isCloseOrEqual(secondHash[lastIndexOfResult], thirdHash[lastIndexOfResult], numberOfBuckets)).isTrue(); | ||
} | ||
|
||
private boolean isCloseOrEqual(int secondHash, int thirdHash, int numberOfBuckets) { | ||
return Math.abs(secondHash - thirdHash) < numberOfBuckets / 2; | ||
} | ||
} | ||
|