domherre 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- domherre-0.1.1/.gitignore +226 -0
- domherre-0.1.1/LICENSE +21 -0
- domherre-0.1.1/PKG-INFO +219 -0
- domherre-0.1.1/README.md +174 -0
- domherre-0.1.1/pyproject.toml +39 -0
- domherre-0.1.1/src/domherre/__init__.py +58 -0
- domherre-0.1.1/src/domherre/_email.py +14 -0
- domherre-0.1.1/src/domherre/_frames.py +115 -0
- domherre-0.1.1/src/domherre/_personnummer.py +32 -0
- domherre-0.1.1/src/domherre/backends.py +252 -0
- domherre-0.1.1/src/domherre/core.py +244 -0
- domherre-0.1.1/src/domherre/py.typed +0 -0
- domherre-0.1.1/tests/__init__.py +0 -0
- domherre-0.1.1/tests/conftest.py +60 -0
- domherre-0.1.1/tests/test_backends.py +147 -0
- domherre-0.1.1/tests/test_frames.py +100 -0
- domherre-0.1.1/tests/test_model.py +50 -0
- domherre-0.1.1/tests/test_personnummer.py +60 -0
- domherre-0.1.1/tests/test_redact.py +135 -0
- domherre-0.1.1/tests/test_report.py +46 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
# Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# UV
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
# uv.lock
|
|
103
|
+
|
|
104
|
+
# poetry
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
106
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
107
|
+
# commonly ignored for libraries.
|
|
108
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
109
|
+
# poetry.lock
|
|
110
|
+
# poetry.toml
|
|
111
|
+
|
|
112
|
+
# pdm
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
114
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
115
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
116
|
+
# pdm.lock
|
|
117
|
+
# pdm.toml
|
|
118
|
+
.pdm-python
|
|
119
|
+
.pdm-build/
|
|
120
|
+
|
|
121
|
+
# pixi
|
|
122
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
123
|
+
# pixi.lock
|
|
124
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
125
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
126
|
+
.pixi
|
|
127
|
+
|
|
128
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
129
|
+
__pypackages__/
|
|
130
|
+
|
|
131
|
+
# Celery stuff
|
|
132
|
+
celerybeat-schedule
|
|
133
|
+
celerybeat.pid
|
|
134
|
+
|
|
135
|
+
# Redis
|
|
136
|
+
*.rdb
|
|
137
|
+
*.aof
|
|
138
|
+
*.pid
|
|
139
|
+
|
|
140
|
+
# RabbitMQ
|
|
141
|
+
mnesia/
|
|
142
|
+
rabbitmq/
|
|
143
|
+
rabbitmq-data/
|
|
144
|
+
|
|
145
|
+
# ActiveMQ
|
|
146
|
+
activemq-data/
|
|
147
|
+
|
|
148
|
+
# SageMath parsed files
|
|
149
|
+
*.sage.py
|
|
150
|
+
|
|
151
|
+
# Environments
|
|
152
|
+
.env
|
|
153
|
+
.envrc
|
|
154
|
+
.venv
|
|
155
|
+
env/
|
|
156
|
+
venv/
|
|
157
|
+
ENV/
|
|
158
|
+
env.bak/
|
|
159
|
+
venv.bak/
|
|
160
|
+
|
|
161
|
+
# Spyder project settings
|
|
162
|
+
.spyderproject
|
|
163
|
+
.spyproject
|
|
164
|
+
|
|
165
|
+
# Rope project settings
|
|
166
|
+
.ropeproject
|
|
167
|
+
|
|
168
|
+
# mkdocs documentation
|
|
169
|
+
/site
|
|
170
|
+
|
|
171
|
+
# mypy
|
|
172
|
+
.mypy_cache/
|
|
173
|
+
.dmypy.json
|
|
174
|
+
dmypy.json
|
|
175
|
+
|
|
176
|
+
# Pyre type checker
|
|
177
|
+
.pyre/
|
|
178
|
+
|
|
179
|
+
# pytype static type analyzer
|
|
180
|
+
.pytype/
|
|
181
|
+
|
|
182
|
+
# Cython debug symbols
|
|
183
|
+
cython_debug/
|
|
184
|
+
|
|
185
|
+
# PyCharm
|
|
186
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
187
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
188
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
189
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
190
|
+
# .idea/
|
|
191
|
+
|
|
192
|
+
# Abstra
|
|
193
|
+
# Abstra is an AI-powered process automation framework.
|
|
194
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
195
|
+
# Learn more at https://abstra.io/docs
|
|
196
|
+
.abstra/
|
|
197
|
+
|
|
198
|
+
# Visual Studio Code
|
|
199
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
200
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
201
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
202
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
203
|
+
# .vscode/
|
|
204
|
+
# Temporary file for partial code execution
|
|
205
|
+
tempCodeRunnerFile.py
|
|
206
|
+
|
|
207
|
+
# Ruff stuff:
|
|
208
|
+
.ruff_cache/
|
|
209
|
+
|
|
210
|
+
# PyPI configuration file
|
|
211
|
+
.pypirc
|
|
212
|
+
|
|
213
|
+
# Marimo
|
|
214
|
+
marimo/_static/
|
|
215
|
+
marimo/_lsp/
|
|
216
|
+
__marimo__/
|
|
217
|
+
|
|
218
|
+
# Streamlit
|
|
219
|
+
.streamlit/secrets.toml
|
|
220
|
+
|
|
221
|
+
# macOS metadata
|
|
222
|
+
.DS_Store
|
|
223
|
+
|
|
224
|
+
# Model comparison output
|
|
225
|
+
compareModels/results_*.txt
|
|
226
|
+
compareModels/stats_file.txt
|
domherre-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander Koutakis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
domherre-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: domherre
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Redact Swedish names and personnummer from polars DataFrames
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 Alexander Koutakis
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Requires-Dist: polars>=1.0.0
|
|
29
|
+
Provides-Extra: all
|
|
30
|
+
Requires-Dist: gliner>=0.2.0; extra == 'all'
|
|
31
|
+
Requires-Dist: pandas>=2.0.0; extra == 'all'
|
|
32
|
+
Requires-Dist: presidio-analyzer>=2.2.0; extra == 'all'
|
|
33
|
+
Requires-Dist: torch>=2.0.0; extra == 'all'
|
|
34
|
+
Requires-Dist: transformers>=4.30.0; extra == 'all'
|
|
35
|
+
Provides-Extra: gliner
|
|
36
|
+
Requires-Dist: gliner>=0.2.0; extra == 'gliner'
|
|
37
|
+
Provides-Extra: pandas
|
|
38
|
+
Requires-Dist: pandas>=2.0.0; extra == 'pandas'
|
|
39
|
+
Provides-Extra: presidio
|
|
40
|
+
Requires-Dist: presidio-analyzer>=2.2.0; extra == 'presidio'
|
|
41
|
+
Provides-Extra: transformers
|
|
42
|
+
Requires-Dist: torch>=2.0.0; extra == 'transformers'
|
|
43
|
+
Requires-Dist: transformers>=4.30.0; extra == 'transformers'
|
|
44
|
+
Description-Content-Type: text/markdown
|
|
45
|
+
|
|
46
|
+
# domherre
|
|
47
|
+
|
|
48
|
+
Redacts Swedish personal names, personnummer and e-mail adresses from string columns in a polars DataFrame.
|
|
49
|
+
|
|
50
|
+
- **Personnummer** — regex (date-shaped) + Luhn checksum.
|
|
51
|
+
- **Names** — `KB/bert-base-swedish-cased-ner`, `PRS` entities only.
|
|
52
|
+
|
|
53
|
+
## Install
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install domherre
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## What you get
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import polars as pl
|
|
63
|
+
import domherre as kt
|
|
64
|
+
|
|
65
|
+
df = pl.DataFrame({
|
|
66
|
+
"id": [1, 2],
|
|
67
|
+
"note": [
|
|
68
|
+
"Engelbert Karlsson, personnummer 850101-0006, ringde idag.",
|
|
69
|
+
"Ingen känslig info här.",
|
|
70
|
+
],
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
kt.redact(df, "note")
|
|
74
|
+
# note -> note_redacted
|
|
75
|
+
# "[NAMN], personnummer [PERSONNUMMER], ringde idag."
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- Redacted columns are **renamed** with a suffix (default `_redacted`) so a redacted column
|
|
79
|
+
- can never be mistaken for the source. The original column is replaced, not kept.
|
|
80
|
+
- Unlisted columns are untouched. Raises if a target name already exists.
|
|
81
|
+
|
|
82
|
+
Several columns at once:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
kt.redact(df, ["note", "kommentar"])
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Single string:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
kt.redact_text("Anna Andersson, 850101-0006")
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
A one-time `PandasPerformanceWarning` fires per process. Silence it with
|
|
96
|
+
`DOMHERRE_QUIET=1` if you have already made your peace with pandas.
|
|
97
|
+
|
|
98
|
+
## Backend models
|
|
99
|
+
|
|
100
|
+
Name detection is pluggable. Personnummer detection is always regex + Luhn and is
|
|
101
|
+
not affected by the backend choice. This project defaults to kb-bert model as it has shown to be the formidable model for swedish name detection.
|
|
102
|
+
|
|
103
|
+
| Backend | Install | Notes |
|
|
104
|
+
|---|---|---|
|
|
105
|
+
| `kb-bert` (default) | included | `KB/bert-base-swedish-cased-ner`. Swedish-only, trained on formal text. |
|
|
106
|
+
| `gliner` | `domherre[gliner]` | Zero-shot, multilingual, CPU-optimised. Change `labels` to detect anything. |
|
|
107
|
+
| `presidio` | `domherre[presidio]` | Full PII framework. Bring your own configured `AnalyzerEngine`. |
|
|
108
|
+
| `none` | included | Skips name detection. Regex only. |
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
kt.redact(df, "note", kt.Config(backend="gliner"))
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### gliner
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
config = kt.Config(
|
|
118
|
+
backend="gliner",
|
|
119
|
+
model="urchade/gliner_multi_pii-v1", # default
|
|
120
|
+
labels=("person", "full name"),
|
|
121
|
+
min_score=0.5,
|
|
122
|
+
backend_options={"map_location": "cpu", "quantize": True},
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
### Custom backends
|
|
128
|
+
|
|
129
|
+
Anything with `load()` and `person_spans(texts) -> list[list[tuple[int, int]]]` works.
|
|
130
|
+
A plain callable works too:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
def detector(texts: list[str]) -> list[list[tuple[int, int]]]:
|
|
134
|
+
return [[(m.start(), m.end()) for m in pattern.finditer(t)] for t in texts]
|
|
135
|
+
|
|
136
|
+
kt.redact(df, "note", kt.Config(backend=detector))
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Register by name to make it selectable like a builtin:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
kt.register_backend("mine", MyBackend)
|
|
143
|
+
kt.available_backends() # ['gliner', 'kb-bert', 'mine', 'none', 'presidio']
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Backends are cached per (name, config), so a Deployment loads each model once.
|
|
147
|
+
|
|
148
|
+
## Config
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
config = kt.Config(
|
|
152
|
+
name_tag="[NAMN]",
|
|
153
|
+
personnummer_tag="[PERSONNUMMER]",
|
|
154
|
+
suffix="_redacted",
|
|
155
|
+
backend="kb-bert", # or "gliner", "presidio", "none", or your own
|
|
156
|
+
model=None, # backend default, or a local path: "/models/ner"
|
|
157
|
+
local_only=False, # True = never hit the network, fail loudly if not cached
|
|
158
|
+
num_threads=2, # pin to your k8s cpu limit
|
|
159
|
+
batch_size=32,
|
|
160
|
+
min_score=0.0, # raise to cut false-positive names, at the cost of misses
|
|
161
|
+
redact_names=True,
|
|
162
|
+
redact_personnummer=True,
|
|
163
|
+
validate_personnummer=True, # False = redact on shape alone, catches typo'd numbers
|
|
164
|
+
enumerate_names=False, # [NAMN_1], [NAMN_2] instead of flat [NAMN]
|
|
165
|
+
extra_patterns={}, # {"[EPOST]": r"\S+@\S+\.\w+"}
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
kt.redact(df, "note", config)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### enumerate_names
|
|
172
|
+
|
|
173
|
+
Distinguishes people within a single string, so downstream analysis can still tell
|
|
174
|
+
that two mentions refer to the same person:
|
|
175
|
+
|
|
176
|
+
```
|
|
177
|
+
"[NAMN_1] mailade [NAMN_2] om [NAMN_1]s ärende"
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Numbering is per string, not per DataFrame. `[NAMN_1]` in row 1 and row 2 are not
|
|
181
|
+
necessarily the same person — that would need identity resolution, which this does not do.
|
|
182
|
+
|
|
183
|
+
## Service deployment
|
|
184
|
+
|
|
185
|
+
Domherre is suited as deployemnts that can be run as services, we thus support models pre-baked into images.
|
|
186
|
+
Load the model at startup so the first request doesn't pay for it:
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
kt.load(backend="kb-bert", num_threads=2, local_only=True)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Bake the weights into the image and set `local_only=True`. Otherwise every cold pod
|
|
193
|
+
pulls ~440MB from HuggingFace, and a network blip becomes a runtime failure
|
|
194
|
+
in the middle of a batch instead of a clear error at boot.
|
|
195
|
+
|
|
196
|
+
```dockerfile
|
|
197
|
+
RUN python -c "from transformers import pipeline; \
|
|
198
|
+
pipeline('token-classification', model='KB/bert-base-swedish-cased-ner')"
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
```yaml
|
|
202
|
+
resources:
|
|
203
|
+
requests: { cpu: "1", memory: "2Gi" }
|
|
204
|
+
limits: { cpu: "2", memory: "3Gi" }
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Set `num_threads` to match the CPU limit. Torch otherwise reads the node's core count,
|
|
208
|
+
not the cgroup limit, and thrashes threads on a large node.
|
|
209
|
+
|
|
210
|
+
## Personnummer formats detected
|
|
211
|
+
|
|
212
|
+
`YYMMDD-XXXX`, `YYMMDD+XXXX`, `YYMMDDXXXX`, `YYYYMMDD-XXXX`, `YYYYMMDDXXXX`,
|
|
213
|
+
plus samordningsnummer (day + 60). All must pass the Luhn check unless
|
|
214
|
+
`validate_personnummer=False`.
|
|
215
|
+
|
|
216
|
+
Not detected: reservnummer/interimsnummer (region-specific, no fixed national format),
|
|
217
|
+
and personnummer with a typo'd control digit — set `validate_personnummer=False`
|
|
218
|
+
to catch those, at the cost of redacting unrelated 10 and 12-digit numbers.
|
|
219
|
+
|
domherre-0.1.1/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# domherre
|
|
2
|
+
|
|
3
|
+
Redacts Swedish personal names, personnummer and e-mail adresses from string columns in a polars DataFrame.
|
|
4
|
+
|
|
5
|
+
- **Personnummer** — regex (date-shaped) + Luhn checksum.
|
|
6
|
+
- **Names** — `KB/bert-base-swedish-cased-ner`, `PRS` entities only.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install domherre
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## What you get
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
import polars as pl
|
|
18
|
+
import domherre as kt
|
|
19
|
+
|
|
20
|
+
df = pl.DataFrame({
|
|
21
|
+
"id": [1, 2],
|
|
22
|
+
"note": [
|
|
23
|
+
"Engelbert Karlsson, personnummer 850101-0006, ringde idag.",
|
|
24
|
+
"Ingen känslig info här.",
|
|
25
|
+
],
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
kt.redact(df, "note")
|
|
29
|
+
# note -> note_redacted
|
|
30
|
+
# "[NAMN], personnummer [PERSONNUMMER], ringde idag."
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- Redacted columns are **renamed** with a suffix (default `_redacted`) so a redacted column
|
|
34
|
+
- can never be mistaken for the source. The original column is replaced, not kept.
|
|
35
|
+
- Unlisted columns are untouched. Raises if a target name already exists.
|
|
36
|
+
|
|
37
|
+
Several columns at once:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
kt.redact(df, ["note", "kommentar"])
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Single string:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
kt.redact_text("Anna Andersson, 850101-0006")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
A one-time `PandasPerformanceWarning` fires per process. Silence it with
|
|
51
|
+
`DOMHERRE_QUIET=1` if you have already made your peace with pandas.
|
|
52
|
+
|
|
53
|
+
## Backend models
|
|
54
|
+
|
|
55
|
+
Name detection is pluggable. Personnummer detection is always regex + Luhn and is
|
|
56
|
+
not affected by the backend choice. This project defaults to kb-bert model as it has shown to be the formidable model for swedish name detection.
|
|
57
|
+
|
|
58
|
+
| Backend | Install | Notes |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| `kb-bert` (default) | included | `KB/bert-base-swedish-cased-ner`. Swedish-only, trained on formal text. |
|
|
61
|
+
| `gliner` | `domherre[gliner]` | Zero-shot, multilingual, CPU-optimised. Change `labels` to detect anything. |
|
|
62
|
+
| `presidio` | `domherre[presidio]` | Full PII framework. Bring your own configured `AnalyzerEngine`. |
|
|
63
|
+
| `none` | included | Skips name detection. Regex only. |
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
kt.redact(df, "note", kt.Config(backend="gliner"))
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### gliner
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
config = kt.Config(
|
|
73
|
+
backend="gliner",
|
|
74
|
+
model="urchade/gliner_multi_pii-v1", # default
|
|
75
|
+
labels=("person", "full name"),
|
|
76
|
+
min_score=0.5,
|
|
77
|
+
backend_options={"map_location": "cpu", "quantize": True},
|
|
78
|
+
)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
### Custom backends
|
|
83
|
+
|
|
84
|
+
Anything with `load()` and `person_spans(texts) -> list[list[tuple[int, int]]]` works.
|
|
85
|
+
A plain callable works too:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
def detector(texts: list[str]) -> list[list[tuple[int, int]]]:
|
|
89
|
+
return [[(m.start(), m.end()) for m in pattern.finditer(t)] for t in texts]
|
|
90
|
+
|
|
91
|
+
kt.redact(df, "note", kt.Config(backend=detector))
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Register by name to make it selectable like a builtin:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
kt.register_backend("mine", MyBackend)
|
|
98
|
+
kt.available_backends() # ['gliner', 'kb-bert', 'mine', 'none', 'presidio']
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Backends are cached per (name, config), so a Deployment loads each model once.
|
|
102
|
+
|
|
103
|
+
## Config
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
config = kt.Config(
|
|
107
|
+
name_tag="[NAMN]",
|
|
108
|
+
personnummer_tag="[PERSONNUMMER]",
|
|
109
|
+
suffix="_redacted",
|
|
110
|
+
backend="kb-bert", # or "gliner", "presidio", "none", or your own
|
|
111
|
+
model=None, # backend default, or a local path: "/models/ner"
|
|
112
|
+
local_only=False, # True = never hit the network, fail loudly if not cached
|
|
113
|
+
num_threads=2, # pin to your k8s cpu limit
|
|
114
|
+
batch_size=32,
|
|
115
|
+
min_score=0.0, # raise to cut false-positive names, at the cost of misses
|
|
116
|
+
redact_names=True,
|
|
117
|
+
redact_personnummer=True,
|
|
118
|
+
validate_personnummer=True, # False = redact on shape alone, catches typo'd numbers
|
|
119
|
+
enumerate_names=False, # [NAMN_1], [NAMN_2] instead of flat [NAMN]
|
|
120
|
+
extra_patterns={}, # {"[EPOST]": r"\S+@\S+\.\w+"}
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
kt.redact(df, "note", config)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### enumerate_names
|
|
127
|
+
|
|
128
|
+
Distinguishes people within a single string, so downstream analysis can still tell
|
|
129
|
+
that two mentions refer to the same person:
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
"[NAMN_1] mailade [NAMN_2] om [NAMN_1]s ärende"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Numbering is per string, not per DataFrame. `[NAMN_1]` in row 1 and row 2 are not
|
|
136
|
+
necessarily the same person — that would need identity resolution, which this does not do.
|
|
137
|
+
|
|
138
|
+
## Service deployment
|
|
139
|
+
|
|
140
|
+
Domherre is suited as deployemnts that can be run as services, we thus support models pre-baked into images.
|
|
141
|
+
Load the model at startup so the first request doesn't pay for it:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
kt.load(backend="kb-bert", num_threads=2, local_only=True)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Bake the weights into the image and set `local_only=True`. Otherwise every cold pod
|
|
148
|
+
pulls ~440MB from HuggingFace, and a network blip becomes a runtime failure
|
|
149
|
+
in the middle of a batch instead of a clear error at boot.
|
|
150
|
+
|
|
151
|
+
```dockerfile
|
|
152
|
+
RUN python -c "from transformers import pipeline; \
|
|
153
|
+
pipeline('token-classification', model='KB/bert-base-swedish-cased-ner')"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
```yaml
|
|
157
|
+
resources:
|
|
158
|
+
requests: { cpu: "1", memory: "2Gi" }
|
|
159
|
+
limits: { cpu: "2", memory: "3Gi" }
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Set `num_threads` to match the CPU limit. Torch otherwise reads the node's core count,
|
|
163
|
+
not the cgroup limit, and thrashes threads on a large node.
|
|
164
|
+
|
|
165
|
+
## Personnummer formats detected
|
|
166
|
+
|
|
167
|
+
`YYMMDD-XXXX`, `YYMMDD+XXXX`, `YYMMDDXXXX`, `YYYYMMDD-XXXX`, `YYYYMMDDXXXX`,
|
|
168
|
+
plus samordningsnummer (day + 60). All must pass the Luhn check unless
|
|
169
|
+
`validate_personnummer=False`.
|
|
170
|
+
|
|
171
|
+
Not detected: reservnummer/interimsnummer (region-specific, no fixed national format),
|
|
172
|
+
and personnummer with a typo'd control digit — set `validate_personnummer=False`
|
|
173
|
+
to catch those, at the cost of redacting unrelated 10 and 12-digit numbers.
|
|
174
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "domherre"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
license = { file = "LICENSE" }
|
|
10
|
+
description = "Redact Swedish names and personnummer from polars DataFrames"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"polars>=1.0.0",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.optional-dependencies]
|
|
17
|
+
transformers = ["transformers>=4.30.0", "torch>=2.0.0"]
|
|
18
|
+
gliner = ["gliner>=0.2.0"]
|
|
19
|
+
presidio = ["presidio-analyzer>=2.2.0"]
|
|
20
|
+
pandas = ["pandas>=2.0.0"]
|
|
21
|
+
all = ["domherre[transformers,gliner,presidio,pandas]"]
|
|
22
|
+
|
|
23
|
+
[dependency-groups]
|
|
24
|
+
dev = [
|
|
25
|
+
"pytest>=7.0.0",
|
|
26
|
+
"pandas>=2.0.0",
|
|
27
|
+
"mypy>=1.10.0",
|
|
28
|
+
"ruff>=0.6.0",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[tool.hatch.build.targets.wheel]
|
|
32
|
+
packages = ["src/domherre"]
|
|
33
|
+
|
|
34
|
+
[tool.hatch.build.targets.sdist]
|
|
35
|
+
include = ["src/", "tests/", "README.md", "LICENSE"]
|
|
36
|
+
|
|
37
|
+
[tool.pytest.ini_options]
|
|
38
|
+
testpaths = ["tests"]
|
|
39
|
+
pythonpath = ["src"]
|