Publish Vulpes Preview 15 sources for desktop, Android and Tundra

Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
OverSu 2026-09-30 23:13:03 +02:00
commit b38dea0597
25901 changed files with 9982925 additions and 369 deletions

View file

@ -0,0 +1,93 @@
/* global InputMethods, PromiseStorage*/
'use strict';
(function() {
function RecentKeys() {
this._items = [];
}
RecentKeys.prototype.RECENT_KEY_COUNT = 18;
RecentKeys.prototype.getItems = function() {
return this._items || [];
};
RecentKeys.prototype.setItems = function(val) {
if (Array.isArray(val) && val.length) {
this._items = val.slice(0, this.RECENT_KEY_COUNT);
}
};
RecentKeys.prototype.getPanelKeys = function() {
if (!this._items) {
throw new Error('Emoji:Unable to get recently used keys');
}
return this.getItems().map(function(val) {
return { compositeKey: val, type: 'emoji' };
});
};
RecentKeys.prototype.add = function(key) {
var duplicateKeyIndex = this._items.indexOf(key);
// splice an element from array can always reduce length by 1.
// this ensure that we remove the last element or existent key,
// before unshifting clicked key into array.
if (this._items.length >= this.RECENT_KEY_COUNT ||
duplicateKeyIndex !== -1) {
this._items.splice(duplicateKeyIndex, 1);
}
this._items.unshift(key);
};
function EmojiInputMethod() {
this._glue = null;
this._dbStore = null;
this._recentKeys = new RecentKeys();
}
EmojiInputMethod.prototype.EMOJI_DB_NAME = 'Emoji';
EmojiInputMethod.prototype.EMOJI_RECENT_STORE_KEY = 'emojiRecentUsedKeys';
EmojiInputMethod.prototype.init = function(glue) {
this._glue = glue;
this._dbStore = new PromiseStorage(this.EMOJI_DB_NAME);
var p = this._dbStore.start()
.then(function() {
return this._dbStore.getItem(this.EMOJI_RECENT_STORE_KEY);
}.bind(this))
.then(function(keys) {
this._recentKeys.setItems(keys);
}.bind(this));
return p;
};
EmojiInputMethod.prototype.deactivate = function() {
return this._dbStore.setItem(
this.EMOJI_RECENT_STORE_KEY, this._recentKeys.getItems());
};
EmojiInputMethod.prototype.click = function(key, repeat) {
return this._glue.sendKey(key, repeat);
};
EmojiInputMethod.prototype.handleKey = function(key, repeat) {
this._recentKeys.add(key.key);
return this._glue.sendKey(key, repeat);
};
EmojiInputMethod.prototype.updatePageKeys = function(page) {
page.panelKeys = this._recentKeys.getPanelKeys();
return page;
};
InputMethods.emoji = new EmojiInputMethod();
}());

View file

