1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-17 16:18:23 +09:00
MisskeyIO/src/tools/analysis/core.ts

50 lines
1.0 KiB
TypeScript
Raw Normal View History

2017-09-06 23:58:28 +09:00
const bayes = require('./naive-bayes.js');
2017-09-07 14:52:07 +09:00
const MeCab = require('./mecab');
2017-09-06 23:58:28 +09:00
import Post from '../../api/models/post';
/**
* 稿稿
*/
export default class Categorizer {
private classifier: any;
private mecab: any;
constructor() {
this.mecab = new MeCab();
// BIND -----------------------------------
this.tokenizer = this.tokenizer.bind(this);
}
private tokenizer(text: string) {
2017-09-07 02:59:33 +09:00
const tokens = this.mecab.parseSync(text)
// 名詞だけに制限
.filter(token => token[1] === '名詞')
// 取り出し
.map(token => token[0]);
return tokens;
2017-09-06 23:58:28 +09:00
}
public async init() {
this.classifier = bayes({
tokenizer: this.tokenizer
});
// 訓練データ取得
const verifiedPosts = await Post.find({
is_category_verified: true
});
// 学習
verifiedPosts.forEach(post => {
this.classifier.learn(post.text, post.category);
});
}
public async predict(text) {
return this.classifier.categorize(text);
}
}