Metadata-Version: 2.4
Name: anynlp
Version: 0.2.2
Summary: One-liner NLP utilities -- summarize, classify, extract entities, analyze sentiment -- with rule-based fallbacks and HuggingFace backends.
Project-URL: Homepage, https://github.com/vietanhdev/anynlp
Project-URL: Documentation, https://github.com/vietanhdev/anynlp#readme
Project-URL: Repository, https://github.com/vietanhdev/anynlp
Project-URL: Issues, https://github.com/vietanhdev/anynlp/issues
Project-URL: Author, https://www.nrl.ai
Author-email: Viet-Anh Nguyen <vietanh.dev@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: classification,ner,nlp,sentiment,summarization,text
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Text Processing :: Linguistic
Requires-Python: >=3.8
Requires-Dist: click>=8.0
Provides-Extra: all
Requires-Dist: anyllm>=0.1.0; extra == 'all'
Requires-Dist: torch>=1.9.0; extra == 'all'
Requires-Dist: transformers>=4.20.0; extra == 'all'
Provides-Extra: llm
Requires-Dist: anyllm>=0.1.0; extra == 'llm'
Provides-Extra: progress
Requires-Dist: tqdm>=4.60.0; extra == 'progress'
Provides-Extra: transformers
Requires-Dist: torch>=1.9.0; extra == 'transformers'
Requires-Dist: transformers>=4.20.0; extra == 'transformers'
Description-Content-Type: text/markdown

<h1 align="center">anynlp</h1>
<p align="center"><em>NLP utilities that just work</em></p>

![PyPI](https://img.shields.io/pypi/v/anynlp)
![Python](https://img.shields.io/pypi/pyversions/anynlp)
![License](https://img.shields.io/pypi/l/anynlp)

**One-liner NLP utilities** -- summarize, classify, extract entities, analyze sentiment, and more.

**Runs completely offline.** Works out of the box with **zero dependencies** using rule-based backends. Optionally upgrade to **HuggingFace transformers** for ML-powered results -- models download once and are cached locally.

Built by [Viet-Anh Nguyen](https://www.nrl.ai) | [GitHub](https://github.com/vietanhdev)

---

## Quick Start

```bash
pip install anynlp
```

```python
import anynlp

# Summarize text
summary = anynlp.summarize("Your long article text here...", ratio=0.3)
print(summary.text)

# Sentiment analysis
sentiment = anynlp.sentiment("I absolutely love this library!")
print(sentiment)  # positive (90.00%)

# Named entity extraction
entities = anynlp.entities("Contact john@example.com or call 555-123-4567 by January 15, 2024.")
for entity in entities:
    print(f"{entity.text} [{entity.label}]")

# Keyword extraction
keywords = anynlp.keywords("Machine learning is transforming artificial intelligence research.", n=5)
for kw in keywords:
    print(f"{kw.word}: {kw.score:.2f}")

# Text classification
label = anynlp.classify("Great product, highly recommend!", labels=["positive", "negative"])
print(label)  # positive (100.00%)

# Text similarity
score = anynlp.similarity("Hello world", "Hi there world")
print(float(score))  # 0.3333
```

## Features

| Task | Function | Rule-Based | HuggingFace |
|------|----------|-----------|-------------|
| Summarization | `anynlp.summarize(text)` | Sentence scoring (position, frequency, length) | Abstractive summarization models |
| Classification | `anynlp.classify(text, labels)` | Keyword overlap scoring | Zero-shot classification |
| NER | `anynlp.entities(text)` | Regex patterns (email, phone, date, URL, etc.) | BERT-based NER models |
| Sentiment | `anynlp.sentiment(text)` | Lexicon-based with negation handling | Transformer sentiment models |
| Keywords | `anynlp.keywords(text, n)` | TF-IDF-like scoring | TF-IDF-like scoring |
| Similarity | `anynlp.similarity(a, b)` | Jaccard similarity on word sets | Jaccard similarity on word sets |

## Installation Options

```bash
# Core (rule-based, zero dependencies)
pip install anynlp

# With HuggingFace transformers
pip install anynlp[transformers]

# Everything
pip install anynlp[all]
```

## Backend Selection

anynlp automatically selects the best available backend:

- If `transformers` and `torch` are installed, uses **HuggingFace** models
- Otherwise, falls back to **rule-based** methods (zero dependencies)

You can force a specific backend:

```python
# Always use rule-based (fast, no downloads)
result = anynlp.sentiment("Great!", backend="rules")

# Always use HuggingFace (requires transformers + torch)
result = anynlp.sentiment("Great!", backend="huggingface")
```

## Plugin System

Add custom NLP tasks using the registry pattern:

```python
from anynlp import NLPTask, register_task

class TranslateTask(NLPTask):
    name = "translate"

    def run(self, text="", target_lang="en", **kwargs):
        # Your translation logic here
        return {"text": text, "lang": target_lang}

# Register and use
register_task("translate", TranslateTask)

from anynlp.core import registry
result = registry.run("translate", text="Bonjour", target_lang="en")
```

## Result Objects

All functions return rich dataclasses with useful representations:

```python
summary = anynlp.summarize(text)
summary.text                  # The summarized text
summary.sentence_count        # Number of sentences in summary
summary.original_sentence_count  # Number in original
str(summary)                  # The text itself
repr(summary)                 # Summary(sentences=3/10, ratio=0.30)

sentiment = anynlp.sentiment("I love this!")
sentiment.label               # "positive"
sentiment.score               # 0.9 (0-1 scale, 0.5 = neutral)

entities = anynlp.entities(text)
len(entities)                 # Number of entities
entities[0].text              # Entity text
entities[0].label             # Entity type (EMAIL, URL, DATE, etc.)

similarity = anynlp.similarity(text1, text2)
float(similarity)             # Score as float (0.0-1.0)
similarity.method             # "jaccard"
```

## Local-First / Edge AI

This package is designed to work completely offline. The rule-based backend
requires zero internet access. For ML-powered results, HuggingFace models
download once and are cached locally -- no internet needed after that.

```bash
# Pre-download HuggingFace models for offline use
python -m anynlp download
```

```python
import anynlp

# Pre-download all HuggingFace models
anynlp.download_models()

# Rule-based backend always works offline (no downloads needed)
result = anynlp.sentiment("Great product!", backend="rules")
```

## Development

```bash
git clone https://github.com/vietanhdev/anynlp.git
cd anynlp
pip install -e ".[all]"
pytest
```

See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on adding new tasks and backends.

## License

MIT License. See [LICENSE](LICENSE) for details.

---

Built with care by [NRL.AI](https://www.nrl.ai)