@ -0,0 +1,83 @@
Handwriting is a handwriting recoginiton input method for gaia and is licensed
under the Apache Version 2.0, 2014. The handwriting recoginition engine is
zinnia (http://zinnia.sourceforge.net/), which is converted to JavaScript as
emscripten-zinnia project and hosted at:
https://github.com/yxl/emscripten-zinnia.
Emscripten (https://github.com/kripken/emscripten) is used to compile the
original C++ zinnia code into JavaScript version.
==========================================================================
Copyright notice for handwriting.
Copyright (c) 2014, Mozilla Online
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==========================================================================
Copyright notice for emscripten-zinnia.
Copyright (c) 2014, Yuan Xulei
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Taku Kudo nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
==========================================================================
Copyright notice for zinnia.
Copyright (c) 2005-2007, Taku Kudo
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of Taku Kudo nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -0,0 +1,288 @@
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
/* global InputMethods, KeyEvent */
/*
* This file implements a "glue" between FxOS Keyboard app UI and
* the handwriting recognition library. The recognition engine works
* in a web worker, it could support multiple languages, and communicates
* with this "glue" with messages.
*/
var HandwritingGaiaKeyboardGlue = function HandwritingGaiaKeyboardGlue() {
this.callbacks = null;
this.worker = null;
// The state of handwriting recognition engine module,
// whether it has been loaded into web worker completely.
this.moduleLoaded = false;
// The state of handwriting recognition engine,
// whether it's ready for for recognition.
this.isReady = false;
this.firstCandidate = '';
this.keypressQueue = [];
this.isWorking = false;
this.isActive = false;
// Current language
this.language = '';
this.isPredicting = false;
// For prediction
this.words = '';
this.cachedPoints = [];
this.unloadWorkerTimer = undefined;
};
HandwritingGaiaKeyboardGlue.prototype.DEACTIVATE_TIME = 60000; // 1 minute
HandwritingGaiaKeyboardGlue.prototype.init = function hr_init(callbacks) {
this.callbacks = callbacks;
};
HandwritingGaiaKeyboardGlue.prototype.uninit = function hr_uninit(callbacks) {
this._unloadWorker();
this.callbacks = null;
};
HandwritingGaiaKeyboardGlue.prototype._loadWorker = function hr_loadWorker() {
if (this.worker) {
return;
}
this.worker = new Worker(this.callbacks.path + '/hwr/worker.js');
this.worker.onmessage = function(evt) {
var data = evt.data;
switch (data.id) {
case 'init':
this.moduleLoaded = true;
this.postMessage('setLang', this.language);
break;
case 'setLang':
this.isReady = data.value;
if (this.isReady) {
this._handleCachedStrokePoints();
}
break;
case 'recognize':
this.isPredicting = false;
this._sendCandidates(JSON.parse(data.value));
break;
case 'getPrediction':
this.isPredicting = true;
var predictions = data.value;
if (predictions) {
this._sendCandidates(predictions.split(' '));
}
break;
default:
break;
}
}.bind(this);
};
HandwritingGaiaKeyboardGlue.prototype._unloadWorker =
function hr_unloadWorker() {
if (this.worker) {
this.worker.terminate();
}
this.handlers = null;
this.worker = null;
this.moduleLoaded = false;
this.isReady = false;
};
HandwritingGaiaKeyboardGlue.prototype.select = function hr_select(text, data) {
this.callbacks.sendString(text);
this.empty();
this._start();
if (this.isPredicting) {
this.words += text;
} else {
this.words = text;
this.isPredicting = true;
}
this.postMessage('getPrediction', this.words);
};
HandwritingGaiaKeyboardGlue.prototype.click = function hr_click(keyCode) {
this.keypressQueue.push(keyCode);
this._start();
};
HandwritingGaiaKeyboardGlue.prototype.activate =
function hr_activate(language, state, options) {
var needsSetLang = this.language != language;
this.language = language;
if (!this.worker) {
this._loadWorker();
}
clearTimeout(this.unloadWorkerTimer);
// The handwriting recognition engine may be used by multiple IMEs,
// we need set language for different IMEs.
if (this.moduleLoaded && needsSetLang) {
this.isReady = false;
this.postMessage('setLang', language);
}
this.isActive = true;
};
HandwritingGaiaKeyboardGlue.prototype.deactivate = function hr_deactivate() {
if (!this._isActive) {
return;
}
this.isActive = false;
this._resetKeypressQueue();
this.empty();
clearTimeout(this.unloadWorkerTimer);
this.unloadWorkerTimer =
setTimeout(this._unloadWorker.bind(this), this.DEACTIVATE_TIME);
};
HandwritingGaiaKeyboardGlue.prototype.empty = function hr_empty() {
this.firstCandidate = '';
this.callbacks.sendCandidates([]);
this.callbacks.endComposition();
this.callbacks.app.handwritingPadsManager.clear();
};
HandwritingGaiaKeyboardGlue.prototype.postMessage = function(id, param) {
if (!this.moduleLoaded) {
return;
}
this.worker.postMessage({
id: id,
param: param
});
};
/**
* Send candidates list and change composition.
* @param {Array.<string>} candidates The candidates to be sent.
* @return {void} No return value.
*/
HandwritingGaiaKeyboardGlue.prototype._sendCandidates =
function hr_sendCandidates(candidates) {
var list = [];
var len = candidates.length;
for (var id = 0; id < len; id++) {
var cand = candidates[id];
list.push([cand, id]);
}
if (len > 0) {
this.firstCandidate = candidates[0];
}
this.callbacks.sendCandidates(list);
if (this.isPredicting) {
return;
}
if (this.firstCandidate) {
this.callbacks.setComposition(this.firstCandidate);
}
};
HandwritingGaiaKeyboardGlue.prototype._start = function hr_start() {
if (this.isWorking) {
return;
}
this.isWorking = true;
this._next();
};
HandwritingGaiaKeyboardGlue.prototype._next = function hr_next() {
if (!this.keypressQueue.length) {
this.isWorking = false;
return;
}
var code = this.keypressQueue.shift();
var sendKey = true;
if (this.firstCandidate) {
switch (code) {
case KeyEvent.DOM_VK_BACK_SPACE:
sendKey = false;
this.empty();
break;
case KeyEvent.DOM_VK_RETURN:
case KeyEvent.DOM_VK_SPACE:
// candidate list exists; output the first candidate
this.select(this.firstCandidate);
this.callbacks.app.handwritingPadsManager.clear();
sendKey = false;
break;
default:
this.callbacks.endComposition(this.firstCandidate);
this.empty();
break;
}
}
//pass the key to IMEManager for default action
if (sendKey) {
this.callbacks.sendKey(code);
}
this._next();
};
HandwritingGaiaKeyboardGlue.prototype._resetKeypressQueue =
function hr_resetKeypressQueue() {
this.keypressQueue = [];
this.isWorking = false;
};
// If handwriting recognition engine is loaded or initialized later
// than user writing, there are some cached stroke points, do
// recognition for cached stroke points.
HandwritingGaiaKeyboardGlue.prototype._handleCachedStrokePoints =
function hr_handleCachedStrokePoints() {
if (this.cachedPoints.length === 0) {
return;
}
this.postMessage('recognize', JSON.stringify(this.cachedPoints));
this.cachedPoints = [];
};
HandwritingGaiaKeyboardGlue.prototype.sendStrokePoints =
function hr_sendStrokePoints(strokePoints) {
// If handwriting recognition engine is not loaded or is not ready for
// recognition, cache the stroke points.
if (!this.moduleLoaded || !this.isReady) {
this.cachedPoints = strokePoints;
return;
}
this.postMessage('recognize', JSON.stringify(strokePoints));
};
// Expose the engine to the Gaia keyboard
if (typeof InputMethods !== 'undefined') {
InputMethods.handwriting = new HandwritingGaiaKeyboardGlue();
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,122 @@
'use strict';
var MAX_STROKE_NUM = 1000;
self.onmessage = function(e) {
var data = e.data;
switch(data.id) {
case 'setLang':
var ret = Recognition.setLang(data.param);
post('setLang', ret);
break;
case 'recognize':
var words = Recognition.recognize(JSON.parse(data.param));
post('recognize', JSON.stringify(words));
break;
case 'getPrediction':
var words = Recognition.getPrediction(data.param);
post('getPrediction', words);
break;
default:
break;
}
};
var Recognition = {
_inited: false,
setLang: function(lang) {
if (!this._lang) {
this._lang = lang;
return this._init(lang);
}
if (this._lang != lang) {
this._lang = lang;
this._uinit();
return this._init(lang);
}
return true;
},
recognize: function(points) {
if (!this._inited) {
return [];
}
var strokes = points.slice(0, 2 * MAX_STROKE_NUM);
// Append [-1, -1] to strokes.
strokes.push.apply(strokes, [-1, -1]);
this._strokeBuffer.set(strokes);
return this._CharacterRecognize(this._strokeBuffer.byteOffset);
},
getPrediction: function(words) {
if (this._getPrediction) {
return this._getPrediction(words);
}
},
_init: function(lang) {
this._inited = false;
var model = '';
if (lang == 'zh-Hans') {
model = 'zh_Hans.model';
}
if (lang == 'zh-Hant') {
model = 'zh_Hant.model';
}
if (!Module.ccall('InitRecog',
'number',
['string', 'number', 'number'],
[model, 140, 140])) {
return false;
}
this._CharacterRecognize =
Module.cwrap('CharacterRecognize', 'string', ['number']);
if (Module.ccall('InitPrediction',
'number',
['string'],
['dict_pinyin.data'])) {
this._getPrediction =
Module.cwrap('GetPrediction', 'string', ['string']);
}
var buf = Module._malloc((MAX_STROKE_NUM + 1) * 4);
this._strokeBuffer =
new Int16Array(Module.HEAPU8.buffer, buf, (MAX_STROKE_NUM + 1) * 2);
this._inited = true;
return true;
},
_uinit: function() {
Module._free(this._strokeBuffer.byteOffset);
Module.ccall('ExitRecog', '', [], []);
},
_strokeBuffer: null,
_CharacterRecognize: null,
_lang: '',
_getPrediction: null
};
var Module = {
filePackagePrefixURL: './',
canvas: {},
stdout: null,
_main: function() {
post('init', true);
}
};
function post(id, value) {
postMessage({
id: id,
value: value
});
}

View file

@ -0,0 +1,7 @@
'use strict';
/**
* Load Recognition, etc. into a Web Worker.
*/
importScripts('recognition.js', 'emscripten_zinnia.js');

View file

@ -0,0 +1,20 @@
'use strict';
/* global InputMethods */
InputMethods.india = {
_glue: null,
init: function(glue) {
this._glue = glue;
},
click: function(keyCode) {
this._glue.setUpperCase(false);
return this._glue.sendKey(keyCode);
},
displaysCandidates: function() {
return false;
}
};

View file

@ -0,0 +1,456 @@
MOZILLA PUBLIC LICENSE
Version 1.1
---------------
1. Definitions.
1.0.1. "Commercial Use" means distribution or otherwise making the
Covered Code available to a third party.
1.1. "Contributor" means each entity that creates or contributes to
the creation of Modifications.
1.2. "Contributor Version" means the combination of the Original
Code, prior Modifications used by a Contributor, and the Modifications
made by that particular Contributor.
1.3. "Covered Code" means the Original Code or Modifications or the
combination of the Original Code and Modifications, in each case
including portions thereof.
1.4. "Electronic Distribution Mechanism" means a mechanism generally
accepted in the software development community for the electronic
transfer of data.
1.5. "Executable" means Covered Code in any form other than Source
Code.
1.6. "Initial Developer" means the individual or entity identified
as the Initial Developer in the Source Code notice required by Exhibit
A.
1.7. "Larger Work" means a work which combines Covered Code or
portions thereof with code not governed by the terms of this License.
1.8. "License" means this document.
1.8.1. "Licensable" means having the right to grant, to the maximum
extent possible, whether at the time of the initial grant or
subsequently acquired, any and all of the rights conveyed herein.
1.9. "Modifications" means any addition to or deletion from the
substance or structure of either the Original Code or any previous
Modifications. When Covered Code is released as a series of files, a
Modification is:
A. Any addition to or deletion from the contents of a file
containing Original Code or previous Modifications.
B. Any new file that contains any part of the Original Code or
previous Modifications.
1.10. "Original Code" means Source Code of computer software code
which is described in the Source Code notice required by Exhibit A as
Original Code, and which, at the time of its release under this
License is not already Covered Code governed by this License.
1.10.1. "Patent Claims" means any patent claim(s), now owned or
hereafter acquired, including without limitation, method, process,
and apparatus claims, in any patent Licensable by grantor.
1.11. "Source Code" means the preferred form of the Covered Code for
making modifications to it, including all modules it contains, plus
any associated interface definition files, scripts used to control
compilation and installation of an Executable, or source code
differential comparisons against either the Original Code or another
well known, available Covered Code of the Contributor's choice. The
Source Code can be in a compressed or archival form, provided the
appropriate decompression or de-archiving software is widely available
for no charge.
1.12. "You" (or "Your") means an individual or a legal entity
exercising rights under, and complying with all of the terms of, this
License or a future version of this License issued under Section 6.1.
For legal entities, "You" includes any entity which controls, is
controlled by, or is under common control with You. For purposes of
this definition, "control" means (a) the power, direct or indirect,
to cause the direction or management of such entity, whether by
contract or otherwise, or (b) ownership of more than fifty percent
(50%) of the outstanding shares or beneficial ownership of such
entity.
2. Source Code License.
2.1. The Initial Developer Grant.
The Initial Developer hereby grants You a world-wide, royalty-free,
non-exclusive license, subject to third party intellectual property
claims:
(a) under intellectual property rights (other than patent or
trademark) Licensable by Initial Developer to use, reproduce,
modify, display, perform, sublicense and distribute the Original
Code (or portions thereof) with or without Modifications, and/or
as part of a Larger Work; and
(b) under Patents Claims infringed by the making, using or
selling of Original Code, to make, have made, use, practice,
sell, and offer for sale, and/or otherwise dispose of the
Original Code (or portions thereof).
(c) the licenses granted in this Section 2.1(a) and (b) are
effective on the date Initial Developer first distributes
Original Code under the terms of this License.
(d) Notwithstanding Section 2.1(b) above, no patent license is
granted: 1) for code that You delete from the Original Code; 2)
separate from the Original Code; or 3) for infringements caused
by: i) the modification of the Original Code or ii) the
combination of the Original Code with other software or devices.
2.2. Contributor Grant.
Subject to third party intellectual property claims, each Contributor
hereby grants You a world-wide, royalty-free, non-exclusive license
(a) under intellectual property rights (other than patent or
trademark) Licensable by Contributor, to use, reproduce, modify,
display, perform, sublicense and distribute the Modifications
created by such Contributor (or portions thereof) either on an
unmodified basis, with other Modifications, as Covered Code
and/or as part of a Larger Work; and
(b) under Patent Claims infringed by the making, using, or
selling of Modifications made by that Contributor either alone
and/or in combination with its Contributor Version (or portions
of such combination), to make, use, sell, offer for sale, have
made, and/or otherwise dispose of: 1) Modifications made by that
Contributor (or portions thereof); and 2) the combination of
Modifications made by that Contributor with its Contributor
Version (or portions of such combination).
(c) the licenses granted in Sections 2.2(a) and 2.2(b) are
effective on the date Contributor first makes Commercial Use of
the Covered Code.
(d) Notwithstanding Section 2.2(b) above, no patent license is
granted: 1) for any code that Contributor has deleted from the
Contributor Version; 2) separate from the Contributor Version;
3) for infringements caused by: i) third party modifications of
Contributor Version or ii) the combination of Modifications made
by that Contributor with other software (except as part of the
Contributor Version) or other devices; or 4) under Patent Claims
infringed by Covered Code in the absence of Modifications made by
that Contributor.
3. Distribution Obligations.
3.1. Application of License.
The Modifications which You create or to which You contribute are
governed by the terms of this License, including without limitation
Section 2.2. The Source Code version of Covered Code may be
distributed only under the terms of this License or a future version
of this License released under Section 6.1, and You must include a
copy of this License with every copy of the Source Code You
distribute. You may not offer or impose any terms on any Source Code
version that alters or restricts the applicable version of this
License or the recipients' rights hereunder. However, You may include
an additional document offering the additional rights described in
Section 3.5.
3.2. Availability of Source Code.
Any Modification which You create or to which You contribute must be
made available in Source Code form under the terms of this License
either on the same media as an Executable version or via an accepted
Electronic Distribution Mechanism to anyone to whom you made an
Executable version available; and if made available via Electronic
Distribution Mechanism, must remain available for at least twelve (12)
months after the date it initially became available, or at least six
(6) months after a subsequent version of that particular Modification
has been made available to such recipients. You are responsible for
ensuring that the Source Code version remains available even if the
Electronic Distribution Mechanism is maintained by a third party.
3.3. Description of Modifications.
You must cause all Covered Code to which You contribute to contain a
file documenting the changes You made to create that Covered Code and
the date of any change. You must include a prominent statement that
the Modification is derived, directly or indirectly, from Original
Code provided by the Initial Developer and including the name of the
Initial Developer in (a) the Source Code, and (b) in any notice in an
Executable version or related documentation in which You describe the
origin or ownership of the Covered Code.
3.4. Intellectual Property Matters
(a) Third Party Claims.
If Contributor has knowledge that a license under a third party's
intellectual property rights is required to exercise the rights
granted by such Contributor under Sections 2.1 or 2.2,
Contributor must include a text file with the Source Code
distribution titled "LEGAL" which describes the claim and the
party making the claim in sufficient detail that a recipient will
know whom to contact. If Contributor obtains such knowledge after
the Modification is made available as described in Section 3.2,
Contributor shall promptly modify the LEGAL file in all copies
Contributor makes available thereafter and shall take other steps
(such as notifying appropriate mailing lists or newsgroups)
reasonably calculated to inform those who received the Covered
Code that new knowledge has been obtained.
(b) Contributor APIs.
If Contributor's Modifications include an application programming
interface and Contributor has knowledge of patent licenses which
are reasonably necessary to implement that API, Contributor must
also include this information in the LEGAL file.
(c) Representations.
Contributor represents that, except as disclosed pursuant to
Section 3.4(a) above, Contributor believes that Contributor's
Modifications are Contributor's original creation(s) and/or
Contributor has sufficient rights to grant the rights conveyed by
this License.
3.5. Required Notices.
You must duplicate the notice in Exhibit A in each file of the Source
Code. If it is not possible to put such notice in a particular Source
Code file due to its structure, then You must include such notice in a
location (such as a relevant directory) where a user would be likely
to look for such a notice. If You created one or more Modification(s)
You may add your name as a Contributor to the notice described in
Exhibit A. You must also duplicate this License in any documentation
for the Source Code where You describe recipients' rights or ownership
rights relating to Covered Code. You may choose to offer, and to
charge a fee for, warranty, support, indemnity or liability
obligations to one or more recipients of Covered Code. However, You
may do so only on Your own behalf, and not on behalf of the Initial
Developer or any Contributor. You must make it absolutely clear than
any such warranty, support, indemnity or liability obligation is
offered by You alone, and You hereby agree to indemnify the Initial
Developer and every Contributor for any liability incurred by the
Initial Developer or such Contributor as a result of warranty,
support, indemnity or liability terms You offer.
3.6. Distribution of Executable Versions.
You may distribute Covered Code in Executable form only if the
requirements of Section 3.1-3.5 have been met for that Covered Code,
and if You include a notice stating that the Source Code version of
the Covered Code is available under the terms of this License,
including a description of how and where You have fulfilled the
obligations of Section 3.2. The notice must be conspicuously included
in any notice in an Executable version, related documentation or
collateral in which You describe recipients' rights relating to the
Covered Code. You may distribute the Executable version of Covered
Code or ownership rights under a license of Your choice, which may
contain terms different from this License, provided that You are in
compliance with the terms of this License and that the license for the
Executable version does not attempt to limit or alter the recipient's
rights in the Source Code version from the rights set forth in this
License. If You distribute the Executable version under a different
license You must make it absolutely clear that any terms which differ
from this License are offered by You alone, not by the Initial
Developer or any Contributor. You hereby agree to indemnify the
Initial Developer and every Contributor for any liability incurred by
the Initial Developer or such Contributor as a result of any such
terms You offer.
3.7. Larger Works.
You may create a Larger Work by combining Covered Code with other code
not governed by the terms of this License and distribute the Larger
Work as a single product. In such a case, You must make sure the
requirements of this License are fulfilled for the Covered Code.
4. Inability to Comply Due to Statute or Regulation.
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Code due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description
must be included in the LEGAL file described in Section 3.4 and must
be included with all distributions of the Source Code. Except to the
extent prohibited by statute or regulation, such description must be
sufficiently detailed for a recipient of ordinary skill to be able to
understand it.
5. Application of this License.
This License applies to code to which the Initial Developer has
attached the notice in Exhibit A and to related Covered Code.
6. Versions of the License.
6.1. New Versions.
Netscape Communications Corporation ("Netscape") may publish revised
and/or new versions of the License from time to time. Each version
will be given a distinguishing version number.
6.2. Effect of New Versions.
Once Covered Code has been published under a particular version of the
License, You may always continue to use it under the terms of that
version. You may also choose to use such Covered Code under the terms
of any subsequent version of the License published by Netscape. No one
other than Netscape has the right to modify the terms applicable to
Covered Code created under this License.
6.3. Derivative Works.
If You create or use a modified version of this License (which you may
only do in order to apply it to code which is not already Covered Code
governed by this License), You must (a) rename Your license so that
the phrases "Mozilla", "MOZILLAPL", "MOZPL", "Netscape",
"MPL", "NPL" or any confusingly similar phrase do not appear in your
license (except to note that your license differs from this License)
and (b) otherwise make it clear that Your version of the license
contains terms which differ from the Mozilla Public License and
Netscape Public License. (Filling in the name of the Initial
Developer, Original Code or Contributor in the notice described in
Exhibit A shall not of themselves be deemed to be modifications of
this License.)
7. DISCLAIMER OF WARRANTY.
COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF
DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING.
THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE
IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT,
YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE
COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER
OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF
ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
8. TERMINATION.
8.1. This License and the rights granted hereunder will terminate
automatically if You fail to comply with terms herein and fail to cure
such breach within 30 days of becoming aware of the breach. All
sublicenses to the Covered Code which are properly granted shall
survive any termination of this License. Provisions which, by their
nature, must remain in effect beyond the termination of this License
shall survive.
8.2. If You initiate litigation by asserting a patent infringement
claim (excluding declatory judgment actions) against Initial Developer
or a Contributor (the Initial Developer or Contributor against whom
You file such action is referred to as "Participant") alleging that:
(a) such Participant's Contributor Version directly or indirectly
infringes any patent, then any and all rights granted by such
Participant to You under Sections 2.1 and/or 2.2 of this License
shall, upon 60 days notice from Participant terminate prospectively,
unless if within 60 days after receipt of notice You either: (i)
agree in writing to pay Participant a mutually agreeable reasonable
royalty for Your past and future use of Modifications made by such
Participant, or (ii) withdraw Your litigation claim with respect to
the Contributor Version against such Participant. If within 60 days
of notice, a reasonable royalty and payment arrangement are not
mutually agreed upon in writing by the parties or the litigation claim
is not withdrawn, the rights granted by Participant to You under
Sections 2.1 and/or 2.2 automatically terminate at the expiration of
the 60 day notice period specified above.
(b) any software, hardware, or device, other than such Participant's
Contributor Version, directly or indirectly infringes any patent, then
any rights granted to You by such Participant under Sections 2.1(b)
and 2.2(b) are revoked effective as of the date You first made, used,
sold, distributed, or had made, Modifications made by that
Participant.
8.3. If You assert a patent infringement claim against Participant
alleging that such Participant's Contributor Version directly or
indirectly infringes any patent where such claim is resolved (such as
by license or settlement) prior to the initiation of patent
infringement litigation, then the reasonable value of the licenses
granted by such Participant under Sections 2.1 or 2.2 shall be taken
into account in determining the amount or value of any payment or
license.
8.4. In the event of termination under Sections 8.1 or 8.2 above,
all end user license agreements (excluding distributors and resellers)
which have been validly granted by You or any distributor hereunder
prior to termination shall survive termination.
9. LIMITATION OF LIABILITY.
UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT
(INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE INITIAL
DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF COVERED CODE,
OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR
ANY INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY
CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL,
WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER
COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN
INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF
LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL INJURY
RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT APPLICABLE LAW
PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO NOT ALLOW THE
EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO
THIS EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU.
10. U.S. GOVERNMENT END USERS.
The Covered Code is a "commercial item," as that term is defined in
48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial computer
software" and "commercial computer software documentation," as such
terms are used in 48 C.F.R. 12.212 (Sept. 1995). Consistent with 48
C.F.R. 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (June 1995),
all U.S. Government End Users acquire Covered Code with only those
rights set forth herein.
11. MISCELLANEOUS.
This License represents the complete agreement concerning subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. This License shall be governed by
California law provisions (except to the extent applicable law, if
any, provides otherwise), excluding its conflict-of-law provisions.
With respect to disputes in which at least one party is a citizen of,
or an entity chartered or registered to do business in the United
States of America, any litigation relating to this License shall be
subject to the jurisdiction of the Federal Courts of the Northern
District of California, with venue lying in Santa Clara County,
California, with the losing party responsible for costs, including
without limitation, court costs and reasonable attorneys' fees and
expenses. The application of the United Nations Convention on
Contracts for the International Sale of Goods is expressly excluded.
Any law or regulation which provides that the language of a contract
shall be construed against the drafter shall not apply to this
License.
12. RESPONSIBILITY FOR CLAIMS.
As between Initial Developer and the Contributors, each party is
responsible for claims and damages arising, directly or indirectly,
out of its utilization of rights under this License and You agree to
work with Initial Developer and Contributors to distribute such
responsibility on an equitable basis. Nothing herein is intended or
shall be deemed to constitute any admission of liability.
13. MULTIPLE-LICENSED CODE.
Initial Developer may designate portions of the Covered Code as
"Multiple-Licensed". "Multiple-Licensed" means that the Initial
Developer permits you to utilize portions of the Covered Code under
Your choice of the NPL or the alternative licenses, if any, specified
by the Initial Developer in the file described in Exhibit A.
EXHIBIT A -Mozilla Public License.
``The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is jsAvroPhonetic.
The Initial Developer of the Original Code is
Rifat Nabi <to.rifat@gmail.com>
Copyright (C) OmicronLab <http://www.omicronlab.com>. All Rights Reserved.
Contributor(s): Mehdi Hasan <mhasan@omicronlab.com>"

View file

@ -0,0 +1,18 @@
README
=======
jsAvroPhonetic is a Javascript implementation of Avro Phonetic, an IME for Bangla.
USING
=====
Get the transliterated text for any input like:
```
var parsed_text = OmicronLab.Avro.Phonetic.parse('latin_input');
```
GET THE SOURCE
==============
Original GitHub Repository: https://github.com/torifat/jsAvroPhonetic/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,4 @@
jshangul is a Korean input method for gaia and is licensed
under the Apache Version 2.0, 2013. The original code is based on the
JangmiIME project hosted at:
https://github.com/sangpire/jangmiIME

View file

@ -0,0 +1,299 @@
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* global KeyEvent, InputMethods */
'use strict';
(function() {
var keyboard, ime, composingText, JangmiIME;
var BACKSPACE = KeyEvent.DOM_VK_BACK_SPACE;
/*
JangmiIME
https://github.com/sangpire/jangmiIME
Copyright 2013, BYUN Sangpil. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(function() {
var isCompatibleJamoKeyCode;
var UTF8_HANGUL_SYLLABLES_START = 0xAC00;
var UTF8_HANGUL_COMPATIBILITY_START = 0x3131;
var UTF8_CHOSEONG_DIFF = '까'.charCodeAt(0) - '가'.charCodeAt(0);
var UTF8_JUNGSEONG_DIFF = '개'.charCodeAt(0) - '가'.charCodeAt(0);
var COMPATIBILITY_MULTI_JAMO_MAP = {
'ㄱㄱ' : 0x1, 'ㄷㄷ' : 0x7, 'ㅂㅂ' : 0x12, 'ㅅㅅ' : 0x15,
'ㅈㅈ' : 0x18, 'ㅗㅏ' : 0x27, 'ㅗㅐ' : 0x28, 'ㅗㅣ' : 0x29,
'ㅜㅓ' : 0x2C, 'ㅜㅔ' : 0x2E, 'ㅜㅣ' : 0x2E, 'ㅡㅣ' : 0x31
};
var CHO_SEONG_MAP = {
'ㄱ' : 0, 'ㄱㄱ' : 1, 'ㄲ' : 1, 'ㄴ' : 2, 'ㄷ' : 3,
'ㄷㄷ' : 4, 'ㄸ' : 4, 'ㄹ' : 5, 'ㅁ' : 6, 'ㅂ' : 7,
'ㅂㅂ' : 8, 'ㅃ' : 8, 'ㅅ' : 9, 'ㅅㅅ' : 10, 'ㅆ' : 10,
'ㅇ' : 11, 'ㅈ' : 12, 'ㅈㅈ' : 13, 'ㅉ' : 13, 'ㅊ' : 14,
'ㅋ' : 15, 'ㅌ' : 16, 'ㅍ' : 17, 'ㅎ' : 18
};
var JUNG_SEONG_MAP = {
'ㅏ' : 0, 'ㅐ' : 1, 'ㅑ' : 2, 'ㅒ' : 3, 'ㅓ' : 4,
'ㅔ' : 5, 'ㅕ' : 6, 'ㅖ' : 7, 'ㅗ' : 8, 'ㅘ' : 9,
'ㅗㅏ' : 9, 'ㅙ' : 10, 'ㅗㅐ' : 10, 'ㅚ' : 11, 'ㅗㅣ' : 11,
'ㅛ' : 12, 'ㅜ' : 13, 'ㅝ' : 14, 'ㅜㅓ' : 14, 'ㅞ' : 15,
'ㅜㅔ' : 15, 'ㅟ' : 16, 'ㅜㅣ' : 16, 'ㅠ' : 17, 'ㅡ' : 18,
'ㅢ' : 19, 'ㅡㅣ' : 19, 'ㅣ' : 20
};
var JONG_SEONG_MAP = {
'ㄱ' : 1, 'ㄲ' : 2, 'ㄱㅅ' : 3, 'ㄴ' : 4, 'ㄴㅈ' : 5,
'ㄴㅎ' : 6, 'ㄷ' : 7, 'ㄹ' : 8, 'ㄹㄱ' : 9, 'ㄹㅁ' : 10,
'ㄹㅂ' : 11, 'ㄹㅅ' : 12, 'ㄹㅌ' : 13, 'ㄹㅍ' : 14, 'ㄹㅎ' : 15,
'ㅁ' : 16, 'ㅂ' : 17, 'ㅂㅅ' : 18, 'ㅅ' : 19, 'ㅆ' : 20,
'ㅇ' : 21, 'ㅈ' : 22, 'ㅊ' : 23, 'ㅋ' : 24, 'ㅌ' : 25,
'ㅍ' : 26, 'ㅎ' : 27
};
isCompatibleJamoKeyCode = function(keyCode) {
return (0x3131 <= keyCode && keyCode <= 0x318e);
};
JangmiIME = (function() {
/*
@handler =
passed: (keyCode) ->
added: (gul) ->
changed: (gul) ->
*/
function JangmiIME(handler) {
this.handler = handler;
this.reset();
}
/*
Reset Current State.
*/
JangmiIME.prototype.reset = function() {
this.curJamo = null;
this.choSeong = [];
this.jungSeong = [];
this.jongSeong = [];
};
JangmiIME.prototype.added = function() {
var curGul;
curGul = this.peep();
if (curGul != null) {
return this.handler.added(curGul);
}
};
JangmiIME.prototype.changed = function() {
var curGul;
curGul = this.peep();
if (curGul != null) {
return this.handler.changed(curGul);
}
};
JangmiIME.prototype.isEmpty = function() {
return (this.choSeong.length +
this.jungSeong.length +
this.jongSeong.length) === 0;
};
/*
Add KeyCode
*/
JangmiIME.prototype.add = function(keyCode) {
var curJamo, jamo, temp;
if (!isCompatibleJamoKeyCode(keyCode)) {
this.reset();
return this.handler.passed(keyCode);
} else {
jamo = String.fromCharCode(keyCode);
if (this.curJamo == null) {
if (CHO_SEONG_MAP.hasOwnProperty(jamo)) {
this.curJamo = this.choSeong;
}
if (JUNG_SEONG_MAP.hasOwnProperty(jamo)) {
this.curJamo = this.jungSeong;
}
this.curJamo.push(jamo);
return this.added();
} else {
curJamo = this.curJamo.join('') + jamo;
switch (this.curJamo) {
case this.choSeong:
if (CHO_SEONG_MAP.hasOwnProperty(curJamo)) {
this.curJamo.push(jamo);
return this.changed();
} else {
if (JUNG_SEONG_MAP.hasOwnProperty(jamo)) {
this.jungSeong.push(jamo);
this.curJamo = this.jungSeong;
return this.changed();
} else {
this.reset();
return this.add(keyCode);
}
}
break;
case this.jungSeong:
if (JUNG_SEONG_MAP.hasOwnProperty(curJamo)) {
this.curJamo.push(jamo);
return this.changed();
} else {
if (this.choSeong.length > 0 &&
JONG_SEONG_MAP.hasOwnProperty(jamo)) {
this.jongSeong.push(jamo);
this.curJamo = this.jongSeong;
return this.changed();
} else {
this.reset();
return this.add(keyCode);
}
}
break;
case this.jongSeong:
if (JONG_SEONG_MAP.hasOwnProperty(curJamo)) {
this.curJamo.push(jamo);
return this.changed();
} else {
if (JUNG_SEONG_MAP.hasOwnProperty(jamo)) {
temp = this.curJamo.pop();
this.changed();
this.reset();
this.choSeong.push(temp);
this.jungSeong.push(jamo);
this.curJamo = this.jungSeong;
return this.added();
} else {
this.reset();
this.curJamo = null;
return this.add(keyCode);
}
}
}
}
}
};
/*
Delete Last Input
*/
JangmiIME.prototype.back = function() {
if (this.curJamo.length > 0) {
this.curJamo.pop();
return this.changed();
} else {
switch (this.curJamo) {
case this.jungSeong:
this.curJamo = this.choSeong;
return this.back();
case this.jongSeong:
this.curJamo = this.jungSeong;
return this.back();
}
}
};
/*
peep current hangul
*/
JangmiIME.prototype.peep = function() {
var code;
if (this.choSeong.length > 0 && this.jungSeong.length > 0) {
code = UTF8_HANGUL_SYLLABLES_START +
CHO_SEONG_MAP[this.choSeong.join('')] * UTF8_CHOSEONG_DIFF;
code += JUNG_SEONG_MAP[this.jungSeong.join('')] * UTF8_JUNGSEONG_DIFF;
if (this.jongSeong.length > 0) {
code += JONG_SEONG_MAP[this.jongSeong.join('')];
}
} else {
if (this.choSeong.length === 1) {
code = this.choSeong[0].charCodeAt(0);
} else if (this.choSeong.length > 1) {
code = UTF8_HANGUL_COMPATIBILITY_START +
COMPATIBILITY_MULTI_JAMO_MAP[this.choSeong.join('')];
} else if (this.jungSeong.length === 1) {
code = this.jungSeong[0].charCodeAt(0);
} else if (this.jungSeong.length > 1) {
code = UTF8_HANGUL_COMPATIBILITY_START +
COMPATIBILITY_MULTI_JAMO_MAP[this.jungSeong.join('')];
} else {
return '';
}
}
return String.fromCharCode(code);
};
return JangmiIME;
})();
})();
InputMethods.jshangul = {
init: init,
click: click,
deactivate: deactivate
};
function setComposition(gul) {
composingText = gul;
keyboard.setComposition(gul);
}
function endComposition() {
if (composingText.length > 0) {
keyboard.endComposition(composingText);
composingText = '';
}
}
function init(interfaceObject) {
keyboard = interfaceObject;
composingText = '';
ime = new JangmiIME({
passed: function(keyCode) {
endComposition();
keyboard.sendKey(keyCode);
},
added: function(gul) {
endComposition();
setComposition(gul);
},
changed: function(gul) {
setComposition(gul);
}
});
}
function deactivate() {
endComposition();
ime.reset();
}
function click(keycode, x, y) {
if (keycode === BACKSPACE && !ime.isEmpty()) {
ime.back();
} else {
ime.add(keycode);
}
keyboard.setUpperCase({
isUpperCase: false
});
}
})();

