Created
March 26, 2020 02:24
-
-
Save PatPeter/80bb15ffa3c821ca6dc9efd920363bf9 to your computer and use it in GitHub Desktop.
Zis to English Translator 1.5
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
function TRANSLATE_TO_ENGLISH(text) { | |
return translate('zi_en', text); | |
} | |
function TRANSLATE_TO_ZIS(text) { | |
return translate('en_zi', text); | |
} |
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
/** | |
* A special function that inserts a custom menu when the spreadsheet opens. | |
*/ | |
function onOpen() { | |
//var menu1 = [{name: 'Translate from Zisian to English', functionName: 'translateZisianToEnglish_'}]; | |
//var menu2 = [{name: 'Translate from English to Zisian', functionName: 'translateEnglishToZisian_'}]; | |
//SpreadsheetApp.getActive().addMenu('Translate Zisian-English', menu1); | |
//SpreadsheetApp.getActive().addMenu('Translate English-Zisian', menu2); | |
// https://developers.google.com/apps-script/reference/spreadsheet/sheet#getdatarange | |
// https://developers.google.com/apps-script/guides/support/best-practices | |
var ss = SpreadsheetApp.getActive(); | |
var sheet = ss.getSheetByName('Dictionary'); | |
var range = sheet.getDataRange(); | |
var values = range.getValues(); | |
var lookupZisianToEnglish = {}; | |
var lookupEnglishToZisian = {}; | |
for (var i = 0; i < values.length; i++) { | |
if (i == 0) continue; | |
if (values[i][0].toString().trim().length > 0 && values[i][1].toString().trim().length > 0) { | |
lookupZisianToEnglish[values[i][1].toString().trim().toLowerCase()] = { | |
translation: values[i][0].toString().trim().toLowerCase(), | |
pos: values[i][2].toString().toLowerCase() | |
}; | |
lookupEnglishToZisian[values[i][0].toString().trim().toLowerCase()] = { | |
translation: values[i][1].toString().trim().toLowerCase(), | |
pos: values[i][2].toString().trim().toLowerCase() | |
}; | |
} | |
//Logger.log("Translate Zisian to English " + values[i][1].toString().trim().toLowerCase() + " to " + values[i][0].toString().trim().toLowerCase() + ' which is a ' + values[i][2].toString().trim().toLowerCase()); | |
//Logger.log("Translate English to Zisian " + values[i][0].toString().toLowerCase() + " to " + values[i][1].toString().toLowerCase()); | |
} | |
var cache = CacheService.getScriptCache(); | |
cache.put("lookupZisianToEnglish", JSON.stringify(lookupZisianToEnglish), 86400); | |
cache.put("lookupEnglishToZisian", JSON.stringify(lookupEnglishToZisian), 86400); | |
SpreadsheetApp.getUi() // Or DocumentApp or FormApp. | |
.createMenu('Translate') | |
.addItem('Translate from Zis to English', 'translateZisianToEnglish_') | |
.addItem('Translate from English to Zis', 'translateEnglishToZisian_') | |
.addToUi(); | |
} | |
/** | |
* A set-up function that uses the conference data in the spreadsheet to create | |
* Google Calendar events, a Google Form, and a trigger that allows the script | |
* to react to form responses. | |
*/ | |
function translateZisianToEnglish_() { | |
var html = HtmlService.createHtmlOutputFromFile('index') | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME); | |
html.append("<script>(function() { var mode1 = document.getElementById('mode1'); mode1.checked = true; })();</script>"); | |
SpreadsheetApp.getUi() // Or DocumentApp or FormApp. | |
.showModalDialog(html, 'Translate from Zisian to English'); | |
} | |
/** | |
* A set-up function that uses the conference data in the spreadsheet to create | |
* Google Calendar events, a Google Form, and a trigger that allows the script | |
* to react to form responses. | |
*/ | |
function translateEnglishToZisian_() { | |
var html = HtmlService.createHtmlOutputFromFile('index') | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME); | |
html.append("<script>(function() { var mode2 = document.getElementById('mode2'); mode2.checked = true; })();</script>"); | |
SpreadsheetApp.getUi() // Or DocumentApp or FormApp. | |
.showModalDialog(html, 'Translate from English to Zisian'); | |
} | |
function translate(direction, text) { | |
var cache = CacheService.getScriptCache(); | |
var lookupZisianToEnglish = cache.get("lookupZisianToEnglish"); | |
lookupZisianToEnglish = JSON.parse(lookupZisianToEnglish); | |
if (lookupZisianToEnglish == null) { | |
throw "Zisian To English lookup is not populated." | |
} | |
var lookupEnglishToZisian = cache.get("lookupEnglishToZisian"); | |
lookupEnglishToZisian = JSON.parse(lookupEnglishToZisian); | |
if (lookupEnglishToZisian == null) { | |
throw "English To Zisian lookup is not populated." | |
} | |
//for (var n in lookupEnglishToZisian) { | |
// Logger.log(n + ' => ' + lookupEnglishToZisian[n]); | |
//} | |
//Logger.log("Size of lookupZisianToEnglish: " + Object.keys(lookupZisianToEnglish).length); | |
//Logger.log("Size of lookupEnglishToZisian: " + Object.keys(lookupEnglishToZisian).length); | |
if (text == null) { | |
throw "No text provided."; | |
} | |
// Must be array for look behind | |
var output = []; | |
var lookup = null; | |
if (direction == 'zi_en') { | |
lookup = lookupZisianToEnglish; | |
} else if (direction == 'en_zi') { | |
lookup = lookupEnglishToZisian; | |
} else { | |
throw "Invalid direction: " + direction; | |
} | |
if (lookup == null) { | |
throw "Lookup was not located."; | |
} | |
var parts = text.split(/(\s+)/); | |
for (var n in parts) { | |
if (parts[n].match(/\s+/g)) { // || parts[n].match(/\W/g) | |
output.push(parts[n]); | |
continue; | |
} | |
var punctuation = null; | |
//if (output != '') output += ' '; | |
parts[n] = parts[n].trim(); | |
if (parts[n].charAt(parts[n].length - 1).match(/\W/g)) { | |
punctuation = parts[n].charAt(parts[n].length - 1); | |
parts[n] = parts[n].substr(0, parts[n].length - 1); | |
} | |
var word = parts[n].toLowerCase(); | |
var translation; | |
// If word is found in lookup, immediately add | |
if (lookup[word]) { | |
translation = lookup[word].translation; | |
if (parts.length > 1 && n > 1) { | |
// Check for lookbehind | |
var lookbehind = parts[n - 2].toLowerCase() + ' ' + word; | |
Logger.log('Does lookbehind ' + parts[n - 2].toLowerCase() + ' ' + word + ' exist?'); | |
if (lookup[lookbehind]) { | |
translation = lookup[lookbehind].translation; | |
// If lookup is a valid Zis word, remove it from the output | |
if (lookup[parts[n - 2]]) { | |
output.pop(); | |
output.pop(); | |
} | |
} | |
} | |
if (direction == 'zi_en' && translation == 'i') { | |
translation = 'I'; | |
} | |
if (translation.length > 0) { | |
output.push(translation); | |
} else { | |
output.push(parts[n].toUpperCase()); | |
} | |
} else { | |
// If NEITHER the first word NOR the second word are in Zis, | |
// | |
if (parts.length > 1 && n > 1) { | |
// Check for lookbehind | |
var lookbehind = parts[n - 2].toLowerCase() + ' ' + word; | |
Logger.log('Does lookbehind ' + parts[n - 2].toLowerCase() + ' ' + word + ' exist?'); | |
if (lookup[lookbehind]) { | |
translation = lookup[lookbehind].translation; | |
output.pop(); | |
output.pop(); | |
output.push(translation); | |
if (punctuation) { | |
output.push(punctuation); | |
} | |
continue; | |
} | |
} | |
// Check plurals | |
var plural = false; | |
if (direction == 'en_zi') { | |
var singular = word.substr(0, word.length - 1); | |
if (lookup[singular] && lookup[singular].pos == 'noun') { | |
translation = lookup[singular].translation.toLowerCase(); | |
if (parts.length > 1 && n > 1) { | |
// Check for lookbehind | |
var lookbehind = parts[n - 2].toLowerCase() + ' ' + word; | |
Logger.log('Does lookbehind ' + parts[n - 2].toLowerCase() + ' ' + word + ' exist?'); | |
if (lookup[lookbehind]) { | |
translation = lookup[lookbehind].translation; | |
output.pop(); | |
output.pop(); | |
} | |
} | |
translation += "'n"; | |
output.push(translation); | |
} else { | |
// Return non-matching words in uppercase | |
output.push(parts[n].toUpperCase()); | |
} | |
} else if (direction == 'zi_en') { | |
translation = lookup[word].translation; | |
if (direction == 'zi_en' && translation == 'i') { | |
translation = 'I'; | |
} | |
output.push(translation); | |
} else { | |
// Return non-matching words in uppercase | |
output.push(parts[n].toUpperCase()); | |
} | |
} | |
if (punctuation) { | |
output.push(punctuation); | |
} | |
} | |
return output.join(''); | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
function onSuccess(result) { | |
var input = document.getElementById('output'); | |
input.value = result; | |
} | |
function onFailure(error) { | |
var input = document.getElementById('output'); | |
input.value = "ERROR: " + error.message; | |
} | |
function submitForm() { | |
var mode1 = document.getElementById('mode1'); | |
var mode2 = document.getElementById('mode2'); | |
var direction = mode1.checked ? mode1.value : mode2.value; | |
var ttt = document.getElementById('ttt'); | |
var text = ttt.value; | |
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).translate(direction, text); | |
} | |
</script> | |
</head> | |
<body> | |
<label for="mode">Translation Mode:</label><br> | |
<input id="mode1" name="mode" value="zi_en" type="radio"><label for="mode">Zis to English</label><br> | |
<input id="mode2" name="mode" value="en_zi" type="radio"><label for="mode">English to Zis</label><br> | |
<br> | |
<label for="ttt">Text to Translate:</label><br> | |
<input id="ttt" name="ttt" type="text" value="" /><br> | |
<br> | |
<label for="output">Output:</label><br> | |
<input id="output" name="output" type="text" value="" /><br> | |
<br> | |
<input type="button" value="Translate" | |
onclick="submitForm();" /> | |
| |
<input type="button" value="Close" | |
onclick="google.script.host.close()" /> | |
<!--<script> | |
/*(function() { | |
var mode1 = document.getElementById('mode1'); | |
var mode2 = document.getElementById('mode2'); | |
if (translationDirection) { | |
var input = document.getElementById('output'); | |
input.value = mode1.value; | |
mode1.checked = true; | |
} else { | |
var input = document.getElementById('output'); | |
input.value = mode2.value; | |
mode2.checked = true; | |
} | |
})();*/ | |
</script>--> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment