Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from zaibacu/config-revamp
Config revamp
- Loading branch information
Showing
19 changed files
with
409 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Config | ||
|
||
Configuration is mostly applied per-rule-basis, meaning, that different rules can have different configuration while running from same process. | ||
|
||
## Syntax | ||
|
||
It is intended to do configuration from within the rule, like so: | ||
|
||
``` | ||
!CONFIG("ignore_case", "Y") | ||
``` | ||
|
||
First argument is config key, second value. `"1"`, `"Y"` and `"T"` results in `True`, `"0"`, `"N"`, `"F"` - in `False` | ||
|
||
## Configurations | ||
|
||
| Setting | Default | Description | | ||
|--------------------|----------------------|----------------------------------------------------------------------------| | ||
| implicit_punct |`T` |Automatically adds punctuation characters `,.!:\;` to the rules | | ||
| ignore_case |`T` |All rules are case-insensitive | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,92 @@ | ||
class Config(object): | ||
@property | ||
def list_ignore_case(self): | ||
""" | ||
Ignore case while doing `IN_LIST` operation | ||
""" | ||
return True | ||
import operator | ||
import logging | ||
from importlib import import_module | ||
|
||
try: | ||
import spacy | ||
from rita.engine.translate_spacy import compile_rules as spacy_engine | ||
except ImportError: | ||
pass | ||
|
||
from rita.engine.translate_standalone import compile_rules as standalone_engine | ||
|
||
from rita.utils import SingletonMixin | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Config(SingletonMixin): | ||
def __init__(self): | ||
self.available_engines = [] | ||
self.engines_by_key = {} | ||
|
||
try: | ||
self.register_engine(1, "spacy", spacy_engine) | ||
except NameError: | ||
# spacy_engine is not imported | ||
pass | ||
self.register_engine(2, "standalone", standalone_engine) | ||
|
||
def register_engine(self, priority, key, compile_fn): | ||
self.available_engines.append((priority, key, compile_fn)) | ||
self.engines_by_key[key] = compile_fn | ||
sorted(self.available_engines, key=operator.itemgetter(0)) | ||
|
||
@property | ||
def implicit_punct(self): | ||
""" | ||
Automatically add optional Punctuation characters inside rule between macros. | ||
eg. `WORD(w1), WORD(w2)` | ||
would be converted into: | ||
`WORD(w1), PUNCT?, WORD(w2)` | ||
""" | ||
return True | ||
def default_engine(self): | ||
(_, _, compile_fn) = self.available_engines[0] | ||
return compile_fn | ||
|
||
def get_engine(self, key): | ||
return self.engines_by_key[key] | ||
|
||
|
||
|
||
class SessionConfig(object): | ||
def __init__(self): | ||
self._root = Config() | ||
self.modules = [] | ||
# Default config | ||
self._data = { | ||
"ignore_case": True, | ||
"implicit_punct": True | ||
|
||
} | ||
self.variables = {} | ||
|
||
def register_module(self, mod_name): | ||
logger.debug("Importing module: {}".format(mod_name)) | ||
self.modules.append(import_module(mod_name)) | ||
|
||
def set_variable(self, k, v): | ||
self.variables[k] = v | ||
|
||
def get_variable(self, k): | ||
return self.variables[k] | ||
|
||
def __getattr__(self, name): | ||
if name == "_root": | ||
return self._root | ||
|
||
elif name in self._data: | ||
return self._data[name] | ||
|
||
return getattr(self._root, name) | ||
|
||
def set_config(self, k, v): | ||
# Handle booleans first | ||
if v.upper() in ["1", "T", "Y"]: | ||
self._data[k] = True | ||
elif v.upper() in ["0", "F", "N"]: | ||
self._data[k] = False | ||
else: | ||
self._data[k] = v | ||
|
||
|
||
def with_config(fn): | ||
def wrapper(*args, **kwargs): | ||
config = SessionConfig() | ||
return fn(*args, config=config, **kwargs) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +0,0 @@ | ||
try: | ||
import spacy | ||
from .translate_spacy import compile_rules | ||
except ImportError: | ||
from .translate_standalone import compile_rules | ||
|
||
|
||
def get_default(): | ||
return compile_rules | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.