View file

@ -0,0 +1,17 @@
# Japanese IME
About Dictionary
The IME dictionary is converted from NAIST Japanese Dictionary project with following steps:
1. Get a copy of `naist-jdic-0.4.3.tar.gz` at
http://sourceforge.jp/projects/naist-jdic/releases/
2. Extract all .dic files to dict/src/.
3. Run `make` under the `dict` directory to generate dict.json.
The tool of `dict/jcconv.py` is getting from https://github.com/taichino/jcconv.
Note:
Running `make` on Windows is not recommended. You have to take care of some encoding issues.

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,31 @@
Copyright (c) 2008, Nara Institute of Science and Technology, Japan.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the Nara Institute of Science and Technology
(NAIST) nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -0,0 +1,15 @@
.PHONY: json clean
json: dict.utf8
python conv.py
dict:
find src -name "*.dic" -exec cat {} >> $@ \;
dict.utf8: dict
iconv -f euc-jp -t utf-8 dict > $@
clean:
rm dict
rm dict.utf8
rm *.pyc

View file

@ -0,0 +1,150 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import codecs
from jcconv import kata2hira
file_list = ['dict.utf8',]
temp_obj = dict()
total_freq = 0
# Converting from EUC-JP to UTF-8 makes unexpected code change for some characters.
# This table recovers them to its original code.
utf8_fix_table = {
u'‾': u' ̄',
u'—': u'―',
u'〜': u'~',
u'‖': u'∥',
u'−': u'-',
u'¥': u'¥',
u'¢': u'¢',
u'£': u'£',
u'¬': u'¬',
u'¦': u'¦'
}
naistjdic_fix_table = {
u'海・島': u'海鱸島',
u'茶・山': u'茶荃山',
u'吐・喇列島': u'吐噶喇列島',
u'屋那・島': u'屋那覇島',
u'流・': u'流硑',
u'辰ケ・': u'辰ケ硑',
u'穴・湾': u'穴澗湾',
u'海・の峰': u'海驢の峰',
u'下・島': u'下甑島',
u'出来・崎': u'出来澗崎',
u'横・岬': u'横澗岬',
u'穴・岬': u'穴澗岬',
u'京の上・島': u'京の上臈島',
u'吐・喇海峡': u'吐噶喇海峡',
u'辰ヶ・': u'辰ヶ硑',
u'滝ノ・ノ岬': u'滝ノ澗ノ岬',
u'中・湾': u'中甑湾',
u'丸・ノ鼻': u'丸硑ノ鼻',
u'勝・山': u'勝澗山',
u'狼・山': u'狼乢山',
u'赤・鼻': u'赤硑鼻',
u'中ノ・崎': u'中ノ澗崎',
u'大松・': u'大松硑',
u'中・島': u'中甑島',
u'螺・島': u'螺蠑島',
u'上・島': u'上甑島'
}
naistjdic_correction_table = {
u'赤硑鼻': u'あかばえはな',
u'与那・岳': '',
u'村碆・': '',
u'笠・岳': ''
}
def naistjdic_patch(kanji, kana):
# Fix encoding convert error by iconv.
for c in utf8_fix_table:
kanji = kanji.replace(c, utf8_fix_table[c])
kana = kana.replace(c, utf8_fix_table[c])
# Fix some lacked kanji in naist-jdic 0.4.3.
if u'・' in kanji:
if kanji in naistjdic_fix_table:
kanji_key = kanji
kanji = naistjdic_fix_table[kanji]
del naistjdic_fix_table[kanji_key]
# Correct kana or remove the item.
if kanji in naistjdic_correction_table:
kanji_key = kanji
kana = naistjdic_correction_table[kanji]
del naistjdic_correction_table[kanji_key]
return [kanji, kana]
# Main loop.
for file_name in file_list:
print file_name
with codecs.open(file_name, 'r', 'utf-8') as f:
for line in f:
arr = line.split()
base = 0
for i, e in enumerate(arr):
if e[0] <= '9' and e[0] >= '0':
base = i
kanji = arr[base-1][1:]
freq = arr[base][:-2]
kana = kata2hira(arr[base+2][:-1])
kanji, kana = naistjdic_patch(kanji, kana)
if kana == '':
# Remove current item from dic.
continue
print kanji.encode('utf-8'), freq, kana
if len(kana) == 0:
continue
elif len(kana) > 1 and kana[0] == '{':
ks = kana[1:kana.index('}')].split('/')
ks = [k + kana[kana.index('}')+1:] for k in ks if len(k) > 0]
else:
ks = [kana, ]
freq = int(freq)
for k in ks:
if k not in temp_obj:
temp_obj[k] = [[freq, kanji], ]
total_freq += freq
else:
kanji_exist = False
for ele in temp_obj[k]:
if ele[1] == kanji:
kanji_exist = True
if ele[0] > freq:
ele[0] = freq
if not kanji_exist:
temp_obj[k].append([freq, kanji])
total_freq += freq
print 'Total frequence %d.' % total_freq
json_obj = []
for k, value in temp_obj.iteritems():
json_obj.append({'kana' : k, 'terms': [{'kanji': v[1], 'freq': v[0],
'kana': k} for v in value]})
print 'Writing to ../dict.json.'
df = codecs.open('../dict.json', 'w', 'utf-8')
df.write(json.dumps(json_obj, ensure_ascii=False).replace(" {\"terms\":", "\n{\"terms\":"))
df.close()
print 'Done!'

View file

