Skip to main content

Lexical Density Tool for Swarmauri.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_tool_lexicaldensity

---

Swarmauri Tool · Lexical Density

A Swarmauri-compatible NLP utility that measures the lexical density of text—the ratio of content words (nouns, verbs, adjectives, adverbs) versus the total token count. Use it to monitor writing complexity, automate readability checks, or surface style signals inside agent conversations.

  • Tokenizes input with NLTK and tags parts of speech to isolate lexical items.
  • Returns a percentage score so changes in density are easy to compare between drafts.
  • Ships as a Swarmauri tool, ready for registration inside agents or pipelines.

Requirements

  • Python 3.10 – 3.13.
  • nltk with the punkt_tab and averaged_perceptron_tagger_eng resources available (downloaded at runtime).
  • textstat for robust lexicon counting.
  • Core dependencies (swarmauri_base, swarmauri_standard, pydantic).

Installation

Pick the installer that matches your project; each command resolves the transitive requirements.

pip

pip install swarmauri_tool_lexicaldensity

Poetry

poetry add swarmauri_tool_lexicaldensity

uv

# Add to the current project and update uv.lock
uv add swarmauri_tool_lexicaldensity

# or install into the running environment without touching pyproject.toml
uv pip install swarmauri_tool_lexicaldensity

Tip: If you deploy in an offline environment, download the required NLTK models during build time (python -m nltk.downloader punkt_tab averaged_perceptron_tagger_eng).

Quick Start

from swarmauri_tool_lexicaldensity import LexicalDensityTool

text = "This report summarizes quarterly revenue growth across all segments."

lexical_density_tool = LexicalDensityTool()
result = lexical_density_tool(text)

print(result)
# {'lexical_density': 58.333333333333336}

The tool returns a floating-point percentage. Use the same instance to score multiple passages.

Usage Scenarios

Enforce Style Guidelines in Content Pipelines

from swarmauri_tool_lexicaldensity import LexicalDensityTool

product_copy = "Introducing our new AI-powered workstation with modular expansion."

checker = LexicalDensityTool()
score = checker(product_copy)["lexical_density"]

if score < 40:
    raise ValueError(f"Copy too simple (density={score:.1f}%). Add more substantive language.")

Gate marketing copy or documentation PRs based on desired complexity thresholds.

Analyze Conversations in a Swarmauri Agent

from swarmauri_core.agent.Agent import Agent
from swarmauri_standard.tools.registry import ToolRegistry
from swarmauri_tool_lexicaldensity import LexicalDensityTool

registry = ToolRegistry()
registry.register(LexicalDensityTool())
agent = Agent(tool_registry=registry)

utterance = "Could you elaborate on the architectural trade-offs in the data plane?"
result = agent.tools["LexicalDensityTool"](utterance)
print(result)

Use lexical density as a signal to adjust agent tone or escalate queries to human operators.

Batch Score Documents and Track Trends

from pathlib import Path
from swarmauri_tool_lexicaldensity import LexicalDensityTool

lexical_density = LexicalDensityTool()
corpus_dir = Path("reports/")

scores = []
for doc in corpus_dir.glob("*.txt"):
    text = doc.read_text(encoding="utf-8")
    scores.append((doc.name, lexical_density(text)["lexical_density"]))

for name, score in sorted(scores, key=lambda item: item[1], reverse=True):
    print(f"{name}: {score:.2f}% lexical words")

Monitor writing complexity across a corpus of articles or support responses.

Troubleshooting

  • LookupError for NLTK resources – Ensure punkt_tab and averaged_perceptron_tagger_eng are downloaded prior to calling the tool (see nltk.download(...)).
  • Low density on short texts – Very short messages yield coarse percentages. Aggregate multiple utterances or relax thresholds for brief content.
  • Non-English text – POS tagging models target English. Swap in language-specific models before using the tool with multilingual corpora.

License

swarmauri_tool_lexicaldensity is released under the Apache 2.0 License. See LICENSE for full details.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swarmauri_tool_lexicaldensity-0.10.1.dev2.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file swarmauri_tool_lexicaldensity-0.10.1.dev2.tar.gz.

File metadata

  • Download URL: swarmauri_tool_lexicaldensity-0.10.1.dev2.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_tool_lexicaldensity-0.10.1.dev2.tar.gz
Algorithm Hash digest
SHA256 dced01b116a2ede7a8e17dc78174a9a7ca407cf49e497ffef6658449246dcb25
MD5 87125354ebcb0397a4d33fe7c30220d5
BLAKE2b-256 07ad1c91daf84b723674a8c4d626f524ebcbb8950db4f054b38c13b5984da8b6

See more details on using hashes here.

File details

Details for the file swarmauri_tool_lexicaldensity-0.10.1.dev2-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_tool_lexicaldensity-0.10.1.dev2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_tool_lexicaldensity-0.10.1.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 e81d47415da7779ca12c7f25ebe1d9db1c1dc7c096ecb0de0230272dff90d574
MD5 ed013a1240f4234e2ff64635dadb0b5e
BLAKE2b-256 926fa3f74e7a02b15d02296a44a0fa56a72017fcb13aeedd26946259d4759274

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