Skip to content

Commit 70e54f4

Browse files
committed
PYTHON-PACKAGE-004 added set_analyzers_folder function
Signed-off-by: David de Hilster <[email protected]>
1 parent 1ff5456 commit 70e54f4

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

NLPPlus/__init__.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Engine:
7777
def __init__(
7878
self,
7979
working_folder: Optional[PathLike] = None,
80-
analyzer_name: str = "parse-en-us",
80+
analyzer_path: str = None,
8181
verbose: bool = False,
8282
initialize: bool = False,
8383
):
@@ -88,6 +88,7 @@ def __init__(
8888
else:
8989
self.tmpdir = None
9090
self.working_folder = Path(working_folder)
91+
self.analyzer_path = None
9192
if initialize:
9293
copytree(
9394
Path(__file__).parent / "analyzers", self.working_folder / "analyzers"
@@ -106,18 +107,29 @@ def __init__(
106107

107108
def analyze(self, text: str, analyzer_name: str) -> Results:
108109
"""Analyze text with the named analyzer."""
109-
self.analyzer_name = analyzer_name
110+
analyzer_name = Path(analyzer_name)
110111
outdir = self.working_folder / "analyzers" / analyzer_name / "output"
111-
outtext = self.engine.analyze(analyzer_name, text)
112+
if self.analyzer_path:
113+
analyzer_name = Path(self.analyzer_path) / analyzer_name
114+
outdir = Path(self.analyzer_path) / "analyzers" / analyzer_name / "output"
115+
outtext = self.engine.analyze(str(analyzer_name), text)
112116
return Results(outtext, outdir)
113117

114118
def input_text(self, analyzer_name: str, file_name: str) -> str:
115119
"""Return the text from a file in the input directory."""
116-
file_path: str = self.working_folder / "analyzers" / analyzer_name / "input" / file_name
120+
file_path = Path(self.analyzer_path) / analyzer_name / "input" / file_name
121+
if not file_path.is_file():
122+
raise EngineException(
123+
f"File not found in input directory '{file_path}'"
124+
)
117125
with open(file_path, "rt", encoding="utf-8") as file:
118126
text = file.read()
119127
return text
120128

129+
def set_analyzers_folder(self, analyzer_name: str):
130+
"""Set analyzers directory path."""
131+
self.analyzer_path = analyzer_name
132+
121133

122134
engine = Engine()
123135

@@ -138,6 +150,11 @@ def set_working_folder(working_folder: Optional[str] = None, initialize: bool =
138150
engine = Engine(Path(working_folder), initialize=initialize)
139151

140152

153+
def set_analyzers_folder(analyzer_folder_path: str):
154+
"""Run the analyzer named on the input string."""
155+
engine.set_analyzers_folder(analyzer_folder_path)
156+
157+
141158
def analyze(str: str, parser: str = "parse-en-us"):
142159
"""Run the analyzer named on the input string."""
143160
return engine.analyze(str, parser).output_text

tests/test_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import NLPPlus
1414

1515
DATADIR = Path(__file__).parent / "data"
16-
ANADATADIR = Path(__file__).parent.parent / "NLPPlus" / "data"
16+
NLPPLUSDIR = Path(__file__).parent.parent / "NLPPlus"
1717

1818

1919
def read_file(path):
@@ -36,7 +36,7 @@ def test_working_dir(self):
3636
"""Test that set_working_folder works."""
3737
tmpdir = TemporaryDirectory(prefix="test-nlpplus")
3838
copytree(DATADIR.parent / "analyzers", Path(tmpdir.name) / "analyzers")
39-
copytree(ANADATADIR, Path(tmpdir.name) / "data")
39+
copytree(NLPPLUSDIR / "data", Path(tmpdir.name) / "data")
4040
NLPPlus.set_working_folder(tmpdir.name)
4141
text = read_file(DATADIR / "basic" / "text.txt")
4242
results = NLPPlus.engine.analyze(text, "basic")

0 commit comments

Comments
 (0)