forked from XPRIZE/GLEXP-Team-Chimple-goa
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from maqsoftware/BikashT/RefractorJson
Fixes #159
- Loading branch information
Showing
12 changed files
with
361 additions
and
124 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
""" | ||
This script has functions to create Excel sheets for the mismatched titles, | ||
to replace titles from JSON reference file and also contains an Excel function | ||
with titles from Hindi JSON file | ||
""" | ||
import json | ||
import xlsxwriter | ||
|
||
|
||
def get_title_miss_matches(ref_json, check_json, xlsx_file, sheet_name="Sheet 1"): | ||
""" | ||
This function creates an Excel sheet which contains all the mismatched | ||
titles in the check_json file. | ||
ref_json: JSON file from where name, title for each object will be referred | ||
check_json: JSON file where titles needed to be checked | ||
xlsx_file: Excel file that will contain all the mismatched titles | ||
<name> <en_json_title> <hi_json_title_eng> | ||
Example: | ||
get_title_miss_matches('alphabet_game_map_en.json', 'alphabet_game_map_hi.json', 'DifferentTitles.xlsx') | ||
""" | ||
dem_char = '$#$' | ||
ident_key = 'name' | ||
target_key = 'title' | ||
wb = xlsxwriter.Workbook(xlsx_file) | ||
ref_json_dict = dict() | ||
# Open the JSON and create a map with the ident_key | ||
with open(ref_json, 'r', encoding='utf-8-sig') as ref_file: | ||
ref_json_data = json.load(ref_file) | ||
for each_object in ref_json_data: | ||
ref_json_dict[each_object[ident_key]] = each_object | ||
sheet = wb.add_worksheet(sheet_name) | ||
row_count = 0 | ||
sheet.write(row_count, 0, 'name') | ||
sheet.write(row_count, 1, 'en_json_title') | ||
sheet.write(row_count, 2, 'hi_json_title_eng') | ||
row_count = row_count + 2 | ||
# Write to Excel file all the mismatched titles | ||
with open(check_json, 'r', encoding='utf-8-sig') as check_file: | ||
check_json_data = json.load(check_file) | ||
for each_check_object in check_json_data: | ||
check_object_name = each_check_object[ident_key] | ||
check_object_title = each_check_object[target_key].split(dem_char) | ||
ref_object = ref_json_dict[check_object_name] | ||
ref_object_title = ref_object[target_key] | ||
if ref_object_title != check_object_title[1]: | ||
sheet.write(row_count, 0, check_object_name) | ||
sheet.write(row_count, 1, ref_object_title) | ||
sheet.write(row_count, 2, check_object_title[1]) | ||
row_count = row_count + 1 | ||
wb.close() | ||
|
||
|
||
def replace_title_ref_(ref_json, check_json, dest_file): | ||
""" | ||
This function creates a new JSON file which contains english part of the | ||
title referred from ref_json file. | ||
ref_json: JSON file from where name,title for each object will be referred | ||
check_json: JSON file where titles needed to be checked | ||
dest_file: destination JSON file which will contains updated objects | ||
Example: | ||
replace_title_ref_('alphabet_game_map_en.json', 'alphabet_game_map_hi.json', 'alphabet_game_map_hi_new.json') | ||
""" | ||
dem_char = '$#$' | ||
ident_key = 'name' | ||
target_key = 'title' | ||
ref_json_dict = dict() | ||
# Create map from the English JSON file | ||
with open(ref_json, 'r', encoding='utf-8-sig') as ref_file: | ||
ref_json_data = json.load(ref_file) | ||
for each_object in ref_json_data: | ||
ref_json_dict[each_object[ident_key]] = each_object | ||
# Update the JSON data from the ref JSON file | ||
with open(check_json, 'r', encoding='utf-8-sig') as check_file: | ||
check_json_data = json.load(check_file) | ||
length = len(check_json_data) | ||
for i in range(length): | ||
each_check_object = check_json_data[i] | ||
check_object_name = each_check_object[ident_key] | ||
check_object_title = each_check_object[target_key].split(dem_char) | ||
ref_object = ref_json_dict[check_object_name] | ||
ref_object_title = ref_object[target_key] | ||
new_check_object_title = check_object_title[0] + ident_key + ref_object_title | ||
each_check_object[target_key] = new_check_object_title | ||
check_json_data[i] = each_check_object | ||
# Create a new JSON file from the file | ||
with open(dest_file, 'w', encoding='utf-8') as destfile: | ||
json.dump(check_json_data, destfile, ensure_ascii=False, indent=2) | ||
|
||
|
||
def get_titles_name_json(ref_json, check_json, xlsx_file, sheet_name="Sheet 1"): | ||
""" | ||
This function creates an Excel sheet which contains name and titles from ref_json, english and hindi separated title | ||
from the check_json file | ||
ref_json: file from where name, title for each object will be referred | ||
check_json: file where titles needed to be checked | ||
xlsx_file: Excel file that will be contain, the above mentioned field | ||
<name> <en_json_title> <hi_json_title_eng> <hi_json_title_hi> | ||
Example: | ||
get_titles_name_json('alphabet_game_map_en.json', 'alphabet_game_map_hi.json', 'TitlesandNames.xlsx') | ||
""" | ||
dem_char = '$#$' | ||
ident_key = 'name' | ||
target_key = 'title' | ||
wb = xlsxwriter.Workbook(xlsx_file) | ||
ref_json_dict = dict() | ||
# Read the JSON file for the reference | ||
with open(ref_json, 'r', encoding='utf-8-sig') as ref_file: | ||
ref_json_data = json.load(ref_file) | ||
for each_object in ref_json_data: | ||
ref_json_dict[each_object[ident_key]] = each_object | ||
sheet = wb.add_worksheet(sheet_name) | ||
|
||
row_count = 0 | ||
sheet.write(row_count, 0, 'name') | ||
sheet.write(row_count, 1, 'en_json_title') | ||
sheet.write(row_count, 2, 'hi_json_title_eng') | ||
sheet.write(row_count, 3, 'hi_json_title_hi') | ||
row_count = row_count+2 | ||
# Write to Excel file all the titles in the JSON file | ||
with open(check_json, 'r', encoding='utf-8-sig') as check_file: | ||
check_json_data = json.load(check_file) | ||
for each_check_object in check_json_data: | ||
check_object_name = each_check_object[ident_key] | ||
check_object_title = each_check_object[target_key].split(dem_char) | ||
ref_object = ref_json_dict[check_object_name] | ||
ref_object_title = ref_object[target_key] | ||
sheet.write(row_count, 0, check_object_name) | ||
sheet.write(row_count, 1, ref_object_title) | ||
sheet.write(row_count, 2, check_object_title[1]) | ||
sheet.write(row_count, 3, check_object_title[0]) | ||
row_count = row_count + 1 | ||
wb.close() | ||
|
||
locales = ['en', 'hi'] | ||
file_root = 'original/' | ||
excel_path = 'check_excels/' | ||
files = ['alphabet_game_map', 'grammar_game_map', 'shapes_game_map', 'writing_game_map'] | ||
for file in files: | ||
get_titles_name_json(file_root+file+'_'+locales[0]+'.json', file_root+file+'_'+locales[1]+'.json', excel_path+file+'.xlsx') | ||
for file in files: | ||
get_title_miss_matches(file_root+file+'_'+locales[0]+'.json', file_root+file+'_'+locales[1]+'.json', excel_path+file+'.xlsx') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
This script updates the Hindi and English titles of the games in the JSON | ||
""" | ||
import json | ||
import xlrd | ||
|
||
|
||
def replace_title_ref_(ref_excel, curr_json, new_json, json_locale='en', title_index=1, sheet_name='Sheet 1'): | ||
""" | ||
This function creates a new JSON file which contains English part of the | ||
title referred from ref_excel file. | ||
ref_excel: Excel file from where name, title for each object will be referred | ||
curr_json: JSON file in where titles needed to be replace | ||
new_json: destination file which will store updated titles | ||
Example: | ||
replace_title_ref_('mismatched_titles.xlsx', 'original/alphabet_game_map_hi.json', 'updated/alphabet_game_map_hi.json', 'hi', sheet_name='alphabet') | ||
replace_title_ref_('mismatched_titles.xlsx', 'original/alphabet_game_map_en.json', 'updated/alphabet_game_map_en.json', sheet_name='alphabet') | ||
""" | ||
# delimiter between Hindi and English Title | ||
dem_char = '$#$' | ||
# identity key(column name/key) for the object in the JSON and Excel | ||
ident_key = 'name' | ||
# column name for the new title | ||
src_key = 'new_title' | ||
# key of the target that needed to be changed | ||
target_key = 'title' | ||
# Read the Excel sheet to create a dictionary | ||
wb = xlrd.open_workbook(ref_excel) | ||
sheet = wb.sheet_by_name(sheet_name) | ||
row_count = sheet.nrows | ||
col_count = sheet.ncols | ||
# Get the column index of identity and new title column in Excel | ||
name_index = -1 | ||
newtitle_index = -1 | ||
for i in range(col_count): | ||
if name_index == -1 and ident_key == sheet.cell_value(0, i): | ||
name_index = i | ||
if newtitle_index == -1 and src_key == sheet.cell_value(0, i): | ||
newtitle_index = i | ||
if name_index != -1 and newtitle_index != -1: | ||
break | ||
print('Sheet: ', sheet_name) | ||
print('name_index: ', name_index) | ||
print('newtitle_index: ', newtitle_index) | ||
print('Rows count: ', row_count) | ||
print('Columns count: ', col_count) | ||
newtitle_dict = dict() | ||
# Create a dictionary between the identity key and the new value | ||
for i in range(1, row_count): | ||
ref_name = sheet.cell_value(i, name_index) | ||
ref_title = sheet.cell_value(i, newtitle_index) | ||
if len(ref_name) > 0: | ||
newtitle_dict[ref_name] = ref_title | ||
# Read the JSON file and replace the english part of the title | ||
# with new title using the dictionary | ||
with open(curr_json, 'r', encoding='utf-8-sig') as curr_file: | ||
check_json_data = json.load(curr_file) | ||
length = len(check_json_data) | ||
for i in range(length): | ||
each_check_object = check_json_data[i] | ||
check_object_name = each_check_object[ident_key] | ||
if check_object_name in newtitle_dict: | ||
ref_object_title = newtitle_dict[check_object_name] | ||
if json_locale != 'en': | ||
check_object_title = each_check_object[target_key].split(dem_char) | ||
check_object_title[title_index] = ref_object_title | ||
new_check_object_title = dem_char.join(check_object_title) | ||
else: | ||
new_check_object_title = ref_object_title | ||
each_check_object[target_key] = new_check_object_title | ||
check_json_data[i] = each_check_object | ||
# Create a new JSON file with updated name | ||
with open(new_json, 'w', encoding='utf-8') as new_jsonfile: | ||
json.dump(check_json_data, new_jsonfile, ensure_ascii=False, indent=2) | ||
|
||
file_root = 'original/' | ||
updated_en = 'en_updated/' | ||
updated_hi = 'hi_updated/' | ||
locales = ['en', 'hi'] | ||
files = ['alphabet_game_map', 'grammar_game_map', 'shapes_game_map', 'writing_game_map'] | ||
sheets = ['alphabet', 'grammar', 'shapes', 'writing'] | ||
|
||
# Update the English titles in all the JSONs files | ||
mismatched_titles_excel = 'mismatched_titles.xlsx' | ||
for locale in locales: | ||
for i in range(0, len(files)): | ||
replace_title_ref_(mismatched_titles_excel, file_root+files[i]+'_'+locale+'.json', updated_en+files[i]+'_'+locale+'.json', locale, 0, sheets[i]) | ||
|
||
# Update the Hindi titles in the Hindi JSONs files | ||
hindi_titles_excel = 'game_titles.xlsx' | ||
hi_index = 1 | ||
for file in files: | ||
replace_title_ref_(hindi_titles_excel, updated_en+file+'_'+locales[hi_index]+'.json', updated_hi+file+'_'+locales[hi_index]+'.json', locale, 1, sheets[i]) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.