@ -0,0 +1,136 @@
# -*- coding: utf-8 -*-
__all__ = ['hira2kata', 'kata2hira', 'half2hira', 'hira2half', 'kata2half',
'half2kata', 'half2wide', 'wide2half', 'convert',
'check_hira', 'check_kata', 'check_half']
import re
# convert hiragana to katakana
def hira2kata(text, reserved=[]):
return convert(text, jcconv.HIRA, jcconv.KATA, reserved)
# convert katakana to hiragana
def kata2hira(text, reserved=[]):
return convert(text, jcconv.KATA, jcconv.HIRA, reserved)
# convert half-width kana to hiragana
def half2hira(text, reserved=[]):
return convert(text, jcconv.HALF, jcconv.HIRA, reserved)
# convert hiragana to half-width kana
def hira2half(text, reserved=[]):
return convert(text, jcconv.HIRA, jcconv.HALF, reserved)
# convert katakana to half-width kana
def kata2half(text, reserved=[]):
return convert(text, jcconv.KATA, jcconv.HALF, reserved)
# convert half-width kana to katakana
def half2kata(text, reserved=[]):
return convert(text, jcconv.HALF, jcconv.KATA, reserved)
# expand half width number and alphabet to wide width
def half2wide(text, reserved=[]):
text = convert(text, jcconv.HNUM, jcconv.WNUM, reserved)
text = convert(text, jcconv.HALP, jcconv.WALP, reserved)
return convert(text, jcconv.HSYM, jcconv.WSYM, reserved)
# shrink wide width number and alphabet to half width
def wide2half(text, reserved=[]):
text = convert(text, jcconv.WNUM, jcconv.HNUM, reserved)
text = convert(text, jcconv.WALP, jcconv.HALP, reserved)
return convert(text, jcconv.WSYM, jcconv.HSYM, reserved)
# check if 'text' consists of hiragana
def check_hira(text):
return check(text, jcconv.HIRA)
# check if 'text' consists of katakana
def check_kata(text):
return check(text, jcconv.KATA)
# check if 'text' consists of half-width kana
def check_half(text):
return check(text, jcconv.HALF)
# convert 'frm' charset to 'to' charset
# input text must be unicode or str(utf-8)
# 'frm' and 'to' can be specified with (HIRA, KATA, HALF, WNUM, HNUM, WALP, HALP, WSYM, HSYM)
__regex_storage = {}
def convert(text, frm, to, reserved=[]):
def _multiple_replace(text, dic):
key = '|'.join(map(re.escape, dic))
if key in __regex_storage:
rx = __regex_storage[key]
else:
rx = re.compile(key)
__regex_storage[key] = rx
def proc_one(match):
return dic[match.group(0)]
return rx.sub(proc_one, text)
uflag = isinstance(text, unicode)
f_set = jcconv.char_sets[frm]
t_set = jcconv.char_sets[to]
text = uflag and text or text.decode('utf-8')
if len(f_set[0].split(' ')) == len(t_set[0].split(' ')):
for i in range(len(f_set)):
conv_table = dict(zip(f_set[i].split(' '), t_set[i].split(' ')))
for r in reserved:
try:
del(conv_table[r])
except:
pass
text = _multiple_replace(text, conv_table)
return uflag and text or text.encode('utf-8')
else:
raise "Invalid Parameter"
def check(text, char_set_type):
uflag = isinstance(text, unicode)
text = uflag and text or text.decode('utf-8')
char_set = []
for set in jcconv.char_sets[char_set_type]:
char_set.extend(set.split(' '))
return all([text_char in char_set for text_char in text])
# define character sets used in japanese
class jcconv:
(HIRA, KATA, HALF, WNUM, HNUM, WALP, HALP, WSYM, HSYM) = (i for i in range(9))
hira = [u'が ぎ ぐ げ ご ざ じ ず ぜ ぞ だ ぢ づ で ど ば び ぶ べ ぼ ぱ ぴ ぷ ぺ ぽ',
u'あ い う え お か き く け こ さ し す せ そ た ち つ て と ' + \
u'な に ぬ ね の は ひ ふ へ ほ ま み む め も や ゆ よ ら り る れ ろ ' + \
u'わ を ん ぁ ぃ ぅ ぇ ぉ ゃ ゅ ょ っ']
kata = [u'ガ ギ グ ゲ ゴ ザ ジ ズ ゼ ゾ ダ ヂ ヅ デ ド バ ビ ブ ベ ボ パ ピ プ ペ ポ',
u'ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ テ ト ' + \
u'ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ' + \
u'ワ ヲ ン ァ ィ ゥ ェ ォ ャ ュ ョ ッ']
half = [u'ガ ギ グ ゲ ゴ ザ ジ ズ ゼ ゾ ダ ヂ ヅ デ ド バ ビ ブ ベ ボ パ ピ プ ペ ポ',
u'ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ' + \
u'ワ ヲ ン ァ ィ ゥ ェ ォ ャ ュ ョ ッ']
wnum = [u'0 1 2 3 4 5 6 7 8 9']
hnum = [u'0 1 2 3 4 5 6 7 8 9']
walp = [u'a b c d e f g h i j k l m n o p q r s t u v w x y z ' + \
u'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z']
halp = [u'a b c d e f g h i j k l m n o p q r s t u v w x y z ' + \
u'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z']
wsym = [u'! ” # $ % & ’ ( ) * + 、 − . / : ; < = > ? @ 「 \ 」 ^ _ ` 『 | 』 〜']
hsym = [u'! \" # $ % & \' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~']
char_sets = [hira, kata, half, wnum, hnum, walp, halp, wsym, hsym]
if __name__ == '__main__':
import codecs, sys
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
print convert(u'あいうえお', jcconv.HIRA, jcconv.HALF, [u'う'])
print convert(u'ばいおりん', jcconv.HIRA, jcconv.HALF)
print convert(u'バイオリン', jcconv.HALF, jcconv.HIRA)
print convert(u'12345', jcconv.HNUM, jcconv.WNUM)
print check_hira(u'ひらがな')
print check_hira(u'カタカナ')
print check_kata(u'ひらがな')
print check_kata(u'カタカナ')

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,213 @@
Jspinyin is a Simplified Chinese input method for gaia and is licensed under
the Apache Version 2.0, 2012. The original code is based on the Google
PinyinIME project as part of the android project hosted at:
http://android.googlesource.com/platform/packages/inputmethods/PinyinIME.
We use Emscripten (https://github.com/kripken/emscripten) to compile the
original code into JavaScript version.
==========================================================================
Copyright notice for jspinyin.
Copyright (c) 2012, Mozilla Online
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==========================================================================
Copyright notice for PinyinIME.
Copyright (c) 2009, The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View file

@ -0,0 +1,27 @@
Jspinyin is a simplified Chinese input method for gaia and is licensed
under the Apache Version 2.0, 2012. The original code is based on the
Google PinyinIME project as part of the android project hosted at:
http://android.googlesource.com/platform/packages/inputmethods/PinyinIME.
Requirements:
1. Emscripten (https://github.com/kripken/emscripten)
Emscripten Compiling Steps:
1. Git clone from https://github.com/luke-chang/Android-PinyinIME.git
2. Git checkout "empinyin" branch.
3. Go to "Android-PinyinIME/jni/empinyin" path.
4. Open "Makefile" and set your own EMSCRIPTEN_HOME
5. Run "make"
6. Refer to jni/empinyin/*.patch and modify the corresponding files manually.
7. Done.

Binary file not shown.

View file

@ -0,0 +1,132 @@
/* jshint sub: true */
/* global Module */
'use strict';
(function() {
if (typeof Module == 'undefined') {
/*jshint -W020 */
Module = {};
/*jshint +W020 */
}
if (!Module['preRun']) {
Module['preRun'] = [];
}
Module['preRun'].push(function() {
function assert(check, msg) {
if (!check) {
throw msg + new Error().stack;
}
}
Module['FS_createPath']('/', 'data', true, true);
function DataRequest() {}
DataRequest.prototype = {
requests: {},
open: function(mode, name) {
this.requests[name] = this;
},
send: function() {}
};
var filePreload0 = new DataRequest();
filePreload0.open('GET', 'data/dict.data', true);
filePreload0.responseType = 'arraybuffer';
filePreload0.onload = function() {
var arrayBuffer = filePreload0.response;
assert(arrayBuffer, 'Loading file data/dict.data failed.');
var byteArray = !arrayBuffer.subarray ?
new Uint8Array(arrayBuffer) : arrayBuffer;
Module['FS_createPreloadedFile'](
'/data', 'dict.data', byteArray, true, true, function() {
Module['removeRunDependency']('fp data/dict.data');
}
);
};
Module['addRunDependency']('fp data/dict.data');
filePreload0.send(null);
if (!Module.expectedDataFileDownloads) {
Module.expectedDataFileDownloads = 0;
Module.finishedDataFileDownloads = 0;
}
Module.expectedDataFileDownloads++;
var PACKAGE_NAME = 'empinyin_files.data';
var REMOTE_PACKAGE_NAME =
(Module['empinyin_files_path'] ? Module['empinyin_files_path'] + '/' : '') +
'empinyin_files.data';
function fetchRemotePackage(packageName, callback, errback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', packageName, true);
xhr.responseType = 'arraybuffer';
xhr.onprogress = function(event) {
var url = packageName;
if (event.loaded && event.total) {
if (!xhr.addedTotal) {
xhr.addedTotal = true;
if (!Module.dataFileDownloads) {
Module.dataFileDownloads = {};
}
Module.dataFileDownloads[url] = {
loaded: event.loaded,
total: event.total
};
} else {
Module.dataFileDownloads[url].loaded = event.loaded;
}
var total = 0;
var loaded = 0;
var num = 0;
for (var download in Module.dataFileDownloads) {
var data = Module.dataFileDownloads[download];
total += data.total;
loaded += data.loaded;
num++;
}
total = Math.ceil(total * Module.expectedDataFileDownloads / num);
Module['setStatus'](
'Downloading data... (' + loaded + '/' + total + ')'
);
} else if (!Module.dataFileDownloads) {
Module['setStatus']('Downloading data...');
}
};
xhr.onload = function(event) {
var packageData = xhr.response;
callback(packageData);
};
xhr.send(null);
}
function processPackageData(arrayBuffer) {
Module.finishedDataFileDownloads++;
assert(arrayBuffer, 'Loading data file failed.');
var byteArray = new Uint8Array(arrayBuffer);
var curr;
curr = DataRequest.prototype.requests['data/dict.data'];
var data = byteArray.subarray(0, 1068442);
var ptr = Module['_malloc'](1068442);
Module['HEAPU8'].set(data, ptr);
curr.response = Module['HEAPU8'].subarray(ptr, ptr + 1068442);
curr.onload();
Module['removeRunDependency']('datafile_empinyin_files.data');
}
Module['addRunDependency']('datafile_empinyin_files.data');
function handleError(error) {
console.error('package error:', error);
}
if (!Module.preloadResults) {
Module.preloadResults = {};
}
Module.preloadResults[PACKAGE_NAME] = {fromCache: false};
fetchRemotePackage(REMOTE_PACKAGE_NAME, processPackageData, handleError);
});
})();

View file

@ -0,0 +1,771 @@
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* global KeyEvent, define, InputMethods */
'use strict';
(function() {
var debugging = false;
var debug = function jspinyin_debug(str) {
if (!debugging) {
return;
}
var done = false;
if (typeof window != 'undefined' && window.dump) {
window.dump('jspinyin: ' + str + '\n');
done = true;
}
if (typeof console != 'undefined' && console.log) {
console.log('jspinyin: ' + str);
if (arguments.length > 1) {
console.log.apply(this, arguments);
}
done = true;
}
if (done) {
return;
}
if (typeof Test != 'undefined') {
print('jspinyin: ' + str + '\n');
}
};
var assert = function jspinyin_assert(condition, msg) {
if (!debugging) {
return;
}
if (!condition) {
var str = typeof msg === 'undefined' ? assert.caller.toString() : msg;
if (typeof alert === 'function') {
alert(msg);
} else {
throw str;
}
}
};
var IMEngineBase = function engineBase_constructor() {
this._glue = {};
};
IMEngineBase.prototype = {
/**
* Glue ojbect between the IMEngieBase and the IMEManager.
*/
_glue: {
/**
* The source code path of the IMEngine
* @type String
*/
path: '',
/**
* Sends candidates to the IMEManager
*/
sendCandidates: function(candidates) {},
/**
* Sends pending symbols to the IMEManager.
*/
sendPendingSymbols: function(symbols) {},
/**
* Passes the clicked key to IMEManager for default action.
* @param {number} keyCode The key code of an integer.
*/
sendKey: function(keyCode) {},
/**
* Sends the input string to the IMEManager.
* @param {String} str The input string.
*/
sendString: function(str) {}
},
/**
* Initialization.
* @param {Glue} glue Glue object of the IMManager.
*/
init: function engineBase_init(glue) {
this._glue = glue;
},
/**
* Destruction.
*/
uninit: function engineBase_uninit() {
},
/**
* Notifies when a keyboard key is clicked.
* @param {number} keyCode The key code of an integer number.
*/
click: function engineBase_click(keyCode) {
},
/**
* Notifies when pending symbols need be cleared
*/
empty: function engineBase_empty() {
},
/**
* Notifies when a candidate is selected.
* @param {String} text The text of the candidate.
* @param {Object} data User data of the candidate.
*/
select: function engineBase_select(text, data) {
},
/**
* Notifies when selection changes
*/
stateChange: function engineBase_stateChange(detail) {
},
/**
* Notifies when surrounding text changes
*/
surroundingtextChange: function engineBase_surroundingtextChange(detail) {
},
/**
* Notifies when the IM is shown
*/
activate: function engineBase_activate(language, state, options) {
},
/**
* Called when the keyboard is hidden
*/
deactivate: function engineBase_deactivate() {
}
};
var emEngineWrapper = {
_worker: null,
_callback: null,
_initialized: false,
post: function(id, param, callback) {
if (!this._initialized && id != 'init') {
throw 'Database not ready!';
}
if (!this._callback[id]) {
this._callback[id] = [];
}
this._callback[id].push(callback);
this._worker.postMessage({
id: id,
param: param
});
return true;
},
init: function(path, byteArray, callback) {
if (this._initialized) {
callback(true);
return;
}
var self = this;
this._callback = {};
this._worker = new Worker(path + '/worker.js');
this._worker.onmessage = function(e) {
var data = e.data;
switch (data.id) {
case 'message':
console.log('emEngineWrapper: ' + data.returnValue);
break;
default:
var msgCallback = self._callback[data.id].shift();
if (msgCallback) {
msgCallback(data.returnValue);
}
}
};
this.post('init', {
userDict: byteArray
}, function(isOk) {
if (isOk) {
self._initialized = true;
} else {
self.uninit();
}
callback(isOk);
});
},
uninit: function() {
if (this._worker) {
this._worker.terminate();
}
this._worker = null;
this._callback = null;
this._initialized = false;
},
isReady: function() {
return this._initialized;
}
};
var IMEngine = function engine_constructor() {
IMEngineBase.call(this);
};
// We use keycode 65 to represent "'" for pinyin input method
var SPECIAL_KEYCODE_FOR_DELIMITER = 65;
IMEngine.prototype = {
// Implements IMEngineBase
__proto__: new IMEngineBase(),
// Buffer limit will force output the longest matching terms
// if the length of the syllables buffer is reached.
_kBufferLenLimit: 30,
// Remember the candidate length of last searching result because we don't
// want to output all candidates at a time.
// Set it to 0 when we don't need the candidates buffer anymore.
_candidatesLength: 0,
_workerTimeout: 600000,
/**
* The last selected text used to generate prediction.
* @type string
*/
_historyText: '',
_pendingSymbols: '',
_firstCandidate: '',
_keypressQueue: [],
_isWorking: false,
_isActive: false,
_uninitTimer: null,
_sendPendingSymbols: function engine_sendPendingSymbols() {
debug('SendPendingSymbol: ' + this._pendingSymbols);
if (this._pendingSymbols) {
var self = this;
emEngineWrapper.post(
'im_get_pending_symbols_info',
{},
function(returnValue) {
var fixedLen = returnValue.fixedLen;
var splStart = returnValue.splStart;
var splStartLen = splStart.length;
var display = self._firstCandidate.substring(0, fixedLen);
if (splStartLen > 1) {
for (var i = fixedLen; i < splStartLen - 1; i++) {
display += self._pendingSymbols.substring(splStart[i],
splStart[i + 1]) + ' ';
}
display +=
self._pendingSymbols.substring(splStart[splStartLen - 1]);
} else {
display += self._pendingSymbols;
}
self._glue.setComposition(display.trim());
}
);
} else {
this._glue.endComposition();
}
},
/**
* Send candidates list.
* @param {Array.<string>} candidates The candidates to be sent.
* @return {void} No return value.
*/
_sendCandidates: function engine_sendCandidates(candidates) {
var list = [];
var len = candidates.length;
for (var id = 0; id < len; id++) {
var cand = candidates[id];
if (id === 0) {
this._firstCandidate = cand;
}
list.push([cand, id]);
}
this._glue.sendCandidates(list);
},
_start: function engine_start() {
if (this._isWorking) {
return;
}
if (!emEngineWrapper.isReady()) {
debug('emEngineWrapper is not ready!');
return;
}
this._isWorking = true;
debug('Start keyQueue loop.');
this._next();
},
_next: function engine_next() {
debug('Processing keypress');
if (!this._keypressQueue.length) {
debug('keyQueue emptied.');
this._isWorking = false;
return;
}
var code = this._keypressQueue.shift();
// We use keycode 65 to represent "'" for pinyin input method
var realCode = (code === SPECIAL_KEYCODE_FOR_DELIMITER) ? 39 : code;
if (code === 0) {
// This is a select function operation.
this._updateCandidatesAndSymbols(this._next.bind(this));
return;
}
debug('key code: ' + code);
// Backspace - delete last input symbol if exists
if (code === KeyEvent.DOM_VK_BACK_SPACE) {
debug('Backspace key');
if (!this._pendingSymbols) {
if (this._firstCandidate) {
debug('Remove candidates.');
// prevent updateCandidateList from making the same suggestions
this.empty();
}
// pass the key to IMEManager for default action
debug('Default action.');
this._glue.sendKey(realCode);
this._next();
} else {
this._pendingSymbols = this._pendingSymbols.substring(0,
this._pendingSymbols.length - 1);
this._updateCandidatesAndSymbols(this._next.bind(this));
}
return;
}
// Select the first candidate if needed.
if (code === KeyEvent.DOM_VK_RETURN ||
!this._isPinyinKey(code) ||
this._pendingSymbols.length >= this._kBufferLenLimit) {
debug('Non-pinyin key is pressed or the input is too long.');
var sendKey = true;
if (this._firstCandidate) {
if (this._pendingSymbols) {
// candidate list exists; output the first candidate
debug('Sending first candidate.');
this._glue.endComposition(this._firstCandidate);
// no return here
if (code === KeyEvent.DOM_VK_RETURN) {
sendKey = false;
}
}
this._sendCandidates([]);
}
//pass the key to IMEManager for default action
debug('Default action.');
this.empty();
if (sendKey) {
this._glue.sendKey(realCode);
}
this._next();
return;
}
debug('Processing symbol: ' + String.fromCharCode(realCode));
// add symbol to pendingSymbols
this._appendNewSymbol(realCode);
this._updateCandidatesAndSymbols(this._next.bind(this));
},
_isPinyinKey: function engine_isPinyinKey(code) {
// keycode 65 is used to represent "'" for pinyin input method
// a-z
if ((code >= 97 && code <= 122) ||
code === SPECIAL_KEYCODE_FOR_DELIMITER) {
return true;
}
return false;
},
_appendNewSymbol: function engine_appendNewSymbol(code) {
var symbol = String.fromCharCode(code);
this._pendingSymbols += symbol;
},
_updateCandidatesAndSymbols: function engine_updateCandsAndSymbols(callback) {
var self = this;
this._updateCandidateList(function() {
self._sendPendingSymbols();
callback();
});
},
_updateCandidateList: function engine_updateCandidateList(callback) {
debug('Update Candidate List.');
var self = this;
var numberOfCandidatesPerRow = this._glue.getNumberOfCandidatesPerRow ?
this._glue.getNumberOfCandidatesPerRow() : Number.Infinity;
this._candidatesLength = 0;
if (!this._pendingSymbols) {
// If there is no pending symbols, make prediction with the previous
// select words.
if (this._historyText) {
debug('Buffer is empty; make suggestions based on select term.');
emEngineWrapper.post('im_search_predicts', {
queryString: this._historyText,
limit: numberOfCandidatesPerRow + 1
}, function(returnValue) {
var num = returnValue.length;
var predicts = returnValue.results;
if (num > numberOfCandidatesPerRow + 1) {
self._candidatesLength = num;
}
self._sendCandidates(predicts);
callback();
});
} else {
this._sendCandidates([]);
callback();
}
} else {
// Update the candidates list by the pending pinyin string.
this._historyText = '';
emEngineWrapper.post('im_search', {
queryString: this._pendingSymbols,
limit: numberOfCandidatesPerRow + 1
}, function(returnValue) {
var num = returnValue.length;
var candidates = returnValue.results;
if (num > numberOfCandidatesPerRow + 1) {
self._candidatesLength = num;
}
self._sendCandidates(candidates);
callback();
});
}
},
_resetKeypressQueue: function engine_abortKeypressQueue() {
this._keypressQueue = [];
this._isWorking = false;
},
_accessUserDict: function engine_loadUserDict(action, param, callback) {
var indexedDB = window.indexedDB;
if (!indexedDB) {
callback(null);
return;
}
// Access user_dict.data from IndexedDB
var dbVersion = 1;
var STORE_NAME = 'files';
var USER_DICT = 'user_dict';
var request = indexedDB.open('EmpinyinDatabase', dbVersion);
request.onerror = function opendb_onerror(event) {
debug('Error occurs when openning database: ' + event.target.errorCode);
callback(null);
};
request.onsuccess = function opendb_onsuccess(event) {
var db = event.target.result;
var request;
if (action == 'load') {
request = db.transaction([STORE_NAME], 'readonly')
.objectStore(STORE_NAME).get(USER_DICT);
request.onsuccess = function readdb_oncomplete(event) {
db.close();
if (!event.target.result) {
callback(null);
} else {
callback(event.target.result.content);
}
};
request.onerror = function readdb_oncomplete(event) {
debug('Failed to read file from DB: ' + event.target.result.name);
db.close();
callback(null);
};
} else if (action == 'save') {
var obj = {
name: USER_DICT,
content: param
};
request = db.transaction([STORE_NAME], 'readwrite')
.objectStore(STORE_NAME).put(obj);
request.onsuccess = function readdb_oncomplete(event) {
db.close();
callback(true);
};
request.onerror = function readdb_oncomplete(event) {
debug('Failed to write file to DB: ' + event.target.result.name);
db.close();
callback(false);
};
}
};
request.onupgradeneeded = function opendb_onupgradeneeded(event) {
var db = event.target.result;
// Delete the old ObjectStore if present
if (db.objectStoreNames.length !== 0) {
db.deleteObjectStore(STORE_NAME);
}
db.createObjectStore(STORE_NAME, { keyPath: 'name' });
};
},
/**
* Override
*/
init: function engine_init(glue) {
IMEngineBase.prototype.init.call(this, glue);
debug('init.');
},
/**
* Override
*/
uninit: function engine_uninit() {
IMEngineBase.prototype.uninit.call(this);
debug('Uninit.');
if (this._uninitTimer) {
clearTimeout(this._uninitTimer);
this._uninitTimer = null;
}
if (emEngineWrapper.isReady()) {
emEngineWrapper.uninit();
}
this._resetKeypressQueue();
this.empty();
},
/**
*Override
*/
click: function engine_click(keyCode) {
IMEngineBase.prototype.click.call(this, keyCode);
this._keypressQueue.push(keyCode);
this._start();
},
/**
* Override
*/
select: function engine_select(text, data) {
IMEngineBase.prototype.select.call(this, text, data);
if (!emEngineWrapper.isReady()) {
return;
}
var self = this;
var nextStep = function(text) {
if (text) {
if (self._pendingSymbols) {
self._pendingSymbols = '';
self._glue.endComposition(text);
} else {
self._glue.setComposition('');
self._glue.endComposition(text);
}
self._historyText = text;
self._candidatesLength = 0;
}
self._keypressQueue.push(0);
self._start();
};
if (this._pendingSymbols) {
emEngineWrapper.post('im_choose', {
candId: parseInt(data)
}, nextStep);
} else {
// A predication candidate is selected.
nextStep(text);
}
},
/**
* Override
*/
empty: function engine_empty() {
IMEngineBase.prototype.empty.call(this);
debug('empty.');
this._pendingSymbols = '';
this._historyText = '';
this._firstCandidate = '';
this._sendPendingSymbols();
this._sendCandidates([]);
},
/**
* Override
*/
stateChange: function engine_stateChange(detail) {
debug('stateChange');
if (detail.ownAction) {
return;
}
this.empty();
},
/**
* Override
*/
activate: function engine_activate(language, state, options) {
IMEngineBase.prototype.activate.call(this, language, state, options);
if (this._uninitTimer) {
clearTimeout(this._uninitTimer);
this._uninitTimer = null;
}
var inputType = state.type;
debug('Activate. Input type: ' + inputType);
if (!emEngineWrapper.isReady()) {
var self = this;
this._accessUserDict('load', null, function(byteArray) {
emEngineWrapper.init(self._glue.path, byteArray, function(isOk) {
if (isOk) {
self._start();
} else {
debug('emEngineWrapper initialize failed!!');
}
});
});
} else {
emEngineWrapper.post('im_flush_cache', {}, null);
}
this._isActive = true;
},
/**
* Override
*/
deactivate: function engine_deactivate() {
IMEngineBase.prototype.deactivate.call(this);
debug('Deactivate.');
if (!this._isActive) {
return;
}
this._isActive = false;
this._resetKeypressQueue();
this.empty();
var self = this;
emEngineWrapper.post('im_get_user_dict_data', {}, function(byteArray) {
if (byteArray) {
self._accessUserDict('save', byteArray, function(isOk) {
if (isOk) {
debug('Saved user dictionary to DB.');
} else {
debug('Failed to save user dictionary to DB.');
}
if (!self._uninitTimer) {
self._uninitTimer =
setTimeout(self.uninit.bind(self), self._workerTimeout);
}
});
}
});
},
getMoreCandidates: function engine_getMore(indicator, maxCount, callback) {
if (this._candidatesLength === 0) {
callback(null);
return;
}
var num = this._candidatesLength;
maxCount = Math.min((maxCount || num) + indicator, num);
var msgId = this._pendingSymbols ? 'im_get_candidates' : 'im_get_predicts';
emEngineWrapper.post(msgId, {
start: indicator,
count: maxCount
}, function(results) {
var len = results.length;
var list = [];
for (var i = 0; i < len; i++) {
list.push([results[i], i + indicator]);
}
callback(list);
});
}
};
var jspinyin = new IMEngine();
// Expose jspinyin as an AMD module
if (typeof define === 'function' && define.amd) {
define('jspinyin', [], function() { return jspinyin; });
}
// Expose the engine to the Gaia keyboard
if (typeof InputMethods !== 'undefined') {
InputMethods.jspinyin = jspinyin;
}
})();

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
<meta charset="UTF-8">
<title>Emscripten Pinyin Engine Test</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Emscripten Pinyin Engine Test</h1>
<section>
<p><input type="text" value="z" id="pinyin"></p>
<p>
<input type="button" value="test" id="test">
<input type="button" value="test x n" id="test2">
</p>
</section>
<div id="status"></div>
<div id="log"></div>
<script type="text/javascript" src="init.js"></script>
</body>
</html>

View file

@ -0,0 +1,192 @@
'use strict';
const testRepeatCount = 10;
var emEngineWrapper = {
_worker: null,
_callback: null,
_initialized: false,
post: function(id, param, callback) {
if (!this._initialized && id != 'init') {
throw 'Database not ready!';
}
if (!this._callback[id]) {
this._callback[id] = [];
}
this._callback[id].push(callback);
this._worker.postMessage({
id: id,
param: param
});
return true;
},
init: function(path, byteArray, callback) {
if (this._initialized) {
callback(true);
}
var self = this;
this._callback = {};
this._worker = new Worker(path + '/worker.js');
this._worker.onmessage = function(e) {
var data = e.data;
switch (data.id) {
case 'message':
console.log('emEngineWrapper: ' + data.returnValue);
break;
default:
var msgCallback = self._callback[data.id].shift();
if (msgCallback) {
msgCallback(data.returnValue);
}
}
};
this.post('init', {
userDict: byteArray
}, function(isOk) {
if (isOk) {
self._initialized = true;
} else {
self.uninit();
}
callback(isOk);
});
},
uninit: function() {
if (this._worker) {
this._worker.terminate();
}
this._worker = null;
this._callback = null;
this._initialized = false;
},
isReady: function() {
return this._initialized;
}
};
window.addEventListener('load', function() {
document.getElementById('status').textContent =
'NOTE: please copy "../empinyin_files.*", "../libpinyin.js" and ' +
'"worker.js" to this "tests" folder first.';
document.getElementById('test2').value = 'test x ' + testRepeatCount;
emEngineWrapper.init('.', null, function(isOk) {
if (isOk) {
document.getElementById('status').textContent = '';
log('Engine ready!');
initTest();
} else {
log('Load data failed!');
}
});
});
function log(msg) {
var date = new Date();
var loggerTime = date.getHours() + ':' + date.getMinutes() + ':' +
date.getSeconds() + ':' + date.getMilliseconds();
var divLog = document.getElementById('log');
var p = document.createElement('p');
p.innerHTML = loggerTime + ' : ' + msg;
divLog.insertBefore(p, divLog.firstChild);
}
function initTest() {
document.getElementById('test').onclick = function() {
var keyword = document.getElementById('pinyin').value;
try {
log('search keyword ' + keyword);
var startTime = new Date().getTime();
emEngineWrapper.post('im_search', {
queryString: keyword,
limit: -1
}, function(returnValue) {
var endTime = new Date().getTime();
log('total cost: ' + (endTime - startTime) + 'ms');
var size = returnValue.length;
var candidates = returnValue.results;
log('length: ' + size);
log('Candidates: ' + candidates.join(' '));
emEngineWrapper.post('im_choose', {
candId: 0
}, function(convertedText) {
log('Choose: ' + convertedText);
if (convertedText) {
startTime = new Date().getTime();
emEngineWrapper.post('im_search_predicts', {
queryString: convertedText,
limit: -1
}, function(returnValue) {
endTime = new Date().getTime();
log('total cost: ' + (endTime - startTime) + 'ms');
var nPredicts = returnValue.length;
var predicts = returnValue.results;
log('length: ' + nPredicts);
log('Predicts: ' + predicts.join(' '));
});
}
});
});
} catch (e) {
log('error: ' + e);
}
};
document.getElementById('test2').onclick = function() {
var keyword = document.getElementById('pinyin').value;
try {
log('search ' + testRepeatCount + ' times keyword ' + keyword);
var startTime = new Date().getTime();
var counter = 0;
var doTest = function() {
emEngineWrapper.post('im_search', {
queryString: keyword,
limit: -1
}, function(returnValue) {
if (++counter == testRepeatCount) {
var endTime = new Date().getTime();
var period = endTime - startTime;
log('total cost: ' + period + 'ms');
log('average cost: ' + (period / testRepeatCount) + 'ms');
log('length: ' + returnValue.length);
} else {
doTest();
}
});
};
doTest();
} catch (e) {
log('error: ' + e);
}
};
}

View file

@ -0,0 +1,22 @@
input[type="text"] {
font-size: 1.5rem;
width: 18rem;
}
button, input[type="button"] {
width: 9rem;
height: 2.5rem;
font-size: 1.5rem;
}
#log, #status {
margin-top: 1rem;
}
#output {
margin-top: 1rem;
}
p {
margin: 0.3rem;
}

