> For the complete documentation index, see [llms.txt](https://infinitode-docs.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infinitode-docs.gitbook.io/documentation/package-documentation/valx-package-documentation.md). Agents can also search and read this documentation through its [MCP server](https://infinitode-docs.gitbook.io/documentation/~gitbook/mcp).

# ValX Package Documentation

Package documentation for ValX, a Python package for handling text-cleaning tasks, including profanity, sensitive information, and hate speech detection and removal.

ValX is an open-source Python library for data cleaning: profanity detection and removal, sensitive-information detection and redaction, and AI-based hate speech and offensive language detection and removal.

## Changelog

* **0.2.7 (Latest)**:
  * Sensitive-information redaction now uses exact match positions on each line and merges overlapping matches, so unrelated copies of a substring are not redacted and no part of an overlapping match is left exposed. Parenthesized phone area codes and signed coordinates are included.
  * Profanity filtering supports the documented Finnish `FI` alias and punctuation in custom terms. Detections are returned in text order and the `Word` value preserves original casing. Lines surrounded by `$` are no longer silently dropped.
  * The bundled hate-speech models load on first use instead of at import time.
  * **Requires Python 3.10 or newer.**
  * The release workflow builds and tests the wheel and source distribution before publishing both to PyPI with signed attestations.
* **0.2.6**: Fixed a major bug ([issue #4](https://github.com/Infinitode/ValX/issues/4)) where profanity lists had missing languages, mixed language lists, and missing words. Introduced case-insensitive language selection, so `"English"`, `"english"`, `"en"`, `"EN"`, or `"EnGliSh"` all work in the `language` parameter.
* **0.2.5**: Enhanced profanity filtering flexibility:
  * Added `custom_words_list` to `detect_profanity` and `remove_profanity` for user-defined lists.
  * Support for standalone custom lists by setting `language=None`.
  * Support for combined lists (built-in language + custom list).
  * New `load_custom_profanity_from_file(filepath)` helper.
  * `detect_profanity` output now specifies the profanity source (for example `"Custom"` or `"Custom + English"`).
* **0.2.4**: Fixed compatibility issues with `scikit-learn` 1.3.0 and later, and removed the pinned `scikit-learn==1.2.2` dependency.
* **0.2.3**: Added sensitive-information detection patterns and an optional `info_type` parameter to control detection and removal.
* **0.2.2**: Refactored `detect_profanity` to return `Line`, `Column`, `Word`, and `Language` for each match, and removed unnecessary printing.
* **0.2.1**: Updated the PyPI project description.
* **0.2.0**: Added `remove_hate_speech` to automatically remove detected hate speech or offensive speech.
* **0.1.8 – 0.1.9**: Updated docstrings.
* **0.1.7**: Added AI models for hate speech detection.
* **0.1.1 – 0.1.6**: Fixed code errors and added several text-cleaning functions.
* **0.1.0**: Initial release.

## Installation

Install ValX from PyPI. ValX **0.2.7 requires Python 3.10 or newer** (CI covers 3.10–3.14):

```bash
pip install --upgrade valx
```

ValX depends on `scikit-learn` and `packaging`. The bundled hate-speech models load on first use, so importing `valx` is cheap and the rule-based profanity and sensitive-information filters never touch the models.

{% hint style="info" %}
Only the hate-speech functions load the scikit-learn models. Loading a model saved with a different scikit-learn version can emit warnings, and cross-version compatibility is not guaranteed. If you must use scikit-learn 1.2.2, pair it with NumPy 1.x.
{% endhint %}

***

#### Supported languages for profanity detection and removal

Pass a language name or code as `language`. Both are case-insensitive, and `All` combines every built-in list.

| Language    | Code              | Language   | Code  |
| ----------- | ----------------- | ---------- | ----- |
| Arabic      | `AR`              | Italian    | `IT`  |
| Czech       | `CS`              | Japanese   | `JA`  |
| Danish      | `DA`              | Kabyle     | `KAB` |
| German      | `DE`              | Korean     | `KO`  |
| English     | `EN`              | Dutch      | `NL`  |
| Esperanto   | `EO`              | Norwegian  | `NO`  |
| Persian     | `FA`              | Polish     | `PL`  |
| Finnish     | `FI`              | Portuguese | `PT`  |
| Filipino    | `FIL`             | Russian    | `RU`  |
| French      | `FR`              | Spanish    | `ES`  |
| French (CA) | `FR-CA-U-SD-CAQC` | Swedish    | `SV`  |
| Hindi       | `HI`              | Thai       | `TH`  |
| Hungarian   | `HU`              | Klingon    | `TLH` |
| Turkish     | `TR`              | Chinese    | `ZH`  |

{% hint style="warning" %}
Language names are matched against the built-in lists, and an unrecognized value resolves to an empty list — so a typo produces no matches instead of an error. Call `load_profanity_words(language)` first if you want to confirm that a language resolves to words.
{% endhint %}

## Example Usage

### Profanity Detection

```python
from valx import detect_profanity

sample_text = [
    "This is a sample text containing some profanity like bad word 1, bad word 2, and bad word 3.",
    "This line doesn't contain any profanity.",
    "But this one has another, just in another language: bad word 4."
]

# Detect profanity
results = detect_profanity(sample_text, language='English')
print("Profanity Evaluation Results", results)
```

Each result is a dictionary with a 1-based `Line`, a 1-based `Column`, the matched `Word` in its original casing, and the `Language` source. Matches are non-overlapping and returned in text order; when two terms match at the same position, the longer one wins.

### Profanity Removal

```python
from valx import remove_profanity

sample_text = [
    "This is a sample text containing some profanity like bad word 1, bad word 2, and bad word 3.",
    "This line doesn't contain any profanity.",
    "But this one has another, just in another language: bad word 4."
]

# Remove profanity, optionally writing the cleaned lines to a file
removed = remove_profanity(sample_text, "text_cleaned.txt", language="English")
```

Matches are replaced with `bad word`. The function returns the cleaned lines as a list.

### PII Detection

```python
from valx import detect_sensitive_information

sample_text = [
    "Please contact john.doe@example.com or call (555) 123-4567 for more information.",
    "We will need your credit card number to complete the transaction: 1234-5678-9012-3456.",
    "My social security number is 123-45-6789 and my ID number is AB123456.",
    "Our office address is 123 Main St, Anytown, USA. Please visit us!",
    "Your IP address is 192.168.1.1. Please don't share it with anyone."
]

# Detect all supported sensitive-information types
detected_information = detect_sensitive_information(sample_text)
print(detected_information)
# [(1, 15, 'Email', 'john.doe@example.com'),
#  (1, 44, 'Phone', '(555) 123-4567'),
#  (1, 23, 'Username', '@example'), ...]
```

`detect_sensitive_information` returns tuples of `(line number, column index, type, value)`. The line number is **1-based** and the column index is **0-based**.

Pass `info_type` with a single type or a list to narrow the scan:

```python
detect_sensitive_information(sample_text, info_type="email")
detect_sensitive_information(sample_text, info_type=["email", "phone"])
```

Available types are `email`, `phone`, `credit_card`, `ssn`, `id`, `address`, `ip`, `iban`, `mrn`, `icd10`, `geo_coords`, `username`, `file_path`, `bitcoin_wallet`, and `ethereum_wallet`.

{% hint style="info" %}
Types can overlap. An email address such as `john.doe@example.com` also matches `username` on its `@example` portion, so one value can produce more than one tuple. Narrow the scan with `info_type` when you only care about one category.
{% endhint %}

### PII Removal

```python
from valx import remove_sensitive_information

sample_text = ["Contact help@example.com or call (555) 123-4567."]

cleaned_information = remove_sensitive_information(sample_text, info_type=["email", "phone"])
print(cleaned_information)
# ['Contact [SENSITIVE] or call [SENSITIVE].']
```

Redaction replaces each match with `[SENSITIVE]`, using exact match positions per line and merging overlaps so no fragment of an overlapping value remains.

{% hint style="warning" %}
These are pattern-based filters, not a guarantee of anonymization. They can miss sensitive values or flag ordinary text — for example, `file_path` looks for filenames with an extension and can miss names containing spaces. Review redacted text before sharing or publishing it.
{% endhint %}

### Hate Speech Detection

```python
from valx import detect_hate_speech

# Detect hate speech or offensive language using the bundled AI model
outcome_of_detection = detect_hate_speech("You are stupid.")
print(outcome_of_detection)
```

The model returns one of:

* `['Hate Speech']`: the text was flagged as hate speech.
* `['Offensive Speech']`: the text was flagged as offensive speech.
* `['No Hate and Offensive Speech']`: the text was not flagged.

### Remove Hate Speech

```python
from valx import remove_hate_speech

sample_text = [
    "This is a sample text containing some profanity like bad word 1, bad word 2, and bad word 3.",
    "This line doesn't contain any profanity.",
    "But this one has another, just in another language: bad word 4."
]

# Keep only the lines classified as neither hate speech nor offensive speech
cleaned_text = remove_hate_speech(sample_text)
```

### Custom Profanity Filtering

ValX allows for flexible profanity filtering using custom word lists.

```python
from valx import detect_profanity, remove_profanity, load_custom_profanity_from_file

# Example: Create a dummy custom profanity file
with open("custom_profanity.txt", "w") as f:
    f.write("# This is a comment and will be ignored\n")
    f.write("sillyword1\n")
    f.write("    \n")  # Empty line, ignored
    f.write("sillyword2\n")
    f.write("custombadword\n")

sample_text_custom = [
    "This text contains a sillyword1.",
    "Another line with sillyword2 and also a standard badword.",
    "This custombadword should be caught."
]

# 1. Using custom_words_list directly (standalone)
custom_list_direct = ["sillyword1", "custombadword"]
detected_custom_direct = detect_profanity(sample_text_custom, language=None, custom_words_list=custom_list_direct)
print("Detected with direct custom list (standalone):", detected_custom_direct)
# Detects "sillyword1" and "custombadword" with source "Custom"

# 2. Loading custom words from a file
loaded_custom_list = load_custom_profanity_from_file("custom_profanity.txt")
print(f"Loaded custom words: {loaded_custom_list}")  # ['sillyword1', 'sillyword2', 'custombadword']

# 3. Using loaded custom list with a built-in language (English)
detected_custom_combined = detect_profanity(sample_text_custom, language="English", custom_words_list=loaded_custom_list)
print("Detected with loaded custom list + English:", detected_custom_combined)
# Source is reported as "Custom + English"

# 4. Removing profanity using a custom list
cleaned_text_custom = remove_profanity(sample_text_custom, language=None, custom_words_list=loaded_custom_list)
print("Text after removing profanity with custom list:", cleaned_text_custom)

# Clean up dummy file (optional)
import os
os.remove("custom_profanity.txt")
```

Custom words are stripped of surrounding whitespace, blank entries are ignored, and punctuation inside a custom term is matched literally. If `language=None`, at least one non-blank custom word is required, otherwise `ValueError` is raised. `load_profanity_words(language, custom_words_list)` returns the resolved word list if you need direct access.

## PyPI provenance

Releases published by the Trusted Publishing workflow attach a [PEP 740 publish attestation](https://docs.pypi.org/attestations/publish/v1/) to each distribution file. The [PyPI Security tab](https://pypi.org/project/valx/#security) shows the status for the wheel and the source archive separately. Older uploads are not retroactively attested.

## License

ValX is released under the **MIT License (Modified)**: substantial changes must be clearly distinguished from the original work and distributed under a different name. Profanity data derives from the [LDNOOBW list](https://github.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/) by Shutterstock, Inc., used under CC BY 4.0.
