Metadata-Version: 2.4
Name: dp-diffractor
Version: 0.1.2
Summary: 1-Diffractor: a highly efficient word-level Metric Differential Privacy mechanism.
Author-email: Stephen Meisenbacher <stephen.meisenbacher@tum.de>
Maintainer-email: Stephen Meisenbacher <stephen.meisenbacher@tum.de>
License: Copyright (c) 2024-2026 Stephen Meisenbacher
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        
Project-URL: Homepage, https://github.com/sjmeis/Diffractor
Project-URL: Documentation, https://github.com/sjmeis/Diffractor
Project-URL: Repository, https://github.com/sjmeis/Diffractor
Project-URL: Issues, https://github.com/sjmeis/Diffractor/issues
Keywords: differential privacy,text privatization,word obfuscation,privacy preserving NLP
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Intended Audience :: Science/Research
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: nltk
Requires-Dist: faiss-cpu
Requires-Dist: gensim
Requires-Dist: requests
Requires-Dist: tqdm
Requires-Dist: importlib_resources
Dynamic: license-file

<div align="left">

  [![PyPI version](https://img.shields.io/pypi/v/dp-diffractor.svg)](https://pypi.org/project/dp-diffractor/)
  [![License](https://img.shields.io/github/license/sjmeis/diffractor.svg)](https://github.com/sjmeis/diffractor/blob/main/LICENSE)

</div>

# 1-Diffractor
`1-Diffractor` is a high-performance library for word-level text perturbation leveraging Metric Differential Privacy. It maps text into 1D sorted embedding spaces to apply noise, ensuring privacy guarantees while maintaining semantic utility.


## Key Features
 - **Metric DP Implementation**: Support for both Truncated Geometric and Truncated Exponential (TEM) mechanisms.
 - **Automated Embedding Management**: Automatically downloads and caches filtered embedding models (GloVe, Word2Vec, Numberbatch).
 - **Parallel Processing**: Uses optimized multiprocessing to perturb large batches of text quickly.
 - **BYOE (Bring Your Own Embeddings)**: CLI tools to clean and integrate custom embedding files into the `1-Diffractor`.

## Quickstart Guide
### Installation
```bash
pip install dp-diffractor
```

### Basic Usage
```python
from diffractor import Diffractor, DiffractorConfig

# Configure the privacy mechanism
config = DiffractorConfig(
    method="geometric", 
    epsilon=1.0, 
    verbose=True
)

with Diffractor(config) as df:
    texts = ["Differential Privacy is really cool!", "Hello world."]
    perturbed = df.rewrite(texts)
    print(perturbed)
```

## Advanced Configuration
The `DiffractorConfig` object allows you to customize the privatization parameters:

| Parameter         | Default       | Description                                              |
|-------------------|---------------|----------------------------------------------------------|
| `method`            | `geometric`   | The DP mechanism: `geometric` or `TEM`.                  |
| `epsilon`           | `1.0`           | Privacy budget (ε). Lower is more private.               |
| `gamma`             | `5`             | Neighborhood radius for the `TEM` scoring function.      |
| `sensitivity`       | `1.0`           | Sensitivity of the scoring function.                     |
| `replace_stopwords` | `False`         | If False, keeps common stopwords unchanged.              |
| `verbose`           | `True`          | Enables progress bars and status logging.                |
| `seed`              | `42`            | Global seed for reproducible perturbations.              |


---

## Managing Embeddings

`1-Diffractor` keeps a local cache (by default, `~/.cache/diffractor`) to store embedding files.

### Custom Embeddings (BYOE)
If you have your own embedding file, you must filter it against the internal vocabulary to ensure it works with the privatization mechanism:

```
# In your terminal
diffractor-clean path/to/my_vectors.txt
```

Then, use it during startup:

```python
df = Diffractor(model_names=["my_vectors_filtered"])
```

### Default Models
By default, `1-Diffractor` fetches and uses the following embedding models:
 - `conceptnet-numberbatch-19-08-300`
 - `glove-twitter-200`
 - `glove-wiki-gigaword-300`
 - `glove-commoncrawl-30`
 - `word2vec-google-news-300`
 
---

## Citation
If you find `1-Diffractor` useful or make use of it in your research, please be sure to cite the original paper:

```
@inproceedings{10.1145/3643651.3659896,
author = {Meisenbacher, Stephen and Chevli, Maulik and Matthes, Florian},
title = {1-Diffractor: Efficient and Utility-Preserving Text Obfuscation Leveraging Word-Level Metric Differential Privacy},
year = {2024},
isbn = {9798400705564},
publisher = {Association for Computing Machinery},
address = {New York, NY, USA},
url = {https://doi.org/10.1145/3643651.3659896},
doi = {10.1145/3643651.3659896},
booktitle = {Proceedings of the 10th ACM International Workshop on Security and Privacy Analytics},
pages = {23–33},
numpages = {11},
keywords = {data privacy, differential privacy, natural language processing},
location = {Porto, Portugal},
series = {IWSPA '24}
}
```

Please also consider citing the hosted embedding files:

```
@dataset{meisenbacher_2026_19701515,
  author       = {Meisenbacher, Stephen},
  title        = {Filtered Embedding Files for 1-Diffractor},
  month        = apr,
  year         = 2026,
  publisher    = {Zenodo},
  doi          = {10.5281/zenodo.19701515},
  url          = {https://doi.org/10.5281/zenodo.19701515},
}
```