View file

@ -0,0 +1,175 @@
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* jshint sub: true */
'use strict';
var Module;
var emEngine;
var procFunction;
self.onmessage = function(e) {
var data = e.data;
if (data.id == 'init') {
initModule(data.id, data.param);
} else if (procFunction[data.id]) {
var result = procFunction[data.id](data.param);
post(data.id, result);
} else {
log('echo: ' + JSON.stringify(e.data));
}
};
function initModule(msgId, param) {
Module = {
canvas: {},
stdout: null,
setStatus: log,
preRun: [function() {
if (param.userDict) {
Module['addRunDependency']('fp data/user_dict.data');
Module['FS_createPreloadedFile'](
'/data', 'user_dict.data', param.userDict, true, true, function() {
Module['removeRunDependency']('fp data/user_dict.data');
});
}
}],
_main: function() {
emEngine = {
openDecoder:
Module.cwrap('im_open_decoder', 'number', ['string', 'string']),
closeDecoder:
Module.cwrap('im_close_decoder', '', []),
search:
Module.cwrap('im_search', 'number', ['string', 'number']),
resetSearch:
Module.cwrap('im_reset_search', '', []),
getCandidateAt:
Module.cwrap('im_get_candidate_char', 'string', ['number']),
getPredicts:
Module.cwrap('im_get_predicts_utf8', 'number', ['string', 'number']),
getPredictAt:
Module.cwrap('im_get_predict_at', 'string', ['number']),
choose:
Module.cwrap('im_choose', 'number', ['number']),
getSplStart:
Module.cwrap('im_get_spl_start', 'number', []),
getSplStartAt:
Module.cwrap('im_get_spl_start_at', 'number', ['number']),
getFixedLen:
Module.cwrap('im_get_fixed_len', 'number', []),
flushCache:
Module.cwrap('im_flush_cache', '', [])
};
if (emEngine.openDecoder('data/dict.data', 'data/user_dict.data')) {
initProcFunctions();
post(msgId, true);
} else {
post(msgId, false);
}
}
};
try {
importScripts('empinyin_files.js');
importScripts('libpinyin.js');
} catch (e) {
log(e.toString());
post(msgId, false);
}
}
function initProcFunctions() {
var getSearchResult = function(emEngineGetAt, param) {
var start = param.start;
var count = param.count;
var results = [];
for (var i = start; i < count; i++) {
results.push(emEngineGetAt(i));
}
return results;
};
var search = function(emEngineSearch, emEngineGetAt, param) {
var query = param.queryString;
var numberToReturn = param.limit;
var resultLen = emEngineSearch(query, query.length);
var results;
if (numberToReturn) {
if (numberToReturn < 0 || numberToReturn > resultLen) {
numberToReturn = resultLen;
}
results = getSearchResult(emEngineGetAt, {
start: 0,
count: numberToReturn
});
}
return {
length: resultLen,
results: results
};
};
procFunction = {};
/*jshint -W040 */
procFunction['im_search'] =
search.bind(this, emEngine.search, emEngine.getCandidateAt);
procFunction['im_get_candidates'] =
getSearchResult.bind(this, emEngine.getCandidateAt);
procFunction['im_search_predicts'] =
search.bind(this, emEngine.getPredicts, emEngine.getPredictAt);
procFunction['im_get_predicts'] =
getSearchResult.bind(this, emEngine.getPredictAt);
/*jshint +W040 */
procFunction['im_flush_cache'] =
emEngine.flushCache;
procFunction['im_choose'] = function(param) {
var candsNum = emEngine.choose(param.candId);
if (candsNum == 1 && emEngine.getFixedLen() == emEngine.getSplStart()) {
var convertedText = emEngine.getCandidateAt(0);
emEngine.resetSearch();
return convertedText;
}
return null;
};
procFunction['im_get_pending_symbols_info'] = function() {
var fixedLen = emEngine.getFixedLen();
var splStartLen = emEngine.getSplStart();
var splStart = [];
for (var i = 0; i <= splStartLen; i++) {
splStart.push(emEngine.getSplStartAt(i));
}
return {
fixedLen: fixedLen,
splStart: splStart
};
};
procFunction['im_get_user_dict_data'] = function() {
if (!Module['FS']) {
return null;
}
emEngine.flushCache();
return Module['FS'].findObject('data/user_dict.data').contents;
};
}
function post(id, returnValue) {
postMessage({
id: id,
returnValue: returnValue
});
}
function log(message) {
post('message', message);
}

View file

