1 | package org.hathitrust.extractedfeatures;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Iterator;
|
---|
6 | import org.apache.spark.api.java.function.FlatMapFunction;
|
---|
7 | import org.apache.spark.util.DoubleAccumulator;
|
---|
8 | import org.json.JSONArray;
|
---|
9 | import org.json.JSONObject;
|
---|
10 |
|
---|
11 | /*
|
---|
12 | class PagedJSON implements Function<String, Boolean> {
|
---|
13 |
|
---|
14 | private static final long serialVersionUID = 1L;
|
---|
15 |
|
---|
16 | public Boolean call(String s) { return s.contains("a"); }
|
---|
17 | }
|
---|
18 | */
|
---|
19 |
|
---|
20 |
|
---|
21 | class PerPageJSONFlatmap implements FlatMapFunction<String, JSONObject>
|
---|
22 | //public class PagedJSON implements VoidFunction<String>
|
---|
23 | {
|
---|
24 | private static final long serialVersionUID = 1L;
|
---|
25 |
|
---|
26 | protected String _input_dir;
|
---|
27 | protected String _whitelist_filename;
|
---|
28 |
|
---|
29 | protected String _solr_url;
|
---|
30 | protected String _output_dir;
|
---|
31 |
|
---|
32 | protected int _verbosity;
|
---|
33 |
|
---|
34 | protected WhitelistBloomFilter _whitelist_bloomfilter;
|
---|
35 | protected UniversalPOSLangMap _universal_langmap = null;
|
---|
36 |
|
---|
37 | protected DoubleAccumulator _progress_accum;
|
---|
38 | protected double _progress_step;
|
---|
39 |
|
---|
40 | boolean _icu_tokenize;
|
---|
41 | boolean _strict_file_io;
|
---|
42 |
|
---|
43 | public PerPageJSONFlatmap(String input_dir, String whitelist_filename,
|
---|
44 | String solr_url, String output_dir, int verbosity,
|
---|
45 | DoubleAccumulator progress_accum, double progress_step,
|
---|
46 | boolean icu_tokenize, boolean strict_file_io)
|
---|
47 | {
|
---|
48 | _input_dir = input_dir;
|
---|
49 | _whitelist_filename = whitelist_filename;
|
---|
50 |
|
---|
51 | _solr_url = solr_url;
|
---|
52 | _output_dir = output_dir;
|
---|
53 | _verbosity = verbosity;
|
---|
54 |
|
---|
55 | _progress_accum = progress_accum;
|
---|
56 | _progress_step = progress_step;
|
---|
57 |
|
---|
58 | _icu_tokenize = icu_tokenize;
|
---|
59 | _strict_file_io = strict_file_io;
|
---|
60 |
|
---|
61 | _whitelist_bloomfilter = null;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | public Iterator<JSONObject> call(String json_file_in) throws IOException
|
---|
66 | {
|
---|
67 | if ((_whitelist_filename != null) && (_whitelist_bloomfilter == null)) {
|
---|
68 | _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
|
---|
69 | }
|
---|
70 |
|
---|
71 | String full_json_file_in = _input_dir + "/" + json_file_in;
|
---|
72 | JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
|
---|
73 |
|
---|
74 | ArrayList<JSONObject> json_pages = new ArrayList<JSONObject>();
|
---|
75 |
|
---|
76 | if (extracted_feature_record != null) {
|
---|
77 | String volume_id = extracted_feature_record.getString("id");
|
---|
78 |
|
---|
79 | //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
|
---|
80 | //String title= ef_metadata.getString("title");
|
---|
81 |
|
---|
82 | JSONObject ef_features = extracted_feature_record.getJSONObject("features");
|
---|
83 |
|
---|
84 |
|
---|
85 | int ef_page_count = ef_features.getInt("pageCount");
|
---|
86 |
|
---|
87 | if (_verbosity >= 1) {
|
---|
88 | System.out.println("Processing: " + json_file_in);
|
---|
89 | System.out.println(" pageCount = " + ef_page_count);
|
---|
90 | }
|
---|
91 |
|
---|
92 | JSONArray ef_pages = ef_features.getJSONArray("pages");
|
---|
93 | int ef_num_pages = ef_pages.length();
|
---|
94 | if (ef_num_pages != ef_page_count) {
|
---|
95 | System.err.println("Warning: number of page elements in JSON (" + ef_num_pages + ")"
|
---|
96 | +" does not match 'pageCount' metadata (" + ef_page_count + ")");
|
---|
97 | }
|
---|
98 |
|
---|
99 | // Make directory for page-level JSON output
|
---|
100 | String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
|
---|
101 | String page_json_dir = json_dir + "/pages";
|
---|
102 |
|
---|
103 | if (_output_dir != null) {
|
---|
104 | // Only need to do this once per volume, so easier to here than in the per-page Map
|
---|
105 | ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
|
---|
106 | }
|
---|
107 | if (_verbosity >= 3) {
|
---|
108 | System.out.print(" Pages: ");
|
---|
109 | }
|
---|
110 |
|
---|
111 | for (int i = 0; i < ef_page_count; i++) {
|
---|
112 | String formatted_i = String.format("page-%06d", i);
|
---|
113 | String page_id = volume_id + "." + formatted_i;
|
---|
114 |
|
---|
115 | if (_verbosity >= 3) {
|
---|
116 | if (i>0) {
|
---|
117 | System.out.print(", ");
|
---|
118 | }
|
---|
119 | System.out.print(page_id);
|
---|
120 | }
|
---|
121 |
|
---|
122 | String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
|
---|
123 |
|
---|
124 | if (i==(ef_page_count-1)) {
|
---|
125 | if (_verbosity >= 3) {
|
---|
126 | System.out.println();
|
---|
127 | }
|
---|
128 | if (_verbosity >= 2) {
|
---|
129 | System.out.println("Sample output JSON page file: " + output_json_bz2);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | JSONObject ef_page = ef_pages.getJSONObject(i);
|
---|
134 |
|
---|
135 | if (ef_page != null) {
|
---|
136 | // Convert to Solr add form
|
---|
137 | JSONObject solr_add_doc_json
|
---|
138 | = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page,
|
---|
139 | _whitelist_bloomfilter, _universal_langmap,
|
---|
140 | _icu_tokenize);
|
---|
141 | solr_add_doc_json.put("filename_json_bz2", output_json_bz2);
|
---|
142 |
|
---|
143 | json_pages.add(solr_add_doc_json);
|
---|
144 |
|
---|
145 |
|
---|
146 | }
|
---|
147 | else {
|
---|
148 | System.err.println("Skipping: " + page_id);
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else {
|
---|
154 | // File did not exist, or could not be parsed
|
---|
155 | String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
|
---|
156 | if (_strict_file_io) {
|
---|
157 | throw new IOException(mess);
|
---|
158 | }
|
---|
159 | else {
|
---|
160 | System.err.println("Warning: " + mess);
|
---|
161 | System.out.println("Warning: " + mess);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | _progress_accum.add(_progress_step);
|
---|
166 |
|
---|
167 | return json_pages.iterator();
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|