
// BloomTester.java
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* A Bloom filter is used to test whether an element is a 
   member of a set. False positive matches are possible, 
   but false negatives are not – in other words, a query 
   returns either "possibly in set" or "definitely not in set". 

   https://en.wikipedia.org/wiki/Bloom_filter

   Utilizes Magnus Skjegstad's java-bloomfilter
   https://github.com/magnuss/java-bloomfilter
   This class is *not* part of Apache Commons Collections

  Usage:
    > compile BloomTester.java
    > run BloomTester
*/



import java.util.*;

import com.skjegstad.utils.BloomFilter;


public class BloomTester 
{

  public static void main(String[] args) 
  {
    double fpp = 0.1;  // false positive probability
    int expectedSize = 100;
    
    BloomFilter<String> bf = 
         new BloomFilter<String>(fpp, expectedSize);

    List<String> addedWords = Arrays.asList(
      "abound", "abundance", "abundant", "accessable", 
      "bloom", "blossom", "bonny", "bonus", "bonuses",
      "cohesive", "colorful", "comely", "comfort", "gems",
      "generosity", "generous", "generously", "genial"
    );
    bf.addAll(addedWords);
    System.out.println("Added: " + addedWords);

    List<String> absentWords = Arrays.asList(
      "sweet", "india", "hate", "war", "humanity", 
      "hurt", "facebook", "twitter"
    );

    List<String> testWords = Arrays.asList(
      "abound", "abundance", "abundant", "accessable", 
      "bloom", "blossom", "bonny", "bonus", "bonuses", 
      "cohesive", "colorful", "nuke", "facebook", "twitter"
    );

    System.out.printf("Current FPP: %.6f%%\n", 
                          bf.getFalsePositiveProbability());

    System.out.println("\nTesting words:");
    for (String w : testWords) {
      if (bf.contains(w)) {
        if (absentWords.contains(w))
          System.out.println("  " + w + " is a false positive");
        else
          System.out.println("  " + w + " is probably present");
      }
      else
        System.out.println(" -" + w + " is not present");
    }
  }  // end of main()

}  // end of BloomTester class