@ -0,0 +1,488 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* global InputMethods */
(function() {
'use strict';
/* BoGo Engine. https://github.com/lewtds/bogo.js
*
* Copyright 2014, Trung Ngo <ndtrung4419@gmail.com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function BoGo() {
var EffectType = {
APPENDING: 0,
MARK: 1,
TONE: 2
};
var Mark = {
NONE: 0,
HAT: 1,
BREVE: 2,
HORN: 3,
DASH: 4
};
var Tone = {
NONE: 0,
GRAVE: 1,
ACUTE: 2,
HOOK: 3,
TILDE: 4,
DOT: 5
};
var VOWELS = 'aàáảãạăằắẳẵặâầấẩẫậeèéẻẽẹêềếểễệiìíỉĩị' +
'oòóỏõọôồốổỗộơờớởỡợuùúủũụưừứửữựyỳýỷỹỵ';
var composition = [];
var rules = [];
var MARKS_MAP = {
'a': 'aâă__',
'â': 'aâă__',
'ă': 'aâă__',
'e': 'eê___',
'ê': 'eê___',
'o': 'oô_ơ_',
'ô': 'oô_ơ_',
'ơ': 'oô_ơ_',
'u': 'u__ư_',
'ư': 'u__ư_',
'd': 'd___đ',
'đ': 'd___đ'
};
var MARK_CHARS = {
'^': Mark.HAT,
'(': Mark.BREVE,
'+': Mark.HORN,
'-': Mark.DASH
};
var TONE_CHARS = {
'~': Tone.TILDE,
'\'': Tone.ACUTE,
'?': Tone.HOOK,
'`': Tone.GRAVE,
'.': Tone.DOT
};
function is_vowel(chr) {
return VOWELS.indexOf(chr) != -1;
}
function add_mark_to_char(chr, mark) {
var result;
var tone = get_tone_from_char(chr);
chr = add_tone_to_char(chr, Tone.NONE);
if (chr in MARKS_MAP && MARKS_MAP[chr][mark] != '_') {
result = MARKS_MAP[chr][mark];
} else {
result = chr;
}
result = add_tone_to_char(result, tone);
return result;
}
function get_tone_from_char(chr) {
var position = VOWELS.indexOf(chr);
if (position != -1) {
return position % 6;
} else {
return Tone.NONE;
}
}
function add_tone_to_char(chr, tone) {
var result;
var position = VOWELS.indexOf(chr);
if (position != -1) {
var current_tone = position % 6;
var offset = tone - current_tone;
result = VOWELS[position + offset];
} else {
result = chr;
}
return result;
}
function find_mark_target(rule) {
var target;
for (var i = composition.length - 1; i > -1; i--) {
if (composition[i].rule.key == rule.effective_on) {
target = composition[i];
}
}
return target;
}
function find_rightmost_vowels() {
var vowels = [];
for (var i = composition.length - 1; i >= 0; i--) {
var trans = composition[i];
if (trans.rule.type == EffectType.APPENDING &&
is_vowel(trans.rule.key)) {
vowels.unshift(trans);
}
}
return vowels;
}
function find_next_appending_trans(trans) {
var from_index = composition.indexOf(trans);
var next_appending_trans;
// FIXME: Need not-found guard.
for (var i = from_index + 1; i < composition.length; i++) {
if (composition[i].rule.type == EffectType.APPENDING) {
next_appending_trans = composition[i];
}
}
return next_appending_trans;
}
function find_tone_target(rule) {
var vowels = find_rightmost_vowels();
var target;
if (vowels.length == 1) {
// cá
target = vowels[0];
} else if (vowels.length == 2) {
if (find_next_appending_trans(vowels[1]) !== undefined ||
flatten(vowels) == 'uo') {
// nước, thuở
target = vowels[1];
} else {
// cáo
target = vowels[0];
}
} else if (vowels.length == 3) {
if (flatten(vowels) == 'uye') {
// chuyển
target = vowels[2];
} else {
// khuỷu
target = vowels[1];
}
}
return target;
}
function refresh_last_tone_target() {
// Refresh the tone position of the last EffectType.TONE transformation.
for (var i = composition.length - 1; i >= 0; i--) {
var trans = composition[i];
if (trans.rule.type == EffectType.TONE) {
var new_target = find_tone_target(trans.rule);
trans.target = new_target;
break;
}
}
}
function process_char(chr) {
var isUpperCase = chr === chr.toUpperCase();
chr = chr.toLowerCase();
var applicable_rules = [];
rules.forEach(function(rule) {
if (rule.key == chr) {
applicable_rules.push(rule);
}
});
// If none of the applicable_rules can actually be applied then this new
// transformation fallbacks to an APPENDING one.
var trans = {
rule: {
type: EffectType.APPENDING,
key: chr
},
isUpperCase: isUpperCase
};
for (var i = 0; i < applicable_rules.length; i++) {
var rule = applicable_rules[i];
var target;
if (rule.type == EffectType.MARK) {
target = find_mark_target(rule);
} else if (rule.type == EffectType.TONE) {
target = find_tone_target(rule);
}
if (target !== undefined) {
// Fix uaw being wrongly processed to muă by skipping
// the aw rule. Then the uw rule will be matched later.
// Note that this requires the aw rule be placed before
// uw in the rule list.
if (chr == 'w') {
var target_index = composition.indexOf(target);
var prev_trans = composition[target_index - 1];
if (target.rule.key == 'a' &&
prev_trans.rule.key == 'u') {
continue;
}
}
trans.rule = rule;
trans.target = target;
break;
}
}
composition.push(trans);
// Implement the uow typing shortcut by creating a virtual
// Mark.HORN rule that targets 'u'.
//
// FIXME: This is a potential slowdown. Perhaps it should be
// toggled by a config key.
if (flatten().match(/uơ.+$/)) {
var vowels = find_rightmost_vowels();
var virtual_trans = {
rule: {
type: EffectType.MARK,
key: '', // This is a virtual rule,
// it should not appear in the raw string.
effect: Mark.HORN
},
target: vowels[0]
};
composition.push(virtual_trans);
}
// Sometimes, a tone's position in a previous state must be changed to
// fit the new state.
//
// e.g.
// prev state: chuyenr -> chuỷen
// this state: chuyenre -> chuyển
if (trans.rule.type == EffectType.APPENDING) {
refresh_last_tone_target();
}
}
function flatten() {
var canvas = [];
composition.forEach(function(trans, index) {
function apply_effect(func, trans) {
var index = trans.target.dest;
var char_with_effect = func(canvas[index], trans.rule.effect);
// Double typing an effect key undoes it. Btw, we're playing
// fast-and-loose here by relying on the fact that Tone.NONE
// equals Mark.None and equals 0.
if (char_with_effect == canvas[index]) {
canvas[index] = func(canvas[index], Tone.NONE);
} else {
canvas[index] = char_with_effect;
}
}
switch (trans.rule.type) {
case EffectType.APPENDING:
trans.dest = canvas.length;
canvas.push(trans.rule.key);
break;
case EffectType.MARK:
apply_effect(add_mark_to_char, trans);
break;
case EffectType.TONE:
apply_effect(add_tone_to_char, trans);
break;
default:
break;
}
});
composition.forEach(function(trans) {
if (trans.rule.type == EffectType.APPENDING) {
if (trans.isUpperCase) {
canvas[trans.dest] = canvas[trans.dest].toUpperCase();
}
}
});
return canvas.join('');
}
function process_string(string) {
for (var i = 0; i < string.length; i++) {
process_char(string[i]);
}
}
// js > parse_rule('a a a^')
// {type: EffectType.MARK, effect: HAT, key: a, effective_on: a}
//
// js > parse_rule('a w a(')
// {type: EffectType.MARK, effect: BREVE, key: w, effective_on: a}
//
// js > parse_rule('a f a`')
// {type: EffectType.MARK, effect: HAT, key: a, effective_on: a}
//
// js > parse_rule('w u+')
// {type: EffectType.APPEND, effect: ư, key: w}
function parse_rule(string) {
var tokens = string.trim().replace(/\s\s+/, ' ').split(' ');
var effective_on = tokens[0];
var key = tokens[1];
var type;
var effect;
var effect_char = tokens[2][1];
if (effect_char in MARK_CHARS) {
type = EffectType.MARK;
effect = MARK_CHARS[effect_char];
} else if (effect_char in TONE_CHARS) {
type = EffectType.TONE;
effect = TONE_CHARS[effect_char];
}
var trans = {
type: type,
key: key,
effect: effect,
effective_on: effective_on
};
return trans;
}
function process_backspace() {
var indexes_to_remove = [];
var last_appending_trans;
var i;
var trans;
// Find the last APPENDING transformation and all
// the transformations that add effects to it.
for (i = composition.length - 1; i >= 0; i--) {
trans = composition[i];
if (trans.rule.type == EffectType.APPENDING) {
last_appending_trans = trans;
indexes_to_remove.push(i);
break;
}
}
for (i = indexes_to_remove[0] + 1; i < composition.length; i++) {
trans = composition[i];
if (trans.hasOwnProperty('target') &&
trans.target === last_appending_trans) {
indexes_to_remove.push(i);
}
}
// Then remove them
indexes_to_remove.sort().reverse();
indexes_to_remove.forEach(function(index) {
composition.splice(index, 1);
});
}
function get_raw_input_string() {
var raw_input_keys = [];
composition.forEach(function(trans) {
raw_input_keys.push(trans.rule.key);
});
return raw_input_keys.join('');
}
function clear_composition() {
composition = [];
}
function clear_rules() {
rules = [];
}
function has_composition() {
return composition.length !== 0;
}
var exports = {
add_rule: function(rule_string) {
rules.push(parse_rule(rule_string));
},
clear_rules: clear_rules,
process_char: process_char,
process_string: process_string,
process_backspace: process_backspace,
clear_composition: clear_composition,
get_processed_string: flatten,
get_raw_input_string: get_raw_input_string,
has_composition: has_composition
};
return exports;
}
var input_context;
var engine;
function init(_input_context) {
console.log('KEYBOARD: ' + _input_context);
input_context = _input_context;
engine = new BoGo();
engine.add_rule('o w o+');
engine.add_rule('u w u+');
engine.add_rule('a a a^');
engine.add_rule('a w a(');
engine.add_rule('e e e^');
engine.add_rule('o o o^');
engine.add_rule('d d d-');
engine.add_rule('_ f _`');
engine.add_rule('_ r _?');
engine.add_rule('_ x _~');
engine.add_rule('_ j _.');
engine.add_rule('_ s _\'');
}
function click(keycode, x, y) {
if (keycode == 32) {
input_context.endComposition(engine.get_processed_string());
engine.clear_composition();
input_context.sendKey(keycode);
} else if (keycode == 8) {
if (engine.has_composition()) {
engine.process_backspace();
input_context.setComposition(engine.get_processed_string());
} else {
engine.clear_composition();
input_context.sendKey(keycode);
}
} else {
var chr = String.fromCharCode(keycode);
engine.process_char(chr);
input_context.setComposition(engine.get_processed_string());
}
}
// Expose the engine to the Gaia keyboard
InputMethods.jstelex = {
init: init,
click: click
};
})();

View file

@ -0,0 +1 @@
48e2b384bd18f2bbd3acbe1b0c69f1273457b7eb

Binary file not shown.

View file

@ -0,0 +1,268 @@
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* global KeyboardEvent, KeyEvent, InputMethods */
'use strict';
/*
* This file implements a "glue" between FxOS Keyboard app UI and
* the JSZhuyin library.
*/
var JSZhuyinGaiaKeyboardGlue = function JSZhuyinGaiaKeyboardGlue() {
this.callbacks = null;
this.worker = null;
this.composing = false;
this.hasCandidate = false;
this.workerReady = false;
this.messageQueue = [];
this.requestId = 0;
this.handledId = 0;
this.unloadWorkerTimer = undefined;
};
JSZhuyinGaiaKeyboardGlue.prototype.kDEBUG = false;
JSZhuyinGaiaKeyboardGlue.prototype.DEACTIVATE_TIME = 60000; // 1 minute
JSZhuyinGaiaKeyboardGlue.prototype.init = function jszg_init(callbacks) {
if (this.kDEBUG) {
console.log('zhuyin:init:' + Date.now());
}
this.callbacks = callbacks;
};
JSZhuyinGaiaKeyboardGlue.prototype.uninit = function jszg_uninit() {
if (this.kDEBUG) {
console.log('zhuyin:uninit:' + Date.now());
}
this._unloadWorker();
this.callbacks = null;
};
JSZhuyinGaiaKeyboardGlue.prototype._loadWorker = function jszg_loadWorker() {
if (this.kDEBUG) {
console.log('zhuyin:loadWorker:' + Date.now());
}
if (this.worker) {
return;
}
var worker = this.worker = new Worker(this.callbacks.path + '/lib/worker.js');
worker.addEventListener('error', this);
worker.addEventListener('message', this);
worker.postMessage({ 'type': 'load' });
};
JSZhuyinGaiaKeyboardGlue.prototype._unloadWorker =
function jszg_unloadWorker() {
if (this.kDEBUG) {
console.log('zhuyin:unloadWorker:' + Date.now());
}
if (!this.worker) {
return;
}
var worker = this.worker;
worker.removeEventListener('error', this);
worker.removeEventListener('message', this);
worker.terminate();
this.worker = null;
this.composing = false;
this.hasCandidate = false;
this.workerReady = false;
this.messageQueue = [];
this.requestId = 0;
this.handledId = 0;
};
JSZhuyinGaiaKeyboardGlue.prototype.click = function jszg_click(code) {
// Let's not use BopomofoEncoder.isBopomofoSymbol(code) here
var BOPOMOFO_START = 0x3105;
var BOPOMOFO_END = 0x3129;
var BOPOMOFO_TONE_1 = 0x02c9;
var BOPOMOFO_TONE_2 = 0x02ca;
var BOPOMOFO_TONE_3 = 0x02c7;
var BOPOMOFO_TONE_4 = 0x02cb;
var BOPOMOFO_TONE_5 = 0x02d9;
// We must handle Bopomofo symbols.
if (code >= BOPOMOFO_START && code <= BOPOMOFO_END ||
code === BOPOMOFO_TONE_1 || code === BOPOMOFO_TONE_2 ||
code === BOPOMOFO_TONE_3 || code === BOPOMOFO_TONE_4 ||
code === BOPOMOFO_TONE_5) {
this.sendMessage('handleKeyEvent', code, ++this.requestId);
return;
}
// Send BOPOMOFO_TONE_1 for the SPACE key.
if (this.composing && code === KeyboardEvent.DOM_VK_SPACE) {
this.sendMessage('handleKeyEvent', BOPOMOFO_TONE_1, ++this.requestId);
return;
}
// Handle the key anyway if we might be currently composing.
if (this.composing || this.requestId !== this.handledId) {
this.sendMessage('handleKeyEvent', code, ++this.requestId);
return;
}
// Before send the key out, remove suggestions.
if (this.hasCandidate) {
this.hasCandidate = false;
this.callbacks.sendCandidates([]);
}
// Not handling the key; send it out directly.
this.callbacks.sendKey(code);
};
JSZhuyinGaiaKeyboardGlue.prototype.select = function jszg_select(text, data) {
this.sendMessage('selectCandidate', [text, data], ++this.requestId);
};
JSZhuyinGaiaKeyboardGlue.prototype.selectionChange = function(detail) {
if (detail.ownAction) {
return;
}
this.empty();
};
JSZhuyinGaiaKeyboardGlue.prototype.activate = function jszg_activate() {
if (this.kDEBUG) {
console.log('zhuyin:activate:' + Date.now());
}
if (!this.worker) {
this._loadWorker();
}
clearTimeout(this.unloadWorkerTimer);
this.empty();
};
JSZhuyinGaiaKeyboardGlue.prototype.deactivate = function jszg_deactivate() {
if (this.kDEBUG) {
console.log('zhuyin:deactivate:' + Date.now());
}
this.empty();
clearTimeout(this.unloadWorkerTimer);
this.unloadWorkerTimer =
setTimeout(this._unloadWorker.bind(this), this.DEACTIVATE_TIME);
};
JSZhuyinGaiaKeyboardGlue.prototype.empty = function jszg_empty() {
// Simply send escape.
if (this.composing) {
this.sendMessage('handleKeyEvent', KeyEvent.DOM_VK_ESCAPE,
++this.requestId);
}
};
JSZhuyinGaiaKeyboardGlue.prototype.handleEvent =
function jszg_handleEvent(evt) {
switch (evt.type) {
case 'error':
throw 'JSZhuyinGaiaKeyboardGlue: Worker error.';
case 'message':
this.handleMessage(evt.data);
break;
}
};
JSZhuyinGaiaKeyboardGlue.prototype.handleMessage =
function jszg_handleMessage(msg) {
var data = msg.data;
if (this.kDEBUG) {
if (Array.isArray(data)) {
console.log('zhuyin:msg:' + Date.now() +
':' + msg.type + ':' + data.length);
} else {
console.log('zhuyin:msg:' + Date.now() + ':' + msg.type + ':' + data);
}
}
switch (msg.type) {
case 'actionhandled':
this.handledId = data;
break;
case 'loadend':
this.workerReady = true;
var message;
while ((message = this.messageQueue.shift())) {
this.sendMessage.apply(this, message);
}
this.messageQueue = undefined;
break;
case 'candidateschange':
this.hasCandidate = data && !!data.length;
this.callbacks.sendCandidates(data);
break;
case 'compositionupdate':
if (data) {
// send compositionstart event
this.callbacks.setComposition(data);
} else {
// send compositionend event if we are indeed is composing.
// (do nothing if we aren't)
if (this.composing) {
this.callbacks.endComposition();
}
}
this.composing = !!data;
break;
case 'compositionend':
// We must manually start the composition
// (i.e. send compositionstart event) first here if we haven't.
if (!this.composing) {
this.callbacks.setComposition(data);
}
// Send compositionend event.
this.callbacks.endComposition(data);
this.composing = false;
break;
}
};
JSZhuyinGaiaKeyboardGlue.prototype.sendMessage =
function jszg_sendMessage(type, data, reqId) {
if (!this.worker) {
throw 'JSZhuyinGaiaKeyboardGlue:' +
' attempt to send message w/o load the worker first.';
}
if (!this.workerReady) {
this.messageQueue.push([type, data, reqId]);
return;
}
this.worker.postMessage(
{ 'type': type, 'data': data, 'requestId': reqId });
};
// Expose the engine to the Gaia keyboard
if (typeof InputMethods !== 'undefined') {
InputMethods.jszhuyin = new JSZhuyinGaiaKeyboardGlue();
}

View file

@ -0,0 +1,515 @@
'use strict';
// This file implements encoding system documented by libtabe,
// inspired by the ETen Chinese System.
// See ./libtabe/doc/BoPoMoFo.shtml
// Get libtabe at http://sourceforge.net/projects/libtabe/
// 1st Group 2nd Group 3rd Group Tone Symbols
// # of Symbols 21 3 13 5
// # of Bits 6 2 4 3
// TODO: Maybe support Extended Bopomofo in the future?
var BopomofoEncoder = {
// Unicode range of each of the Bopomofo symbol groups
// See https://en.wikipedia.org/wiki/Bopomofo_(script)#Unicode
BOPOMOFO_START_GROUP_1: 0x3105,
BOPOMOFO_END_GROUP_1: 0x3119,
BOPOMOFO_START_GROUP_2: 0x3127,
BOPOMOFO_END_GROUP_2: 0x3129,
BOPOMOFO_START_GROUP_3: 0x311A,
BOPOMOFO_END_GROUP_3: 0x3126,
// Tone symbols are placed in Spacing Modifier Letters Unicode block
BOPOMOFO_TONE_1: 0x02c9,
BOPOMOFO_TONE_2: 0x02ca,
BOPOMOFO_TONE_3: 0x02c7,
BOPOMOFO_TONE_4: 0x02cb,
BOPOMOFO_TONE_5: 0x02d9,
// Bitmask for each group
BOPOMOFO_GROUP_1_BITMASK: 0x7e00,
BOPOMOFO_GROUP_2_BITMASK: 0x0180,
BOPOMOFO_GROUP_3_BITMASK: 0x0078,
BOPOMOFO_TONE_BITMASK: 0x0007,
/**
* Encode a Bopomofo symbols string into encoded sounds.
* into encoded string.
* @param {string} symbols Symbols string.
* @param {object} options Options.
* - reorder: true for reorder.
* @return {array(number)} Encoded sounds array.
* @this BopomofoEncoder
*/
encode: function be_encode(symbols, options) {
options = options || {};
var encodedSoundsArr = [];
var currentEncodeSymbolsCode = 0;
var filled1 = false;
var filled2 = false;
var filled3 = false;
var filled4 = false;
var reorder = options.reorder;
var next = function next() {
encodedSoundsArr.push(currentEncodeSymbolsCode);
currentEncodeSymbolsCode = 0;
filled1 = filled2 = filled3 = filled4 = false;
};
for (var j = 0; j < symbols.length; j++) {
var encodedSymbolCode = this.encodeOne(symbols[j]);
if (encodedSymbolCode & this.BOPOMOFO_GROUP_1_BITMASK) {
if (!reorder && (filled1 || filled2 || filled3 || filled4)) {
next();
}
if (reorder && (filled1 || filled4)) {
next();
}
filled1 = true;
currentEncodeSymbolsCode |= encodedSymbolCode;
continue;
}
if (encodedSymbolCode & this.BOPOMOFO_GROUP_2_BITMASK) {
if (!reorder && (filled2 || filled3 || filled4)) {
next();
}
if (reorder && (filled2 || filled4)) {
next();
}
filled2 = true;
currentEncodeSymbolsCode |= encodedSymbolCode;
continue;
}
if (encodedSymbolCode & this.BOPOMOFO_GROUP_3_BITMASK) {
if (filled3 || filled4) {
next();
}
filled3 = true;
currentEncodeSymbolsCode |= encodedSymbolCode;
continue;
}
if (encodedSymbolCode & this.BOPOMOFO_TONE_BITMASK) {
filled4 = true;
currentEncodeSymbolsCode |= encodedSymbolCode;
continue;
}
throw new Error('Should not reach here.');
}
next();
return encodedSoundsArr;
},
/**
* Encode exactly one Bopomofo symbol
* @param {string} symbol Bopomofo symbol
* @return {number} Encoded code representing the symbol.
*/
encodeOne: function be_encodeOne(symbol) {
var symbolCode = symbol.charCodeAt(0);
if (symbolCode >= this.BOPOMOFO_START_GROUP_1 &&
symbolCode <= this.BOPOMOFO_END_GROUP_1) {
return (symbolCode - this.BOPOMOFO_START_GROUP_1 + 1) << 9;
}
if (symbolCode >= this.BOPOMOFO_START_GROUP_2 &&
symbolCode <= this.BOPOMOFO_END_GROUP_2) {
return (symbolCode - this.BOPOMOFO_START_GROUP_2 + 1) << 7;
}
if (symbolCode >= this.BOPOMOFO_START_GROUP_3 &&
symbolCode <= this.BOPOMOFO_END_GROUP_3) {
return (symbolCode - this.BOPOMOFO_START_GROUP_3 + 1) << 3;
}
if (symbolCode == this.BOPOMOFO_TONE_1) {
return 0x1;
}
if (symbolCode == this.BOPOMOFO_TONE_2) {
return 0x2;
}
if (symbolCode == this.BOPOMOFO_TONE_3) {
return 0x3;
}
if (symbolCode == this.BOPOMOFO_TONE_4) {
return 0x4;
}
if (symbolCode == this.BOPOMOFO_TONE_5) {
return 0x5;
}
throw new Error('Unknown symbol: ' + symbol);
},
/**
* Decode an encoded sounds string into Bopomofo symbols.
* @param {array(number)} encodedArr Encoded sounds string or array.
* @return {string} Symbols string.
* @this BopomofoEncoder
*/
decode: function be_decode(encodedArr) {
var symbols = '';
for (var i = 0; i < encodedArr.length; i++) {
var symbolsCode = encodedArr[i];
var group1Code = (symbolsCode & this.BOPOMOFO_GROUP_1_BITMASK) >> 9;
var group2Code = (symbolsCode & this.BOPOMOFO_GROUP_2_BITMASK) >> 7;
var group3Code = (symbolsCode & this.BOPOMOFO_GROUP_3_BITMASK) >> 3;
var toneCode = symbolsCode & this.BOPOMOFO_TONE_BITMASK;
if (group1Code) {
symbols +=
String.fromCharCode(this.BOPOMOFO_START_GROUP_1 - 1 + group1Code);
}
if (group2Code) {
symbols +=
String.fromCharCode(this.BOPOMOFO_START_GROUP_2 - 1 + group2Code);
}
if (group3Code) {
symbols +=
String.fromCharCode(this.BOPOMOFO_START_GROUP_3 - 1 + group3Code);
}
switch (toneCode) {
case 1:
symbols += String.fromCharCode(this.BOPOMOFO_TONE_1);
break;
case 2:
symbols += String.fromCharCode(this.BOPOMOFO_TONE_2);
break;
case 3:
symbols += String.fromCharCode(this.BOPOMOFO_TONE_3);
break;
case 4:
symbols += String.fromCharCode(this.BOPOMOFO_TONE_4);
break;
case 5:
symbols += String.fromCharCode(this.BOPOMOFO_TONE_5);
break;
}
}
return symbols;
},
/**
* Return true if the character or charCode given represents
* an accepted Bopomofo symbol.
* @param {string|number} chr String or a charCode.
* @return {boolean} Return true if the character or charCode given
* represents an accepted Bopomofo symbol.
* @this BopomofoEncoder
*/
isBopomofoSymbol: function be_isBopomofoSymbol(chr) {
var code = (typeof chr === 'string') ? chr.charCodeAt(0) : chr;
if (code >= this.BOPOMOFO_START_GROUP_1 &&
code <= this.BOPOMOFO_END_GROUP_1) {
return true;
}
if (code >= this.BOPOMOFO_START_GROUP_2 &&
code <= this.BOPOMOFO_END_GROUP_2) {
return true;
}
if (code >= this.BOPOMOFO_START_GROUP_3 &&
code <= this.BOPOMOFO_END_GROUP_3) {
return true;
}
if (code === this.BOPOMOFO_TONE_1 ||
code === this.BOPOMOFO_TONE_2 ||
code === this.BOPOMOFO_TONE_3 ||
code === this.BOPOMOFO_TONE_4 ||
code === this.BOPOMOFO_TONE_5) {
return true;
}
return false;
},
APPEND_MODE_NONE: 0,
APPEND_MODE_REORDER: 1,
appendToSymbols: function(symbols, symbolToAttach, mode) {
mode = mode || this.APPEND_MODE_NONE;
if (!this.isBopomofoSymbol(symbolToAttach)) {
throw new Error('BopomofoEncoder: ' +
'Symbol to attach is not a Bopomofo symbol.');
}
switch (mode) {
case this.APPEND_MODE_NONE:
return symbols + symbolToAttach;
case this.APPEND_MODE_REORDER:
// TODO: FIX THIS.
return this.decode(this.encode(symbols + symbolToAttach, {
reorder: true
}));
}
},
/**
* Construct and encoded sounds array that have all non-completed sounds
* expended as seperate symbols.
* Useful for getSymbolsCompositions() and also to decide the minimal # of
* composing elements of the symbols string.
*
* @param {string} symbols String of Bopomofo symbols.
* @returns {array(number)} Array consist of code of the
* encoded sounds.
*/
encodeExpended: function(symbols) {
var encodedSoundsReversedArr = [0];
var i = symbols.length;
var pos = 0;
var placeIntoCurrentSound = false;
var filled1, filled2, filled3;
var encodedSymbolCode;
while (i--) {
encodedSymbolCode = this.encodeOne(symbols[i]);
if (this.isCompleted(encodedSymbolCode)) {
placeIntoCurrentSound = true;
filled1 = filled2 = filled3 = false;
encodedSoundsReversedArr.push(encodedSymbolCode);
pos++;
} else {
if (placeIntoCurrentSound) {
if ((encodedSymbolCode & this.BOPOMOFO_GROUP_3_BITMASK) &&
!filled3 && !filled2 && !filled1) {
filled3 = true;
encodedSoundsReversedArr[pos] |= encodedSymbolCode;
} else if ((encodedSymbolCode & this.BOPOMOFO_GROUP_2_BITMASK) &&
!filled2 && !filled1) {
filled2 = true;
encodedSoundsReversedArr[pos] |= encodedSymbolCode;
} else if ((encodedSymbolCode & this.BOPOMOFO_GROUP_1_BITMASK) &&
!filled1) {
filled1 = true;
encodedSoundsReversedArr[pos] |= encodedSymbolCode;
} else {
placeIntoCurrentSound = false;
encodedSoundsReversedArr.push(encodedSymbolCode);
pos++;
}
} else {
encodedSoundsReversedArr.push(encodedSymbolCode);
pos++;
}
}
}
if (encodedSoundsReversedArr[0] === 0) {
encodedSoundsReversedArr.shift();
}
return encodedSoundsReversedArr.reverse();
},
/**
* Trim symbols to remove the part that will only match more words specify by
* length.
* @param {string|array(number)} symbols Symbols
* @param {number} length Length to trim.
* @return {string} Trimmed symbols.
*/
trimToLength: function(symbols, length) {
symbols = (typeof symbols === 'string') ? symbols : this.decode(symbols);
// No need to trim.
if (symbols.length <= length) {
return symbols;
}
var encodedSounds;
var i = symbols.length;
// XXX: Improve this; instead of a loop we could introduce a cap in
// the state machine in the encode() method.
do {
encodedSounds = this.encode(symbols.substr(0, i));
if (encodedSounds.length <= length) {
break;
}
} while (i--);
return this.decode(encodedSounds);
},
/**
* Trim symbols (from the end) to remove the part that will only match more
* words specify by length.
* @param {string|array(number)} symbols Symbols
* @param {number} length Length to trim.
* @return {string} Trimmed symbols.
*/
trimToLengthFromEnd: function(symbols, length) {
symbols = (typeof symbols === 'string') ? symbols : this.decode(symbols);
var encodedSounds;
var totalLength = this.encode(symbols).length;
var i = 0;
// XXX: Improve this; instead of a loop we could introduce a cap in
// the state machine in the encode() method.
while (++i < symbols.length) {
encodedSounds = this.encode(symbols.substr(0, i));
if (encodedSounds.length + length > totalLength) {
break;
}
}
return symbols.substr(i - 1);
},
/**
* Return all possible combinations of the given symbols string.
* @param {string|array(number)} symbols Symbol string or array
* returned from
* encodeExpended.
* @return {array(array(number))} All combinations of
* the given symbols string.
*/
getSymbolCombinations: function(symbols) {
// Construct an encodeExpended encoded sounds array,
// or use the input as such.
var expendedEncodedSounds =
(typeof symbols === 'string') ? this.encodeExpended(symbols) : symbols;
var combinations = expendedEncodedSounds
.reduce(function(currentCombinations, code, i) {
if (i === 0) {
currentCombinations.push([ code ]);
return currentCombinations;
}
currentCombinations.forEach(function(combination) {
var k = combination.length - 1;
// If the previous symbol is a complete one of this is a completed
// one, they must not occupy the same place.
if (this.isCompleted(combination[k]) || this.isCompleted(code)) {
combination.push(code);
return;
}
var filled2 = !!(combination[k] & this.BOPOMOFO_GROUP_2_BITMASK);
var filled3 = !!(combination[k] & this.BOPOMOFO_GROUP_3_BITMASK);
// If the previous one contain the same or the lower place of
// symbol, the new symbol must not occupy the same place.
if ((code & this.BOPOMOFO_GROUP_1_BITMASK) ||
(code & this.BOPOMOFO_GROUP_2_BITMASK &&
(filled2 || filled3)) ||
(code & this.BOPOMOFO_GROUP_3_BITMASK && filled3)) {
combination.push(code);
return;
}
// Create a copy of the composition, so that we could still
// put the symbols into the new place.
var newCombination = [].concat(combination);
newCombination.push(code);
currentCombinations.push(newCombination);
// Combine the current symbol into the previous symbol.
combination[k] |= code;
}.bind(this));
return currentCombinations;
}.bind(this), []);
return combinations;
},
isIncompletionOf: function(code, codeToMatch) {
var group1Code = (code & this.BOPOMOFO_GROUP_1_BITMASK) >> 9;
var group2Code = (code & this.BOPOMOFO_GROUP_2_BITMASK) >> 7;
var group3Code = (code & this.BOPOMOFO_GROUP_3_BITMASK) >> 3;
var toneCode = code & this.BOPOMOFO_TONE_BITMASK;
var group1CodeToMatch = (codeToMatch & this.BOPOMOFO_GROUP_1_BITMASK) >> 9;
var group2CodeToMatch = (codeToMatch & this.BOPOMOFO_GROUP_2_BITMASK) >> 7;
var group3CodeToMatch = (codeToMatch & this.BOPOMOFO_GROUP_3_BITMASK) >> 3;
var toneCodeToMatch = codeToMatch & this.BOPOMOFO_TONE_BITMASK;
// This is fairly complex because not only we have to consider mis-match
// of symbols at the same place, but also evaluate to false when there is
// a following up symbol but the previous one is missing.
// To future self: The best way to understand this is by reading test cases.
return (
!(group1CodeToMatch && !group1Code &&
(group2Code || group3Code || toneCode)) &&
!(group1Code && group1CodeToMatch !== group1Code) &&
!(group2CodeToMatch && !group2Code &&
(group3Code || toneCode)) &&
!(group2Code && group2CodeToMatch !== group2Code) &&
!(group3CodeToMatch && !group3Code && toneCode) &&
!(group3Code && group3CodeToMatch !== group3Code) &&
!(toneCode && toneCodeToMatch !== toneCode) &&
true);
},
isCompleted: function(code) {
// Only phontics with tone is considered completed.
return !!(code & this.BOPOMOFO_TONE_BITMASK);
},
replace: function(code, fromCode, toCode) {
var match = false;
if (fromCode & this.BOPOMOFO_GROUP_1_BITMASK) {
match = ((code & this.BOPOMOFO_GROUP_1_BITMASK) ===
(fromCode & this.BOPOMOFO_GROUP_1_BITMASK));
}
if (fromCode & this.BOPOMOFO_GROUP_2_BITMASK) {
match = ((code & this.BOPOMOFO_GROUP_2_BITMASK) ===
(fromCode & this.BOPOMOFO_GROUP_2_BITMASK));
}
if (fromCode & this.BOPOMOFO_GROUP_3_BITMASK) {
match = ((code & this.BOPOMOFO_GROUP_3_BITMASK) ===
(fromCode & this.BOPOMOFO_GROUP_3_BITMASK));
}
if (fromCode & this.BOPOMOFO_TONE_BITMASK) {
match = ((code & this.BOPOMOFO_TONE_BITMASK) ===
(fromCode & this.BOPOMOFO_TONE_BITMASK));
}
if (!match) {
return code;
}
return code & ~fromCode | toCode;
}
};
// Export as a CommonJS module if we are loaded as one.
if (typeof module === 'object' && module.exports) {
module.exports = BopomofoEncoder;
}

View file

@ -0,0 +1,78 @@
'use strict';
/**
* DataLoader wraps XMLHTTPRequest and pull the JSZhuyin dictionary if it is
* not passed into the setup.
*/
var DataLoader = function() {
this.loaded = false;
this.loading = false;
};
/**
* Database file to load.
* @type {string}
*/
DataLoader.prototype.DATA_URL = '../data/database.data';
/**
* Once loaded, data will be available here.
* @type {object}
*/
DataLoader.prototype.data = null;
/**
* Run when the loading is complete.
* @type {function}
*/
DataLoader.prototype.onloadend = null;
/**
* Run when loading is successful.
* @type {function}
*/
DataLoader.prototype.onload = null;
/**
* Run when error occours.
* @type {function}
*/
DataLoader.prototype.onerror = null;
/**
* Load the data
*/
DataLoader.prototype.load = function bs_load() {
if (this.loading) {
throw new Error('DataLoader: You cannot call load() twice.');
}
var xhr = new XMLHttpRequest();
xhr.open('GET', this.DATA_URL);
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function xhrReadystatechange() {
if (xhr.readyState !== xhr.DONE) {
return;
}
var data = this.data = xhr.response;
if (!data || (xhr.status && xhr.status !== 200)) {
this.loading = false;
if (typeof this.onerror === 'function') {
this.onerror();
}
if (typeof this.onloadend === 'function') {
this.onloadend();
}
return;
}
this.loaded = true;
this.loading = false;
if (typeof this.onload === 'function') {
this.onload();
}
if (typeof this.onloadend === 'function') {
this.onloadend();
}
}.bind(this);
xhr.send();
};

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,363 @@
'use strict';
// This implements a packer/unpacker of the arraybuffer we get from storage.
// We would like to keep structured data in the JS world as few as possible to
// conserve memory and GC time.
/**
* Float32Encoder encodes a given float number to an arraybuffer,
* and vise versa.
* @type {Object}
*/
var Float32Encoder = {
isSupported: (typeof DataView !== 'undefined'),
BUFFER_BYTE_LENGTH: 4,
encode: function encodeFloat32Number(number, type) {
type = type || 'arraybuffer';
switch (type) {
case 'arraybuffer':
return this.encodeArrayBuffer(number);
default:
throw new Error('Unsupported encode to type.');
}
},
encodeArrayBuffer: function Float32NumberToArrayBuffer(number) {
if (typeof number !== 'number') {
throw new Error('Argument received is not a number.');
}
var buf = new ArrayBuffer(4);
new DataView(buf).setFloat32(0, number, true);
return buf;
},
decode: function decodeFloat32Number(data) {
switch (data.constructor) {
case ArrayBuffer:
return this.decodeArrayBuffer(data);
default:
throw new Error('Unsupported data type.');
}
},
decodeArrayBuffer: function ArrayBufferToFloat32Number(buffer, byteOffset) {
return (new DataView(buffer)).getFloat32(byteOffset, true);
}
};
var JSZhuyinDataPackCollection = function(dataPacks) {
this.dataPacks = dataPacks;
if (!dataPacks.length) {
throw new Error('JSZhuyinDataPackCollection: ' +
'Expects an non-empty array.');
}
this.firstResultDataPack = null;
this.results = [];
};
JSZhuyinDataPackCollection.prototype.getFirstResultScore = function() {
if (this.results.length) {
return this.results[0].score;
}
if (!this.firstResultDataPack) {
this._getFirstResultDataPack();
}
return this.firstResultDataPack.getFirstResultScore();
};
JSZhuyinDataPackCollection.prototype.getFirstResult = function() {
if (this.results.length) {
return this.results[0];
}
if (!this.firstResultDataPack) {
this._getFirstResultDataPack();
}
return this.firstResultDataPack.getFirstResult();
};
JSZhuyinDataPackCollection.prototype._getFirstResultDataPack = function() {
if (this.dataPacks.length === 1) {
this.firstResultDataPack = this.dataPacks[0];
return;
}
var score = -Infinity;
this.dataPacks.forEach(function(dataPack, i) {
if (dataPack.getFirstResultScore() > score) {
this.firstResultDataPack = dataPack;
score = dataPack.getFirstResultScore();
}
}.bind(this));
};
JSZhuyinDataPackCollection.prototype.getResults = function() {
if (this.results.length) {
return this.results;
}
if (this.dataPacks.length === 1) {
this.results = this.dataPacks[0].getResults();
return this.results;
}
var results = [];
// TODO: Optimize the sorting here.
this.dataPacks.forEach(function(dataPack, i) {
var resArr = dataPack.getResults();
resArr.forEach(function(res) {
var found = results.some(function(currentRes) {
return (currentRes.str === res.str);
});
if (!found) {
results.push(res);
}
});
}.bind(this));
results = results.sort(function(a, b) {
return (b.score - a.score);
});
this.results = results;
return results;
};
/**
* JSZhuyinDataPack instance is the representation of the data in the database.
* @param {arraybuffer|array} imeData arraybuffer the packed data, or an
* array of the structured data.
* @constructor
*/
var JSZhuyinDataPack = function(imeData, byteOffset, length, index) {
if (imeData.constructor === ArrayBuffer) {
this.packed = imeData;
this.byteOffset = byteOffset || 0;
this.length = length || (imeData.byteLength >> 1);
this.unpacked = undefined;
} else if (Array.isArray(imeData)) {
this.packed = undefined;
this.unpacked = imeData;
} else {
this.packed = undefined;
this.unpacked = undefined;
}
this.index = index;
};
/**
* Get the score of the first item.
* The arraybuffer will not be unpacked.
* @return {number} Score.
*/
JSZhuyinDataPack.prototype.getFirstResultScore = function() {
if (this.unpacked) {
return this.unpacked[0].score;
}
return Float32Encoder.decodeArrayBuffer(this.packed, this.byteOffset + 2);
};
/**
* Get the first item.
* The arraybuffer will not be unpacked.
* @return {object} The first item.
*/
JSZhuyinDataPack.prototype.getFirstResult = function() {
if (this.unpacked) {
return this.unpacked[0];
}
var view = new DataView(this.packed, this.byteOffset, this.length << 1);
var controlByte = view.getUint16(0, true);
var length = controlByte & 0x0f;
var result = {
str: this._getStringFromDataView(view, 3 << 1, length),
score: this.getFirstResultScore(),
index: this.index
};
return result;
};
/**
* Get all items in an array. The arraybuffer will be unpacked automatically.
* @return {array({str: string, score: number})} Results.
*/
JSZhuyinDataPack.prototype.getResults = function() {
this.unpack();
return this.unpacked;
};
/**
* Get items in the packed result but only with the ones begins with
* giving string.
* @param {string} str String to match.
* @return {array({str: string, score: number})} Results.
*/
JSZhuyinDataPack.prototype.getResultsBeginsWith = function(str) {
var filterFn = function(res) {
return (res.str.substr(0, str.length) === str);
};
if (this.unpacked) {
return this.unpacked.filter(filterFn);
}
return this._getPackedResults(filterFn);
};
/**
* Get the packed array buffer. The data will be packed automatically.
* The byteLength is always dividable by 4.
* @return {arraybuffer} The packed arraybuffer.
*/
JSZhuyinDataPack.prototype.getPacked = function() {
this.pack();
return this.packed;
};
/**
* Unpack the arraybuffer and remove it.
*/
JSZhuyinDataPack.prototype.unpack = function() {
if (this.unpacked) {
return;
}
if (typeof this.packed === 'undefined') {
throw new Error('No packed IME data.');
}
this.unpacked = this._getPackedResults();
this.packed = undefined;
};
/**
* Go through the packed buffer and extract the results.
* @param {function} filterFn Test function for testing results. Optional.
* @return {array({str: string, score: number})} Results.
*/
JSZhuyinDataPack.prototype._getPackedResults = function(filterFn) {
var results = [];
var view = new DataView(this.packed, this.byteOffset, this.length << 1);
var controlByte = view.getUint16(0, true);
var length = controlByte & 0x0f;
var bytePos = 2;
while (bytePos < view.byteLength) {
// Ensure we'll not be getting out of range error.
if ((bytePos + 4 + (length << 1)) > view.byteLength) {
break;
}
var result = {
str: this._getStringFromDataView(view, bytePos + 4, length),
score: Float32Encoder.decodeArrayBuffer(
this.packed, this.byteOffset + bytePos),
index: this.index
};
bytePos += (length + 2) << 1;
if (typeof filterFn === 'function' && !filterFn(result)) {
continue;
}
results.push(result);
}
return results;
};
/**
* Pack the arraybuffer and remove the structured data object.
*/
JSZhuyinDataPack.prototype.pack = function() {
if (this.packed) {
return;
}
if (typeof this.unpacked === 'undefined') {
throw new Error('No unpacked IME data.');
}
var length = 0;
this.unpacked.forEach(function(result, i) {
if (result.str.length > length) {
length = result.str.length;
}
});
if (length > 0xf) {
throw new Error(
'JSZhuyinDataPack: Longest string length is longer than expected.');
}
var arrayLength = 1 + (length + 2) * this.unpacked.length;
var packedView = new DataView(new ArrayBuffer(arrayLength << 1));
// 0x40 puts the control byte into printable characters region.
packedView.setUint16(0, 0x40 ^ length, true);
var bytePos = 1 << 1;
this.unpacked.forEach(function(result, i) {
packedView.setFloat32(bytePos, result.score, true);
bytePos += (2 << 1);
this._setStringToDataView(packedView, bytePos, result.str);
bytePos += (length << 1);
}, this);
this.packed = packedView.buffer;
this.byteOffset = 0;
this.length = arrayLength;
};
/**
* Overwrite the native toString() method.
* @return {string} String representation of the JSZhuyinDataPack instance.
*/
JSZhuyinDataPack.prototype.toString = function() {
if (this.unpacked) {
return this.unpacked.toString();
}
if (this.packed) {
return this.packed.toString();
}
return '[object JSZhuyinDataPack]';
};
/**
* Get the string from given DataView instance.
*/
JSZhuyinDataPack.prototype._getStringFromDataView =
function(view, byteOffset, length) {
var charCodes = [], charCode;
for (var i = 0; i < length; i++) {
charCode = view.getUint16(byteOffset + (i << 1), true);
if (charCode) {
charCodes.push(charCode);
}
}
return String.fromCharCode.apply(String, charCodes);
};
/**
* Set the string to given DataView instance.
*/
JSZhuyinDataPack.prototype._setStringToDataView =
function(view, byteOffset, str) {
var i = 0;
while (i < str.length) {
view.setUint16(byteOffset + (i << 1), str.charCodeAt(i), true);
i++;
}
};
// Export as a CommonJS module if we are loaded as one.
if (typeof module === 'object' && module.exports) {
module.exports = JSZhuyinDataPack;
}

View file

@ -0,0 +1,52 @@
'use strict';
/* global JSZhuyin */
// JSZhuyinServer provides a postMessage interface to communicate
// with JSZhuyin.
var JSZhuyinServer = {
engine: null,
messageTarget: self.opener || self.parent || self,
load: function w_load() {
self.addEventListener('message', this);
var engine = this.engine = new JSZhuyin();
(['loadend',
'load',
'error',
'unload',
'compositionupdate',
'compositionend',
'candidateschange',
'actionhandled']).forEach((function attachCallback(type) {
engine['on' + type] = this.getSender(type);
}).bind(this));
},
getSender: function w_getSender(type) {
return this.sendMessage.bind(this, type);
},
handleEvent: function w_handleEvent(evt) {
var msg = evt.data;
if (msg.sender === 'worker') {
return;
}
if (typeof this.engine[msg.type] !== 'function') {
throw 'Unknown message type: ' + msg.type;
}
this.engine[msg.type](msg.data, msg.requestId);
},
sendMessage: function w_sendMessage(type, data, reqId) {
if (typeof window === 'object') {
this.messageTarget.postMessage(
{ 'type': type, 'data': data,
'requestId': reqId, 'sender': 'worker' }, '*');
} else {
this.messageTarget.postMessage(
{ 'type': type, 'data': data, 'requestId': reqId });
}
}
};
JSZhuyinServer.load();

View file

@ -0,0 +1,493 @@
'use strict';
/* global JSZhuyinDataPack, JSZhuyinDataPackCollection, BopomofoEncoder */
// This file implemented BinStorage, an in-memory key-value storage (for now).
// Additional sugar is put onto JSZhuyinDataPackStorage to more effectively
// manage JSZhuyin-specific data.
/**
* A simple key-value store wrapps with JavaScript object.
* @this {object} CacheStore instance.
*/
var CacheStore = function CacheStore() {
this.dataMap = new Map();
};
/**
* set the value of a key.
* @param {array(number)} codes Key.
* @param {any} value Value.
* @this {object} CacheStore instance.
*/
CacheStore.prototype.add = function cs_add(codes, value) {
// In the interest of not doing deep comparsion on arrays, we would convert
// them to string here.
this.dataMap.set(String.fromCharCode.apply(String, codes), value);
};
/**
* get the value of a key.
* @param {array(number)} codes Key.
* @return {any} Value of the key.
* @this {object} CacheStore instance.
*/
CacheStore.prototype.get = function cs_get(codes) {
return this.dataMap.get(String.fromCharCode.apply(String, codes));
};
/**
* Clean up the store. Any key that is not a substring of the superset
* string will have their value removed.
*
* @param {array(number)} supersetCodes The superset code array.
* @this {object} CacheStore instance.
*/
CacheStore.prototype.cleanup = function cs_cleanup(supersetCodes) {
if (!supersetCodes) {
this.dataMap.clear();
return;
}
var supersetStr = String.fromCharCode.apply(String, supersetCodes);
this.dataMap.forEach(function(v, key) {
if (supersetStr.indexOf(key) === -1) {
this.dataMap.delete(key);
}
}, this);
};
/**
* BinStorage rely on a big chunk of encoded ArrayBuffer to do the lookup.
* @constructor
*/
var BinStorage = function BinStorage() {
this.loaded = false;
this._bin = undefined;
};
/**
* Load the database.
* @this {object} BinStorage instance.
*/
BinStorage.prototype.load = function bs_load(data) {
if (this.loaded) {
this.unload();
}
this.loaded = true;
this._bin = data;
};
/**
* Unoad the database.
* @this {object} BinStorage instance.
*/
BinStorage.prototype.unload = function bs_unload() {
this._bin = undefined;
this.loaded = false;
};
/**
* Get values from storage.
* @param {array(number)} codes Array of numbers to query.
* @return {arraybuffer} the result.
*/
BinStorage.prototype.get = function bs_get(codes) {
if (!this.loaded) {
throw new Error('BinStorage: not loaded.');
}
var code;
var byteOffset = 0;
while ((code = codes.shift()) !== undefined) {
byteOffset = this.searchBlock(code, byteOffset);
if (byteOffset === -1) {
return undefined;
}
}
return this._getBlockContent(byteOffset);
};
/**
* Look for all value begin with this key.
* @param {array(number)} codes Array of numbers to query.
* @return {array(array(...))} Array of results of _getBlockContent
*/
BinStorage.prototype.getRange = function bs_getRange(codes) {
if (!this.loaded) {
throw new Error('BinStorage: not loaded.');
}
var code;
var byteOffset = 0;
while ((code = codes.shift()) !== undefined) {
byteOffset = this.searchBlock(code, byteOffset);
if (byteOffset === -1) {
return [];
}
}
return this.getRangeFromContentIndex(byteOffset);
};
/**
* Get all of the values from this address.
* @param {numbber} byteOffset Byte offset of the block.
* @return {array(array(...))} Array of results of _getBlockContent
*/
BinStorage.prototype.getRangeFromContentIndex = function(byteOffset) {
var bin = this._bin;
var result = [];
var getBlockContents = function bs_getBlockContents(byteOffset) {
var view = new DataView(bin, byteOffset);
var length = view.getUint16(0, true);
var contentLength = view.getUint16(2, true);
if (length === 0) {
return;
}
var addressBlockByteOffset =
byteOffset + ((2 + contentLength + length) << 1);
var addressBlockView =
new DataView(bin, addressBlockByteOffset, length << 2);
var i = length;
while (i--) {
var blockAddress = addressBlockView.getUint32(i << 2, true);
var content = this._getBlockContent(blockAddress);
if (content) {
result.push(content);
}
getBlockContents(blockAddress);
}
}.bind(this);
getBlockContents(byteOffset);
return result;
};
/**
* Internal method for search a given block for a single character.
* @param {number} code code of the character.
* @param {numbber} byteOffset Byte offset of the block.
* @return {number} byteOffset of the block found, or -1.
*/
BinStorage.prototype.searchBlock = function bs_searchBlock(code, byteOffset) {
var bin = this._bin;
var view = new DataView(bin, byteOffset);
var length = view.getUint16(0, true);
var contentLength = view.getUint16(2, true);
var keyBlockByteOffset =
byteOffset + ((2 + contentLength) << 1);
var addressBlockByteOffset =
byteOffset + ((2 + contentLength + length) << 1);
var keyBlockView = new DataView(bin, keyBlockByteOffset, length << 1);
var addressBlockView = new DataView(bin, addressBlockByteOffset, length << 2);
// Do a interpolation search
var low = 0;
var high = length - 1;
var mid;
var lowCode, highCode, midCode;
while (low < length &&
(lowCode = keyBlockView.getUint16(low << 1, true)) <= code &&
(highCode = keyBlockView.getUint16(high << 1, true)) >= code) {
mid = low + (((code - lowCode) * (high - low)) / (highCode - lowCode)) | 0;
midCode = keyBlockView.getUint16(mid << 1, true);
if (midCode < code) {
low = mid + 1;
} else if (midCode > code) {
high = mid - 1;
} else {
return addressBlockView.getUint32(mid << 2, true);
}
}
if (lowCode === code) {
return addressBlockView.getUint32(low << 2, true);
} else {
return -1;
}
};
/**
* Internal method for getting the content of the block.
* @param {numbber} byteOffset Byte offset of the block.
* @return {array} array contain 4 elements
* - First element is the reference to the arrayBuffer
* - Second element is the byteOffset the content begins.
* - Third element is the length of the content.
* - Forth element is the address of the block.
*/
BinStorage.prototype._getBlockContent =
function bs_getBlockContent(byteOffset) {
var bin = this._bin;
var view = new DataView(bin, byteOffset);
var contentLength = view.getUint16(2, true);
if (contentLength === 0) {
return undefined;
}
return [bin, byteOffset + (2 << 1), contentLength, byteOffset];
};
var JSZhuyinDataPackStorage = function() {
this.incompleteMatchedCache = new CacheStore();
this.getCache = new CacheStore();
this._interchangeablePairs = '';
this._interchangeablePairsArr = new Uint16Array(0);
};
JSZhuyinDataPackStorage.prototype = Object.create(BinStorage.prototype);
/**
* When searching for matching words/phrases, consider these pairs of symbols
* are interchangables.
* Must be a string representing 2n sounds in Bopomofo symbols.
*
* Example string: 'ㄣㄥㄌㄖㄨㄛㄡ', making ㄣ interchangable with ㄥ and
* ㄌ interchangable with ㄖ, and ㄨㄛ with ㄡ.
*
* @param {string} str String representing the pairs.
*/
JSZhuyinDataPackStorage.prototype.setInterchangeablePairs = function(str) {
if (str === this._interchangeablePairs) {
return;
}
// String has changed, invalidate the entire cache.
this.incompleteMatchedCache.cleanup();
var encodedSounds = BopomofoEncoder.encode(str);
if (encodedSounds.length % 2) {
throw new Error('JSZhuyinDataPackStorage: Expects string to store pairs.');
}
var arr = new Uint16Array(encodedSounds);
this._interchangeablePairs = str;
this._interchangeablePairsArr = arr;
};
/**
* Get values from storage, disregards interchangeable pairs.
* @param {array(number)} codes Array of numbers to query.
* @return {JSZhuyinData} The resulting JSZhuyinData instance.
*/
JSZhuyinDataPackStorage.prototype.get = function(codes) {
// Should be null or the previous instance.
if (typeof this.getCache.get(codes) === 'object') {
return this.getCache.get(codes);
}
var result = BinStorage.prototype.get.call(this, codes);
if (result) {
// This is equal to |new JSZhuyinDataPack(..)| but take array of arugments
var dataPack = Object.create(JSZhuyinDataPack.prototype);
JSZhuyinDataPack.apply(dataPack, result);
this.getCache.add(codes, dataPack);
return dataPack;
} else {
this.getCache.add(codes, null);
return null;
}
};
/**
* Look for all value begin from this block address.
* @param {numbber} byteOffset Byte offset of the block.
* @return {array(JSZhuyinData)} The resulting JSZhuyinData instances.
*/
JSZhuyinDataPackStorage.prototype.getRangeFromContentIndex = function() {
return BinStorage.prototype.getRangeFromContentIndex.apply(this, arguments)
.map(function(result) {
var dataPack = Object.create(JSZhuyinDataPack.prototype);
JSZhuyinDataPack.apply(dataPack, result);
return dataPack;
});
};
/**
* Return all results that matches to given partial encoded Bopomofo string.
* @param {array(number)} codes string to query.
* @return {JSZhuyinDataPackCollection?}
* A JSZhuyinDataPackCollection instance
* representing the results.
*/
JSZhuyinDataPackStorage.prototype.getIncompleteMatched = function(codes) {
if (!this.loaded) {
throw new Error('JSZhuyinDataPackStorage: not loaded.');
}
// Should be null or the previous instance.
if (typeof this.incompleteMatchedCache.get(codes) === 'object') {
return this.incompleteMatchedCache.get(codes);
}
// addresses is the reduced result of checking the codes in sequences,
// start by looking at matched keys at table 0.
var addresses = codes
.reduce(function(addresses, code) {
// Thie reduces to the every addresses that matches to the code, starting
// from empty array.
// Return back the reduced result and replaces the previous array of
// addresses.
return addresses.reduce(function(codeAddresses, address) {
// If the code represents a completed sound, we should
// use searchBlock() instead of doing linear workathough with
// _getIncompleteMatchedCodesInBlock() since there can be only
// one match.
var a;
if (!this._interchangeablePairs &&
BopomofoEncoder.isCompleted(code)) {
a = this.searchBlock(code, address);
if (a !== -1) {
return codeAddresses.concat(a);
}
return codeAddresses;
}
// Depend on the setting, the result should be matched either by
// trying to split the encoded sound into multiple encoded sounds,
// or not.
var results =
this._getIncompleteMatchedSingleCodesInBlock(code, address);
// Matched addresses are appended into the current list.
return codeAddresses.concat(results);
}.bind(this), /* codeAddresses */ []);
}.bind(this), /* addresses */ [0]);
// DataPacks are constructed by getting the content of these addresses,
// remove the null results, and construct with the content argument array.
var dataPacks = addresses
.map(function(address) {
return this._getBlockContent(address);
}, this)
.filter(function(result) {
return !!result;
})
.map(function(result) {
var dataPack = Object.create(JSZhuyinDataPack.prototype);
JSZhuyinDataPack.apply(dataPack, result);
return dataPack;
});
if (!dataPacks.length) {
this.incompleteMatchedCache.add(codes, null);
return null;
}
var dataPackCollection = new JSZhuyinDataPackCollection(dataPacks);
this.incompleteMatchedCache.add(codes, dataPackCollection);
return dataPackCollection;
};
/**
* Internal method to get all the addresses point from a given block that
* matches the code with BopomofoEncoder.isIncompletionOf().
* @param {number} code code of the character.
* @param {numbber} byteOffset Byte offset of the block.
* @return {array(number)} Array of byte offsets
*/
JSZhuyinDataPackStorage.prototype._getIncompleteMatchedSingleCodesInBlock =
function jdps__getIncompleteMatchedSingleCodesInBlock(code, byteOffset) {
var bin = this._bin;
var view = new DataView(bin, byteOffset);
var length = view.getUint16(0, true);
var contentLength = view.getUint16(2, true);
var keyBlockByteOffset =
byteOffset + ((2 + contentLength) << 1);
var addressBlockByteOffset =
byteOffset + ((2 + contentLength + length) << 1);
var keyBlockView = new DataView(bin, keyBlockByteOffset, length << 1);
var addressBlockView = new DataView(bin, addressBlockByteOffset, length << 2);
code = this._replaceInterchangeableSymbols(code);
var addresses = [], c;
for (var i = 0; i < length; i++) {
c = this._replaceInterchangeableSymbols(
keyBlockView.getUint16(i << 1, true));
if (BopomofoEncoder.isIncompletionOf(code, c)) {
addresses.push(addressBlockView.getUint32(i << 2, true));
}
}
return addresses;
};
/**
* Attempt to find the sounds for a set of characters.
* Only search for the 0th block (one-sound entries), does not consider
* word-phrases.
* Very very slow operation.
* @param {String} str Characters to search
* @return {String} Encoded sounds of Bopomofo. \u0000 for sounds can't be
* found.
*/
JSZhuyinDataPackStorage.prototype.reverseGet = function(str) {
// Construct an buffer of the same length and filled with zero.
var res = [];
// Since the Go through the zero-th block
var bin = this._bin;
var view = new DataView(bin);
var length = view.getUint16(0, true);
var contentLength = view.getUint16(2, true);
var keyBlockByteOffset = (2 + contentLength) << 1;
var addressBlockByteOffset = (2 + contentLength + length) << 1;
var keyBlockView = new DataView(bin, keyBlockByteOffset, length << 1);
var addressBlockView = new DataView(bin, addressBlockByteOffset, length << 2);
var j, result, dataPack;
for (var i = 0; i < length; i++) {
result = this._getBlockContent(addressBlockView.getUint32(i << 2, true));
if (!result) {
continue;
}
dataPack = Object.create(JSZhuyinDataPack.prototype);
JSZhuyinDataPack.apply(dataPack, result);
for (j = 0; j < str.length; j++) {
if (dataPack.getResultsBeginsWith(str[j]).length) {
res[j] = keyBlockView.getUint16(i << 1, true);
}
}
}
return res;
};
/**
* Clean up the get() cache. Any key that is not a substring of the superset
* string will have their value removed.
* @param {array(number)?} supersetCodes The superset code array.
*/
JSZhuyinDataPackStorage.prototype.cleanupCache = function(supersetCodes) {
this.incompleteMatchedCache.cleanup(supersetCodes);
};
/**
* Internal method to reduce the bits in code to be 2nd symbols of each pair,
* in the interchange symbols property, if 1st symbols present in the phontics.
* @param {number} c One phontic represented in number
* @return {number} Converted number.
*/
JSZhuyinDataPackStorage.prototype._replaceInterchangeableSymbols = function(c) {
if (!this._interchangeablePairs) {
return c;
}
var arr = this._interchangeablePairsArr;
var n = arr.length;
for (var i = 0; i < n; i += 2) {
c = BopomofoEncoder.replace(c, arr[i], arr[i + 1]);
}
return c;
};

View file

@ -0,0 +1,9 @@
'use strict';
/**
* Load JSZhuyin, etc. into a Web Workers.
*
*/
importScripts('bopomofo_encoder.js', 'jszhuyin_data_pack.js',
'storage.js', 'data_loader.js', 'jszhuyin.js', 'jszhuyin_server.js');

View file

@ -0,0 +1,48 @@
#!/bin/bash
#####################################################
#
# Run this file to pull JSZhuyin from upstream.
# Should be replaced once Gaia got |repo| power.
#
# The upstream code is hosted at
# https://github.com/timdream/jszhuyin
#
#####################################################
cd `dirname $0`
if [ -z $JSZHUYIN_DIR ]; then
echo 'Error: Please specify JSZHUYIN_DIR.'
exit
fi
if [ ! -d $JSZHUYIN_DIR/lib ]; then
echo 'Error: JSZhuyin file not found.'
exit
fi
if [ ! -d $JSZHUYIN_DIR/data ]; then
echo 'Error: No JSZhuyin data. Did you run make to generate the database there?'
exit
fi
echo 'Copying JSZhuyin code and data to Gaia ...'
# Remove all files
rm -rf ./lib ./data
# Copy only necessary files
mkdir lib data
cp $JSZHUYIN_DIR/lib/bopomofo_encoder.js ./lib/
cp $JSZHUYIN_DIR/lib/jszhuyin.js ./lib/
cp $JSZHUYIN_DIR/lib/storage.js ./lib/
cp $JSZHUYIN_DIR/lib/data_loader.js ./lib/
cp $JSZHUYIN_DIR/lib/jszhuyin_data_pack.js ./lib/
cp $JSZHUYIN_DIR/lib/jszhuyin_server.js ./lib/
cp $JSZHUYIN_DIR/lib/worker.js ./lib/
cp $JSZHUYIN_DIR/data/data-commit-hash ./data/
cp $JSZHUYIN_DIR/data/database.data ./data/
echo 'Done.'

View file

@ -0,0 +1 @@
node_modules/

View file

@ -0,0 +1,61 @@
# After building a dictionary here, move the resulting .dict file to
# apps/keyboard/js/imes/latin/dictionaries/ and check it in.
#
# Available:
# af.dict, bg.dict, bs.dict, ca.dict, cs.dict, da.dict, de.dict, el.dict, en.dict, en_gb.dict, en_us.dict, es.dict, eu.dict, ff.dict,
# fi.dict, fr.dict, fy.dict, ga.dict, gd.dict, gl.dict, gv.dict, hr.dict, hu.dict, it.dict, lt.dict, lv.dict, nb.dict, nl.dict, pl.dict,
# pt_br.dict, pt_pt.dict, ro.dict, ru.dict, sk.dict, sl.dict, sq.dict, sr.dict, sr-Cyrl.dict, sr-Latn.dict, sr.dict, sv.dict, tr.dict
WHITELIST = \
af.dict \
bg.dict \
bs.dict \
ca.dict \
cs.dict \
cy.dict \
da.dict \
de.dict \
el.dict \
en.dict \
en_gb.dict \
en_us.dict \
es.dict \
eu.dict \
fi.dict \
ff.dict \
fr.dict \
fy.dict \
ga.dict \
gd.dict \
gl.dict \
gv.dict \
hr.dict \
hu.dict \
it.dict \
lt.dict \
lv.dict \
pl.dict \
pt_br.dict \
nb.dict \
nl.dict \
pl.dict \
pt_br.dict \
pt_pt.dict \
ro.dict \
ru.dict \
sk.dict \
sl.dict \
sq.dict \
sr.dict \
sr-Cyrl.dict \
sr-Latn.dict \
sv.dict \
tr.dict
%.dict: %_wordlist.xml xml2dict.js dependency
node --harmony xml2dict.js -o $@ $<
dependency:
npm install
all: $(WHITELIST)

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Binary file not shown.

File diff suppressed because it is too large Load diff

Some files were not shown because too many files have changed in this diff Show more