Skip to content

Commit

Permalink
Fix lint errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
emfomy committed Apr 10, 2020
1 parent 43ec7a9 commit 6b43502
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 169 deletions.
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
[MASTER]

disable =
arguments-differ,
bad-continuation,
duplicate-code,
logging-fstring-interpolation,
missing-docstring,
too-few-public-methods,
too-many-ancestors,
too-many-branches,
too-many-instance-attributes,
too-many-nested-blocks,

[FORMAT]

Expand Down
4 changes: 2 additions & 2 deletions ckipnlp/container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def to_text(self):

################################################################################################################################

class _InterfaceItem: # pylint: disable=too-few-public-methods
class _InterfaceItem:
"""Container has ckipnlp item class."""

@classmethod
Expand Down Expand Up @@ -277,7 +277,7 @@ def _item_to_list(cls, item):

################################################################################################################################

class _InterfaceBuiltInItem: # pylint: disable=too-few-public-methods
class _InterfaceBuiltInItem:
@classmethod
def _item_from_text(cls, data):
return data
Expand Down
10 changes: 5 additions & 5 deletions ckipnlp/container/parsed_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class ParsedTree(_Base, _Tree):

@staticmethod
def normalize_text(tree_text):
"""Text normalization for :class:`ckipnlp.parser.CkipParsed` output.
"""Text normalization.
Remove leading number and trailing ``#``.
"""
Expand All @@ -290,7 +290,7 @@ def __str__(self):
self.to_text()

@classmethod
def from_text(cls, data, *, normalize=True): # pylint: disable=arguments-differ
def from_text(cls, data, *, normalize=True):
"""Construct an instance from text format.
Parameters
Expand Down Expand Up @@ -343,7 +343,7 @@ def from_text(cls, data, *, normalize=True): # pylint: disable=arguments-differ

return tree

def to_text(self, node_id=0): # pylint: disable=arguments-differ
def to_text(self, node_id=0):
"""Transform to plain text.
Parameters
Expand Down Expand Up @@ -389,7 +389,7 @@ def from_dict(cls, data):

return tree

def to_dict(self, node_id=0): # pylint: disable=arguments-differ
def to_dict(self, node_id=0):
"""Construct an instance a from python built-in containers.
Parameters
Expand All @@ -409,7 +409,7 @@ def to_dict(self, node_id=0): # pylint: disable=arguments-differ

return tree_dict

