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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4556 | jQuery |
프로그래머7
|
8년 전 | 1691 | |
| 4555 | jQuery |
프로그래머7
|
8년 전 | 1610 | |
| 4554 | jQuery |
프로그래머7
|
8년 전 | 1665 | |
| 4553 | jQuery |
프로그래머7
|
8년 전 | 2041 | |
| 4552 | PHP | 8년 전 | 1880 | ||
| 4551 | PHP | 8년 전 | 1468 | ||
| 4550 | PHP | 8년 전 | 1708 | ||
| 4549 | jQuery |
프로그래머7
|
8년 전 | 2745 | |
| 4548 | jQuery |
프로그래머7
|
8년 전 | 1443 | |
| 4547 | jQuery |
프로그래머7
|
8년 전 | 1368 | |
| 4546 | jQuery |
프로그래머7
|
8년 전 | 1684 | |
| 4545 | jQuery |
프로그래머7
|
8년 전 | 1712 | |
| 4544 | jQuery |
프로그래머7
|
8년 전 | 1866 | |
| 4543 | jQuery |
프로그래머7
|
8년 전 | 1653 | |
| 4542 | jQuery |
프로그래머7
|
8년 전 | 1585 | |
| 4541 | jQuery |
프로그래머7
|
8년 전 | 1634 | |
| 4540 | jQuery |
프로그래머7
|
8년 전 | 1667 | |
| 4539 | jQuery |
프로그래머7
|
8년 전 | 1864 | |
| 4538 | jQuery |
프로그래머7
|
8년 전 | 1536 | |
| 4537 | jQuery |
프로그래머7
|
8년 전 | 1698 | |
| 4536 | jQuery |
프로그래머7
|
8년 전 | 1936 | |
| 4535 | jQuery |
프로그래머7
|
8년 전 | 1521 | |
| 4534 | jQuery |
프로그래머7
|
8년 전 | 1856 | |
| 4533 | jQuery |
프로그래머7
|
8년 전 | 2346 | |
| 4532 | jQuery |
프로그래머7
|
8년 전 | 1918 | |
| 4531 | PHP |
|
8년 전 | 3714 | |
| 4530 | jQuery |
프로그래머7
|
8년 전 | 1801 | |
| 4529 | jQuery |
프로그래머7
|
8년 전 | 1903 | |
| 4528 | jQuery |
프로그래머7
|
8년 전 | 1701 | |
| 4527 | jQuery |
|
8년 전 | 2700 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기