Skip to content

Commit 16b0b76

Browse files
authored
0.1.3 Release
Komoran 객체 생성 시 모델 지정이 필요하도록 변경
2 parents 039c933 + 4a5f92a commit 16b0b76

File tree

9 files changed

+69
-28
lines changed

9 files changed

+69
-28
lines changed

README.en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949

5050
```python
5151
from PyKomoran import *
52-
komoran = Komoran()
52+
komoran = Komoran(DEFAULT_MODEL['LIGHT'])
5353
```
5454

5555
* After then, run analyzing method.
5656

5757
```python
5858
komoran.get_plain_text("① 대한민국은 민주공화국이다.")
5959
# # Result
60-
# ①/SW 대한민국/NNP 은/JX 민주공화국/NNP 이/VCP 다/EF ./SF
60+
# '①/SW 대한민국/NNP 은/JX 민주/NNP 공화국/NNG 이/VCP 다/EF ./SF'
6161
```
6262

6363
### Usage in detail

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949

5050
```python
5151
from PyKomoran import *
52-
komoran = Komoran()
52+
komoran = Komoran(DEFAULT_MODEL['LIGHT'])
5353
```
5454

5555
* 분석 메소드를 이용하여 문장을 분석합니다.
5656

5757
```python
5858
komoran.get_plain_text("① 대한민국은 민주공화국이다.")
5959
# # 실행 결과
60-
# ①/SW 대한민국/NNP 은/JX 민주공화국/NNP 이/VCP 다/EF ./SF
60+
# '①/SW 대한민국/NNP 은/JX 민주/NNP 공화국/NNG 이/VCP 다/EF ./SF'
6161
```
6262

6363
### 자세한 사용법

docs/firststep/tutorial.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
:linenos:
1919
2020
from PyKomoran import *
21-
komoran = Komoran()
21+
komoran = Komoran(DEFAULT_MODEL['FULL'])
2222
print(komoran.get_plain_text("KOMORAN은 한국어 형태소 분석기입니다."))
2323
2424
실행 결과는 다음과 같습니다.
@@ -44,15 +44,24 @@ Python 명령어를 실행한 후, 다음과 같이 PyKomoran을 불러옵니다
4444
Komoran 객체 생성하기
4545
---------------------------------------
4646
이제, 형태소 분석을 위한 ``Komoran`` 객체를 생성합니다.
47+
여기에서는 기본으로 제공하는 모델 중 FULL 모델을 불러오겠습니다.
4748

4849
.. code-block:: python
4950
:linenos:
5051
51-
komoran = Komoran()
52+
komoran = Komoran(DEFAULT_MODEL['FULL'])
5253
5354
이 과정에서 Java 버전의 KOMORAN을 불러오게 되며, 약간의 시간이 소요됩니다.
5455
이제 ``Komoran`` 객체의 메소드를 이용하여 형태소를 분석할 수 있습니다.
5556

57+
.. Note::
58+
``DEFAULT_MODEL`` 은 기본적으로 PyKomoran에 포함된 모델로, KOMORAN의 ``DEFAULT_MODEL`` 에 대응합니다.
59+
즉, PyKOMORAN의 ``DEFAULT_MODEL['FULL']`` 과 ``DEFAULT_MODEL['LIGHT']`` 은 각각 KOMORAN의 ``DEFAULT_MODEL.FULL`` 과
60+
``DEFAULT_MODEL.LIGHT`` 에 대응합니다.
61+
62+
.. TODO::
63+
DFEAULT_MODEL에 대한 설명을 추가합니다.
64+
5665
형태소 분석하기
5766
---------------------------------------
5867
PyKOMORAN은 KOMORAN에서 제공하는 다양한 형태의 형태소 분석 결과를 제공합니다.
@@ -80,7 +89,7 @@ PyKOMORAN은 KOMORAN에서 제공하는 다양한 형태의 형태소 분석 결
8089
from PyKomoran import *
8190
8291
# Komoran 객체 생성
83-
komoran = Komoran()
92+
komoran = Komoran(DEFAULT_MODEL['FULL'])
8493
8594
# 분석할 문장 준비
8695
str_to_analyze = "① 대한민국은 민주공화국이다. ② 대한민국의 주권은 국민에게 있고, 모든 권력은 국민으로부터 나온다."

python/PyKomoran/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .__version__ import __author__, __author_email__, __license__, __version__
33

44
from .jvm import init_jvm
5-
from .type import Pair, Token, Pos
5+
from .type import Pair, Token, Pos, DEFAULT_MODEL
66
from .core import Komoran
77

8-
__all__ = ['jvm', 'Pair', 'Token', 'Pos', 'Komoran']
8+
__all__ = ['jvm', 'Pair', 'Token', 'Pos', 'DEFAULT_MODEL', 'Komoran']

python/PyKomoran/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__title__ = 'PyKomoran'
99
__description__ = 'PyKomoran is Python wrapper for KOMORAN, KOrean MORphical ANalyzer.'
1010
__url__ = 'https://pydocs.komoran.kr'
11-
__version__ = '0.1.2'
11+
__version__ = '0.1.3'
1212
__author__ = 'Junghwan Park'
1313
__author_email__ = 'reserve.dev@gmail.com'
1414
__license__ = 'Apache 2.0'

python/PyKomoran/core.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from PyKomoran.type import Pair
66
from PyKomoran.type import Token
77
from PyKomoran.type import Pos
8+
from PyKomoran.type import DEFAULT_MODEL
89

910
__all__ = ['Komoran']
1011

