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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#!/usr/bin/env gjs
/*
=============================================================================
*****************************************************************************
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 https://mozilla.org/MPL/2.0/.
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
Mehdi Hasan Khan <mhasan@omicronlab.com>
Copyright (C) OmicronLab (http://www.omicronlab.com). All Rights Reserved.
Contributor(s): ______________________________________.
*****************************************************************************
=============================================================================
*/
const gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gda = imports.gi.Gda;
function DB () {}
var database = {};
var suffixDict = {};
DB.prototype = {
loadDb: function () {
this._init();
//TODO: Change path
this.connection = new Gda.Connection ({provider: Gda.Config.get_provider("SQLite"),
cnc_string:"DB_DIR=" + GLib.get_current_dir() + ";DB_NAME=Database.db3"});
this.connection.open ();
//Vowels
this._loadOneTable('A', database.w_a);
this._loadOneTable('AA', database.w_aa);
this._loadOneTable('I', database.w_i);
this._loadOneTable('II', database.w_ii);
this._loadOneTable('U', database.w_u);
this._loadOneTable('UU', database.w_uu);
this._loadOneTable('RRI', database.w_rri);
this._loadOneTable('E', database.w_e);
this._loadOneTable('OI', database.w_oi);
this._loadOneTable('O', database.w_o);
this._loadOneTable('OU', database.w_ou);
//Consonants
this._loadOneTable('B', database.w_b);
this._loadOneTable('BH', database.w_bh);
this._loadOneTable('C', database.w_c);
this._loadOneTable('CH', database.w_ch);
this._loadOneTable('D', database.w_d);
this._loadOneTable('Dd', database.w_dd);
this._loadOneTable('Dh', database.w_dh);
this._loadOneTable('Ddh', database.w_ddh);
this._loadOneTable('G', database.w_g);
this._loadOneTable('Gh', database.w_gh);
this._loadOneTable('H', database.w_h);
this._loadOneTable('J', database.w_j);
this._loadOneTable('Jh', database.w_jh);
this._loadOneTable('K', database.w_k);
this._loadOneTable('Kh', database.w_kh);
this._loadOneTable('L', database.w_l);
this._loadOneTable('M', database.w_m);
this._loadOneTable('N', database.w_n);
this._loadOneTable('NN', database.w_nn);
this._loadOneTable('NGA', database.w_nga);
this._loadOneTable('NYA', database.w_nya);
this._loadOneTable('P', database.w_p);
this._loadOneTable('Ph', database.w_ph);
this._loadOneTable('R', database.w_r);
this._loadOneTable('Rr', database.w_rr);
this._loadOneTable('Rrh', database.w_rrh);
this._loadOneTable('S', database.w_s);
this._loadOneTable('Sh', database.w_sh);
this._loadOneTable('Ss', database.w_ss);
this._loadOneTable('T', database.w_t);
this._loadOneTable('TT', database.w_tt);
this._loadOneTable('TH', database.w_th);
this._loadOneTable('TTH', database.w_tth);
this._loadOneTable('Y', database.w_y);
this._loadOneTable('Z', database.w_z);
this._loadOneTable('Khandatta', database.w_khandatta);
this._loadSuffix();
this.connection.close ();
},
unloadDb: function () {
this._init();
},
_loadSuffix: function(){
if (this.connection){
if (this.connection.is_opened){
var dm = this.connection.execute_select_command ("select * from Suffix");
var iter = dm.create_iter();
while (iter.move_next ()) {
suffixDict[Gda.value_stringify (iter.get_value_at (0))] = Gda.value_stringify (iter.get_value_at (1));
}
}
}
},
_loadOneTable: function (tableName, wArray) {
if (this.connection){
if (this.connection.is_opened){
var dm = this.connection.execute_select_command ("select * from " + tableName);
var iter = dm.create_iter();
while (iter.move_next ()) {
var w = Gda.value_stringify (iter.get_value_at (0));
wArray.push(w);
}
}
}
},
_init: function () {
database = {};
suffixDict = {};
database.w_a = [];
database.w_aa = [];
database.w_i = [];
database.w_ii = [];
database.w_u = [];
database.w_uu = [];
database.w_rri = [];
database.w_e = [];
database.w_oi = [];
database.w_o = [];
database.w_ou = [];
database.w_b = [];
database.w_bh = [];
database.w_c = [];
database.w_ch = [];
database.w_d = [];
database.w_dh = [];
database.w_dd = [];
database.w_ddh = [];
database.w_g = [];
database.w_gh = [];
database.w_h = [];
database.w_j = [];
database.w_jh = [];
database.w_k = [];
database.w_kh = [];
database.w_l = [];
database.w_m = [];
database.w_n = [];
database.w_nga = [];
database.w_nya = [];
database.w_nn = [];
database.w_p = [];
database.w_ph = [];
database.w_r = [];
database.w_rr = [];
database.w_rrh = [];
database.w_s = [];
database.w_sh = [];
database.w_ss = [];
database.w_t = [];
database.w_th = [];
database.w_tt = [];
database.w_tth = [];
database.w_y = [];
database.w_z = [];
database.w_khandatta = [];
}
}
function _convertToUnicodeValue(input){
var output = '';
for (var i = 0; i < input.length; i++){
var charCode = input.charCodeAt(i);
if (charCode >= 255){
output += '\\u0' + charCode.toString(16);
} else {
output += input.charAt(i);
}
}
return output;
}
function saveSuffix () {
try {
var file = gio.File.new_for_path ("suffixdict.js");
if (file.query_exists (null)) {
file.delete (null);
}
// Create a new file with this name
var file_stream = file.create (gio.FileCreateFlags.NONE, null);
var json = JSON.stringify(suffixDict);
json = "var db = " + _convertToUnicodeValue(json) + ";";
// Write text data to file
var data_stream = gio.DataOutputStream.new (file_stream);
data_stream.put_string (json, null);
} catch (e) {
print ("Error: " + e.message);
}
}
function saveData () {
try {
var file = gio.File.new_for_path ("avrodict.js");
if (file.query_exists (null)) {
file.delete (null);
}
// Create a new file with this name
var file_stream = file.create (gio.FileCreateFlags.NONE, null);
var json = JSON.stringify(database);
json = "var tables = " + _convertToUnicodeValue(json) + ";";
// Write text data to file
var data_stream = gio.DataOutputStream.new (file_stream);
data_stream.put_string (json, null);
} catch (e) {
print ("Error: " + e.message);
}
}
var __db = new DB ();
__db.loadDb();
saveData();
saveSuffix();
|