Add glyph migration script

This commit is contained in:
Shaunak Kishore
2015-09-27 13:50:58 -04:00
parent 0d239432d1
commit 30f6b8e11c
4 changed files with 57 additions and 9 deletions

View File

@ -7,6 +7,7 @@ const CHARACTER_FIELDS = ['character', 'decomposition', 'definition',
this.cjklib = {
characters: {},
gb2312: {},
promise: undefined,
radicals: {
primary_radical: {},
index_to_radical_map: {},
@ -214,7 +215,7 @@ Meteor.startup(() => {
readFile('unihan/Unihan_RadicalStrokeCounts.txt').then(getUnihanRows);
const readings = readFile('unihan/Unihan_Readings.txt').then(getUnihanRows);
Promise.all([
cjklib.promise = Promise.all([
// Per-character data.
fillDecompositions(decomposition, glyphs,
cjklib.characters.decomposition),
@ -229,5 +230,6 @@ Meteor.startup(() => {
cjklib.radicals.radical_to_character_map),
// Extract the list of characters in the GB2312 character set.
readFile('gb2312').then((data) => fillGB2312(data, cjklib.gb2312)),
]).then(cleanupCJKLibData).catch(console.error.bind(console));
]).then(cleanupCJKLibData);
cjklib.promise.catch(console.error.bind(console));
});

View File

@ -13,13 +13,16 @@ Glyphs.get_svg_path = function(glyph) {
var terms = [];
for (var i = 0; i < glyph.path.length; i++) {
var segment = glyph.path[i];
assert('LMQZ'.indexOf(segment.type) >= 0, segment.type);
terms.push(segment.type);
if (segment.x1 !== undefined) {
terms.push(segment.x1);
terms.push(segment.y1);
}
terms.push(segment.x);
terms.push(segment.y);
if (segment.x !== undefined) {
terms.push(segment.x);
terms.push(segment.y);
}
}
return terms.join(' ');
}

View File

@ -54,8 +54,3 @@ Meteor.methods({
}
},
});
Meteor.startup(function() {
Glyphs._ensureIndex({name: 1}, {unique: true});
Glyphs._ensureIndex({'manual.verified': 1});
});

48
server/migration.js Normal file
View File

@ -0,0 +1,48 @@
// Given an old-form glyph entry, returns the new glyph entry.
migrate_glyph = (glyph) => {
const codepoint = parseInt(glyph.name.substr(3), 16);
const character = String.fromCodePoint(codepoint);
const data = cjklib.getCharacterData(character);
assert(glyph.manual.verified !== undefined);
const result = {
character: character,
codepoint: codepoint,
metadata: {
definition: undefined,
kangxi_index: data.kangxi_index,
pinyin: undefined,
strokes: undefined,
},
stages: {
path: Glyphs.get_svg_path(glyph),
bridges: glyph.manual.bridges,
strokes: glyph.derived.strokes,
analysis: undefined,
order: undefined,
settled: undefined,
},
};
assert(result.stages.path !== undefined);
assert(result.stages.bridges !== undefined);
assert(result.stages.strokes !== undefined);
return result;
}
migrate_glyphs = () => {
const names = Glyphs.find({}, {fields: {name: 1}, sort: {name: 1}}).fetch();
names.reverse();
for (name of names) {
const glyph = Glyphs.findOne({name: name.name});
const migrated_glyph = migrate_glyph(glyph);
Glyphs.insert(migrated_glyph);
}
console.log('Migration complete.');
}
Meteor.startup(() => {
//console.log('Running migration...');
//cjklib.promise.then(Meteor.bindEnvironment(migrate_glyphs))
// .catch(console.error.bind(console));
Glyphs._ensureIndex({character: 1}, {unique: true});
Glyphs._ensureIndex({codepoint: 1}, {unique: true});
});