Diff of /edsnlp/__init__.py [000000] .. [cad161]

Switch to unified view

a b/edsnlp/__init__.py
1
"""
2
EDS-NLP
3
"""
4
5
import sys
6
import importlib.abc
7
import importlib.util
8
from pathlib import Path
9
from spacy import pipeline as _spacy_pipeline  # noqa: F401
10
from . import extensions
11
from . import patch_spacy
12
from .core.pipeline import Pipeline, blank, load
13
from .core.registries import registry
14
import edsnlp.data  # noqa: F401
15
import edsnlp.pipes
16
from . import reducers
17
18
__version__ = "0.17.0"
19
20
BASE_DIR = Path(__file__).parent
21
22
23
# Everything below is to support deprecated use of edsnlp.pipelines
24
# route imports of submodules of edsnlp.pipelines to their edsnlp.pipes counterparts.
25
# The same is done for edsnlp.scorers -> edsnlp.metrics
26
27
class AliasPathFinder(importlib.abc.MetaPathFinder):
28
    def find_spec(self, fullname, path, target=None):  # pragma: no cover
29
        if not fullname.startswith("edsnlp."):
30
            return None
31
        if fullname.startswith("edsnlp.pipelines"):
32
            new_name = "edsnlp.pipes" + fullname[16:]
33
            spec = importlib.util.spec_from_loader(fullname, AliasLoader(new_name))
34
            return spec
35
        if fullname.startswith("edsnlp.core.lazy_collection"):
36
            new_name = "edsnlp.core.stream" + fullname[27:]
37
            spec = importlib.util.spec_from_loader(fullname, AliasLoader(new_name))
38
            return spec
39
        if fullname.startswith("edsnlp.optimization"):
40
            new_name = "edsnlp.training.optimizer" + fullname[19:]
41
            spec = importlib.util.spec_from_loader(fullname, AliasLoader(new_name))
42
            return spec
43
        if fullname.startswith("edsnlp.scorers"):
44
            new_name = "edsnlp.metrics" + fullname[14:]
45
            spec = importlib.util.spec_from_loader(fullname, AliasLoader(new_name))
46
            return spec
47
        if fullname.startswith("edsnlp.metrics.span_classification"):
48
            new_name = "edsnlp.metrics.span_attributes" + fullname[34:]
49
            spec = importlib.util.spec_from_loader(fullname, AliasLoader(new_name))
50
            return spec
51
        if "span_qualifier" in fullname.split("."):
52
            new_name = fullname.replace("span_qualifier", "span_classifier")
53
            spec = importlib.util.spec_from_loader(fullname, AliasLoader(new_name))
54
            return spec
55
        if "measurements" in fullname.split("."):
56
            new_name = fullname.replace("measurements", "quantities")
57
            spec = importlib.util.spec_from_loader(fullname, AliasLoader(new_name))
58
            return spec
59
60
61
class AliasLoader(importlib.abc.Loader):
62
    def __init__(self, alias):
63
        self.alias = alias
64
65
    def create_module(self, spec):
66
        # import the edsnlp.pipe.* module first, then alias it to edsnlp.pipelines.*
67
        module = importlib.import_module(self.alias)
68
        sys.modules[spec.name] = module
69
        return module
70
71
    def exec_module(self, module):
72
        pass
73
74
75
# Add the custom finder to sys.path_hooks
76
sys.meta_path.insert(0, AliasPathFinder())