irish-verb-conjugator/conj.ts

346 lines
12 KiB
TypeScript
Raw Normal View History

2021-01-10 18:20:05 +08:00
var conjNoList = (<HTMLInputElement> document.getElementById('conj-no'));
var conjOptDiv = document.getElementById('conj1-opt-div');
var startConj = document.getElementById('start-conjugate');
var original = (<HTMLInputElement> document.getElementById('original'));
var conjStem = (<HTMLInputElement> document.getElementById('conj-stem'));
var conj_no = parseInt(conjNoList.value);
2021-01-12 23:12:44 +08:00
var type_of_conj1 = "general";
2021-01-10 18:20:05 +08:00
var typesOfConj1Rads = document.getElementsByName("types-of-conj1");
2021-01-10 21:17:59 +08:00
// reset the dropdown list and the 1st conj opt div while refreshing the page.
window.addEventListener('pageshow', function(event) {
conjNoList.value = "1";
conjOptDiv.style.display = "block";
});
2021-01-10 18:20:05 +08:00
for(var i = 0; i < typesOfConj1Rads.length; i++) {
let radValue = (<HTMLInputElement> typesOfConj1Rads[i]).value;
typesOfConj1Rads[i].addEventListener('change', () => {
type_of_conj1 = radValue;
})
}
conjNoList.addEventListener('change', (event) => {
conj_no = parseInt(conjNoList.value);
if (conj_no == 2) {
conjOptDiv.style.display = "none";
} else {
conjOptDiv.style.display = "block";
}
})
function emptyString(str : string){
var isContainedSpace = str.match(/^[  \t\n\r]+$/);
var isReallyEmpty = (str == "");
return isReallyEmpty || isContainedSpace;
}
2021-01-12 23:12:44 +08:00
original.addEventListener('keyup', generateConjMain);
conjStem.addEventListener('keyup', generateConjMain);
2021-01-12 23:12:44 +08:00
original.addEventListener('change', generateConjMain);
conjStem.addEventListener('change', generateConjMain);
startConj.addEventListener('click', generateConjMain);
function generateConjMain(event : any){
2021-01-10 18:20:05 +08:00
var original_text = original.value;
var conjstem_text = conjStem.value;
if (emptyString(original_text) && emptyString(conjstem_text)){
2021-01-10 21:17:59 +08:00
alert("您輸入的是空字串或空白字元,請重新輸入。")
2021-01-10 18:20:05 +08:00
}
else{
if (emptyString(conjstem_text)){
conjstem_text = original_text;
};
generateConj(original_text, conjstem_text, conj_no);
}
}
2021-01-10 18:20:05 +08:00
function checkedIfBroadEnd(str_array : string[]){
for (var i = str_array.length - 1; i >=0; i--){
if (str_array[i].match(/[aouáóúAOUÁÓÚ]/)){
return true;
}
else if (str_array[i].match(/[eiéíEIÉÍ]/)){
return false;
}
}
return true;
}
let first_conj =
{ "broad" :
{
"pres" : ["aim", "air", "ann", "aimíd", "ann sibh", "aid", null, "tar"],
"past" : ["as", "ais", null, "amair", "abhair", "adar", null, "adh"],
"pa_ha" : ["ainn", "tá", "adh", "aimís", "adh sibh", "aidís", "taí"],
"futu" : ["fad", "fair", "faidh", "faimíd", "faidh sibh", "faid", "far"],
"cond" : ["fainn", "fá", "fadh", "faimís", "fadh sibh", "faidís", "faí"],
"pr_su" : ["ad", "air", "aidh", "aimíd", "aidh sibh", "aid", null],
"impe" : ["aim", null, "adh", "aimís", "aidh", "aidís", null],
},
"slender" :
{
"pres" : ["im", "ir", "eann", "imíd", "eann sibh", "id", null, "tear"],
"past" : ["eas", "is", null, "eamair", "eabhair", "eadar", null, "eadh"],
"pa_ha" : ["inn", "teá", "eadh", "imís", "eadh sibh", "idís", "tí"],
"futu" : ["fead", "fir", "fidh", "fimíd", "fidh sibh", "fid", "fear"],
"cond" : ["finn", "feá", "feadh", "fimís", "feadh sibh", "fidís", "fí"],
"pr_su" : ["ead", "ir", "idh", "imíd", "idh sibh", "id", null],
"impe" : ["im", null, "eadh", "imís", "idh", "idís", null],
}
}
let second_conj =
{ "broad" :
{
2021-01-10 21:17:59 +08:00
"pres" : ["aím", "aír", "aíonn", "aímíd", "aíonn sibh", "aíd", null, "aítear"],
"past" : ["aíos", "aís", null, "aíomair", "aíobhair", "aíodar", null, "aíodh"],
"pa_ha" : ["aínn", "aíteá", "aíodh", "aímís", "aíodh sibh", "aídís", "aítí"],
"futu" : ["ód", "óir", "óidh", "óimíd", "óidh sibh", "óid", "ófar"],
"cond" : ["óinn", "ófá", "ódh", "óimís", "ódh sibh", "óidís", "ófaí"],
"pr_su" : ["aíod", "aír", "aídh", "aímíd", "aídh sibh", "aíd", null],
"impe" : ["aím", null, "aíodh", "aímís", "aídh", "aídís", null],
2021-01-10 18:20:05 +08:00
},
"slender" :
{
"pres" : ["ím", "ír", "íonn", "ímíd", "íonn sibh", "íd", null, "ítear"],
2021-01-10 21:17:59 +08:00
"past" : ["íos", "ís", null, "íomair", "íobhair", "íodar", null, "íodh"],
"pa_ha" : ["ínn", "íteá", "íodh", "ímís", "íodh sibh", "ídís", "ítí"],
2021-01-10 18:20:05 +08:00
"futu" : ["eod", "eoir", "eoidh", "eoimíd", "eoidh sibh", "eoid", "eofar"],
"cond" : ["eoinn", "eofá", "eodh", "eoimís", "eodh sibh", "eoidís", "eofaí"],
"pr_su" : ["íod", "ír", "ídh", "ímíd", "ídh sibh", "íd", null],
"impe" : ["ím", null, "íodh", "ímís", "ídh", "ídís", null],
}
}
var ighType;
// values for ighType
let shortVowelEnd = 0;
let longBroadEnd = 1;
let longSlenderEnd = 2;
let firstConjIgh =
{ 0 : // shortVowelEnd
{
"pres" : ["ím", "ír", "íonn", "ímíd", "íonn sibh", "íd", null, "itear"],
"past" : ["íos", "ís", null, "íomair", "íobhair", "íodar", null, "íodh"],
"pa_ha" : ["ínn", "iteá", "íodh", "ímís", "íodh sibh", "ídís", "ití"],
"futu" : ["ífead", "ífir", "ífidh", "ífimíd", "ífidh sibh", "ífid", "ífear"],
"cond" : ["ífinn", "ífeá", "ífeadh", "ífimís", "ífeadh sibh", "ífidís", "ífí"],
"pr_su" : ["íod", "ír", "ídh", "ímíd", "ídh sibh", "íd", null],
"impe" : ["ím", null, "íodh", "ímís", "ídh", "ídís", null],
},
1 : // longBroadEnd
{
"pres" : ["im", "ir", "nn", "imíd", "nn sibh", "id", null, "tar"],
"past" : ["s", "is", null, "mair", "bhair", "dar", null, "dh"],
"pa_ha" : ["inn", "iteá", "dh", "imís", "dh sibh", "idís", "ití"],
"futu" : ["fad", "fair", "faidh", "faimíd", "faidh sibh", "faid", "far"],
"cond" : ["fainn", "fá", "fadh", "faimís", "fadh sibh", "faidís", "faí"],
"pr_su" : ["d", "ir", "idh", "imíd", "idh sibh", "id", null],
"impe" : ["im", null, "dh", "imís", "idh", "idís", null],
},
2 : // longSlenderEnd
{
"pres" : ["im", "ir", "ann", "imíd", "ann sibh", "id", null, "tear"],
"past" : ["as", "is", null, "amair", "abhair", "adar", null, "adh"],
"pa_ha" : ["inn", "iteá", "adh", "imís", "adh sibh", "idís", "tí"],
"futu" : ["ifead", "ifir", "ifidh", "ifimíd", "ifidh sibh", "ifid", "ifear"],
"cond" : ["ifinn", "ifeá", "ifeadh", "ifimís", "ifeadh sibh", "ifidís", "ifí"],
"pr_su" : ["ad", "ir", "idh", "imíd", "idh sibh", "id", null],
"impe" : ["im", null, "adh", "imís", "idh", "idís", null],
}
}
function generateConj(orig_txt : string, stem_txt : string, conj_no : Number){
var impe_ls = document.getElementById("impe");
var past_ls = document.getElementById("past");
var _2sgimpe = impe_ls.getElementsByTagName("td")[2];
var _3sgpast = past_ls.getElementsByTagName("td")[3];
_2sgimpe.innerHTML = orig_txt;
_3sgpast.innerHTML = orig_txt;
// Gael => ['G', 'ae', 'l']; ithim => [i, th]
2021-01-10 21:17:59 +08:00
function split_text(txt : string){
return txt.split(/([aA][eE]|[bcdfgmpstBCDFGMPST][hH]|[A-Za-z])/).filter(text => text);
}
var isBroadEnd;
if (conj_no == 1){
var stem_txt_splitted = split_text(stem_txt);
checkedIfBroadEnd(stem_txt_splitted);
2021-01-10 18:20:05 +08:00
if (type_of_conj1 == "igh"){
if (!orig_txt.match('[iI][gG][hH]$')){
alert("原來的詞不以 -igh 結尾。");
}
if (stem_txt.match('([áóúÁÓÚ]|[eE][oO]|[uU][aA])$')){
ighType = longBroadEnd;
}
else if (stem_txt.match('[éÉ]$')){
ighType = longSlenderEnd;
}else{
ighType = shortVowelEnd;
}
var suffixTable = firstConjIgh[ighType];
fillTablesInner(suffixTable);
}
else if (type_of_conj1 == "ail"){
if (!orig_txt.match('[áÁ][iI][lL]$')){
alert("原來的詞不以 -áil 結尾。");
}
2021-03-07 16:25:11 +08:00
isBroadEnd = true;
2021-01-10 18:20:05 +08:00
firstConjFillTheArray();
2021-03-07 16:25:11 +08:00
// convert stem -áil to -ál
2021-01-10 18:20:05 +08:00
var stemAilLeft = stem_txt.substring(0, stem_txt.length-1);
var stemAil;
if (stemAilLeft.substring(stemAilLeft.length-1) == 'á'){
stemAil = stemAilLeft + 'i' + stem_txt.substring(stem_txt.length-1);
}
else{
stemAil = stemAilLeft + 'I' + stem_txt.substring(stem_txt.length-1);
}
var autoPres = document.getElementById("pres").getElementsByTagName("td")[8];
var _2SgPaHa = document.getElementById("pa_ha").getElementsByTagName("td")[2];
var autoPaHa = document.getElementById("pa_ha").getElementsByTagName("td")[7];
var suffixTable = first_conj["slender"];
var autoPresSuffix = suffixTable["pres"][7];
var _2sgPaHaSuffix = suffixTable["pa_ha"][1];
var autoPaHaSuffix = suffixTable["pa_ha"][6];
autoPres.innerHTML = stemAil + autoPresSuffix;
_2SgPaHa.innerHTML = stemAil + _2sgPaHaSuffix;
autoPaHa.innerHTML = stemAil + autoPaHaSuffix;
}
else{
2021-03-26 21:30:48 +08:00
isBroadEnd = checkedIfBroadEnd(split_text(stem_txt));
2021-01-10 18:20:05 +08:00
// general 1st vonj verbs
firstConjFillTheArray();
// irreagularity of the pres. auto. form of lean
2021-01-12 23:12:44 +08:00
if (stem_txt.match(/[lL][eE][aA][nN]/)){
2021-01-10 18:20:05 +08:00
var relPres = document.getElementById("pres").getElementsByTagName("td")[7];
2021-01-12 23:12:44 +08:00
relPres.innerHTML = stem_txt + "as";
2021-01-10 18:20:05 +08:00
}
}
2021-01-10 21:17:59 +08:00
}
// conj_no == 2
else{
2021-01-10 18:20:05 +08:00
2021-01-10 21:17:59 +08:00
// remove the -(a)igh of orig. text as the stem txt
if (orig_txt.match(/.*[aA]?[iI][gG][hH]$/)){
console.log("matched!");
stem_txt = /(.*?)[aA]?[iI][gG][hH]/.exec(orig_txt)[1];
2021-01-10 18:20:05 +08:00
}
2021-01-10 21:17:59 +08:00
console.log(stem_txt);
var stem_txt_splitted = split_text(stem_txt);
isBroadEnd = checkedIfBroadEnd(stem_txt_splitted);
2021-01-10 18:20:05 +08:00
2021-01-10 21:17:59 +08:00
var suffixTable;
if (isBroadEnd) {
suffixTable = second_conj["broad"];
} else {
suffixTable = second_conj["slender"];
}
fillTablesInner(suffixTable);
}
// general 1st vonj verbs
function firstConjFillTheArray() {
2021-03-26 21:30:48 +08:00
2021-01-10 21:17:59 +08:00
var suffixTable;
if (isBroadEnd) {
suffixTable = first_conj["broad"];
} else {
suffixTable = first_conj["slender"];
}
fillTablesInner(suffixTable);
}
2021-01-10 18:20:05 +08:00
function fillTablesInner(suffixTable : any) {
var suffixTableKeys = Object.keys(suffixTable);
for (var i = 0; i < suffixTableKeys.length; i++) {
var currentSuffixTableKey = suffixTableKeys[i];
var personalArray = document.getElementById(currentSuffixTableKey);
for (var j = 0; j < suffixTable[currentSuffixTableKey].length; j++) {
var item = personalArray.getElementsByTagName("td")[1 + j];
var currentSuffix = suffixTable[currentSuffixTableKey][j];
if (currentSuffix != null) {
item.innerHTML = stem_txt + currentSuffix;
// remove duplicated t
if (stem_txt.match(/.*[tT]$/) && currentSuffix[0] == 't'){
item.innerHTML = stem_txt + currentSuffix.substring(1);
}
2021-01-10 18:20:05 +08:00
}
}
2021-01-10 21:17:59 +08:00
2021-04-03 02:00:30 +08:00
}
var futuAnalSuffixPs : string = "<br/><span style=\"font-size:50%;\">-idh接第三人稱代名詞和指示代名詞不發音其餘唸 /ɟ/</span>";
var futuTr = document.getElementById("futu");
var futuAnal : HTMLTableDataCellElement = futuTr.getElementsByTagName("td")[3];
futuAnal.innerHTML = futuAnal.innerHTML + futuAnalSuffixPs;
var condAnalSuffixPs : string = "<br/><span style=\"font-size:50%;\">-dh 唸 /x/</span>";
var condTr = document.getElementById("cond");
var condAnal : HTMLTableDataCellElement = condTr.getElementsByTagName("td")[3];
condAnal.innerHTML = condAnal.innerHTML + condAnalSuffixPs;
var impeAnalSuffixPs : string = "<br/><span style=\"font-size:50%;\">-dh 於科克唸 /x/</span>";
var impeTr = document.getElementById("impe");
var impeAnal : HTMLTableDataCellElement = impeTr.getElementsByTagName("td")[3];
impeAnal.innerHTML = impeAnal.innerHTML + impeAnalSuffixPs;
var pastAutoAndPahaAnalSuffixPs : string = "<br/><span style=\"font-size:50%;\">-dh 於科克唸 /g/</span>";
var pahaTr = document.getElementById("pa_ha");
var pahaAnal : HTMLTableDataCellElement = pahaTr.getElementsByTagName("td")[3];
var pastTr = document.getElementById("past");
var pastAuto : HTMLTableDataCellElement = pastTr.getElementsByTagName("td")[8];
pahaAnal.innerHTML = pahaAnal.innerHTML + pastAutoAndPahaAnalSuffixPs;
pastAuto.innerHTML = pastAuto.innerHTML + pastAutoAndPahaAnalSuffixPs;
}
2021-01-10 21:17:59 +08:00
}