def show(self, *, # pylint: disable=arguments-differ
def show(self, *,
key=lambda node: node.identifier,
idhidden=False,
**kwargs,
Expand Down
18 changes: 9 additions & 9 deletions ckipnlp/container/wspos.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class WsPosToken(_BaseTuple, _WsPosToken):
Note
----
This class is an subclass of :class:`tuple`. To change the attribute, please create a new instance instead.
This class is an subclass of *tuple*. To change the attribute, please create a new instance instead.
.. admonition:: Data Structure Examples
Expand Down Expand Up @@ -148,9 +148,9 @@ def from_text(cls, data):
Returns
-------
:class:`SegSentence`:
:class:`SegSentence <.seg.SegSentence>`:
the word sentence
:class:`SegSentence`:
:class:`SegSentence <.seg.SegSentence>`:
the POS-tag sentence.
"""
return tuple(map(_SegSentence.from_list, _sentence_from_text(data)))
Expand All @@ -161,9 +161,9 @@ def to_text(word, pos):
Parameters
----------
word : :class:`SegSentence`
word : :class:`SegSentence <.seg.SegSentence>`
the word sentence
pos : :class:`SegSentence`
pos : :class:`SegSentence <.seg.SegSentence>`
the POS-tag sentence.
Returns
Expand Down Expand Up @@ -195,9 +195,9 @@ def from_text(cls, data):
Returns
-------
:class:`SegParagraph`:
:class:`SegParagraph <.seg.SegParagraph>`:
the word sentence list
:class:`SegParagraph`:
:class:`SegParagraph <.seg.SegParagraph>`:
the POS-tag sentence list.
"""
return tuple(map(_SegParagraph.from_list, _paragraph_from_text(data)))
Expand All @@ -208,9 +208,9 @@ def to_text(word, pos):
Parameters
----------
word : :class:`SegParagraph`
word : :class:`SegParagraph <.seg.SegParagraph>`
the word sentence list
pos : :class:`SegParagraph`
pos : :class:`SegParagraph <.seg.SegParagraph>`
the POS-tag sentence list.
Returns
Expand Down
93 changes: 51 additions & 42 deletions ckipnlp/driver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
abstractmethod as _abstractmethod,
)

from collections import (
UserDict as _UserDict,
)

from enum import (
IntEnum as _IntEnum,
auto as _enum_auto,
Expand All @@ -31,11 +27,11 @@

class DriverType(_IntEnum):
SENTENCE_SEGMENTER = _enum_auto() #: Sentence segmentation
WORD_SEGMENTER = _enum_auto() #: Word segmentation
POS_TAGGER = _enum_auto() #: Part-of-speech tagging
SENTENCE_PARSER = _enum_auto() #: Sentence parsing
NER_CHUNKER = _enum_auto() #: Named-entity recognition
COREF_CHUNKER = _enum_auto() #: Co-reference
WORD_SEGMENTER = _enum_auto() #: Word segmentation
POS_TAGGER = _enum_auto() #: Part-of-speech tagging
SENTENCE_PARSER = _enum_auto() #: Sentence parsing
NER_CHUNKER = _enum_auto() #: Named-entity recognition
COREF_CHUNKER = _enum_auto() #: Co-reference

class DriverKind(_IntEnum):
BUILTIN = _enum_auto() #: Built-in Implementation
Expand All @@ -44,18 +40,38 @@ class DriverKind(_IntEnum):

################################################################################################################################

_DRIVERS = {}
class DriverRegester:

_DRIVERS = {}

@staticmethod
def get(driver_type, driver_kind):
if driver_kind is None:
return DummyDriver

assert driver_type is None or isinstance(driver_type, DriverType), f'{driver_type} is not a DriverType'
assert driver_kind is None or isinstance(driver_kind, DriverKind), f'{driver_kind} is not a DriverKind'

driver = DriverRegester._DRIVERS.get((driver_type, driver_kind,))
if not driver:
raise KeyError(f'{driver_type.name} is not implemented for type {driver_kind.name}')
if not driver.is_dummy:
_get_logger().debug(f'Using {driver.__name__} ...')

return driver

################################################################################################################################

class BaseDriver(metaclass=_ABCMeta): # pylint: disable=too-few-public-methods
class BaseDriver(metaclass=_ABCMeta):
"""The base CKIPNLP driver."""

is_dummy = False

def __init__(self, *, init=True):
def __init__(self, *, lazy=False):
self._core = None
self._inited = False

if init:
if not lazy:
self.init()

def init(self):
Expand All @@ -69,6 +85,16 @@ def __call__(self, *args, **kwargs):
self.init()
return self._call(*args, **kwargs)

########################################################################################################################

@_abstractmethod
def driver_type(self):
return NotImplemented

@_abstractmethod
def driver_kind(self):
return NotImplemented

@_abstractmethod
def _init(self):
return NotImplemented
Expand All @@ -79,47 +105,30 @@ def _call(self):

########################################################################################################################

def __init_subclass__(cls, *, driver_type, driver_kind, **kwargs):
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)

if driver_type:
assert isinstance(driver_type, DriverType), f'{driver_type} is not a DriverType'
driver_type = cls.driver_type
driver_kind = cls.driver_kind

if driver_kind:
assert isinstance(driver_kind, DriverKind), f'{driver_kind} is not a DriverKind'
assert driver_type is None or isinstance(driver_type, DriverType), f'{driver_type} is not a DriverType'
assert driver_kind is None or isinstance(driver_kind, DriverKind), f'{driver_kind} is not a DriverKind'

key = (driver_type, driver_kind,)
assert key not in _DRIVERS, f'{key} already registered!'
_DRIVERS[key] = cls

@staticmethod
def get(driver_type, driver_kind):
if driver_kind is None:
return DummyDriver

if driver_type:
assert isinstance(driver_type, DriverType), f'{driver_type} is not a DriverType'

if driver_kind:
assert isinstance(driver_kind, DriverKind), f'{driver_kind} is not a DriverKind'

driver = _DRIVERS.get((driver_type, driver_kind,))
if not driver:
raise KeyError(f'{driver_type.name} is not implemented for type {driver_kind.name}')
if not driver.is_dummy:
_get_logger().debug(f'Using {driver.__name__} ...')

return driver
assert key not in DriverRegester._DRIVERS, f'{key} already registered!' # pylint: disable=protected-access
DriverRegester._DRIVERS[key] = cls # pylint: disable=protected-access

################################################################################################################################

class DummyDriver(BaseDriver, driver_type=None, driver_kind=None): # pylint: disable=too-few-public-methods
class DummyDriver(BaseDriver):
"""The dummy driver."""

driver_type = None
driver_kind = None
is_dummy = True

def __init__(self, *, init=True):
super().__init__(init=init)
def __init__(self, *, lazy=False):
super().__init__(lazy=lazy)
self._inited = True

def _init(self):
Expand Down
34 changes: 16 additions & 18 deletions ckipnlp/driver/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@

################################################################################################################################

class CkipClassicWordSegmenter(_BaseDriver, # pylint: disable=too-few-public-methods
driver_type=_DriverType.WORD_SEGMENTER,
driver_kind=_DriverKind.CLASSIC,
):
class CkipClassicWordSegmenter(_BaseDriver):
"""The CKIP word segmentation driver with CkipClassic backend."""

driver_type = _DriverType.WORD_SEGMENTER
driver_kind = _DriverKind.CLASSIC

_count = 0

def __init__(self, *, do_pos=False, init=True):
super().__init__(init=init)
def __init__(self, *, do_pos=False, lazy=False):
super().__init__(lazy=lazy)
self._do_pos = do_pos

def _init(self):
if self.__class__._count >= 1:
self.__class__._count += 1 # pylint: disable=protected-access
if self.__class__._count > 1: # pylint: disable=protected-access
raise RuntimeError(f'Never instance more than one {self.__class__.__name__}!')
self.__class__._count += 1

import ckip_classic.ws
self._core = ckip_classic.ws.CkipWs()
Expand All @@ -48,20 +48,18 @@ def _call(self, *, text):

return ws, pos if self._do_pos else ws

class CkipClassicSentenceParser(_BaseDriver, # pylint: disable=too-few-public-methods
driver_type=_DriverType.SENTENCE_PARSER,
driver_kind=_DriverKind.CLASSIC,
):
class CkipClassicSentenceParser(_BaseDriver):
"""The CKIP sentence parsing driver with CkipClassic backend."""

driver_type = _DriverType.SENTENCE_PARSER
driver_kind = _DriverKind.CLASSIC

_count = 0

def _init(self):
super()._init()

if self.__class__._count >= 1:
self.__class__._count += 1 # pylint: disable=protected-access
if self.__class__._count > 1: # pylint: disable=protected-access
raise RuntimeError(f'Never instance more than one {self.__class__.__name__}!')
self.__class__._count += 1

import ckip_classic.parser
self._core = ckip_classic.parser.CkipParser(do_ws=False)
Expand All @@ -70,8 +68,8 @@ def _call(self, *, ws, pos):
assert isinstance(ws, _SegParagraph)
assert isinstance(pos, _SegParagraph)

ws_text = _WsPosParagraph.to_text(ws, pos)
parsed_text = self._core.apply_list(ws_text)
wspos_text = _WsPosParagraph.to_text(ws, pos)
parsed_text = self._core.apply_list(wspos_text)
parsed = _ParsedParagraph.from_text(parsed_text)

return parsed
Expand Down

0 comments on commit 6b43502

Please sign in to comment.