forked from pkgw/bloomdemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchkdict
executable file
·42 lines (30 loc) · 1.09 KB
/
chkdict
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#! /usr/bin/env python
# -*- mode: python; coding: utf-8 -*-
# Copyright 2014, 2017 Peter Williams
# Licensed under the MIT License.
"""Prints out whether words might be in the dictionary or not."""
from __future__ import absolute_import, division, print_function
import sys, gzip
try:
from cPickle import load as pickle_load
except ImportError:
import pickle
pickle_load = lambda f: pickle.load(f, encoding='bytes')
import bloom
# Poor man's argument handling
skip_misses = '-s' in sys.argv
if skip_misses:
sys.argv.remove('-s')
words_to_check = sys.argv[1:]
# Load up our special data structure. pickle loads up a preexisting
# BloomFilter object from disk and returns it to us. The class is
# implemented in bloom.py.
bf = pickle_load(gzip.GzipFile('dictbf.dat.gz', 'rb', 9))
# Compute the false-positive rate of the filter.
# FIXME: do something useful with this number?
fp = bf.fprate()
for word in words_to_check:
if bf.may_contain(word):
print(word, 'MIGHT BE in the dictionary')
elif not skip_misses:
print(word, 'is DEFINITELY NOT in the dictionary')