narrative-harm-classifier 0.1.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.
- narrative_harm_classifier-0.1.0/.env.example +32 -0
- narrative_harm_classifier-0.1.0/.github/workflows/ci.yml +52 -0
- narrative_harm_classifier-0.1.0/.github/workflows/publish.yml +50 -0
- narrative_harm_classifier-0.1.0/.gitignore +22 -0
- narrative_harm_classifier-0.1.0/CONTRIBUTING.md +159 -0
- narrative_harm_classifier-0.1.0/Dockerfile +12 -0
- narrative_harm_classifier-0.1.0/LICENSE +201 -0
- narrative_harm_classifier-0.1.0/PKG-INFO +825 -0
- narrative_harm_classifier-0.1.0/README.md +789 -0
- narrative_harm_classifier-0.1.0/assets/screenshot-benchmark.png +0 -0
- narrative_harm_classifier-0.1.0/assets/screenshot-classify.png +0 -0
- narrative_harm_classifier-0.1.0/assets/screenshot-track.png +0 -0
- narrative_harm_classifier-0.1.0/docker-compose.yml +13 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/__init__.py +29 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/main.py +50 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/routes/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/routes/benchmark.py +32 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/routes/classify.py +59 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/routes/health.py +23 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/routes/tracking.py +81 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/api/routes/validate.py +61 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/counter_narrative.py +65 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/factory.py +61 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/provenance.py +51 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/rules/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/rules/azure_nlp.py +137 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/rules/dogwhistles.py +68 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/rules/engine.py +360 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/rules/patterns_loader.py +87 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/taxonomy/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/taxonomy/loader.py +125 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/tracking/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/tracking/models.py +102 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/tracking/store.py +126 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/tracking/tracker.py +155 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/validators/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/validators/benchmark.py +221 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/validators/i18n_smoke.py +62 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/classifier/validators/performance.py +132 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/cli.py +180 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/core/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/core/config.py +60 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/core/models.py +134 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/core/yaml_loader.py +19 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/benchmark_templates.yaml +336 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/dogwhistles.yaml +134 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/i18n_smoke_tests.yaml +82 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/ar.yaml +97 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/en.yaml +133 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/es.yaml +99 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/fr.yaml +96 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/ha.yaml +40 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/ig.yaml +40 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/ru.yaml +89 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/patterns/yo.yaml +40 -0
- narrative_harm_classifier-0.1.0/narrative_harm_classifier/data/taxonomy_v1.yaml +115 -0
- narrative_harm_classifier-0.1.0/pyproject.toml +72 -0
- narrative_harm_classifier-0.1.0/requirements.txt +15 -0
- narrative_harm_classifier-0.1.0/scripts/measure_performance.py +82 -0
- narrative_harm_classifier-0.1.0/tests/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/tests/api/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/tests/api/test_api.py +129 -0
- narrative_harm_classifier-0.1.0/tests/benchmark/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/tests/benchmark/test_benchmark.py +130 -0
- narrative_harm_classifier-0.1.0/tests/integration/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/tests/integration/test_validation.py +57 -0
- narrative_harm_classifier-0.1.0/tests/unit/__init__.py +0 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_cli.py +84 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_counter_narrative.py +42 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_dogwhistles.py +60 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_engine.py +135 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_factory.py +33 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_i18n_smoke.py +28 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_multilingual.py +65 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_provenance.py +96 -0
- narrative_harm_classifier-0.1.0/tests/unit/test_tracking.py +122 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# --- App ---
|
|
2
|
+
APP_NAME=Narrative Harm Classifier
|
|
3
|
+
DEBUG=false
|
|
4
|
+
|
|
5
|
+
# --- Azure Text Analytics ---
|
|
6
|
+
# Get from Azure Portal → Cognitive Services → Text Analytics
|
|
7
|
+
AZURE_TEXT_ANALYTICS_ENDPOINT=https://<your-resource>.cognitiveservices.azure.com/
|
|
8
|
+
AZURE_TEXT_ANALYTICS_KEY=<your-key>
|
|
9
|
+
|
|
10
|
+
# --- Azure Storage (optional — for audit log persistence) ---
|
|
11
|
+
AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=...
|
|
12
|
+
AZURE_STORAGE_CONTAINER=classifications
|
|
13
|
+
|
|
14
|
+
# --- Database (Postgres/Azure SQL in prod, SQLite for local dev) ---
|
|
15
|
+
DATABASE_URL=sqlite:///./dev.db
|
|
16
|
+
# DATABASE_URL=postgresql://<user>:<pass>@<host>:5432/<db>
|
|
17
|
+
# DATABASE_URL=mssql+pyodbc://<user>:<pass>@<server>.database.windows.net/<db>?driver=ODBC+Driver+18+for+SQL+Server
|
|
18
|
+
|
|
19
|
+
# --- Escalation tracking (defaults to DATABASE_URL when unset) ---
|
|
20
|
+
TRACKING_DB_URL=sqlite:///./tracking.db
|
|
21
|
+
|
|
22
|
+
# --- Classification ---
|
|
23
|
+
# TAXONOMY_CONFIG_PATH, BENCHMARK_TEMPLATES_PATH, PATTERNS_DIR, DOGWHISTLES_PATH, and
|
|
24
|
+
# I18N_SMOKE_TESTS_PATH all default to the files shipped inside the installed package —
|
|
25
|
+
# only set these to override with your own.
|
|
26
|
+
# TAXONOMY_CONFIG_PATH=/path/to/custom_taxonomy.yaml
|
|
27
|
+
# BENCHMARK_TEMPLATES_PATH=/path/to/custom_benchmark_templates.yaml
|
|
28
|
+
# PATTERNS_DIR=/path/to/custom_patterns_dir
|
|
29
|
+
# DOGWHISTLES_PATH=/path/to/custom_dogwhistles.yaml
|
|
30
|
+
# I18N_SMOKE_TESTS_PATH=/path/to/custom_i18n_smoke_tests.yaml
|
|
31
|
+
TAXONOMY_VERSION=1.0.0
|
|
32
|
+
DEFAULT_CONFIDENCE_THRESHOLD=0.65
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.12"]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
19
|
+
uses: actions/setup-python@v5
|
|
20
|
+
with:
|
|
21
|
+
python-version: ${{ matrix.python-version }}
|
|
22
|
+
|
|
23
|
+
- name: Install package (dev extras)
|
|
24
|
+
run: pip install -e ".[dev]"
|
|
25
|
+
|
|
26
|
+
- name: Run unit + integration + API tests with coverage
|
|
27
|
+
run: |
|
|
28
|
+
pytest tests/unit tests/integration tests/api -v \
|
|
29
|
+
--cov=narrative_harm_classifier --cov-report=term-missing --cov-fail-under=80
|
|
30
|
+
|
|
31
|
+
- name: Print benchmark report (human-readable summary alongside the gate below)
|
|
32
|
+
run: python -m narrative_harm_classifier.cli benchmark run
|
|
33
|
+
|
|
34
|
+
- name: Enforce benchmark gate (precision 1.0 / recall 1.0 / FPR 0.0, full cross-group consistency)
|
|
35
|
+
run: pytest tests/benchmark -v
|
|
36
|
+
|
|
37
|
+
milestone-gate:
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
needs: test
|
|
40
|
+
steps:
|
|
41
|
+
- uses: actions/checkout@v4
|
|
42
|
+
|
|
43
|
+
- name: Set up Python
|
|
44
|
+
uses: actions/setup-python@v5
|
|
45
|
+
with:
|
|
46
|
+
python-version: "3.12"
|
|
47
|
+
|
|
48
|
+
- name: Install package (dev extras)
|
|
49
|
+
run: pip install -e ".[dev]"
|
|
50
|
+
|
|
51
|
+
- name: Enforce Phase 1 milestone gate (dehumanization precision/recall/FPR)
|
|
52
|
+
run: pytest tests/integration/test_validation.py -v
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
# Triggers on a GitHub Release being published, or manually via the Actions
|
|
4
|
+
# tab. Uses PyPI Trusted Publishing (OIDC) — no API token is stored in this
|
|
5
|
+
# repo. One-time setup required on pypi.org before this will work: create
|
|
6
|
+
# the project (or its first release manually) and add this repo/workflow as
|
|
7
|
+
# a "trusted publisher" under the project's Publishing settings.
|
|
8
|
+
on:
|
|
9
|
+
release:
|
|
10
|
+
types: [published]
|
|
11
|
+
workflow_dispatch: {}
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
build:
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: "3.12"
|
|
23
|
+
|
|
24
|
+
- name: Install build tooling
|
|
25
|
+
run: pip install build
|
|
26
|
+
|
|
27
|
+
- name: Build sdist + wheel
|
|
28
|
+
run: python -m build
|
|
29
|
+
|
|
30
|
+
- name: Upload build artifacts
|
|
31
|
+
uses: actions/upload-artifact@v4
|
|
32
|
+
with:
|
|
33
|
+
name: dist
|
|
34
|
+
path: dist/
|
|
35
|
+
|
|
36
|
+
publish:
|
|
37
|
+
needs: build
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
environment: pypi
|
|
40
|
+
permissions:
|
|
41
|
+
id-token: write # required for PyPI Trusted Publishing
|
|
42
|
+
steps:
|
|
43
|
+
- name: Download build artifacts
|
|
44
|
+
uses: actions/download-artifact@v4
|
|
45
|
+
with:
|
|
46
|
+
name: dist
|
|
47
|
+
path: dist/
|
|
48
|
+
|
|
49
|
+
- name: Publish to PyPI
|
|
50
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Setup
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
git clone https://github.com/HenryMorganDibie/narrative-harm-classifier.git
|
|
7
|
+
cd narrative-harm-classifier
|
|
8
|
+
python -m venv .venv && source .venv/bin/activate # or .venv\Scripts\activate on Windows
|
|
9
|
+
pip install -e ".[dev]"
|
|
10
|
+
pytest tests/ -v
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Architecture overview
|
|
14
|
+
|
|
15
|
+
- `classifier/factory.py` — the single place that constructs a `ClassificationEngine`/`EscalationTracker`/
|
|
16
|
+
`BenchmarkRunner`/`PerformanceValidator`. Every API route and CLI command goes through this instead of
|
|
17
|
+
reconstructing dependencies itself — if you're adding a new route or command that needs the engine, use
|
|
18
|
+
`build_engine(settings)` rather than wiring `load_taxonomy` + `AzureNLPClient` + `ClassificationEngine`
|
|
19
|
+
by hand again.
|
|
20
|
+
- `classifier/rules/patterns_loader.py` — loads and precompiles a language's vocabulary
|
|
21
|
+
(`data/patterns/<lang>.yaml`) into a `LanguagePatterns` object. `classifier/rules/engine.py` is
|
|
22
|
+
language-agnostic; all per-language vocabulary lives in data, not code.
|
|
23
|
+
- `classifier/rules/dogwhistles.py` and `classifier/counter_narrative.py` and `classifier/provenance.py`
|
|
24
|
+
are small, focused modules — see their docstrings and the corresponding README sections
|
|
25
|
+
([Dog-whistle lexicon](README.md#dog-whistle-lexicon-in-depth),
|
|
26
|
+
[Counter-narrative guidance](README.md#counter-narrative-guidance),
|
|
27
|
+
[Provenance & tamper-evidence](README.md#provenance--tamper-evidence-in-depth)).
|
|
28
|
+
|
|
29
|
+
## Adding a taxonomy row
|
|
30
|
+
|
|
31
|
+
Taxonomy rows live in `narrative_harm_classifier/data/taxonomy_v1.yaml`. Each row needs a `row_id`,
|
|
32
|
+
`target_type`, `harm_mechanism`, `identity_axis`, `signal_weight`, and `decision_threshold`. Corresponding
|
|
33
|
+
regex patterns for a new `harm_mechanism` go under `harm_patterns` in each language file under
|
|
34
|
+
`narrative_harm_classifier/data/patterns/` (at minimum `en.yaml` — a harm mechanism only checked in some
|
|
35
|
+
languages will simply never fire in the others until someone adds it there too).
|
|
36
|
+
|
|
37
|
+
If a new `harm_mechanism` should participate in escalation tracking, add it to
|
|
38
|
+
`HARM_MECHANISM_SEVERITY` in `narrative_harm_classifier/classifier/tracking/models.py` so it maps to a
|
|
39
|
+
severity level.
|
|
40
|
+
|
|
41
|
+
## Adding or improving a language
|
|
42
|
+
|
|
43
|
+
Each language is one file: `narrative_harm_classifier/data/patterns/<iso-639-1-code>.yaml`. Copy an
|
|
44
|
+
existing file's structure — `identity_anchors` and `harm_patterns` are regex pattern lists per
|
|
45
|
+
axis/mechanism; `negation_cues`, `reporting_cues`, `condemnation_cues`, and `benign_context_cues` are
|
|
46
|
+
plain literal phrase lists (no regex syntax needed — they're auto-escaped and combined); `obfuscation_map`
|
|
47
|
+
is a character-substitution table for Latin-script leetspeak evasion (leave it `{}` for non-Latin
|
|
48
|
+
scripts, where that specific evasion technique doesn't apply).
|
|
49
|
+
|
|
50
|
+
Set `confidence: verified` only if you're confident in the vocabulary's correctness (ideally with
|
|
51
|
+
native-speaker review); otherwise use `confidence: experimental` — this is surfaced directly to API
|
|
52
|
+
consumers via `language_confidence`, so it's not just a label, don't set it to `verified` optimistically.
|
|
53
|
+
|
|
54
|
+
**A structural pitfall to check for, found while building the Arabic vocabulary**: languages where
|
|
55
|
+
articles, plurals, or other morphology attach directly to a word with no space (Arabic's `ال` definite
|
|
56
|
+
article, for example) will silently fail to match a plain `\b(word)\b` pattern on the attached form —
|
|
57
|
+
`\bمسلم\b` never matches inside `المسلمون`. If your language has this property, make sure patterns account
|
|
58
|
+
for it explicitly (see the comment at the top of `ar.yaml`) rather than assuming `\b` alone is enough.
|
|
59
|
+
|
|
60
|
+
Add a few cases to `narrative_harm_classifier/data/i18n_smoke_tests.yaml` for your language and run
|
|
61
|
+
`nhc benchmark i18n` (or `pytest tests/unit/test_i18n_smoke.py`) to confirm basic detection works.
|
|
62
|
+
|
|
63
|
+
## Adding a dog-whistle entry
|
|
64
|
+
|
|
65
|
+
Entries live in `narrative_harm_classifier/data/dogwhistles.yaml`. Each needs a `term`, `harm_mechanism`,
|
|
66
|
+
`identity_axis`, `signal_weight`, `decision_threshold` (both below 1.0, with headroom between them the
|
|
67
|
+
same way taxonomy rows work), and a `source_note` citing where the term is publicly documented (ADL Hate
|
|
68
|
+
Symbols Database, SPLC, or academic literature on coded hate speech) — this lexicon only takes
|
|
69
|
+
well-documented terms with a citable source, not personal judgment calls about what sounds coded. Use a
|
|
70
|
+
lower `signal_weight`/`decision_threshold` for ambiguous terms with legitimate non-bigoted uses, and a
|
|
71
|
+
higher one for unambiguous, purpose-built coded terms (numeric codes, explicit slogans).
|
|
72
|
+
|
|
73
|
+
## Adding a benchmark case
|
|
74
|
+
|
|
75
|
+
Benchmark templates live in `narrative_harm_classifier/data/benchmark_templates.yaml`.
|
|
76
|
+
|
|
77
|
+
- To test a new phrasing for an existing harm mechanism, add a template with a `{group}` placeholder — it
|
|
78
|
+
will automatically be expanded across every group in the `groups` list, which also feeds the
|
|
79
|
+
cross-group-consistency check.
|
|
80
|
+
- To add a one-off hard negative that shouldn't be slot-filled, add it to `standalone_cases` instead.
|
|
81
|
+
- Every template/case needs a `test_type` (`explicit_positive`, `implicit_positive`, `negation`,
|
|
82
|
+
`counter_speech`, `obfuscated_spelling`, or `benign_trigger_word`) so regressions in a specific capability
|
|
83
|
+
are visible in the per-test-type breakdown, not just the aggregate.
|
|
84
|
+
|
|
85
|
+
Run `nhc benchmark run` (or `pytest tests/benchmark -v`) to see the effect of your change.
|
|
86
|
+
|
|
87
|
+
## CI gate
|
|
88
|
+
|
|
89
|
+
Pull requests run the full test suite plus the Phase 1 milestone gate
|
|
90
|
+
(`tests/integration/test_validation.py`), which fails the build if the dehumanization category's
|
|
91
|
+
precision/recall/FPR regress below the thresholds in `taxonomy_v1.yaml`. `tests/benchmark/` asserts the
|
|
92
|
+
templated benchmark suite stays at precision 1.0 / recall 1.0 / FPR 0.0 overall and 100% cross-group
|
|
93
|
+
consistency — this **is** a hard gate: a PR that regresses any of those numbers fails CI. CI also enforces
|
|
94
|
+
a minimum 80% coverage (`--cov-fail-under=80`) across `tests/unit tests/integration tests/api`.
|
|
95
|
+
|
|
96
|
+
## Coverage badge
|
|
97
|
+
|
|
98
|
+
The coverage badge in the README is a static number (currently ~90%), captured from a real
|
|
99
|
+
`pytest --cov` run at the time it was last updated — it is **not** a live badge that recalculates itself
|
|
100
|
+
on every push. If you add or remove a meaningful chunk of tested code, regenerate it:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
pytest tests/unit tests/integration tests/api --cov=narrative_harm_classifier --cov-report=term-missing
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
and update the percentage in the badge URL in `README.md`. (A live-updating badge would need a service
|
|
107
|
+
like Codecov, which requires the maintainer to connect the repo on codecov.io — not set up here yet.)
|
|
108
|
+
|
|
109
|
+
## Performance numbers
|
|
110
|
+
|
|
111
|
+
The numbers in the README's [Performance](README.md#performance) section come from running
|
|
112
|
+
[`scripts/measure_performance.py`](scripts/measure_performance.py) — re-run it and update the table if
|
|
113
|
+
you change anything in the hot classification path (`classifier/rules/engine.py`).
|
|
114
|
+
|
|
115
|
+
## How the engine handles negation, counter-speech, and obfuscation
|
|
116
|
+
|
|
117
|
+
The engine is still a regex/rule engine, not a trained model — these are handled with explainable
|
|
118
|
+
heuristics in the suppression pipeline (`_rule_negation`, `_rule_counter_speech`, `_rule_benign_context`
|
|
119
|
+
in `narrative_harm_classifier/classifier/rules/engine.py`), reading their cue lists/maps from the active
|
|
120
|
+
language's `LanguagePatterns` (`data/patterns/<lang>.yaml`) rather than understanding language in general:
|
|
121
|
+
|
|
122
|
+
- **Negation** — a matched harm pattern is discarded if a `negation_cues` entry (`not`, `never`, `isn't`,
|
|
123
|
+
`false that`, ...) appears in the ~60 characters immediately before the match. This is a local window,
|
|
124
|
+
not full-sentence parsing, so it can be evaded by negation placed far from the trigger word, or fooled
|
|
125
|
+
by an unrelated negation word elsewhere in a long sentence.
|
|
126
|
+
- **Counter-speech** — a match is discarded only when the text contains *both* a `reporting_cues` entry
|
|
127
|
+
("some say", "calling", "rhetoric claiming", ...) *and* a `condemnation_cues` entry ("dangerous",
|
|
128
|
+
"bigoted", "led to violence", ...). Requiring both reduces false suppression, but a genuinely harmful
|
|
129
|
+
post that happens to use one of these words in a non-condemning way could still slip through.
|
|
130
|
+
- **Obfuscated spelling** — each language's `obfuscation_map` (a character-substitution table, e.g.
|
|
131
|
+
`0→o`, `1→i`, `3→e` for English/Spanish/French) is applied and matched alongside the original text. It
|
|
132
|
+
only reverses substitutions in that map — homoglyphs, spacing tricks (`v e r m i n`), or substitutions
|
|
133
|
+
outside the map will not be caught until someone extends the map for that language. Non-Latin-script
|
|
134
|
+
languages (Russian, Arabic) currently have an empty map since Latin-style digit-substitution doesn't
|
|
135
|
+
apply the same way — a real gap for whatever the equivalent evasion technique is in those scripts.
|
|
136
|
+
- **Benign-context override** — a language's `benign_context_cues` (pest control, wildlife, film,
|
|
137
|
+
academic theory, ...) suppress a match when present, so a trigger word discussed in an unrelated
|
|
138
|
+
literal context doesn't get flagged just because a group is also named nearby. This is a coarse
|
|
139
|
+
allowlist, not sarcasm/context understanding — it covers the specific hard-negative categories in the
|
|
140
|
+
benchmark, not every possible benign context.
|
|
141
|
+
|
|
142
|
+
Only English, Spanish, French, Russian, and Arabic have these cue lists populated; Igbo, Yoruba, and
|
|
143
|
+
Hausa intentionally have them empty (see [Multilingual support](README.md#multilingual-support-in-depth))
|
|
144
|
+
rather than guessed.
|
|
145
|
+
|
|
146
|
+
**If you find real-world text that evades one of these** (and you will — this is fundamentally a
|
|
147
|
+
keyword/heuristic system, not semantic understanding), the fix is almost always to extend the relevant
|
|
148
|
+
cue list/map in that language's YAML file, or add a new `harm_patterns` entry, then add a benchmark case
|
|
149
|
+
for it so the gap can't silently regress. That's the highest-value kind of contribution here: the
|
|
150
|
+
benchmark passing cleanly today means it's clean against *this* test suite, not that evasion is a solved
|
|
151
|
+
problem in general.
|
|
152
|
+
|
|
153
|
+
## Database schema changes (pre-1.0)
|
|
154
|
+
|
|
155
|
+
`classifier/tracking/store.py` uses `metadata.create_all()`, which creates missing tables but does **not**
|
|
156
|
+
alter existing ones. If you add/rename a column (as the provenance hash-chain fields did), delete your
|
|
157
|
+
local `dev.db`/`tracking.db` rather than expecting an in-place migration — there's no Alembic-style
|
|
158
|
+
migration system yet, which is a reasonable gap pre-1.0 but will need addressing before this is used
|
|
159
|
+
somewhere a real schema migration matters.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing
|
|
141
|
+
the origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Henry Morgan Dibie
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|