@@ -26,29 +27,31 @@ class Komoran:
2627
max_heap (int): JVM의 Max Heap Size (기본값: ``1024``, 단위: ``MB``)
2728
2829
Examples:
29-
기본 모델(FULL, LIGHT) 외에 사용자가 직접 생성한 모델이 존재하는 곳의 ``절대 경로`` 를 이용하여 Komoran 객체를 생성할 수 있습니다.
30+
기본 모델( ``DEFAULT_MODEL['FULL']`` , ``DEFAULT_MODEL['LIGHT']`` ) 외에 사용자가 직접 생성한 모델이 위치하는
31+
``절대 경로`` 를 이용하여 Komoran 객체를 생성할 수 있습니다.
3032
31-
>>> # DEFAULT_MODEL.FULL 로 Komoran 객체를 생성합니다.
32-
>>> komoran_full = Komoran()
33-
>>> # DEFAULT_MODEL.LIGHT 로 Komoran 객체를 생성합니다.
34-
>>> komoran_light = Komoran("./models_light")
33+
>>> # 기본으로 제공하는 LIGHT 모델로 Komoran 객체를 생성합니다.
34+
>>> komoran = Komoran(DEFAULT_MODEL['LIGHT'])
35+
>>> # 기본으로 제공하는 FULL 모델로 Komoran 객체를 생성합니다.
36+
>>> komoran = Komoran(DEFAULT_MODEL['FULL'])
3537
>>> # 사용자가 미리 생성 모델로 Komoran 객체를 생성합니다.
36-
>>> komoran_user = Komoran("/home/user/Komoran/Model")
38+
>>> komoran_user = Komoran("/some/where/path/Komoran/Model")
3739
3840
"""
3941

40-
def __init__(self, model_path="./models_full", max_heap=1024):
41-
self._base_path = os.path.dirname(os.path.realpath(__file__))
42-
self._model_path = os.path.abspath(os.path.join(self._base_path, model_path))
42+
def __init__(self, model_path, max_heap=1024):
43+
if max_heap <= 0:
44+
raise KomoranError("Heap size for JVM is too small!")
4345

44-
assert os.path.exists(self._model_path)
46+
if not os.path.exists(model_path):
47+
raise KomoranError("model does NOT exist!")
4548

4649
self.pos_table = Pos()
4750

4851
jvm.init_jvm(max_heap)
4952
self._komoran = jvm.get_jvm().kr.co.shineware.nlp.pykomoran.KomoranEntryPoint()
5053

51-
self._komoran.init(self._model_path)
54+
self._komoran.init(model_path)
5255
if not self._komoran.isInitialized():
5356
raise KomoranError("Komoran is NOT initialized!")
5457

@@ -287,7 +290,7 @@ def pos(self, sentence, flatten=True):
287290

288291

289292
if __name__ == '__main__':
290-
komoran = Komoran(DEFAULT_MODELS['FULL'])
293+
komoran = Komoran(DEFAULT_MODEL['FULL'])
291294
str_to_analyze = "① 대한민국은 민주공화국이다. ② 대한민국의 주권은 국민에게 있고, 모든 권력은 국민으로부터 나온다."
292295

293296
print(komoran.get_nouns(str_to_analyze))

python/PyKomoran/tests/test_core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_to_init_Komoran():
1515
"""
1616
global komoran
1717

18-
komoran = Komoran(model_path='./models_full')
18+
komoran = Komoran(DEFAULT_MODEL['FULL'])
1919

2020
assert komoran is not None
2121
assert komoran._komoran.isInitialized()
@@ -306,7 +306,7 @@ def test_to_set_user_dic():
306306
global komoran
307307

308308
if komoran is None:
309-
komoran = Komoran(model_path='./models_full')
309+
komoran = Komoran(DEFAULT_MODEL['FULL'])
310310

311311
tokens = komoran.get_token_list("테스트 단어")
312312

@@ -357,7 +357,7 @@ def test_to_set_fw_dic():
357357
global komoran
358358

359359
if komoran is None:
360-
komoran = Komoran(model_path='./models_full')
360+
komoran = Komoran(DEFAULT_MODEL['FULL'])
361361

362362
tokens = komoran.get_token_list("테스트")
363363

python/PyKomoran/type.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
__all__ = ['Token', 'Pair', 'Pos']
1+
import os
2+
3+
__all__ = ['Token', 'Pair', 'Pos', 'DEFAULT_MODEL']
24

35

46
class Token:
@@ -243,3 +245,30 @@ def items(self):
243245

244246
def has_key(self, key):
245247
return key in self.pos_type
248+
249+
250+
class DefaultModel:
251+
def __init__(self):
252+
base_path = os.path.dirname(os.path.realpath(__file__))
253+
254+
self._models = {
255+
'FULL': '{0}{1}models_full'.format(base_path, os.sep),
256+
'LIGHT': "{0}{1}models_light".format(base_path, os.sep)
257+
}
258+
259+
def __getitem__(self, model):
260+
if model in self._models.keys():
261+
return self._models[model]
262+
else:
263+
return ''
264+
265+
def __contains__(self, model):
266+
return model in self._models
267+
268+
def __str__(self):
269+
return str(self._models)
270+
271+
def __repr__(self):
272+
return repr(self._models)
273+
274+
DEFAULT_MODEL = DefaultModel()

python/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@
4747

4848
```python
4949
from PyKomoran import *
50-
komoran = Komoran()
50+
komoran = Komoran(DEFAULT_MODEL['LIGHT'])
5151
```
5252

5353
* After then, run analyzing method.
5454

5555
```python
5656
komoran.get_plain_text("① 대한민국은 민주공화국이다.")
5757
# # Result
58-
# ①/SW 대한민국/NNP 은/JX 민주공화국/NNP 이/VCP 다/EF ./SF
58+
# '①/SW 대한민국/NNP 은/JX 민주/NNP 공화국/NNG 이/VCP 다/EF ./SF'
5959
```
6060

6161
### Usage in detail

0 commit comments

Comments
 (0)