1 | #!/usr/bin/env python
|
---|
2 |
|
---|
3 | from __future__ import print_function
|
---|
4 |
|
---|
5 | import os
|
---|
6 | import re
|
---|
7 | import sys
|
---|
8 | import json
|
---|
9 |
|
---|
10 | import argparse
|
---|
11 | import openpyxl
|
---|
12 |
|
---|
13 | import util
|
---|
14 | import csvutil
|
---|
15 | import xlsxutil
|
---|
16 |
|
---|
17 | jury_from_countries = {}
|
---|
18 | tele_from_countries = {}
|
---|
19 |
|
---|
20 |
|
---|
21 | def create_to_country_voting_groups(data_hashmap_array):
|
---|
22 |
|
---|
23 | # Example values for header-names
|
---|
24 | # (semi-) final: f, sf
|
---|
25 | # Jury or Televoting: J, T
|
---|
26 | # Year: 1975, ...
|
---|
27 | # To country: Belgium
|
---|
28 | # From country: Belgium
|
---|
29 | # Edition: 1975f, 1975sf
|
---|
30 |
|
---|
31 |
|
---|
32 | # Sort so array entries are grouped by the country receiving the votes in a given year
|
---|
33 | data_hashmap_array.sort(key=xlsxutil.sortkey_year_to_country)
|
---|
34 |
|
---|
35 |
|
---|
36 | # Debug output
|
---|
37 | #
|
---|
38 | # for data_hashmap in data_hashmap_array:
|
---|
39 | # print(data_hashmap.get('To country'), data_hashmap.get('Year'), data_hashmap.get('Points'), "(Points from " + data_hashmap.get('From country') + ")")
|
---|
40 |
|
---|
41 |
|
---|
42 | # Build array of country groups
|
---|
43 | # A country grouping includes all the votes that country receive that year
|
---|
44 | country_groups = []
|
---|
45 |
|
---|
46 | prev_data_hashmap = data_hashmap_array[0]
|
---|
47 | country_group = [ ]
|
---|
48 |
|
---|
49 | i = 1
|
---|
50 | num_rows = len(data_hashmap_array)
|
---|
51 |
|
---|
52 | while (i < num_rows):
|
---|
53 | country_group.append(prev_data_hashmap)
|
---|
54 | data_hashmap = data_hashmap_array[i]
|
---|
55 |
|
---|
56 | if (data_hashmap.get('To country') != prev_data_hashmap.get('To country')):
|
---|
57 | # moving on to a new country group
|
---|
58 | country_groups.append(country_group)
|
---|
59 | country_group = [ ]
|
---|
60 |
|
---|
61 | prev_data_hashmap = data_hashmap
|
---|
62 | i = i + 1
|
---|
63 |
|
---|
64 | country_group.append(prev_data_hashmap)
|
---|
65 | country_groups.append(country_group)
|
---|
66 |
|
---|
67 | return country_groups
|
---|
68 |
|
---|
69 |
|
---|
70 | def fileset_voting_for_esc_country_in_year(to_country_year_votes,countrylabel_to_uri_mapping):
|
---|
71 |
|
---|
72 | # Looking to build data-structure (for output as JSON) in the form
|
---|
73 | # { "FileSet":
|
---|
74 | # [
|
---|
75 | # { "FileName": "France1991\.nul" },
|
---|
76 | # { "Description":
|
---|
77 | # {
|
---|
78 | # "Metadata":
|
---|
79 | # [
|
---|
80 | # { "name": "Germany-J", "content": 12 }, # J = Jury Vote
|
---|
81 | # { "name": "Germany-T", "content": 6 }, # T = Televote (if present)
|
---|
82 | # ...
|
---|
83 | # ]
|
---|
84 | # }
|
---|
85 | # }
|
---|
86 | # ]
|
---|
87 | # }
|
---|
88 |
|
---|
89 | metadata_array = []
|
---|
90 |
|
---|
91 | jury_metadata_vals = []
|
---|
92 | tele_metadata_vals = []
|
---|
93 |
|
---|
94 | to_country_jury_total = 0
|
---|
95 | to_country_tele_total = 0
|
---|
96 |
|
---|
97 | # Use the first record to be a representative for 'top level' (tl)
|
---|
98 | # metadata about the votes cast 'To country'
|
---|
99 | tl_rec = to_country_year_votes[0]
|
---|
100 |
|
---|
101 | tl_to_country = tl_rec.get('To country')
|
---|
102 | tl_year = tl_rec.get('Year')
|
---|
103 | tl_final = tl_rec.get("(semi-) final") == "f"
|
---|
104 |
|
---|
105 | tl_to_country_id = re.sub(r'\s+', '', tl_to_country) + str(tl_year)
|
---|
106 |
|
---|
107 | metadata_array.append({ "name": "Country","content": tl_to_country })
|
---|
108 | metadata_array.append({ "name": "Year", "content": tl_year })
|
---|
109 | metadata_array.append({ "name": "Final", "content": tl_final })
|
---|
110 |
|
---|
111 | # tl_to_country_lc = tl_to_country.lower()
|
---|
112 | tl_to_country_uri = countrylabel_to_uri_mapping.get(tl_to_country)
|
---|
113 | if (tl_to_country_uri != None):
|
---|
114 | metadata_array.append({ "name": "CountryDBURI","content": tl_to_country_uri })
|
---|
115 | else:
|
---|
116 | util.eprint("Warning: Failed to find DBpedia Country URI match for: " + tl_to_country)
|
---|
117 |
|
---|
118 | for to_country_year_vote in to_country_year_votes:
|
---|
119 | to_country = to_country_year_vote.get('To country')
|
---|
120 | year = to_country_year_vote.get('Year')
|
---|
121 | from_country = to_country_year_vote.get('From country')
|
---|
122 | vote_type = to_country_year_vote.get('Jury or Televoting')
|
---|
123 | points = to_country_year_vote.get('Points')
|
---|
124 |
|
---|
125 | id_from_country = re.sub(r'\s+', '', from_country)
|
---|
126 |
|
---|
127 | voting_rec = { "name": id_from_country+"-"+vote_type, "content": points }
|
---|
128 |
|
---|
129 | metadata_array.append(voting_rec)
|
---|
130 |
|
---|
131 | if (vote_type == "J"):
|
---|
132 | jury_metadata_vals.append(id_from_country+"-J")
|
---|
133 | jury_from_countries[id_from_country] = 1
|
---|
134 | to_country_jury_total = to_country_jury_total + points
|
---|
135 |
|
---|
136 | elif (vote_type == "T"):
|
---|
137 | tele_metadata_vals.append(id_from_country+"-T")
|
---|
138 | tele_from_countries[id_from_country] = 1
|
---|
139 | to_country_tele_total = to_country_tele_total + points
|
---|
140 | else:
|
---|
141 | util.eprint("Warning: Unrecognized voting type: " + vote_type)
|
---|
142 |
|
---|
143 | if (len(jury_metadata_vals)>0):
|
---|
144 | metadata_array.append({ "name": "JuryVotesJSON", "content": json.dumps(jury_metadata_vals) })
|
---|
145 | metadata_array.append({ "name": "JuryVotesTotal", "content": to_country_jury_total})
|
---|
146 | if (len(tele_metadata_vals)>0):
|
---|
147 | metadata_array.append({ "name": "TeleVotesJSON", "content": json.dumps(tele_metadata_vals) })
|
---|
148 | metadata_array.append({ "name": "TeleVotesTotal", "content": to_country_tele_total})
|
---|
149 |
|
---|
150 | filename_id = tl_to_country_id + "\\.nul"
|
---|
151 |
|
---|
152 | fileset = {
|
---|
153 | "FileSet" : [
|
---|
154 | { "FileName": filename_id, },
|
---|
155 | { "Description" : { "Metadata" : metadata_array } }
|
---|
156 | ]
|
---|
157 | }
|
---|
158 |
|
---|
159 | return fileset
|
---|
160 |
|
---|
161 |
|
---|
162 | def gs_directory_metadata(to_country_year_voting_groups,countrylabel_to_uri_mapping):
|
---|
163 | # Next step is to express the grouped to-country voting data
|
---|
164 | # in the Greenstone JSON metadata format:
|
---|
165 |
|
---|
166 | # { "DirectoryMetadata":
|
---|
167 | # [
|
---|
168 | # { "FileSet":
|
---|
169 | # [
|
---|
170 | # { "FileName": "France1991\.nul" },
|
---|
171 | # { "Description":
|
---|
172 | # {
|
---|
173 | # "Metadata":
|
---|
174 | # [
|
---|
175 | # { "name": "Germany-J", "content": "12" }, # J = Jury Vote
|
---|
176 | # ...
|
---|
177 | # ]
|
---|
178 | # }
|
---|
179 | # }
|
---|
180 | # ]
|
---|
181 | # }
|
---|
182 | # ...
|
---|
183 | # ]
|
---|
184 | # }
|
---|
185 |
|
---|
186 | directory_metadata = []
|
---|
187 |
|
---|
188 | for to_country_year_votes in to_country_year_voting_groups:
|
---|
189 |
|
---|
190 | fileset = fileset_voting_for_esc_country_in_year(to_country_year_votes,countrylabel_to_uri_mapping)
|
---|
191 | directory_metadata.append(fileset)
|
---|
192 |
|
---|
193 | filename_id = fileset.get('FileSet')[0].get('FileName')
|
---|
194 | num_countries_voting_data = len(fileset.get('FileSet')[1].get('Description').get('Metadata'))
|
---|
195 |
|
---|
196 | print(" " + filename_id.ljust(28) + ": " + str(num_countries_voting_data) + " votes")
|
---|
197 |
|
---|
198 |
|
---|
199 | greenstone_metadata_json = { "DirectoryMetadata": directory_metadata }
|
---|
200 |
|
---|
201 | return greenstone_metadata_json
|
---|
202 |
|
---|
203 |
|
---|
204 | def display_gs_head_metadata_tags():
|
---|
205 |
|
---|
206 | print()
|
---|
207 | print()
|
---|
208 | print("For, e.g., '<display><format>' section of collectionConfig.xml:")
|
---|
209 | print()
|
---|
210 | print(" <gsf:headMetaTags>")
|
---|
211 |
|
---|
212 | for from_country in sorted(jury_from_countries.keys()):
|
---|
213 | print(" <gsf:metadata name=\""+from_country+"-J\" />")
|
---|
214 |
|
---|
215 | print()
|
---|
216 | for from_country in sorted(tele_from_countries.keys()):
|
---|
217 | print(" <gsf:metadata name=\""+from_country+"-T\" />")
|
---|
218 |
|
---|
219 | print(" </gsf:headMetaTags>")
|
---|
220 |
|
---|
221 |
|
---|
222 | if __name__ == "__main__":
|
---|
223 |
|
---|
224 | parser = argparse.ArgumentParser()
|
---|
225 | parser.add_argument('--sheetname', help="The name of the sheet within the Excel file to extractc data from")
|
---|
226 | parser.add_argument('--votingtype', choices=['J','T'], help="Filter to only J=Jury or T=Tele cast votes")
|
---|
227 | parser.add_argument('input-file.xlsx')
|
---|
228 | parser.add_argument('output-file.json', nargs='?')
|
---|
229 |
|
---|
230 |
|
---|
231 | args = parser.parse_args()
|
---|
232 |
|
---|
233 | sheetname = getattr(args,'sheetname');
|
---|
234 | voting_type = getattr(args,'votingtype');
|
---|
235 |
|
---|
236 | excel_input_filename = getattr(args,'input-file.xlsx');
|
---|
237 | json_output_filename = getattr(args,'output-file.json');
|
---|
238 |
|
---|
239 | if (json_output_filename == None):
|
---|
240 | json_output_filename = os.path.splitext(excel_input_filename)[0]+'.json'
|
---|
241 |
|
---|
242 | countrylabel_to_uri_mapping = csvutil.load_and_tidy_csv("dbpedia--european-countries.csv")
|
---|
243 |
|
---|
244 | worksheet = xlsxutil.load_xslx_sheet(excel_input_filename,sheetname)
|
---|
245 |
|
---|
246 | data_hashmap_array = xlsxutil.convert_worksheet_to_hashmaps(worksheet)
|
---|
247 |
|
---|
248 | print("Number of data rows in Excel file: " + str(len(data_hashmap_array)))
|
---|
249 |
|
---|
250 | # Filter down to just the voting results concerning finals
|
---|
251 | data_hashmap_array_filtered = list(filter(xlsxutil.filter_finalist_votes, data_hashmap_array))
|
---|
252 |
|
---|
253 |
|
---|
254 | print("Number of finalist voting data rows: " + str(len(data_hashmap_array_filtered)))
|
---|
255 |
|
---|
256 | if voting_type != None:
|
---|
257 | # Further filter down by the type of voting results cast
|
---|
258 | if (voting_type == "J"):
|
---|
259 | data_hashmap_array_filtered = list(filter(xlsxutil.filter_jury_votes, data_hashmap_array_filtered))
|
---|
260 | print("Number Jury cast data rows: " + str(len(data_hashmap_array_filtered)))
|
---|
261 | else:
|
---|
262 | # Must be "T"
|
---|
263 | data_hashmap_array_filtered = list(filter(xlsxutil.filter_tele_votes, data_hashmap_array_filtered))
|
---|
264 | print("Number Televoting cast data rows: " + str(len(data_hashmap_array_filtered)))
|
---|
265 |
|
---|
266 |
|
---|
267 | to_country_year_voting_groups = create_to_country_voting_groups(data_hashmap_array_filtered)
|
---|
268 |
|
---|
269 | # Debug output
|
---|
270 | #
|
---|
271 | # print(to_country_year_voting_groups)
|
---|
272 |
|
---|
273 | print()
|
---|
274 | print("Generating Greenstone JSON to-country voting metadata received by:")
|
---|
275 | greenstone_metadata_json = gs_directory_metadata(to_country_year_voting_groups,countrylabel_to_uri_mapping)
|
---|
276 |
|
---|
277 | print("Saving output as: " + json_output_filename)
|
---|
278 | xlsxutil.save_greenstone_json_metadata(greenstone_metadata_json,json_output_filename)
|
---|
279 |
|
---|
280 | display_gs_head_metadata_tags()
|
---|
281 |
|
---|
282 | print()
|
---|
283 |
|
---|