Skip to content

Commit

Permalink
Revamp project structure and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlls committed Apr 3, 2024
1 parent 85a99af commit 7977b15
Show file tree
Hide file tree
Showing 14 changed files with 992 additions and 613 deletions.
5 changes: 2 additions & 3 deletions negate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .negate import Negator
from .tokens import Token
from .utils.tokens import Token

# Don't expose the following submodules.
#del globals()["negate"]
#del globals()["tokens"]
del globals()["negate"]
63 changes: 63 additions & 0 deletions negate/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Base negator."""

from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional


class BaseNegator(ABC):
"""Base negator.
Specific negators for different languages must inherit from this class.
"""

@abstractmethod
def __init__(
self,
use_transformers: Optional[bool] = None,
use_gpu: Optional[bool] = None,
fail_on_unsupported: Optional[bool] = None,
log_level: Optional[int] = None,
**kwargs,
):
"""Instanciate a :obj:`Negator`.
Args:
use_transformers (:obj:`Optional[bool]`, defaults to :obj:`False`):
Whether to use a Transformer model for POS tagging and
dependency parsing.
use_gpu (:obj:`Optional[bool]`, defaults to :obj:`False`):
Whether to use the GPU, if available. This parameter is ignored
when :param:`use_transformers` is set to :obj:`False`.
fail_on_unsupported (:obj:`Optional[bool]`, defaults to :obj:`False`):
Whether to fail upon non-supported sentences. If set to
:obj:`False`, a warning will be printed, and the sentence will
try to be negated in a best-effort fashion.
log_level (:obj:`Optional[int]`, defaults to ``logging.INFO``):
The level of the logger.
Raises:
:obj:`RuntimeError`: If the sentence is not supported and
:arg:`fail_on_unsupported` is set to :obj:`True`.
"""
pass

@abstractmethod
def negate_sentence(
self,
sentence: str,
**kwargs: Dict[str, Any],
) -> List[str]:
"""Negate a sentence.
Affirmative sentences will be turned into negative ones and vice versa.
Args:
sentence (:obj:`str`):
The sentence to negate.
**kwargs (:obj:`Dict[str, Any]`):
Additional parameters to pass to the concrete language negator.
Returns:
:obj:`List[str]`: The negated sentence(s).
"""
pass
82 changes: 61 additions & 21 deletions negate/negate.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,70 @@
from negate.negator_en import Negator_EN
from negate.negator_de import Negator_DE
from typing import Optional
"""High-level negator."""

import importlib
from pathlib import Path
from typing import Dict, List, Optional

from .negators.supported_languages import Language


class Negator:
"""High-level negator."""

def __init__(
self, language: str = "EN",
use_transformers: Optional[bool] = None,
use_gpu: Optional[bool] = None,
fail_on_unsupported: Optional[bool] = None,
log_level: Optional[int] = None
self,
language: str,
*,
use_transformers: Optional[bool] = None,
use_gpu: Optional[bool] = None,
fail_on_unsupported: Optional[bool] = None,
log_level: Optional[int] = None,
**kwargs: Dict,
):
self.language = language
if language == "EN":
self.negator = Negator_EN(use_transformers, use_gpu, fail_on_unsupported, log_level)
elif language == "DE":
self.negator = Negator_DE(use_transformers, use_gpu, fail_on_unsupported, log_level)
else:
raise ValueError("Language not supported, supported languages are EN and DE")
"""Instanciate a :obj:`Negator`.
def negate_sentence(
self,
sentence: str,
*args, **kwargs
) -> set[str] | str:
return self.negator.negate_sentence(sentence, *args, **kwargs)
Args:
use_transformers (:obj:`Optional[bool]`, defaults to :obj:`False`):
Whether to use a Transformer model for POS tagging and
dependency parsing.
.. note::
When set to :obj:`True` the model `en_core_web_trf
<https://spacy.io/models/en#en_core_web_trf>`__ is used.
use_gpu (:obj:`Optional[bool]`, defaults to :obj:`False`):
Whether to use the GPU, if available. This parameter is
ignored when :param:`use_transformers` is set to :obj:`False`.
fail_on_unsupported (:obj:`Optional[bool]`, defaults to :obj:`False`):
Whether to fail upon non-supported sentences. If set to
:obj:`False`, a warning will be printed, and the negator
will try to negate the sentence in a best-effort fashion.
log_level (:obj:`Optional[int]`, defaults to ``logging.INFO``):
The level of the logger.
kwargs (:obj:`Dict`):
Any other parameters to pass to the language-specific
negators.
Raises:
:obj:`ValueError`: If the specified language is not supported.
"""
if not Language.is_supported(language):
raise ValueError(
f'The language "{language}" is currently not supported.\n'
f"Valid values are {Language.get_all()}"
)
self.language = language
self.negator = getattr(
importlib.import_module(
f".negators.{language}.negator", package=Path(__file__).parent.name
),
"Negator",
)(
use_transformers=use_transformers,
use_gpu=use_gpu,
fail_on_unsupported=fail_on_unsupported,
log_level=log_level,
**kwargs,
)

def negate_sentence(self, sentence: str, **kwargs) -> List[str]:
return self.negator.negate_sentence(sentence, **kwargs)
Loading

0 comments on commit 7977b15

Please sign in to comment.