1 | package org.hathitrust.extractedfeatures;
|
---|
2 |
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.BufferedOutputStream;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileInputStream;
|
---|
7 | import java.io.FileNotFoundException;
|
---|
8 | import java.io.FileOutputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.io.Serializable;
|
---|
12 | import java.nio.charset.Charset;
|
---|
13 | import java.nio.charset.StandardCharsets;
|
---|
14 | import java.nio.file.Files;
|
---|
15 | import java.nio.file.Paths;
|
---|
16 | import java.util.stream.Stream;
|
---|
17 |
|
---|
18 | import javax.annotation.Nullable;
|
---|
19 |
|
---|
20 | import com.google.common.base.Preconditions;
|
---|
21 | import com.google.common.hash.BloomFilter;
|
---|
22 | import com.google.common.hash.Funnel;
|
---|
23 | import com.google.common.hash.Funnels;
|
---|
24 | import com.google.common.hash.PrimitiveSink;
|
---|
25 |
|
---|
26 | public class WhitelistBloomFilter {
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | protected BloomFilter<CharSequence> _bloomFilter;
|
---|
31 | protected static final String SERIALIZED_SUFFIX = "-serialized";
|
---|
32 | protected static final double FALSE_POSITIVE_PERCENTAGE = 0.01;
|
---|
33 |
|
---|
34 |
|
---|
35 | // http://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java
|
---|
36 | public static int countLines(String filename) throws IOException
|
---|
37 | {
|
---|
38 | InputStream is = new BufferedInputStream(new FileInputStream(filename));
|
---|
39 |
|
---|
40 | try {
|
---|
41 | byte[] c = new byte[1024];
|
---|
42 | int count = 0;
|
---|
43 | int readChars = 0;
|
---|
44 | boolean empty = true;
|
---|
45 | while ((readChars = is.read(c)) != -1) {
|
---|
46 | empty = false;
|
---|
47 | for (int i = 0; i < readChars; ++i) {
|
---|
48 | if (c[i] == '\n') {
|
---|
49 | ++count;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | return (count == 0 && !empty) ? 1 : count;
|
---|
54 | } finally {
|
---|
55 | is.close();
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | public WhitelistBloomFilter(String dictionary_filename, boolean serialize) {
|
---|
61 | System.out.println("Constructing: WhitelistBloomFilter");
|
---|
62 |
|
---|
63 | File ser_dictionary_file = new File(dictionary_filename + SERIALIZED_SUFFIX);
|
---|
64 |
|
---|
65 | if (ser_dictionary_file.exists()) {
|
---|
66 | System.out.println("Loading Serialized Bloom filter ...");
|
---|
67 | _bloomFilter = serializeIn(ser_dictionary_file);
|
---|
68 | System.out.println("... done");
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | // Need to generate the Bloom filter from the given raw text file
|
---|
72 |
|
---|
73 | System.out.println("Counting lines in: " + dictionary_filename);
|
---|
74 | int num_lines = -1;
|
---|
75 | try {
|
---|
76 | num_lines = countLines(dictionary_filename);
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 | Funnel<CharSequence> string_funnel = Funnels.stringFunnel(StandardCharsets.UTF_8);
|
---|
81 | _bloomFilter = BloomFilter.create(string_funnel, num_lines,FALSE_POSITIVE_PERCENTAGE);
|
---|
82 | }
|
---|
83 | catch (IOException e) {
|
---|
84 | e.printStackTrace();
|
---|
85 | }
|
---|
86 | System.out.println("Number of lines: " + num_lines);
|
---|
87 |
|
---|
88 | storeEntries(dictionary_filename,serialize);
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void storeEntries(String filename, boolean serialize)
|
---|
94 | {
|
---|
95 | System.out.println("Building Bloom filter ...");
|
---|
96 |
|
---|
97 | //read file into stream, try-with-resources
|
---|
98 | try (Stream<String> stream = Files.lines(Paths.get(filename))) {
|
---|
99 | stream.forEach(word -> {_bloomFilter.put(word);});
|
---|
100 | } catch (IOException e) {
|
---|
101 | e.printStackTrace();
|
---|
102 | }
|
---|
103 |
|
---|
104 | System.out.println("... done");
|
---|
105 |
|
---|
106 | if (serialize) {
|
---|
107 | System.out.println("Serializing Bloom filter ...");
|
---|
108 |
|
---|
109 | File ser_dictionary = new File(filename + SERIALIZED_SUFFIX);
|
---|
110 | serializeOut(ser_dictionary);
|
---|
111 |
|
---|
112 | System.out.println("... done");
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 | public boolean contains(String key)
|
---|
118 | {
|
---|
119 | return _bloomFilter.mightContain(key);
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected void serializeOut(File ser_file)
|
---|
123 | {
|
---|
124 | try {
|
---|
125 | FileOutputStream fos = new FileOutputStream(ser_file);
|
---|
126 |
|
---|
127 | BufferedOutputStream bfos = new BufferedOutputStream(fos);
|
---|
128 |
|
---|
129 | _bloomFilter.writeTo(bfos);
|
---|
130 |
|
---|
131 | bfos.close();
|
---|
132 | }
|
---|
133 | catch (FileNotFoundException e) {
|
---|
134 | System.err.println("Unable to open Bloom file:" + ser_file.getAbsolutePath());
|
---|
135 | e.printStackTrace();
|
---|
136 | } catch (IOException e) {
|
---|
137 | System.err.println("Error reading in Bloom file:" + ser_file.getAbsolutePath());
|
---|
138 | e.printStackTrace();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | protected static BloomFilter<CharSequence> serializeIn(File ser_file)
|
---|
143 | {
|
---|
144 | BloomFilter<CharSequence> bloomFilter = null;
|
---|
145 |
|
---|
146 | try {
|
---|
147 | FileInputStream fis = new FileInputStream(ser_file);
|
---|
148 | BufferedInputStream bfis = new BufferedInputStream(fis);
|
---|
149 |
|
---|
150 | //Funnel<CharSequence> string_funnel = Funnels.stringFunnel(StandardCharsets.UTF_8);
|
---|
151 | Funnel<CharSequence> string_funnel = stringFunnel(StandardCharsets.UTF_8);
|
---|
152 | bloomFilter = BloomFilter.readFrom(bfis,string_funnel);
|
---|
153 |
|
---|
154 | bfis.close();
|
---|
155 | }
|
---|
156 | catch (FileNotFoundException e) {
|
---|
157 | System.err.println("Unable to open Bloom file:" + ser_file.getAbsolutePath());
|
---|
158 | e.printStackTrace();
|
---|
159 | } catch (IOException e) {
|
---|
160 | System.err.println("Error writing out Bloom file:" + ser_file.getAbsolutePath());
|
---|
161 | e.printStackTrace();
|
---|
162 | }
|
---|
163 | return bloomFilter;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | // Spark uses Guava 14.0, the following is future-ported from Guava 20.0
|
---|
168 | // Added in here, rather then Funnel, and StringCharsetFunnel -> MyStringCharsetFunnel
|
---|
169 |
|
---|
170 | public static Funnel<CharSequence> stringFunnel(Charset charset) {
|
---|
171 |
|
---|
172 | return new MyStringCharsetFunnel(charset);
|
---|
173 |
|
---|
174 | }
|
---|
175 |
|
---|
176 | private static class MyStringCharsetFunnel implements Funnel<CharSequence>, Serializable {
|
---|
177 |
|
---|
178 | private static final long serialVersionUID = 1L;
|
---|
179 |
|
---|
180 | private final Charset charset;
|
---|
181 |
|
---|
182 | MyStringCharsetFunnel(Charset charset) {
|
---|
183 | this.charset = Preconditions.checkNotNull(charset);
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void funnel(CharSequence from, PrimitiveSink into) {
|
---|
187 | into.putString(from, charset);
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | public String toString() {
|
---|
192 | return "Funnels.stringFunnel(" + charset.name() + ")";
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public boolean equals(@Nullable Object o) {
|
---|
197 |
|
---|
198 | if (o instanceof MyStringCharsetFunnel) {
|
---|
199 | MyStringCharsetFunnel funnel = (MyStringCharsetFunnel) o;
|
---|
200 | return this.charset.equals(funnel.charset);
|
---|
201 | }
|
---|
202 | return false;
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public int hashCode() {
|
---|
207 | return MyStringCharsetFunnel.class.hashCode() ^ charset.hashCode();
|
---|
208 | }
|
---|
209 |
|
---|
210 | Object writeReplace() {
|
---|
211 | return new SerializedForm(charset);
|
---|
212 | }
|
---|
213 |
|
---|
214 | private static class SerializedForm implements Serializable {
|
---|
215 | private final String charsetCanonicalName;
|
---|
216 |
|
---|
217 | SerializedForm(Charset charset) {
|
---|
218 | this.charsetCanonicalName = charset.name();
|
---|
219 | }
|
---|
220 |
|
---|
221 | private Object readResolve() {
|
---|
222 | return stringFunnel(Charset.forName(charsetCanonicalName));
|
---|
223 | }
|
---|
224 |
|
---|
225 | private static final long serialVersionUID = 0;
|
---|
226 |
|
---|
227 | }
|
---|
228 |
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | } |
---|