One-liner NLP utilities -- summarize, classify, extract entities, analyze sentiment -- with rule-based fallbacks and HuggingFace backends.
Project description
anynlp
NLP utilities that just work
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 | GitHub
Quick Start
pip install anynlp
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
# 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
transformersandtorchare installed, uses HuggingFace models - Otherwise, falls back to rule-based methods (zero dependencies)
You can force a specific backend:
# 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:
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:
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.
# Pre-download HuggingFace models for offline use
python -m anynlp download
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
git clone https://github.com/vietanhdev/anynlp.git
cd anynlp
pip install -e ".[all]"
pytest
See CONTRIBUTING.md for guidelines on adding new tasks and backends.
License
MIT License. See LICENSE for details.
Built with care by NRL.AI
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file anynlp-0.2.2.tar.gz.
File metadata
- Download URL: anynlp-0.2.2.tar.gz
- Upload date:
- Size: 43.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbc4de8cff6d30a6c6fc15a5bcc3f725f86e98afff59f2c926845151f7d08c98
|
|
| MD5 |
ecd1aeecc756084bb727a7bb205fc934
|
|
| BLAKE2b-256 |
972b845eefc90d337ac2f7c2c89ba365ec00b0ccd553114af8d03576a7df3a52
|
File details
Details for the file anynlp-0.2.2-py3-none-any.whl.
File metadata
- Download URL: anynlp-0.2.2-py3-none-any.whl
- Upload date:
- Size: 35.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8574e7d10e3023df5300f12df422310657c8b8cdf0f608db099f98825291418
|
|
| MD5 |
319c8a7587f24de3566a35a57f197a3a
|
|
| BLAKE2b-256 |
63415319b0517b1b698127e3a13caf006bc67fd45d4a41b3f764431d06a1b936
|