forked from johnheroy/hsk-words
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (34 loc) · 938 Bytes
/
index.js
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
var _ = require('underscore');
var path = require('path');
var Sequelize = require('sequelize');
var sequelize = new Sequelize(null, null, null, {
dialect: 'sqlite',
storage: path.join(__dirname, './db/hsk-words.sqlite')
});
var Word = sequelize.define('Word', {
level: Sequelize.INTEGER,
simplified: Sequelize.STRING,
pronunciation: Sequelize.STRING,
definitions: Sequelize.STRING
});
// convert between traditional and simplified
var cnchars = require('cn-chars');
module.exports.findLevel = function(word, cb){
// handle traditional characters
var simplified = word.slice().split('');
for (var i = 0; i < word.length; i++){
simplified[i] = cnchars.toSimplifiedChar(word[i]);
}
simplified = simplified.join('');
var query = {
where: {simplified: simplified}
};
Word
.findOne(query)
.success(function(word){
cb(word.level);
})
.error(function(){
cb(-1);
});
};