Skip to content

Instantly share code, notes, and snippets.

@oxUnd
Last active April 17, 2016 10:35
Show Gist options
  • Save oxUnd/69989318dbe59aa2b2bb65e2b19ddbe2 to your computer and use it in GitHub Desktop.
Save oxUnd/69989318dbe59aa2b2bb65e2b19ddbe2 to your computer and use it in GitHub Desktop.
小鹤双拼双形码表转换到 Rime(官网手心输入法挂表,需要转换为 UTF8)
/**
* @author xiangshouding<[email protected]>
*/
var fs = require('fs');
var util = require('util');
var assert = require('assert');
var argv = process.argv;
function readline(content) {
return content
.replace('\n\r', '\n')
.replace('\r\n', '\n')
.split('\n');
}
function assistCode(file, wordTypeMap, words) {
var assist = '';
// include assist code
if (!file) {
return;
}
fs.readFile(file, {encoding: 'utf8'}, function (err, data) {
if (err) {
throw err;
return;
}
var lines = readline(data);
for (var i = 0, len = lines.length; i < len; i++) {
var line = lines[i];
var items = line.split('=').map(function (s) {
return s.trim();
});
var type = items[1];
var word = items[0];
if (wordTypeMap[word]) {
assist += util.format('%s\t%s\n', word, wordTypeMap[word] + type);
}
}
assist += words;
fs.writeFileSync(__dirname + '/dict.assist.txt', assist);
});
}
function main() {
var file = argv[2];
var includeAssist = false;
if (!file) {
console.log(util.format('Usage: %s %s <dict.txt>\n', argv[0], argv[1]));
return 1;
}
var all = '';
var single = '';
var assist = '';
var wordTypeMap = {};
fs.readFile(file, { encoding: 'utf8' }, function (err, data) {
if (err) {
throw err;
return;
}
var lines = readline(data);
for (var i = 0, len = lines.length; i < len; i++) {
var line = lines[i];
if (line.length == 0) {
continue;
}
var items = line
.replace('=', ',')
.split(',');
assert(items.length == 3);
if (items[0].length == items[2].length * 2) {
var type = items[0].replace(/(\w{2})/, '$1 ').trim();
all += util.format('%s\t%s\t%s\n', items[2], type, items[1]);
var types = type.split(' ');
items[2].split('').forEach(function (word, idx) {
wordTypeMap[word] = types[idx];
});
} else {
assist += util.format('%s\t%s\n', items[2], items[0]);
}
}
fs.writeFileSync(__dirname + '/dict.all.txt', all);
argv[3] && assistCode(argv[3], wordTypeMap, assist);
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment