Skip to main content

One-liner NLP utilities -- summarize, classify, extract entities, analyze sentiment -- with rule-based fallbacks and HuggingFace backends.

Project description

anynlp

anynlp logo

PyPI Python License

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 transformers and torch are 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

anynlp-0.2.0.tar.gz (43.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anynlp-0.2.0-py3-none-any.whl (35.5 kB view details)

Uploaded Python 3

File details

Details for the file anynlp-0.2.0.tar.gz.

File metadata

  • Download URL: anynlp-0.2.0.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

Hashes for anynlp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1bd45a81a13b43a9967bbfc0150f2ce72991e2f8d2fc2ba0199c0d4ca1bcedcd
MD5 8ac31b06a3afaef279d63416c2f4aac7
BLAKE2b-256 3a91ab1a6366f5de2375fa37b76ec0fd4565e8dcb9de5b730ed11db38cf22a28

See more details on using hashes here.

File details

Details for the file anynlp-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: anynlp-0.2.0-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

Hashes for anynlp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5acfa68900ef085a6f3e413aa122221e44a4c47f591480db62d9a6d13397bb52
MD5 4316e7eade4ca830f636c96ba8eb59b9
BLAKE2b-256 2186b1bd596c773993f82bccdd9c82e8d3b3003d456227e4549af6e26261d5a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page