Refactoring
This commit is contained in:
		
							parent
							
								
									a1bcfd39da
								
							
						
					
					
						commit
						ed3e188bfc
					
				
					 2 changed files with 49 additions and 26 deletions
				
			
		
							
								
								
									
										45
									
								
								src/tools/ai/core.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/tools/ai/core.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,45 @@
 | 
			
		|||
const bayes = require('./naive-bayes.js');
 | 
			
		||||
const MeCab = require('mecab-async');
 | 
			
		||||
 | 
			
		||||
import Post from '../../api/models/post';
 | 
			
		||||
import config from '../../conf';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 投稿を学習したり与えられた投稿のカテゴリを予測します
 | 
			
		||||
 */
 | 
			
		||||
export default class Categorizer {
 | 
			
		||||
	private classifier: any;
 | 
			
		||||
	private mecab: any;
 | 
			
		||||
 | 
			
		||||
	constructor() {
 | 
			
		||||
		this.mecab = new MeCab();
 | 
			
		||||
		if (config.categorizer.mecab_command) this.mecab.command = config.categorizer.mecab_command;
 | 
			
		||||
 | 
			
		||||
		// BIND -----------------------------------
 | 
			
		||||
		this.tokenizer = this.tokenizer.bind(this);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private tokenizer(text: string) {
 | 
			
		||||
		return this.mecab.wakachiSync(text);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,31 +1,9 @@
 | 
			
		|||
const bayes = require('./naive-bayes.js');
 | 
			
		||||
const MeCab = require('mecab-async');
 | 
			
		||||
 | 
			
		||||
import Post from '../../api/models/post';
 | 
			
		||||
import config from '../../conf';
 | 
			
		||||
import Core from './core';
 | 
			
		||||
 | 
			
		||||
const classifier = bayes({
 | 
			
		||||
	tokenizer: this.tokenizer
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const mecab = new MeCab();
 | 
			
		||||
if (config.categorizer.mecab_command) mecab.command = config.categorizer.mecab_command;
 | 
			
		||||
 | 
			
		||||
// 訓練データ取得
 | 
			
		||||
Post.find({
 | 
			
		||||
	is_category_verified: true
 | 
			
		||||
}, {
 | 
			
		||||
	fields: {
 | 
			
		||||
		_id: false,
 | 
			
		||||
		text: true,
 | 
			
		||||
		category: true
 | 
			
		||||
	}
 | 
			
		||||
}).then(verifiedPosts => {
 | 
			
		||||
	// 学習
 | 
			
		||||
	verifiedPosts.forEach(post => {
 | 
			
		||||
		classifier.learn(post.text, post.category);
 | 
			
		||||
	});
 | 
			
		||||
const c = new Core();
 | 
			
		||||
 | 
			
		||||
c.init().then(() => {
 | 
			
		||||
	// 全ての(人間によって証明されていない)投稿を取得
 | 
			
		||||
	Post.find({
 | 
			
		||||
		text: {
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +23,7 @@ Post.find({
 | 
			
		|||
	}).then(posts => {
 | 
			
		||||
		posts.forEach(post => {
 | 
			
		||||
			console.log(`predicting... ${post._id}`);
 | 
			
		||||
			const category = classifier.categorize(post.text);
 | 
			
		||||
			const category = c.predict(post.text);
 | 
			
		||||
 | 
			
		||||
			Post.update({ _id: post._id }, {
 | 
			
		||||
				$set: {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue