Skip to content

Instantly share code, notes, and snippets.

@Hiromi-Kai
Last active October 13, 2017 05:56
Show Gist options
  • Save Hiromi-Kai/f39ce52b31b0394f3025c453765f7e75 to your computer and use it in GitHub Desktop.
Save Hiromi-Kai/f39ce52b31b0394f3025c453765f7e75 to your computer and use it in GitHub Desktop.
Bug fix
var fs = require('fs');
var file_name = 'ru_RU'
BJSpell = {
create: function() {
files = {
dic: fs.readFileSync('dictionary/' + file_name + '.dic', 'utf8'),
aff: fs.readFileSync('dictionary/' + file_name + '.aff', 'utf8'),
}
var global = 'BJSpell.' + file_name + '=' + this.dic(files.dic.split(/\r\n|\r|\n/));
fs.writeFileSync('dictionary.js/' + file_name + '.js', global.substring(0, global.length - 1).concat(",rules:",
this.aff(files.aff.split(/\r\n|\r|\n/)), "}"), {
flag: 'w'
});
},
aff: function(lines) {
for (var rules = {
PFX: [],
SFX: []
},
i = 0,
length = lines.length,
line; i < length; i++
) {
line = lines[i];
switch (line.substr(0, 3)) {
case "PFX":
case "SFX":
var type = line.substr(0, 3),
name = line.substr(4, 1),
product = line.substr(6, 1) === "Y",
many = i + (line.substring(8) >> 0),
referer = rules[type],
j = i,
info;
while (j++ < many)
referer[referer.length] = lines[j].substring(8).split(/\s+/).slice(0, 3).concat([name]);
i = j;
break;
}
};
return JSON.stringify(rules);
},
dic: function(lines) {
for (var ignore = String.fromCharCode(65533),
dictionary = {},
alphabet = {},
i = 1,
length = lines.length,
re = "/\\b[",
line, split, key; i < length; i++
) {
line = lines[i].replace(/^\s+|\s+$/g, "");
if (line.length) {
split = line.split("/");
line = split[0].toLowerCase();
if (line !== ignore) {
dictionary[line] = split[1] || true;
for (var j = 0, c; j < line.length; j++)
alphabet[line.charAt(j)] = true;
}
}
};
for (key in alphabet) {
if (key !== ignore && /[^\.\-\_\,\/\:\;\@\?\=\+\-\*£$^\(\)\[\]\{\}~<>]/.test(key))
re += key;
};
re += "]+\\b/ig";
alphabet = {
words: dictionary,
alphabet: null
};
line = JSON.stringify(alphabet);
return line.substring(0, line.length - 5).concat(re, "}");
},
};
BJSpell.create();
@Hiromi-Kai
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment