Skip to content

Commit

Permalink
ToneOZ 澳聲通字典工具整合:改進IVS詞彙查詢邏輯
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyxuan committed Jun 1, 2021
1 parent e3cbba2 commit fbcfc84
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 100 deletions.
40 changes: 25 additions & 15 deletions ime/ime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var text = [];
var curr = -1;
var vsbase = 0xe01e0;
window.textinfo = {};

function chr(uni) {
if (String.fromCodePoint) return String.fromCodePoint(uni); // ES6
Expand All @@ -21,25 +22,33 @@ function match(c, i, p, j, onlydic) {
var phrase = p.replace("*",c);
for (var x=0; x<p.length; x++) {
var a = i-pos+x;
var spobj = $('#sp' + a);
var phraseattr = spobj.attr("phrase");
if(phraseattr === undefined){
phraseattr = phrase;
} else {
var phrasearr = phraseattr.split(",");
if(phrasearr.indexOf(phrase)<0){
phrasearr.push(phrase);
}
phraseattr = phrasearr.join(",");
}

var spDom = spobj.attr({
"phrase":phraseattr
});
var spDom = $('#sp' + a);

// update ivs
if (p.charAt(x) == '*' && !onlydic) {
text[a] = c + (j > 0 ? chr(vsbase + j*1) : '');
spDom.text(text[a]).addClass('auto');
}

// update phrase for dic
if(!textinfo[a]){
textinfo[a] = {};
}
var ivsinfo = textinfo[a];
var phrasearr = ivsinfo.phrasearr;
var phraseidx = ivsinfo.phraseidx;
var phrasedata = {phrase:phrase,x:x,a:a,ivs:text[a]};;
if(phraseidx === undefined){
phraseidx = {};
}
if(phrasearr === undefined){
phrasearr = [phrasedata];
} else {
phrasearr.push(phrasedata);
}
phraseidx[""+x+phrase] = phrasearr.length-1;
ivsinfo.phrasearr = phrasearr;
ivsinfo.phraseidx = phraseidx;
}
}

Expand Down Expand Up @@ -109,6 +118,7 @@ function setEditorText(t) {
// [\ud800-\udfff] means surrogate pairs of UTF-16
// Here I wrote /(.|\n)/g because MS Edge doesn't support /(.)/s.
text = t.replace(/(.|\n)/g, "\x01$1").replace(/\x01([\ud800-\udfff])/g, "$1").split(/\x01/);
textinfo = {};

var editor = $('#editor');
$('#editor').empty();
Expand Down
6 changes: 3 additions & 3 deletions ime/tzdic/tzdic.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
Website : https://toneoz.com (Pinyin Zhuyin Graphical Editor)
Github : https://github.com/jeffreyxuan/ToneOZDic
License : MIT License. Free for both commercial and personal uses
Usage example (with demo tzdic folder):
Usage example :
tzdic.html?q=[{"id":1,"q":"和"},{"id":1,"q":"樂"}]
-->
<html lang="zh-hant-TW">
<html lang="zh-hant-TW" style="height:100%;">
<head>
<meta charset="utf-8">
<title>ToneOZ 澳聲通字典</title>
<style>
body{
color:#fff;
background-image: linear-gradient(45deg, rgba(16, 16, 16, 0.7) 0%, rgba(0,0,0,0.9) 40%,rgba(16,16,16,0.7) 100%);
}
.diccard{
padding: 5px 0 5px 0;
border-bottom:2px solid #fff;
}
.dicq{
font-size:2em;

}
.dicz{
font-size:1.5em;
Expand Down
124 changes: 62 additions & 62 deletions ime/tzdic/tzdicui.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,82 +98,82 @@ function TZDicUI(){
return hash;
}

window.tzUpdateEvent = function(dicParam){
if(tzparam.fnGetPhrases){
let phrasesInfo = tzparam.fnGetPhrases(dicParam);
if(!phrasesInfo.phrases){
// not a valid query
return;
let tmrUpdateEvent = null;
window.tzUpdateEvent = function(dicParam){
if(tmrUpdateEvent){
clearTimeout(tmrUpdateEvent);
tmrUpdateEvent = null;
}
tmrUpdateEvent = setTimeout(function(){
if(tzparam.fnGetPhrases){
let phrasesInfo = tzparam.fnGetPhrases(dicParam);
if(!phrasesInfo.phrases){
// not a valid query
return;
}
Object.assign(dicParam, phrasesInfo);
}
Object.assign(dicParam, phrasesInfo);
}
UpdateDic(dicParam);
UpdateDic(dicParam);
},200);
}

let tmrUpdateDic = null;
function UpdateDic(dicparam){
if(tmrUpdateDic){
clearTimeout(tmrUpdateDic);
tmrUpdateDic = null;
}
if(!dicparam){
dicparam = {};
}
let {event, phrases, rawstr} = dicparam;
tmrUpdateDic = setTimeout(function(){
let qArray = [];
let tmpq, id, hash, isChinese, objSrc;
let qArray = [];
let tmpq, id, hash, isChinese, objSrc;

if(!phrases && !rawstr){
if(event && event.target){
objSrc = $(event.target);
} else {
objSrc = tzparam.objInput;
}
// get selected string from a textarea
rawstr = GetSelectedString(objSrc);
if(!phrases && !rawstr){
if(event && event.target){
objSrc = $(event.target);
} else {
objSrc = tzparam.objInput;
}
if(rawstr){
rawstr = rawstr.trim();
// query each words in the selected string
let rawstrarr = splitx(rawstr);
if(!phrases){
phrases = [];
}
phrases = phrases.concat(rawstrarr);

// add selected string as the first query phrase
if(phrases.indexOf(rawstr)<0){
qArray.push(GetQuery({
phrase : rawstr
}));
}
// get selected string from a textarea
rawstr = GetSelectedString(objSrc);
}
if(rawstr){
rawstr = rawstr.trim();
// query each words in the selected string
let rawstrarr = splitx(rawstr);
if(!phrases){
phrases = [];
}
phrases = phrases.concat(rawstrarr);

// get query parameters for ToneOZDic
for(let idxPhrase in phrases){
let phrase = phrases[idxPhrase];
// add selected string as the first query phrase
if(phrases.indexOf(rawstr)<0){
qArray.push(GetQuery({
phrase : phrase
}));
phrase : rawstr
}));
}

let dicURLParam = JSON.stringify(qArray);

// do dictionary query
let dicURLBase = tzDicEntryHTML + "?"
+ (tzparam.strVer ? "v="+tzparam.strVer+"&" : "");
let URL = dicURLBase+"q="+dicURLParam;
if(tzparam.pathCssDic){
URL += "&css=" + encodeURIComponent(tzparam.pathCssDic+".css");
}
if(tzparam.pathJsIVSLookup){
URL += "&ivs=" + encodeURIComponent(tzparam.pathJsIVSLookup+".js");
}
tzparam.objIframe.attr("src", URL).show();
tmrUpdateDic = null;
console.log(URL);
},200);
}

// get query parameters for ToneOZDic
for(let idxPhrase in phrases){
let phrase = phrases[idxPhrase];
qArray.push(GetQuery({
phrase : phrase
}));
}

let dicURLParam = JSON.stringify(qArray);

// do dictionary query
let dicURLBase = tzDicEntryHTML + "?"
+ (tzparam.strVer ? "v="+tzparam.strVer+"&" : "");
let URL = dicURLBase+"q="+dicURLParam;
if(tzparam.pathCssDic){
URL += "&css=" + encodeURIComponent(tzparam.pathCssDic+".css");
}
if(tzparam.pathJsIVSLookup){
URL += "&ivs=" + encodeURIComponent(tzparam.pathJsIVSLookup+".js");
}
tzparam.objIframe.attr("src", URL).show();
tmrUpdateDic = null;
//console.log(URL);
};

function GetQuery(param){
Expand Down
24 changes: 22 additions & 2 deletions ime/tzdic/tzlib/tzdicentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ $(document).ready(function () {
if(item.q){
let qarray = null;
let yinarray = null;
let noivs = removeIVS(item.q);
// check is the dictionary data tzdata loaded
if(tzdic[dicSlot]){
// dictionary lookup
result = tzdic[dicSlot][item.q+posfix];
result = tzdic[dicSlot][noivs+posfix];
if(ivsdic){
qarray = item.q.split("");
qarray = noivs.split("");
if(result && result.z){
yinarray = result.z.split(new RegExp(separators.join('|'), 'g'));
}
Expand Down Expand Up @@ -105,6 +106,25 @@ function getIVS(c, j){
return c + (j > 0 ? chr(vsbase + j*1) : '');
}

function removeIVS(q){
let noivs = "";
if(q){
let qarr = q.split("");
let prevIsVbase = false;
for(iq=0;iq<qarr.length;iq++){
if(qarr[iq] == "\udb40"){
prevIsVbase = true;
continue;
}
if(!prevIsVbase){
noivs += qarr[iq];
}
prevIsVbase = false;
}
}
return noivs;
}

function chr(uni) {
if (String.fromCodePoint) return String.fromCodePoint(uni); // ES6
if (uni <= 0xffff) return String.fromCharCode(uni);
Expand Down
73 changes: 55 additions & 18 deletions ime/zhdicui.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,70 @@ function ZiHaiDicUI(){
function fnGetPhrases(param){
let {event,srcObj} = param;
let phrases = null;
let selectedString = null;
isIframeReady = false;
if(!srcObj && event && event.target){
srcObj = $(event.target);
srcObj = $(event.target);
if (window.getSelection) {
selectedString = window.getSelection().toString();
}
}
if(srcObj){
if(srcObj && !selectedString){
let currDataI = srcObj.attr("data-i");
if(currDataI !== undefined){
currDataI = parseInt(currDataI);
let phrase = srcObj.attr("phrase");
phrases = [];
let content = srcObj.text();
if(phrase){
phrases = phrase.split(",");
let ivsinfo = textinfo[currDataI];
if(ivsinfo){
let phrasearr = ivsinfo.phrasearr;
for(let ip in phrasearr){
let phrase = phrasearr[ip].phrase;
if(phrase && phrases.indexOf(phrase)<0){
// update ivs info
phrase = getIVSPhrase(phrasearr[ip]);
phrases.push(phrase);
}
}
}
if(content){
if(!phrases){
phrases = [];
}
phrases.push(content.charAt(0));
}
}
}
} else if(selectedString){
phrases = [selectedString];
}

return {
phrases:phrases
};
}

function getIVSPhrase(param){
let {phrase, x, a, ivs} = param;
let phraseivs = "";
for(let ip=0; ip<phrase.length; ip++){
let preva = a-x+ip;
if(preva==a){
phraseivs += ivs;
} else {
let ivsinfo = textinfo[preva];
let previdx = ""+ip+phrase;
if(ivsinfo
&& ivsinfo.phrasearr
&& ivsinfo.phraseidx
&& ivsinfo.phraseidx[previdx] !== undefined){
phrasedata = ivsinfo.phrasearr[ivsinfo.phraseidx[previdx]];
phraseivs += phrasedata.ivs;
} else {
phraseivs += phrase[ip];
}
}
}

return phraseivs;
}

function initDom(){
// dom init
domDic["editor"] = $("#editor");
Expand Down Expand Up @@ -131,20 +168,20 @@ function ZiHaiDicUI(){
}

function ShowDicByCurr(){
let currObj = $(".curr");
if(currObj.length > 0){
let dicParam = {
srcObj:currObj
};
tzUpdateEvent(dicParam);
if(!dicMinimized){
let currObj = $(".curr");
if(currObj.length > 0){
let dicParam = {
srcObj:currObj
};
tzUpdateEvent(dicParam);
}
}
}

function toggleDicWin(){
dicMinimized = !dicMinimized;
if(!dicMinimized){
ShowDicByCurr();
}
ShowDicByCurr();
}

function render(){
Expand Down

0 comments on commit fbcfc84

Please sign in to comment.