Skip to content

Commit e0b0408

Browse files
Added singleton to common code
1 parent 5fbef45 commit e0b0408

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

extractors/personal_identifiers/location_extraction/code_snippet_common.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
import spacy
33
from typing import List, Tuple
44

5-
nlp = spacy.load("en_core_web_sm")
5+
loaded_models = {}
6+
def load_spacy(spacy_model):
7+
if spacy_model not in loaded_models:
8+
loaded_models[spacy_model] = spacy.load(spacy_model)
9+
return loaded_models[spacy_model]
610

7-
def location_extraction(text: str, extraction_keyword: str) -> List[Tuple[str, int]]:
11+
12+
def location_extraction(text: str, extraction_keyword: str, spacy_model: str = "en_core_web_sm") -> List[Tuple[str, int]]:
813
"""
914
@param text: the input text
1015
@param extraction_keyword: the label that is assigned to extracted words
1116
@return: positions of extracted names of persons
1217
"""
13-
18+
nlp = load_spacy(spacy_model)
1419
doc = nlp(text)
1520

1621
name_positions = []
@@ -19,6 +24,7 @@ def location_extraction(text: str, extraction_keyword: str) -> List[Tuple[str, i
1924
name_positions.append((extraction_keyword, ent.start, ent.end))
2025
return name_positions
2126

27+
2228
# ↑ necessary bricks function
2329
# -----------------------------------------------------------------------------------------
2430
# ↓ example implementation
@@ -34,4 +40,4 @@ def example_integration():
3440
print(f"text: \"{text}\" doesn't have {label}")
3541

3642
example_integration()
37-
```
43+
```

0 commit comments

Comments
 (0)