prenomen 2.0.0__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.
- prenomen-2.0.0/.gitignore +22 -0
- prenomen-2.0.0/CITATION.cff +23 -0
- prenomen-2.0.0/LICENSE +21 -0
- prenomen-2.0.0/PKG-INFO +486 -0
- prenomen-2.0.0/README.md +451 -0
- prenomen-2.0.0/THIRD-PARTY-LICENSES.md +144 -0
- prenomen-2.0.0/examples/authors.csv +26 -0
- prenomen-2.0.0/examples/batch.py +78 -0
- prenomen-2.0.0/examples/prenomen.yaml +96 -0
- prenomen-2.0.0/examples/quickstart.py +28 -0
- prenomen-2.0.0/pyproject.toml +71 -0
- prenomen-2.0.0/src/prenomen/__init__.py +64 -0
- prenomen-2.0.0/src/prenomen/client.py +57 -0
- prenomen-2.0.0/src/prenomen/confidence.py +133 -0
- prenomen-2.0.0/src/prenomen/config.py +150 -0
- prenomen-2.0.0/src/prenomen/dict_lookup.py +357 -0
- prenomen-2.0.0/src/prenomen/dictionaries.py +478 -0
- prenomen-2.0.0/src/prenomen/inference.py +574 -0
- prenomen-2.0.0/src/prenomen/models.py +55 -0
- prenomen-2.0.0/src/prenomen/names.py +97 -0
- prenomen-2.0.0/src/prenomen/prompt.py +36 -0
- prenomen-2.0.0/src/prenomen/results.py +159 -0
- prenomen-2.0.0/src/prenomen/templates/gender_prompt.j2 +54 -0
- prenomen-2.0.0/tests/conftest.py +125 -0
- prenomen-2.0.0/tests/fixtures/prompt_country_label_country.txt +38 -0
- prenomen-2.0.0/tests/fixtures/prompt_country_label_custom.txt +38 -0
- prenomen-2.0.0/tests/fixtures/prompt_dicts_and_countries.txt +38 -0
- prenomen-2.0.0/tests/fixtures/prompt_dicts_no_country.txt +36 -0
- prenomen-2.0.0/tests/fixtures/prompt_extra_context.txt +40 -0
- prenomen-2.0.0/tests/fixtures/prompt_llm_only.txt +28 -0
- prenomen-2.0.0/tests/test_api.py +622 -0
- prenomen-2.0.0/tests/test_client.py +68 -0
- prenomen-2.0.0/tests/test_coalescing.py +215 -0
- prenomen-2.0.0/tests/test_confidence.py +195 -0
- prenomen-2.0.0/tests/test_config.py +195 -0
- prenomen-2.0.0/tests/test_dict_lookup.py +332 -0
- prenomen-2.0.0/tests/test_dictionaries.py +110 -0
- prenomen-2.0.0/tests/test_names.py +57 -0
- prenomen-2.0.0/tests/test_over_http.py +203 -0
- prenomen-2.0.0/tests/test_prompt.py +240 -0
- prenomen-2.0.0/tests/test_results.py +162 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Generated results and figures
|
|
2
|
+
/output/
|
|
3
|
+
|
|
4
|
+
# Dictionary files: large, separately licensed, download them yourself
|
|
5
|
+
/data/
|
|
6
|
+
|
|
7
|
+
# Python
|
|
8
|
+
__pycache__/
|
|
9
|
+
*.py[cod]
|
|
10
|
+
.venv/
|
|
11
|
+
*.egg-info/
|
|
12
|
+
build/
|
|
13
|
+
dist/
|
|
14
|
+
.pytest_cache/
|
|
15
|
+
|
|
16
|
+
# Institutional paperwork and editor lock files, kept out of the package repo
|
|
17
|
+
*.odt
|
|
18
|
+
*.pdf
|
|
19
|
+
.~lock.*#
|
|
20
|
+
|
|
21
|
+
# Local AI-assistant working notes
|
|
22
|
+
/.claude/
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use this software, please cite it as below."
|
|
3
|
+
title: "Prenomen: hybrid dictionary and language-model gender inference from personal names"
|
|
4
|
+
type: software
|
|
5
|
+
authors:
|
|
6
|
+
- family-names: Philippe
|
|
7
|
+
given-names: Olivier
|
|
8
|
+
orcid: "https://orcid.org/0000-0003-3180-2025"
|
|
9
|
+
affiliation: "Barcelona Supercomputing Center"
|
|
10
|
+
version: 2.0.0
|
|
11
|
+
date-released: "2026-12-01"
|
|
12
|
+
license: MIT
|
|
13
|
+
repository-code: "https://github.com/Oliph/prenomen"
|
|
14
|
+
abstract: >-
|
|
15
|
+
Infer gender from a personal name, combining name-gender dictionaries with a
|
|
16
|
+
language model. Names the dictionaries settle conclusively never reach the
|
|
17
|
+
model; the rest go to the model with the dictionary evidence in the prompt.
|
|
18
|
+
keywords:
|
|
19
|
+
- gender
|
|
20
|
+
- name
|
|
21
|
+
- bibliometrics
|
|
22
|
+
- scientometrics
|
|
23
|
+
- llm
|
prenomen-2.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Olivier Philippe
|
|
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.
|
prenomen-2.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: prenomen
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Infer gender from a personal name, combining name-gender dictionaries with a language model.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Oliph/prenomen
|
|
6
|
+
Project-URL: Source, https://github.com/Oliph/prenomen
|
|
7
|
+
Project-URL: Issues, https://github.com/Oliph/prenomen/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/Oliph/prenomen/releases
|
|
9
|
+
Author: Olivier Philippe
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
License-File: THIRD-PARTY-LICENSES.md
|
|
13
|
+
Keywords: bibliometrics,gender,llm,name,scientometrics
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Requires-Dist: jinja2>=3.1
|
|
28
|
+
Requires-Dist: openai>=1.92
|
|
29
|
+
Requires-Dist: pandas>=2.0
|
|
30
|
+
Requires-Dist: pydantic>=2.0
|
|
31
|
+
Requires-Dist: pyyaml>=6.0
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# Prenomen
|
|
37
|
+
|
|
38
|
+
Infer gender from a personal name, combining name-gender dictionaries with a
|
|
39
|
+
language model. One name in, one result out:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from prenomen import Prenomen
|
|
43
|
+
|
|
44
|
+
p = Prenomen(model="gpt-4o-mini", base_url="https://api.openai.com/v1",
|
|
45
|
+
api_key_env="OPENAI_API_KEY", dictionaries=["wgnd"])
|
|
46
|
+
|
|
47
|
+
r = p.infer("Marie", "Curie", countries=["FR", "PL"])
|
|
48
|
+
r.gender # <Gender.FEMALE: 'female'>
|
|
49
|
+
r.confidence # 0.97
|
|
50
|
+
r.decided_by # <Verdict.DICTIONARY: 'dictionary'> -- the model was never called
|
|
51
|
+
r.dict_matches # the evidence, one entry per dictionary
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Reading your data, iterating it and storing the answers are yours. Prenomen does
|
|
55
|
+
not know what a CSV is, and that is deliberate: every workflow already has a way to
|
|
56
|
+
do that — pandas, polars, Spark, an Airflow task, a notebook cell, a database
|
|
57
|
+
cursor — and none of them needs a library's opinion about it.
|
|
58
|
+
|
|
59
|
+
The method is hybrid on purpose. Dictionary lookups are fast, cheap and
|
|
60
|
+
transparent, but they miss unlisted names and cannot resolve a name that is
|
|
61
|
+
common to both genders in different regions. A language model handles those, but
|
|
62
|
+
is expensive to run over millions of records and works blind on rare names. So:
|
|
63
|
+
|
|
64
|
+
- Names the dictionaries settle conclusively never reach the model. On a large
|
|
65
|
+
bibliographic corpus this was roughly 45% of records.
|
|
66
|
+
- Everything else goes to the model **with the dictionary evidence in the
|
|
67
|
+
prompt**, so it arbitrates between sources rather than guessing unaided.
|
|
68
|
+
- Repeated names with the same country context share one request, including
|
|
69
|
+
concurrent ones, and answers are remembered for the life of the object.
|
|
70
|
+
|
|
71
|
+
Dictionaries are optional. Without them every name goes to the model.
|
|
72
|
+
|
|
73
|
+
## Install
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install prenomen
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Python 3.10 or newer; tested on 3.10 and 3.14.
|
|
80
|
+
|
|
81
|
+
## Build it once
|
|
82
|
+
|
|
83
|
+
Constructing a `Prenomen` parses the dictionaries, which is the expensive part of
|
|
84
|
+
the whole library:
|
|
85
|
+
|
|
86
|
+
| Dictionary | Coverage | Download | RAM to load | Load time |
|
|
87
|
+
|---|---|---|---|---|
|
|
88
|
+
| [WGND 2.0](https://doi.org/10.7910/DVN/MSEGSJ) | 3.8M names, 195 countries and territories | 87 MB | 5.5 GB | 5 min |
|
|
89
|
+
| [PinyinGender](https://doi.org/10.7910/DVN/UAARYY) | 96,797 romanized Chinese given names | 2.6 MB | negligible | 1 s |
|
|
90
|
+
| [CnGender](https://doi.org/10.7910/DVN/UAARYY) | 1,051,891 Chinese-character given names | 22 MB | 1 GB | 3 s |
|
|
91
|
+
| [naampy](https://doi.org/10.7910/DVN/WZGJBM) | 197,344 Indian given names, 31 states | 117 MB | 2 GB | 9 s |
|
|
92
|
+
|
|
93
|
+
All four together measured 6.8 GB and a five-and-a-half-minute load, almost all of
|
|
94
|
+
it WGND. So **build one object per process and reuse it for every name.** On a
|
|
95
|
+
laptop, start with the specialist lists that match your corpus and add WGND when
|
|
96
|
+
you have the memory for it.
|
|
97
|
+
|
|
98
|
+
Construction validates immediately and cheaply — every file must exist and carry
|
|
99
|
+
the columns its reader needs, which takes milliseconds — and defers the parse until
|
|
100
|
+
the first name. Pass `preload=True` in a script or a worker, so the wait happens at
|
|
101
|
+
a predictable moment rather than mid-run.
|
|
102
|
+
|
|
103
|
+
It cannot be pickled, because it holds an open HTTP client and those gigabytes. For
|
|
104
|
+
process-parallel work, build one per worker:
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
# Spark
|
|
108
|
+
def resolve(rows):
|
|
109
|
+
p = Prenomen(model=..., base_url=..., dictionaries=["wgnd"], preload=True)
|
|
110
|
+
return [p.infer(r.first, r.last).to_dict() for r in rows]
|
|
111
|
+
|
|
112
|
+
df.rdd.mapPartitions(resolve)
|
|
113
|
+
|
|
114
|
+
# joblib: one per process, built on first use
|
|
115
|
+
_engine = None
|
|
116
|
+
def engine():
|
|
117
|
+
global _engine
|
|
118
|
+
if _engine is None:
|
|
119
|
+
_engine = Prenomen(model=..., base_url=..., dictionaries=["wgnd"])
|
|
120
|
+
return _engine
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Many names
|
|
124
|
+
|
|
125
|
+
`infer_many` gives you bounded concurrency, request sharing and the cache without
|
|
126
|
+
writing any of it:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
results = p.infer_many([("Ada", "Lovelace"), ("Alan", "Turing")])
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
It takes `NameQuery` objects, `(first, last)` tuples, or mappings, and yields one
|
|
133
|
+
result per input **in input order**, so this lines up:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
frame["gender"] = [r.gender.value for r in
|
|
137
|
+
p.infer_many(zip(frame.first_name, frame.last_name))]
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The input may be a lazy generator over millions of rows: submission runs in a
|
|
141
|
+
sliding window, so neither the inputs nor the results are all held at once, and a
|
|
142
|
+
slow consumer applies backpressure instead of filling memory. Ordering costs
|
|
143
|
+
head-of-line blocking — one slow name delays yielding, though not the other
|
|
144
|
+
in-flight requests — so pass `ordered=False` to take answers as they arrive and
|
|
145
|
+
join on `record` instead.
|
|
146
|
+
|
|
147
|
+
`record` is anything you want echoed back on the result, untouched: a row id, a
|
|
148
|
+
namedtuple, the whole source row. It is how results find their way home.
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
queries = [NameQuery(r.first, r.last, countries=r.countries, record=r.id)
|
|
152
|
+
for r in rows]
|
|
153
|
+
for r in p.infer_many(queries):
|
|
154
|
+
store(r.record, r.gender)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
To build a frame, `to_dict()` flattens a result to one row:
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
import pandas as pd
|
|
161
|
+
pd.DataFrame(r.to_dict() for r in p.infer_many(queries))
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
See `examples/batch.py` for the 1.x pipeline rebuilt on this — CSV in, TSV out,
|
|
165
|
+
restartable — in about twenty lines.
|
|
166
|
+
|
|
167
|
+
### Async
|
|
168
|
+
|
|
169
|
+
Prenomen is synchronous, which is why `infer` and `infer_many` both work inside a
|
|
170
|
+
Jupyter cell, inside `asyncio.run`, and on any thread. From async code, hand it to
|
|
171
|
+
a thread:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
result = await asyncio.to_thread(p.infer, "Marie", "Curie")
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
That is real concurrency: the call is I/O-bound and releases the GIL.
|
|
178
|
+
|
|
179
|
+
## What the prompt says besides the name
|
|
180
|
+
|
|
181
|
+
Country codes filter the country-aware dictionaries and are shown to the model as
|
|
182
|
+
a soft hint. `country_label` sets how that hint is worded, since the codes are not
|
|
183
|
+
always affiliations:
|
|
184
|
+
|
|
185
|
+
| `country_label` | rendered line |
|
|
186
|
+
|---|---|
|
|
187
|
+
| `"affiliation"` (default, as published) | `The author is affiliated with institution(s) in: FR, IT.` |
|
|
188
|
+
| `"country"` | `The author is associated with the following country/countries: FR, IT.` |
|
|
189
|
+
| free text, e.g. `"The author's country of birth"` | `The author's country of birth: FR, IT.` |
|
|
190
|
+
|
|
191
|
+
`context` adds one `"<label>: <value>."` line per pair, in order, to the prompt
|
|
192
|
+
only; missing values (None, blank, NaN) are dropped. `context_labels` maps your
|
|
193
|
+
keys to the labels the model sees.
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
p = Prenomen(..., country_label="country",
|
|
197
|
+
context_labels={"nationality": "The author's nationality"})
|
|
198
|
+
p.infer("Andrea", "Rossi", countries="IT", context={"nationality": "FR"})
|
|
199
|
+
# The author is associated with the following country/countries: IT.
|
|
200
|
+
# Use this as a soft hint for regional naming conventions, not as a hard constraint.
|
|
201
|
+
# The author's nationality: FR.
|
|
202
|
+
|
|
203
|
+
# Engine settings are defaults; a query can override them.
|
|
204
|
+
NameQuery("Andrea", "Rossi", countries="IT", country_label="affiliation")
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
In a config, for a dataset of name–country pairs:
|
|
208
|
+
|
|
209
|
+
```yaml
|
|
210
|
+
prompt:
|
|
211
|
+
country_label: country
|
|
212
|
+
context_labels:
|
|
213
|
+
nationality: "The author's nationality"
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
The defaults render the published prompt byte for byte. Label and context are
|
|
217
|
+
part of the cache key, and are reported on `result.country_label` and
|
|
218
|
+
`result.context` (not in `to_dict()`).
|
|
219
|
+
|
|
220
|
+
## Results
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
r = p.infer("Marie", "Curie", countries=["FR"])
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
| Field | Meaning |
|
|
227
|
+
|---|---|
|
|
228
|
+
| `first_name`, `last_name` | the name as you gave it, never the model's echo of it |
|
|
229
|
+
| `gender` | `Gender.MALE`, `Gender.FEMALE`, or `Gender.NOT_RETRIEVABLE` |
|
|
230
|
+
| `confidence` | 0 to 1; the model's own, or the dictionary confidence on a bypass |
|
|
231
|
+
| `reasoning_code` | why: `empirical_database`, `cultural_origin`, `linguistic_pattern`, `ambiguous`, `insufficient_data`, `dict_bypass`. `None` on a failure |
|
|
232
|
+
| `decided_by` | `Verdict.DICTIONARY`, `Verdict.MODEL`, or `Verdict.ERROR` |
|
|
233
|
+
| `cached` | a model answer was reused rather than requested |
|
|
234
|
+
| `countries` | as normalised: trimmed, upper-cased, deduplicated, sorted |
|
|
235
|
+
| `dict_matches` | one entry per loaded dictionary, matched or not, so you can audit the evidence |
|
|
236
|
+
| `dict_gender`, `dict_confidence` | what the dictionaries said, reported **even when the model overruled them** |
|
|
237
|
+
| `record` | whatever you passed in |
|
|
238
|
+
| `error`, `exception` | set only when `decided_by` is `ERROR` |
|
|
239
|
+
| `ok` | `False` when no answer was obtained |
|
|
240
|
+
|
|
241
|
+
`to_dict()` flattens this to the column names the 1.x TSV used, so analysis scripts
|
|
242
|
+
written against published output still read: `gender`, `llm_confidence`,
|
|
243
|
+
`reasoning_code`, `dict_bypass`, plus `<source>_gender` and `<source>_freq` per
|
|
244
|
+
dictionary, and `cached` and `error`. Two of those names are inherited misnomers —
|
|
245
|
+
`llm_confidence` carries the *dictionary* confidence on a bypassed row — and they
|
|
246
|
+
are kept anyway, because renaming the data buys nothing and costs comparability.
|
|
247
|
+
|
|
248
|
+
`p.columns(record_keys=["author_id"])` gives the matching header, including
|
|
249
|
+
whichever dictionaries are loaded, and refuses a `record` key that would collide
|
|
250
|
+
with a result field. Pass it to `to_dict(columns=...)` and a `csv.DictWriter` never
|
|
251
|
+
raises on an unexpected key.
|
|
252
|
+
|
|
253
|
+
### Failures are results, not exceptions
|
|
254
|
+
|
|
255
|
+
A timeout, an API error or an unparsable answer yields a result with
|
|
256
|
+
`decided_by=Verdict.ERROR`, `gender=NOT_RETRIEVABLE` and an `error` string. It is
|
|
257
|
+
not raised, because `infer_many` over a million names must not die on one 503, and
|
|
258
|
+
because you need a row to retry. Failures are never cached.
|
|
259
|
+
|
|
260
|
+
```python
|
|
261
|
+
for r in p.infer_many(queries):
|
|
262
|
+
if not r.ok:
|
|
263
|
+
retry_later(r.record, r.error)
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Mistakes *you* can fix are raised as `ValueError`, synchronously, before any
|
|
267
|
+
request: a blank name, an unknown dictionary, a missing dictionary file, an unset
|
|
268
|
+
key variable, a bad threshold. `r.raise_for_error()` is there if you want the
|
|
269
|
+
exception after all.
|
|
270
|
+
|
|
271
|
+
### On resuming
|
|
272
|
+
|
|
273
|
+
Resuming is yours now, and there are two pieces of it. Model answers are remembered
|
|
274
|
+
for the life of the object, so within one run a repeated name is free. Across runs,
|
|
275
|
+
the output is the record of what is done — `examples/batch.py` reads back its own
|
|
276
|
+
TSV in six lines and skips what is already there, retrying only the rows that carry
|
|
277
|
+
an `error`.
|
|
278
|
+
|
|
279
|
+
## Dictionaries
|
|
280
|
+
|
|
281
|
+
Optional. They are not shipped with this package: they are large, separately
|
|
282
|
+
published, and each wants its own citation. A helper downloads them for you.
|
|
283
|
+
|
|
284
|
+
```bash
|
|
285
|
+
prenomen-dicts list # what is available, and what you already have
|
|
286
|
+
prenomen-dicts path # where they are read from and written to
|
|
287
|
+
prenomen-dicts get wgnd # one of them
|
|
288
|
+
prenomen-dicts get all # all four
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Then name them, and nothing else:
|
|
292
|
+
|
|
293
|
+
```python
|
|
294
|
+
Prenomen(model=..., base_url=..., dictionaries=["wgnd", "naampy"])
|
|
295
|
+
Prenomen(model=..., base_url=..., dictionaries=prenomen.ALL_DICTIONARIES)
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
No config file is involved: the downloader and the library resolve the same
|
|
299
|
+
directory, in this order — the `dictionary_dir=` argument, `$PRENOMEN_DICT_DIR`,
|
|
300
|
+
`./data/dictionaries` if it already holds one of the files, then
|
|
301
|
+
`$XDG_DATA_HOME/prenomen/dictionaries`. `prenomen-dicts path` prints the answer, so
|
|
302
|
+
which one won is never a mystery. `--dest` puts files elsewhere, `--force`
|
|
303
|
+
refetches, and an interrupted download resumes where it stopped.
|
|
304
|
+
|
|
305
|
+
A dictionary that is named but missing, unreadable or misconfigured stops
|
|
306
|
+
construction, naming the source and the path. Leaving one out is how you turn it
|
|
307
|
+
off, never a missing file: a corpus-scale run that silently fell back to LLM-only
|
|
308
|
+
would be slower, less accurate and more expensive, with only a log line to show for
|
|
309
|
+
it.
|
|
310
|
+
|
|
311
|
+
All four are public CC0 deposits on Harvard Dataverse, so no account or token is
|
|
312
|
+
involved.
|
|
313
|
+
|
|
314
|
+
**Adding your own** is a `DictionarySpec` and no code, as long as the file is one
|
|
315
|
+
of two shapes:
|
|
316
|
+
|
|
317
|
+
```python
|
|
318
|
+
from prenomen import DictionarySpec
|
|
319
|
+
|
|
320
|
+
mine = DictionarySpec(name="my_list", path="~/names.tsv", format="counts",
|
|
321
|
+
separator="\t", name_col="name", male_col="m", female_col="f",
|
|
322
|
+
family="regional", weight=2.0)
|
|
323
|
+
Prenomen(model=..., base_url=..., dictionaries=["wgnd", mine])
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
- `format="counts"` — one row per name plus a male count and a female count, in
|
|
327
|
+
any units. Rows repeating a name across regions or years are summed.
|
|
328
|
+
- `format="wgnd"` — one row per name, country and gender, plus a frequency weight.
|
|
329
|
+
Set `country_aware=True` to filter by the countries on each record.
|
|
330
|
+
|
|
331
|
+
Column names are configurable for both. Two further keys control how much a
|
|
332
|
+
source is trusted:
|
|
333
|
+
|
|
334
|
+
- `family` groups near-duplicate sources, so two lists covering the same language
|
|
335
|
+
share one vote instead of compounding against a broad source.
|
|
336
|
+
- `weight` sets reliability. A specialist list covering one language or country
|
|
337
|
+
is narrow but precise, so a hit weighs double a global list.
|
|
338
|
+
|
|
339
|
+
### Confidence and the bypass gate
|
|
340
|
+
|
|
341
|
+
For each dictionary that matched, `p` is its frequency when it agrees with the
|
|
342
|
+
predicted gender and `1 − frequency` when it disagrees, where frequency is that
|
|
343
|
+
source's empirical P(gender | name). The dictionary confidence is the
|
|
344
|
+
reliability-weighted mean of those, bounded in [0, 1]:
|
|
345
|
+
|
|
346
|
+
```
|
|
347
|
+
confidence = Σ(wₛ · pₛ) / Σ wₛ
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
With one matching source this reduces to its own frequency ratio, so
|
|
351
|
+
`dict_bypass_threshold=0.85` reads as "at least 0.85 of occurrences of this name
|
|
352
|
+
carry this gender". Sources that disagree pull the score below the gate, and the
|
|
353
|
+
record goes to the model. A genuine tie scores 0.5 and never bypasses.
|
|
354
|
+
|
|
355
|
+
Lower the threshold to spend less on model calls, raise it to send more borderline
|
|
356
|
+
names to the model, set it to `0.0` to always call the model.
|
|
357
|
+
|
|
358
|
+
## Interpreting the output
|
|
359
|
+
|
|
360
|
+
`not retrievable` is an abstention, not a prediction. Score it as such, keeping
|
|
361
|
+
coverage and accuracy-on-answered separate.
|
|
362
|
+
|
|
363
|
+
A caution if you validate against known-correct labels. Gold labels derived from
|
|
364
|
+
names, including any name-gender dictionary, are circular here: the pipeline
|
|
365
|
+
already consumes those, so it shares their failure modes and will look better
|
|
366
|
+
than it is. A usable gold standard has to come from something independent of the
|
|
367
|
+
name, such as a curated biographical record or self-reported data.
|
|
368
|
+
|
|
369
|
+
**On the gender categories.** The model is constrained to a binary plus an
|
|
370
|
+
abstention. This reflects what the training signal and the dictionaries can
|
|
371
|
+
actually support, and it means the pipeline cannot represent non-binary people.
|
|
372
|
+
Anyone using it should say so in their methods, and should not treat its output
|
|
373
|
+
as a statement about identity. It estimates how a name is conventionally
|
|
374
|
+
gendered in a region, which is a different thing.
|
|
375
|
+
|
|
376
|
+
## Configuration
|
|
377
|
+
|
|
378
|
+
Every setting is a constructor argument. A YAML file is available for the ones you
|
|
379
|
+
want to keep and share:
|
|
380
|
+
|
|
381
|
+
```python
|
|
382
|
+
p = Prenomen.from_config("prenomen.yaml", max_workers=4) # kwargs win
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
See `examples/prenomen.yaml`, which documents every key.
|
|
386
|
+
|
|
387
|
+
## Migrating from 1.x
|
|
388
|
+
|
|
389
|
+
1.x was a batch pipeline driven by three YAML files and a `prenomen` command. That
|
|
390
|
+
command is gone, along with the data config: there is nothing to map your columns
|
|
391
|
+
onto any more, because you do the reading.
|
|
392
|
+
|
|
393
|
+
| 1.x | 2.x |
|
|
394
|
+
|---|---|
|
|
395
|
+
| `prenomen --data-config d.yaml` | `p.infer(...)` / `p.infer_many(...)` in your own loop |
|
|
396
|
+
| `data.name_columns` | you read the table; `prenomen.split_full_name` if you have one name column |
|
|
397
|
+
| `data.extra_columns`, `data.id_column` | `record=` on each query, echoed on the result |
|
|
398
|
+
| `data.country_column` | `countries=` per call; takes a list or `"FR;PL"` |
|
|
399
|
+
| `output.dir`, the derived TSV path | you choose the path and the writer |
|
|
400
|
+
| `output_header()` | `p.columns(record_keys=[...])` |
|
|
401
|
+
| `processing.continue`, `--continue` | your own check against your own output; see `examples/batch.py` |
|
|
402
|
+
| `processing.max_concurrent` | `max_workers=` |
|
|
403
|
+
| `processing.dict_bypass_threshold` | `dict_bypass_threshold=` |
|
|
404
|
+
| `dictionaries:` blocks with `enabled`/`path` | `dictionaries=["wgnd", ...]`, or a `DictionarySpec` |
|
|
405
|
+
| `prenomen-dicts get --no-enable` | the default; nothing edits a config any more |
|
|
406
|
+
| `model.yaml` | `model=`, `base_url=`, `api_key_env=`, `parameters=` |
|
|
407
|
+
| a row missing from the output meant failure | a result with `decided_by=ERROR` and an `error` |
|
|
408
|
+
| `dict_bypass` column | `decided_by`, plus `cached`; still `dict_bypass` in `to_dict()` |
|
|
409
|
+
|
|
410
|
+
The prompt is unchanged, byte for byte, so results are comparable across the two.
|
|
411
|
+
|
|
412
|
+
## Method and provenance
|
|
413
|
+
|
|
414
|
+
This is the reusable extraction of the pipeline built for a methods paper on
|
|
415
|
+
large-scale gender inference in bibliometric data, where it was validated against
|
|
416
|
+
an independent gold standard of several hundred thousand authors. The validation
|
|
417
|
+
harness, the corpus-scale HPC job scripts and the paper itself are not part of
|
|
418
|
+
this repository, which keeps only what transfers to other datasets.
|
|
419
|
+
|
|
420
|
+
## Development
|
|
421
|
+
|
|
422
|
+
```bash
|
|
423
|
+
uv run --extra dev pytest -q # the whole suite, no network
|
|
424
|
+
uv run --python 3.10 --extra dev pytest -q # the oldest supported interpreter
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
Tested on Python 3.10 and 3.14. Those two are what CI runs, and they are not the
|
|
428
|
+
same configuration: the lockfile resolves pandas 2 below Python 3.11 and pandas 3
|
|
429
|
+
above it, and the two differ in how they treat missing values. Run both before
|
|
430
|
+
releasing.
|
|
431
|
+
|
|
432
|
+
`tests/test_over_http.py` drives the real openai SDK against a loopback stub
|
|
433
|
+
endpoint, covering its retry path, timeout handling and connection reuse. It is the
|
|
434
|
+
only test that exercises the SDK itself, so it runs in CI rather than being skipped
|
|
435
|
+
as slow.
|
|
436
|
+
|
|
437
|
+
The prompt fixtures under `tests/fixtures/` pin the exact text sent to the model,
|
|
438
|
+
and `tests/test_prompt.py` checks them two ways: that the template renders them, and
|
|
439
|
+
that a live `infer` call still produces them. When a prompt change is intended,
|
|
440
|
+
regenerate them with `python -c "import tests.test_prompt as t; t.regenerate()"` and
|
|
441
|
+
review the diff — understanding that results from before and after are no longer
|
|
442
|
+
comparable.
|
|
443
|
+
|
|
444
|
+
## Intended use and responsibility
|
|
445
|
+
|
|
446
|
+
Prenomen is a research tool for aggregate analysis, such as measuring gender
|
|
447
|
+
balance across a population of authors. Its output is a statistical estimate of
|
|
448
|
+
how a name is conventionally gendered, not a fact about any person, and it will be
|
|
449
|
+
wrong for some of them.
|
|
450
|
+
|
|
451
|
+
Do not use it to make decisions about individuals: hiring, admission, funding,
|
|
452
|
+
access to services, or anything else that affects a specific person. Do not use it
|
|
453
|
+
to profile, target or discriminate against people. Names are personal data, and
|
|
454
|
+
with a hosted model they are sent to a third-party provider; making sure your use
|
|
455
|
+
complies with the applicable data-protection law (such as the GDPR) and with your
|
|
456
|
+
institution's ethics requirements is your responsibility.
|
|
457
|
+
|
|
458
|
+
The software is provided "as is", without warranty of any kind, as set out in the
|
|
459
|
+
`LICENSE`. The author accepts no responsibility for how it is used, for decisions
|
|
460
|
+
made from its output, or for any harm arising from its use or misuse. Responsibility
|
|
461
|
+
for that rests with the user.
|
|
462
|
+
|
|
463
|
+
## Use of AI assistance
|
|
464
|
+
|
|
465
|
+
Parts of this code, its tests and its documentation were written with the help of
|
|
466
|
+
AI coding assistants. Every change was reviewed, tested and accepted by the
|
|
467
|
+
author, who remains solely responsible for the code, its correctness and any
|
|
468
|
+
errors in it. Report problems through the issue tracker, as you would for any
|
|
469
|
+
other code.
|
|
470
|
+
|
|
471
|
+
## Author and citation
|
|
472
|
+
|
|
473
|
+
Olivier Philippe ([ORCID 0000-0003-3180-2025](https://orcid.org/0000-0003-3180-2025)).
|
|
474
|
+
|
|
475
|
+
To cite Prenomen, use the metadata in `CITATION.cff`, or the "Cite this
|
|
476
|
+
repository" button on GitHub.
|
|
477
|
+
|
|
478
|
+
## License
|
|
479
|
+
|
|
480
|
+
MIT. See `LICENSE`.
|
|
481
|
+
|
|
482
|
+
The dictionaries are **not** covered by it, and are not distributed with it.
|
|
483
|
+
The four `prenomen-dicts` knows about are all deposited under CC0 1.0, but they
|
|
484
|
+
are research datasets with papers behind them: cite them. `prenomen-dicts get`
|
|
485
|
+
prints the citation for everything it downloads. Check the terms yourself for any
|
|
486
|
+
dictionary you add, and before redistributing anything derived from one.
|