Created
December 9, 2012 17:41
-
-
Save kotobukid/4246242 to your computer and use it in GitHub Desktop.
node.jsでMeCabを通す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var express = require('express') | |
, routes = require('./routes') | |
, user = require('./routes/user') | |
, http = require('http') | |
, path = require('path'); | |
var mecab = require('./mecab'); | |
var app = express(); | |
app.configure(function(){ | |
app.set('port', process.env.PORT || 3000); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.favicon()); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler()); | |
}); | |
app.get('/', routes.index); | |
app.get('/users', user.list); | |
app.get('/api.mecab.json', function (req, res) { | |
var words = req.query.words; | |
if (words) { | |
mecab.tag(words, function (err, rep) { | |
//console.log(rep); | |
res.json({ | |
success: true, | |
result: rep | |
}); | |
}); | |
} else { | |
res.json({ | |
result: '', | |
success: false | |
}); | |
} | |
}); | |
http.createServer(app).listen(app.get('port'), function(){ | |
console.log("Express server listening on port " + app.get('port')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function ($) { | |
'use strict'; | |
var li_text = '<li style="display: none;"></li>'; | |
var ul = $('#results'); | |
$(document).on('click', '[type="submit"]', function () { | |
$.ajax({ | |
url: '/api.mecab.json', | |
data: {words: $('#words').val()}, | |
dataType: 'json', | |
type: 'GET' | |
}).success(function (data) { | |
if (data.success) { | |
ul.empty(); | |
_.each(data.result.split('\n'), function (item) { | |
if ((item) && (item !== 'EOS')) { | |
var li = $(li_text).text(item); | |
setTimeout(function () { | |
ul.append(li); | |
setTimeout(function () { | |
li.show('slow'); | |
}, 0); | |
}, 0); | |
} | |
}); | |
} else { | |
alert('解析に失敗'); | |
} | |
}); | |
return false; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
var exec, Mecab; | |
exec = require('child_process').exec; | |
Mecab = function () { | |
var that = this; | |
that.tag = function (words, next) { | |
var command = 'echo "' + words.split('\\').join('\\\\') + '"|' + this.PATH; | |
exec(command, function (err, stdout, stderr) { | |
if (err || stderr) { | |
console.log(err); | |
console.log(stderr); | |
return false; | |
} | |
return process.nextTick(function () { | |
next.call(that, err, stdout); | |
}); | |
}); | |
}; | |
that.PATH = '/usr/bin/mecab'; | |
}; | |
module.exports = new Mecab(); | |
})(); |
投げられたワードをクオートしてmecabに投げるようにしたので、見た感じ防げているみたい。円記号投げられた時などの挙動は明日以降に。。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OSコマンドインジェクションが出来る事に気づいたので気を付けないと死ぬ