Skip to content

Commit

Permalink
Merge pull request #321 from nyanye/master
Browse files Browse the repository at this point in the history
Fix #295 with assertion warning for phrase input #320
  • Loading branch information
Jung Daun committed Jul 28, 2020
2 parents ed4f2b2 + 6c545eb commit 7b9281a
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 0 deletions.
14 changes: 14 additions & 0 deletions konlpy/tag/_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
""" Common utility function for tagger classes """
from __future__ import absolute_import
from __future__ import unicode_literals


def validate_phrase_inputs(phrase):
"""validate if phrase input is provided in str format
Args:
phrase (str): phrase input
"""
msg = "phrase input should be string, not %s" % type(phrase)
assert isinstance(phrase, str), msg
2 changes: 2 additions & 0 deletions konlpy/tag/_hannanum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jpype

from konlpy import jvm, utils
from konlpy.tag._common import validate_phrase_inputs


__all__ = ['Hannanum']
Expand Down Expand Up @@ -83,6 +84,7 @@ def pos(self, phrase, ntags=9, flatten=True, join=False):
:param flatten: If False, preserves eojeols.
:param join: If True, returns joined sets of morph and tag.
"""
validate_phrase_inputs(phrase)

if ntags == 9:
result = self.jhi.simplePos09(phrase)
Expand Down
2 changes: 2 additions & 0 deletions konlpy/tag/_kkma.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jpype

from konlpy import jvm, utils
from konlpy.tag._common import validate_phrase_inputs


__all__ = ['Kkma']
Expand Down Expand Up @@ -51,6 +52,7 @@ def pos(self, phrase, flatten=True, join=False):
:param flatten: If False, preserves eojeols.
:param join: If True, returns joined sets of morph and tag.
"""
validate_phrase_inputs(phrase)

sentences = self.jki.morphAnalyzer(phrase)
morphemes = []
Expand Down
2 changes: 2 additions & 0 deletions konlpy/tag/_komoran.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jpype

from konlpy import jvm, utils
from konlpy.tag._common import validate_phrase_inputs


__all__ = ['Komoran']
Expand Down Expand Up @@ -56,6 +57,7 @@ def pos(self, phrase, flatten=True, join=False):
:param flatten: If False, preserves eojeols.
:param join: If True, returns joined sets of morph and tag.
"""
validate_phrase_inputs(phrase)

sentences = phrase.split('\n')
morphemes = []
Expand Down
2 changes: 2 additions & 0 deletions konlpy/tag/_mecab.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pass

from konlpy import utils
from konlpy.tag._common import validate_phrase_inputs


__all__ = ['Mecab']
Expand Down Expand Up @@ -74,6 +75,7 @@ def pos(self, phrase, flatten=True, join=False):
:param flatten: If False, preserves eojeols.
:param join: If True, returns joined sets of morph and tag.
"""
validate_phrase_inputs(phrase)

if sys.version_info[0] < 3:
phrase = phrase.encode('utf-8')
Expand Down
2 changes: 2 additions & 0 deletions konlpy/tag/_okt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jpype

from konlpy import jvm, utils
from konlpy.tag._common import validate_phrase_inputs


def Twitter(jvmpath=None):
Expand Down Expand Up @@ -56,6 +57,7 @@ def pos(self, phrase, norm=False, stem=False, join=False):
:param stem: If True, stem tokens.
:param join: If True, returns joined sets of morph and tag.
"""
validate_phrase_inputs(phrase)

tokens = self.jki.tokenize(
phrase,
Expand Down
6 changes: 6 additions & 0 deletions test/test_hannanum.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ def test_hannanum_pos_join(hannanum_instance, string):
u'\uac00/P',
u'\uc790/E',
u'!/S']

def test_hannanum_typechecking(hannanum_instance):
try:
hannanum_instance.pos([])
except AssertionError:
pass
6 changes: 6 additions & 0 deletions test/test_kkma.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ def test_kkma_pos_join(kkma_instance, string):
def test_kkma_sentences(kkma_instance, string):
assert kkma_instance.sentences(string) ==\
[u'\uaf43\uac00\ub9c8 \ud0c0\uace0 \uac15\ub0a8 \uac00\uc790!']

def test_kkma_typechecking(kkma_instance):
try:
kkma_instance.pos([])
except AssertionError:
pass
6 changes: 6 additions & 0 deletions test/test_komoran.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ def test_komoran_pos_join(komoran_instance, string):
def test_komoran_morphs(komoran_instance, string):
assert komoran_instance.morphs(string) ==\
[u'\uaf43\uac00\ub9c8', u'\ud0c0', u'\uace0', u'\uac15\ub0a8', u'\uac00', u'\uc790', u'!']

def test_komoran_typechecking(komoran_instance):
try:
komoran_instance.pos([])
except AssertionError:
pass
6 changes: 6 additions & 0 deletions test/test_mecab.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ def test_mecab_morphs(mecab_instance, string):
def test_mecab_nouns(mecab_instance, string):
assert mecab_instance.nouns(string) ==\
[u'\uaf43\uac00\ub9c8', u'\uac15\ub0a8']

def test_mecab_typechecking(mecab_instance):
try:
mecab_instance.pos([])
except AssertionError:
pass
6 changes: 6 additions & 0 deletions test/test_openkoreantext.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ def test_tkorean_morphs(tkorean_instance, string):
def test_tkorean_normalize(tkorean_instance, string):
assert tkorean_instance.normalize(string) ==\
u'\uaf43\uac00\ub9c8 \ud0c0\uace0 \uac15\ub0a8 \uac00\ub098\uc694\u314b\u314b\u314b'

def test_tkorean_typechecking(tkorean_instance):
try:
tkorean_instance.pos([])
except AssertionError:
pass

0 comments on commit 7b9281a

Please sign in to comment.