2018年3月10日 星期六

台羅音輸入法--一逝編輯器

推捒台語一向是台語陣營的目標,台羅音音調輸入目前有一寡困難。網路目前嘛可能無會當輸入2,3,5,7,8音調的羅馬符號的方法。下跤的javascript是一个小小的試驗,有需要的人會當掠轉去家己電腦內面試用。連去試用
用法:拍著2,3,5,7,8的時,會將進前5字以內的字母判斷自動轉做台語羅馬音符號。進前若無字好轉,遮仔袂刣掉。


<!doctype html>
<head>
<meta charset="utf-8">
<title>一逝編輯器</title>
<script>
  var cArr = new Array(); var cur = 0, front = 0;
  var tones = [2, 3, 5, 7, 8]; var mothers = ['a', 'e', 'o', 'i', 'u'];
  var a = ["á", "à", "â", "ā", "a̍"],
      e = [" é", "è", "ê", "ē", "e̍"],
      o = ["ó", "ò", "ô", "ō", "o̍"],
      i = ["í", "ì", "î", "ī", "i̍"],
      u = ["ú", "ù", "û", "ū", "u̍"],
      n = ["ń", "ǹ", "n̂", "n̄", "n"],
      m = ["ḿ", "m", "m", "m̄", "m"];
  var rel = {'2': 0, '3': 1, '5': 2, '7': 3, '8': 4};  //調號關連到array
  var strtofunc = {'a': a, 'e': e, 'o': o, 'i': i, 'u': u };
  var ntofunc   = {'n': n}; var mtofunc = {'m': m};
  var token_len = 5;
  var key;

  function getString(start, end) {
    var str = '';
    for (var j = start; j < end; j++) {
      str = str.concat(cArr[j]);
    }
    return str;
  }

  function subStr(curr) {
    substr = '';
    if ( (curr - token_len) < 0) substr = cArr.slice(0, curr);
    else substr = cArr.slice(curr - token_len, curr);  // at most 5
    return substr.join('');  //join 用''連接每一个元素
  }

  function getMoIndex(arr) {  //if no mother letter , return length of arr
    var count = 0;
    for (var j = 0; j < arr.length; j++) {
      if (isMother(arr[j])) {
        if ( arr[j] == 'a' || arr[j] == 'e' || arr[j] == 'o') {
          return count;
        }
        else {  //i, u
          if (isMother(arr[j+1])) return count+1;
          else return count;
        }
      }
      else count++;
    }
    return count;
  }
  /*
  function getTokenMoIndex(curr) {
    var index = subStr(curr).indexOf(' ');
    substr = substr.slice(index+1);
    var len = substr.length;
    return curr - len + getMoIndex(substr);
  }
*/
  function getTokenMoIndex(curr) {
    var substr = subStr(curr);
    var len = substr.length;
    var index = substr.indexOf(' ');
    substr = substr.slice(index+1);
    return curr - len + index + 1 + getMoIndex(substr);
  }

  function isTone(key) {
    for (var j = 0; j < tones.length; j++) {
      if (key == tones[j]) {
        return true;
      }
    }
    return false;
  }

  function isMother(key) {
    for (var j = 0; j < mothers.length; j++) {
      if (key == mothers[j]) {
        return true;
      }
    }
    return false;
  }

  document.onkeypress = function(event) {
    if (event.keyCode >= 32 && event.keyCode <= 127) {
      key = String.fromCharCode(event.keyCode);

      var num = getTokenMoIndex(cur);

      if ( isTone(key) && num < cur) {  //本字母有tone數字
        cArr[num] = strtofunc[cArr[num]][rel[key]];
      }
      else {
        if (isTone(key) && subStr(cur).indexOf('ng') >= 0) {
          var len = subStr(cur).length;
          var loc = cur - (len - subStr(cur).indexOf('ng'));
          cArr[loc] = ntofunc[cArr[loc]][rel[key]];
        }
        else if (isTone(key) && (subStr(cur) == 'm' || subStr(cur).indexOf(' m') >=0)) {
          var len = subStr(cur).length;
          var loc = cur - (len - subStr(cur).indexOf(' m'));
          cArr[loc+1] = mtofunc[cArr[loc+1]][rel[key]];
        }
        else {
          cArr.splice(cur, 0, key);
          front++; cur++;
        }
      }
    }
    else return;
    str.value = getString(0, cur) + '|' + getString(cur, front);
  }

  document.onkeydown = function(event) {
    if (event.keyCode == 8) {
      if (cur >= 1) {
        cArr.splice(cur-1, 1);
        cur--; front--;
      }
    }
    else if (event.keyCode == 37) { // left arrow
        if (cur >= 1) cur--;
    }
    else if (event.keyCode == 39) { //right arrow
      if (cur < front) cur++;
    }
    else if (event.keyCode == 13) {
      cur = front;
      //str.value = getString(0, front) + '|';
    }
    str.value = getString(0, cur) + '|' + getString(cur, front);
  }
</script>
</head>

<body>
  <span style="color: orange">瀏覽器執行台羅音一逝編輯器</span><br>
  <textarea id="str" style="font-size: 24px; border: 2px solid #336600;color:#800000;"
  disabled rows="1" cols="50">sin dâi lô tsi̍ tsuā bpian tsi̍p kgì|</textarea><br>
  <span style="color: blue">說明:音調數字拍出了後揣前5字的token共台羅韻母注音。</span>
  <script>
    var str = document.getElementById("str");
  </script>
</body>
</html>

沒有留言:

張貼留言