Skip to content

Commit

Permalink
0.1.3 Release
Browse files Browse the repository at this point in the history
Komoran 객체 생성 시 모델 지정이 필요하도록 변경
  • Loading branch information
9bow committed Apr 11, 2019
2 parents 039c933 + 4a5f92a commit 16b0b76
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@

```python
from PyKomoran import *
komoran = Komoran()
komoran = Komoran(DEFAULT_MODEL['LIGHT'])
```

* After then, run analyzing method.

```python
komoran.get_plain_text("① 대한민국은 민주공화국이다.")
# # Result
# ①/SW 대한민국/NNP 은/JX 민주공화국/NNP 이/VCP 다/EF ./SF
# '①/SW 대한민국/NNP 은/JX 민주/NNP 공화국/NNG 이/VCP 다/EF ./SF'
```

### Usage in detail
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@

```python
from PyKomoran import *
komoran = Komoran()
komoran = Komoran(DEFAULT_MODEL['LIGHT'])
```

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

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

### 자세한 사용법
Expand Down
15 changes: 12 additions & 3 deletions docs/firststep/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
:linenos:
from PyKomoran import *
komoran = Komoran()
komoran = Komoran(DEFAULT_MODEL['FULL'])
print(komoran.get_plain_text("KOMORAN은 한국어 형태소 분석기입니다."))
실행 결과는 다음과 같습니다.
Expand All @@ -44,15 +44,24 @@ Python 명령어를 실행한 후, 다음과 같이 PyKomoran을 불러옵니다
Komoran 객체 생성하기
---------------------------------------
이제, 형태소 분석을 위한 ``Komoran`` 객체를 생성합니다.
여기에서는 기본으로 제공하는 모델 중 FULL 모델을 불러오겠습니다.

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

.. Note::
``DEFAULT_MODEL`` 은 기본적으로 PyKomoran에 포함된 모델로, KOMORAN의 ``DEFAULT_MODEL`` 에 대응합니다.
즉, PyKOMORAN의 ``DEFAULT_MODEL['FULL']`` 과 ``DEFAULT_MODEL['LIGHT']`` 은 각각 KOMORAN의 ``DEFAULT_MODEL.FULL`` 과
``DEFAULT_MODEL.LIGHT`` 에 대응합니다.

.. TODO::
DFEAULT_MODEL에 대한 설명을 추가합니다.

형태소 분석하기
---------------------------------------
PyKOMORAN은 KOMORAN에서 제공하는 다양한 형태의 형태소 분석 결과를 제공합니다.
Expand Down Expand Up @@ -80,7 +89,7 @@ PyKOMORAN은 KOMORAN에서 제공하는 다양한 형태의 형태소 분석 결
from PyKomoran import *
# Komoran 객체 생성
komoran = Komoran()
komoran = Komoran(DEFAULT_MODEL['FULL'])
# 분석할 문장 준비
str_to_analyze = "① 대한민국은 민주공화국이다. ② 대한민국의 주권은 국민에게 있고, 모든 권력은 국민으로부터 나온다."
Expand Down
4 changes: 2 additions & 2 deletions python/PyKomoran/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .__version__ import __author__, __author_email__, __license__, __version__

from .jvm import init_jvm
from .type import Pair, Token, Pos
from .type import Pair, Token, Pos, DEFAULT_MODEL
from .core import Komoran

__all__ = ['jvm', 'Pair', 'Token', 'Pos', 'Komoran']
__all__ = ['jvm', 'Pair', 'Token', 'Pos', 'DEFAULT_MODEL', 'Komoran']
2 changes: 1 addition & 1 deletion python/PyKomoran/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__title__ = 'PyKomoran'
__description__ = 'PyKomoran is Python wrapper for KOMORAN, KOrean MORphical ANalyzer.'
__url__ = 'https://pydocs.komoran.kr'
__version__ = '0.1.2'
__version__ = '0.1.3'
__author__ = 'Junghwan Park'
__author_email__ = '[email protected]'
__license__ = 'Apache 2.0'
Expand Down
27 changes: 15 additions & 12 deletions python/PyKomoran/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from PyKomoran.type import Pair
from PyKomoran.type import Token
from PyKomoran.type import Pos
from PyKomoran.type import DEFAULT_MODEL

__all__ = ['Komoran']

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

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

assert os.path.exists(self._model_path)
if not os.path.exists(model_path):
raise KomoranError("model does NOT exist!")

self.pos_table = Pos()

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

self._komoran.init(self._model_path)
self._komoran.init(model_path)
if not self._komoran.isInitialized():
raise KomoranError("Komoran is NOT initialized!")

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


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

print(komoran.get_nouns(str_to_analyze))
Expand Down
6 changes: 3 additions & 3 deletions python/PyKomoran/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_to_init_Komoran():
"""
global komoran

komoran = Komoran(model_path='./models_full')
komoran = Komoran(DEFAULT_MODEL['FULL'])

assert komoran is not None
assert komoran._komoran.isInitialized()
Expand Down Expand Up @@ -306,7 +306,7 @@ def test_to_set_user_dic():
global komoran

if komoran is None:
komoran = Komoran(model_path='./models_full')
komoran = Komoran(DEFAULT_MODEL['FULL'])

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

Expand Down Expand Up @@ -357,7 +357,7 @@ def test_to_set_fw_dic():
global komoran

if komoran is None:
komoran = Komoran(model_path='./models_full')
komoran = Komoran(DEFAULT_MODEL['FULL'])

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

Expand Down
31 changes: 30 additions & 1 deletion python/PyKomoran/type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__all__ = ['Token', 'Pair', 'Pos']
import os

__all__ = ['Token', 'Pair', 'Pos', 'DEFAULT_MODEL']


class Token:
Expand Down Expand Up @@ -243,3 +245,30 @@ def items(self):

def has_key(self, key):
return key in self.pos_type


class DefaultModel:
def __init__(self):
base_path = os.path.dirname(os.path.realpath(__file__))

self._models = {
'FULL': '{0}{1}models_full'.format(base_path, os.sep),
'LIGHT': "{0}{1}models_light".format(base_path, os.sep)
}

def __getitem__(self, model):
if model in self._models.keys():
return self._models[model]
else:
return ''

def __contains__(self, model):
return model in self._models

def __str__(self):
return str(self._models)

def __repr__(self):
return repr(self._models)

DEFAULT_MODEL = DefaultModel()
4 changes: 2 additions & 2 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@

```python
from PyKomoran import *
komoran = Komoran()
komoran = Komoran(DEFAULT_MODEL['LIGHT'])
```

* After then, run analyzing method.

```python
komoran.get_plain_text("① 대한민국은 민주공화국이다.")
# # Result
# ①/SW 대한민국/NNP 은/JX 민주공화국/NNP 이/VCP 다/EF ./SF
# '①/SW 대한민국/NNP 은/JX 민주/NNP 공화국/NNG 이/VCP 다/EF ./SF'
```

### Usage in detail
Expand Down

0 comments on commit 16b0b76

Please sign in to comment.