Metadata-Version: 2.4
Name: colsemantics
Version: 0.3.1
Summary: Infer semantic roles and domains for tabular columns from names, values, and table context.
Author: Caio
License-Expression: MIT
Project-URL: Homepage, https://github.com/Caio-Analytics/colsemantics
Project-URL: Repository, https://github.com/Caio-Analytics/colsemantics
Project-URL: Issues, https://github.com/Caio-Analytics/colsemantics/issues
Project-URL: Changelog, https://github.com/Caio-Analytics/colsemantics/blob/main/CHANGELOG.md
Keywords: semantic-inference,data-profiling,schema-inference,column-classification,pii,etl
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: rapidfuzz>=3.14.6
Requires-Dist: unidecode>=1.4.0
Requires-Dist: PyYAML>=6.0.3
Provides-Extra: dev
Requires-Dist: pytest>=9.2.0; extra == "dev"
Requires-Dist: pytest-cov>=7.1.0; extra == "dev"
Requires-Dist: mypy>=2.4.1; extra == "dev"
Requires-Dist: ruff>=0.17.4; extra == "dev"
Dynamic: license-file

# colsemantics

[![PyPI version](https://img.shields.io/pypi/v/colsemantics)](https://pypi.org/project/colsemantics/)
[![Python versions](https://img.shields.io/pypi/pyversions/colsemantics)](https://pypi.org/project/colsemantics/)
[![CI](https://github.com/Caio-Analytics/colsemantics/actions/workflows/ci.yml/badge.svg)](https://github.com/Caio-Analytics/colsemantics/actions/workflows/ci.yml)
[![License](https://img.shields.io/github/license/Caio-Analytics/colsemantics)](LICENSE)
[![PyPI downloads](https://img.shields.io/pypi/dm/colsemantics)](https://pypi.org/project/colsemantics/)

`colsemantics` infers the structural role and business domain of tabular columns from column names, sampled values, and neighboring columns.

It is useful when a dataset has unclear headers such as `cd_dpto_lot`, `f27`, or `SUPPLIER_CONTACT_CODE`.

## Installation

```bash
pip install colsemantics
```

## Quick start

```python
from colsemantics import infer_column

result = infer_column("SUPPLIER_CONTACT_CODE")
print(result["role"])
```

The English API returns English field names and category labels.

## Analyze a CSV file

Install the command-line interface with the package, then create a JSON report from a bounded CSV sample:

```bash
colsemantics infer employees.csv --profile pt-BR --sample 10000 --output report.json
```

CSV is the supported file format in this release. The report records its source, profile, sample size, inferred columns, and a semantic summary.

## Use sampled values

```python
from colsemantics import ContentProfile, infer_column

profile = ContentProfile(
    data_type="Text",
    distinct_values=["SP", "RJ", "MG", "BA"],
    distinct_count=4,
    uniqueness_ratio=0.1,
)

result = infer_column("f27", profile=profile)
print(result["domain"])
```

## Analyze a table

```python
from colsemantics import infer_table

results = infer_table(
    [
        {"column_name": "employee_id"},
        {"column_name": "department_name"},
        {"column_name": "start_date"},
    ]
)
```

`infer_table` uses high-confidence classifications as table context when it evaluates ambiguous columns.

## Vocabulary profiles and extensions

`pt-BR` is the embedded profile. Use `available_profiles()` to list embedded profiles and `load_profile()` when an application needs an explicit context.

```python
from colsemantics import infer_column, temporary_profile

with temporary_profile("pt-BR", "company-vocabulary.yaml"):
    result = infer_column("cost_bucket")
```

YAML extensions use the English schema and apply only within the selected context:

Provide one or more comma-separated YAML files to `load_vocabularies`, then use the returned context for the current operation.

```yaml
strong_categories:
  Project domain:
    - workstream
column_overrides:
  cost_bucket: Finance / Cost
```

Custom vocabularies are scoped with `ContextVar`, so separate concurrent analyses do not share vocabulary changes.

## Benchmark inference quality

Run the synthetic benchmark fixture included with the repository:

```bash
colsemantics benchmark benchmarks/synthetic.json --output benchmark-report.json
```

The report includes semantic, role, and domain accuracy, coverage, per-profile metrics, a confusion matrix, and calibration buckets with expected calibration error. Public corpus definitions are stored as source manifests; they are not bundled or downloaded automatically.

## Confidence and review decisions

`raw_confidence` is the direct score assembled from independent evidence. `confidence` is the profile calibration for that score bucket, derived from the versioned calibration fixture. A result sets `review_required` when calibrated confidence is below `0.80`.

The embedded calibration is provisional: it is a controlled regression fixture, not evidence of production-grade accuracy. A held-out, versioned corpus is required before treating its precision as a production measurement.

## Sensitive-data recommendations

Each result includes a descriptive `sensitivity` recommendation. It does not modify input data.

- Validated CPF patterns: `high` / `mask`
- Person identity semantics: `high` / `restrict`
- Contact semantics: `high` / `mask`
- Other identifiers: `medium` / `review`
- Everything else: `none` / `allow`

## Result shape

```python
{
    "semantic": str,
    "role": str | None,
    "domain": str | None,
    "raw_confidence": float,
    "confidence": float,
    "evidence": str,
    "conclusive": bool,
    "review_required": bool,
    "sensitivity": {"level": str, "action": str, "reason": str},
    "hypotheses": list[dict],
}
```

## Migrating from 0.2

`normalizar` and `tokenizar` were renamed to `normalize` and `tokenize`. The old names still work but emit a `DeprecationWarning` and will be removed in 1.0. `infer_table`, `assess_sensitivity`, `calibrate`, and `requires_review` are now listed in `__all__`.

Python 3.10 is no longer supported (it reached end of life, and current `rapidfuzz` requires 3.11).

## Contributing

Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for setup, checks, and the release process, and [CHANGELOG.md](CHANGELOG.md) for version history.

```bash
python -m pip install -e '.[dev]'
pytest --cov
ruff check .
ruff format --check .
mypy
```

## License

MIT.
