Skip to content

Commit 26c0fa7

Browse files
authored
Merge pull request #7 from jftuga/feature-automate-download
advise user on model download
2 parents afb0cd1 + 713437d commit 26c0fa7

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

Pipfile.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deidentification/deidentification.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ def __init__(self, config: DeidentificationConfig = DeidentificationConfig()):
8989
torch.load = self.__safe_load
9090
spacy.prefer_gpu()
9191
if not Deidentification.nlp:
92-
Deidentification.nlp = spacy.load(self.config.spacy_model)
92+
try:
93+
Deidentification.nlp = spacy.load(self.config.spacy_model)
94+
except OSError as err:
95+
self.model_not_found_error(str(err))
96+
except Exception as err:
97+
raise err
9398

9499
def __str__(self) -> str:
95100
"""Return a string representation of the Deidentification instance.
@@ -107,6 +112,26 @@ def __str__(self) -> str:
107112
]
108113
return "\n".join(program_info) + "\n" + str(self.config)
109114

115+
def model_not_found_error(self, err: str):
116+
"""Handles errors related to missing spaCy models and provides installation instructions.
117+
118+
This function processes spaCy model errors, specifically handling cases where
119+
a required model cannot be found. If the error indicates a missing model,
120+
it prints installation instructions to stderr and exits the program.
121+
122+
Args:
123+
err: Error message string from the spaCy library.
124+
"""
125+
print(file=sys.stderr)
126+
print(str(err), file=sys.stderr)
127+
if "Can't find model" in str(err):
128+
print(file=sys.stderr)
129+
print("Please manually run the following command one time to download the required model:", file=sys.stderr)
130+
print(file=sys.stderr)
131+
print(f"python -m spacy download {self.config.spacy_model}", file=sys.stderr)
132+
print(file=sys.stderr)
133+
sys.exit(1)
134+
110135
def deidentify(self, text: str) -> str:
111136
"""De-identify personal information in the input text.
112137

deidentification/deidentification_constants.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pgmName = "deidentification"
22
pgmUrl = "https://github.com/jftuga/deidentification"
3-
pgmVersion = "1.1.2"
3+
pgmVersion = "1.2.0"
44

55
GENDER_PRONOUNS = {
66
"he": "HE/SHE",
@@ -15,30 +15,6 @@
1515
"mrs.": "",
1616
"ms.": ""}
1717

18-
# ALL_CONTRACTIONS = (
19-
# "it",
20-
# "that",
21-
# "what",
22-
# "there",
23-
# "here",
24-
# "let",
25-
# "how",
26-
# "where",
27-
# "who",
28-
# "when",
29-
# "one",
30-
# "somebody",
31-
# "someone",
32-
# "something",
33-
# "nobody",
34-
# "everyone",
35-
# "anybody",
36-
# "nothing",
37-
# "why",
38-
# "this",
39-
# "which")
40-
41-
4218
HTML_BEGIN = """<!DOCTYPE html>
4319
<html>
4420
<head>
@@ -81,4 +57,3 @@ class bcolors:
8157
ENDC = '\033[0m'
8258
BOLD = '\033[1m'
8359
UNDERLINE = '\033[4m'
84-

0 commit comments

Comments
 (0)