Class CommonTokensBloom
The common-token lists are only ever queried for membership ("is this token one of the N most common tokens in language X?"), so they are shipped as one Bloom filter per language instead of the raw token lists. This is dramatically smaller (the filter is sized by the token count, not the token text) at the cost of a small, bounded false-positive rate: a token that is not common may occasionally be reported as common. There are never false negatives. For the OOV/common-token ratio this feeds, that bias is negligible.
Both the runtime loader (CommonTokenCountManager) and the offline generator use the
methods here so the hashing and on-disk format can never drift apart. The format is:
int magic (0x54424631, "TBF1") int k (number of hash functions) int m (number of bits) int nLongs (length of the bit-map array) long[nLongs] bit map (big-endian)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleTarget false-positive rate for the generated filters. -
Method Summary
Modifier and TypeMethodDescriptionstatic org.apache.commons.collections4.bloomfilter.HasherDeterministic hasher for a token, used identically at generation and query time.static booleanmightContain(org.apache.commons.collections4.bloomfilter.BloomFilter filter, String token) static org.apache.commons.collections4.bloomfilter.BloomFilterread(InputStream is) Reads a filter written bywrite(OutputStream, BloomFilter).static org.apache.commons.collections4.bloomfilter.ShapeshapeFor(int numTokens) static voidwrite(OutputStream os, org.apache.commons.collections4.bloomfilter.BloomFilter filter) Writesfiltertoosin the format documented on this class.
-
Field Details
-
DEFAULT_FPP
public static final double DEFAULT_FPPTarget false-positive rate for the generated filters. See the class comment for why a non-zero rate is acceptable here. Changing this requires regenerating the shippedcommon_tokens_bloom/resources.0.0001 (1 in 10,000) keeps the OOV metric close to a literal lookup list: the candidate filter already removes numeric/short/HTML false positives, and this rate makes a residual collision on a genuine word ~10x rarer, which matters for repetitive junk docs where a single dominant token can swing occurrence-weighted OOV. Costs ~2 MB over 0.001.
- See Also:
-
-
Method Details
-
shapeFor
public static org.apache.commons.collections4.bloomfilter.Shape shapeFor(int numTokens) - Parameters:
numTokens- number of tokens that will be added to the filter- Returns:
- a
Shapesized fornumTokensatDEFAULT_FPP
-
hasher
Deterministic hasher for a token, used identically at generation and query time. -
mightContain
public static boolean mightContain(org.apache.commons.collections4.bloomfilter.BloomFilter filter, String token) - Returns:
trueiftokenmight be infilter(subject to the filter's false-positive rate);falsemeans it is definitely not present.
-
write
public static void write(OutputStream os, org.apache.commons.collections4.bloomfilter.BloomFilter filter) throws IOException Writesfiltertoosin the format documented on this class. Does not close the stream.- Throws:
IOException
-
read
public static org.apache.commons.collections4.bloomfilter.BloomFilter read(InputStream is) throws IOException Reads a filter written bywrite(OutputStream, BloomFilter). Does not close the stream.- Throws:
IOException
-