Skip to content

Commit

Permalink
문자열 포맷 API 추가
Browse files Browse the repository at this point in the history
tossi.format 을 이용해 문자열에 조사를 포맷할수 있습니다.

ex) tossi.format('{0:을}', '나오') == '나오를'
  • Loading branch information
youknowone committed Aug 2, 2017
1 parent 26c3b36 commit 49666ea
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
11 changes: 11 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import functools

import pytest
from six import PY2, text_type as str, with_metaclass

Expand Down Expand Up @@ -367,6 +369,15 @@ def test_unmatch():
assert Eul[u'예제':u'는'] is None


def test_formatter():
t = u'{0:으로} {0:을}'
f1 = functools.partial(tossi.Formatter(registry).format, t)
f2 = functools.partial(tossi.format, t)
assert f1(u'나오') == f2(u'나오') == u'나오로 나오를'
assert f1(u'키홀') == f2(u'키홀') == u'키홀로 키홀을'
assert f1(u'모리안') == f2(u'모리안') == u'모리안으로 모리안을'


def test_singleton_error():
with pytest.raises(TypeError):
class Fail(with_metaclass(SingletonParticleMeta, object)):
Expand Down
11 changes: 10 additions & 1 deletion tossi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import warnings

from tossi.coda import guess_coda
from tossi.formatter import Formatter
from tossi.particles import Euro, Ida, Particle
from tossi.tolerance import (
MORPH1_AND_OPTIONAL_MORPH2, MORPH2_AND_OPTIONAL_MORPH1,
Expand All @@ -23,7 +24,8 @@
__all__ = ['get_particle', 'guess_coda', 'MORPH1_AND_OPTIONAL_MORPH2',
'MORPH2_AND_OPTIONAL_MORPH1', 'OPTIONAL_MORPH1_AND_MORPH2',
'OPTIONAL_MORPH2_AND_MORPH1', 'parse', 'parse_tolerance_style',
'Particle', 'pick', 'postfix', 'postfix_particle']
'Particle', 'pick', 'postfix', 'postfix_particle',
'Formatter', 'format']


def index_particles(particles):
Expand Down Expand Up @@ -107,6 +109,7 @@ def postfix_particle(self, word, morph, **kwargs):
# Special particles:
Euro,
])
formatter = Formatter(registry)


def parse(morph):
Expand Down Expand Up @@ -134,3 +137,9 @@ def get_particle(morph):
def postfix_particle(word, morph, **kwargs):
warnings.warn(DeprecationWarning('Use postfix() instead'))
return postfix(word, morph, **kwargs)


def format(message, *args, **kwargs):
"""Shortcut for :class:`tossi.Formatter.format` of the default registry.
"""
return formatter.vformat(message, args, kwargs)
34 changes: 34 additions & 0 deletions tossi/formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""
tossi.formatter
~~~~~~~~~~~~~~~
String formatter for Tossi.
:copyright: (c) 2016-2017 by What! Studio
:license: BSD, see LICENSE for more details.
"""
import re
from string import Formatter as StringFormatter


class Formatter(StringFormatter):
"""String formatter supports tossi format spec.
>>> f = Formatter(tossi.registry)
>>> t = u'{0:으로} {0:을}'
>>> assert f.format(t, u'나오') == u'나오로 나오를'
>>> assert f.format(t, u'키홀') == u'키홀로 키홀을'
>>> assert f.format(t, u'모리안') == u'모리안으로 모리안을'
"""
hangul_pattern = re.compile(u'[가-힣]+')

def __init__(self, registry):
self.registry = registry

def format_field(self, value, format_spec):
if re.match(self.hangul_pattern, format_spec):
return self.registry.postfix(value, format_spec)
else:
return super(Formatter, self).format_field(value, format_spec)

0 comments on commit 49666ea

Please sign in to comment.