python3+nodejs를 이용한 형태소 검색기
# python3+nodejs를 이용한 형태소 검색기
## 준비물
python3 (엔진, KoNLPy, MeCab)
java
nodejs (엔진, express, python-shell, colors)
## 참조자료
KoNLPy 설치법: http://konlpy.org/ko/v0.4.4/install/
MeCab class 사용법: http://konlpy.org/ko/v0.4.4/api/konlpy.tag/#mecab-class
mecab.py
[code]
# -*- coding: utf-8 -*-
"""
1. sudo apt-get install curl
2. bash <(curl -s https://raw.githubusercontent.com/konlpy/konlpy/master/scripts/mecab.sh)
3. http://searchtool.tistory.com/3
curl -L https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/mecab-ko-dic-2.0.1-20150920.tar.gz | tar -xz
cd mecab-ko-dic-2.0.1-20150920
./autogen.sh
./configure
make
make install
mecab -d /usr/local/lib/mecab/dic/mecab-ko-dic
# 설치 및 가동 확인 후 사용자 사전 추가 후 재설치
vi ./user-dic/nnp.csv
./tools/add-userdic.sh
make install
mecab -d /usr/local/lib/mecab/dic/mecab-ko-dic
"""
import sys
from konlpy.tag import Mecab
mecab = Mecab()
total = len(sys.argv)
cmdargs = str(sys.argv)
cm = str(sys.argv[1])
ret = mecab.pos(u''+cm)
print(ret)
[/code]
py.js
[code]
var app = require('express')();
var http = require('http').Server(app);
var url = require('url');
var PythonShell = require('python-shell');
var colors = require('colors');
if(!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
// 전체 치환
if(!String.prototype.replaceAll) {
String.prototype.replaceAll = function(org, dest) {
return this.split(org).join(dest);
}
}
app.get('/', function(req, res){
res.send('주소/data/?k=한글문장을 입력');
});
app.get('/data', function(req, res, next){
res.set('Content-Type', 'text/plain');
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
if(query['k'] != undefined) {
var options = {
mode: 'text',
pythonPath: 'python3',
scriptPath: '',
args: [query['k']]
};
PythonShell.run('mecab.py', options, function (err, results) {
if(err) throw err;
var data = String(results);
data = data.replace(/\('(.*?)',\s'(.*?)'\)/g, '{"value":"$1", "type":"$2"}');
res.send(data);
});
}
});
http.listen(7777, function(){
console.log('konlpy server is ready!!'.rainbow);
});
[/code]
정규식에 약해서 nodejs측은 난잡합니다 ㅠ.ㅠ
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4586 | jQuery |
프로그래머7
|
8년 전 | 2088 | |
| 4585 | PHP | 8년 전 | 2159 | ||
| 4584 | PHP | 8년 전 | 2411 | ||
| 4583 | PHP | 8년 전 | 2354 | ||
| 4582 | jQuery |
프로그래머7
|
8년 전 | 1425 | |
| 4581 | jQuery |
프로그래머7
|
8년 전 | 1373 | |
| 4580 | jQuery |
프로그래머7
|
8년 전 | 1905 | |
| 4579 | PHP | 8년 전 | 2229 | ||
| 4578 | PHP | 8년 전 | 1740 | ||
| 4577 | PHP | 8년 전 | 2470 | ||
| 4576 | OS |
프로그래머7
|
8년 전 | 2697 | |
| 4575 | PHP | 8년 전 | 1924 | ||
| 4574 | PHP | 8년 전 | 1558 | ||
| 4573 | PHP | 8년 전 | 1966 | ||
| 4572 | PHP | 8년 전 | 2063 | ||
| 4571 | PHP | 8년 전 | 2415 | ||
| 4570 | PHP | 8년 전 | 2273 | ||
| 4569 | jQuery |
프로그래머7
|
8년 전 | 1863 | |
| 4568 | jQuery |
프로그래머7
|
8년 전 | 1402 | |
| 4567 | jQuery |
프로그래머7
|
8년 전 | 1967 | |
| 4566 | jQuery |
프로그래머7
|
8년 전 | 1897 | |
| 4565 | jQuery |
프로그래머7
|
8년 전 | 1648 | |
| 4564 | PHP | 8년 전 | 2578 | ||
| 4563 | PHP | 8년 전 | 1917 | ||
| 4562 | PHP | 8년 전 | 2426 | ||
| 4561 | jQuery |
프로그래머7
|
8년 전 | 1363 | |
| 4560 | jQuery |
프로그래머7
|
8년 전 | 1903 | |
| 4559 | jQuery |
프로그래머7
|
8년 전 | 1449 | |
| 4558 | node.js |
|
8년 전 | 3431 | |
| 4557 | jQuery |
프로그래머7
|
8년 전 | 1727 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기