Skip to main content

A tool for expanding Danbooru tags with their implications and aliases

Project description

Danbooru Tag Expander

A Python tool for expanding Danbooru tags with their implications and aliases. This tool helps you get a complete set of related tags when working with Danbooru's tagging system.

Features

  • Expand tags with their implications and aliases
  • Support for both command-line and programmatic usage
  • Configurable output formats (text, JSON, CSV)
  • Progress tracking and detailed logging
  • Caching support for better performance

Graph Theory Concepts

The tag expansion system can be understood through graph theory:

Tag Graph Structure

  • Tags are nodes in a directed graph
  • Two types of edges exist:
    1. Implications: Directed edges between different concepts (A → B means "A implies B")
    2. Aliases: Form equivalence classes (subgraphs) where all nodes represent the same concept

Frequency Calculation

  • For implications:
    • Multiple implications to the same tag sum their frequencies
    • Example: If A implies X and B implies X, then freq(X) = freq(A) + freq(B)
  • For aliases:
    • All nodes in an alias subgraph share the same frequency
    • Example: If X and Y are aliases, then freq(X) = freq(Y) = total frequency of their concept
    • This reflects that aliases are different names for the same underlying concept

Example

Given:
- Tags: [cat, feline, kitten]
- Aliases: cat ↔ feline (they're the same concept)
- Implications: kitten → cat

Results:
- Expanded tags: [cat, feline, kitten]
- Frequencies:
  - cat: 2 (1 from original + 1 from kitten implication)
  - feline: 2 (same as cat since they're aliases)
  - kitten: 1 (from original tag)

Installation

You can install the package using pip:

pip install danbooru-tag-expander

Usage

Command Line

# Basic usage with tags
danbooru-tag-expander --tags "1girl" "solo"

# Using a file containing tags
danbooru-tag-expander --file tags.txt

# Output in different formats
danbooru-tag-expander --tags "1girl" --format json
danbooru-tag-expander --tags "1girl" --format csv

# Control logging verbosity
danbooru-tag-expander --tags "1girl" --quiet
danbooru-tag-expander --tags "1girl" --log-level DEBUG

Python API

from danbooru_tag_expander.tag_expander import TagExpander

# Create an expander instance
expander = TagExpander(
    username="your-username",  # Optional, can be set via environment
    api_key="your-api-key",    # Optional, can be set via environment
    use_cache=True             # Enable caching for better performance
)

# Expand tags
expanded_tags, frequencies = expander.expand_tags(["1girl", "solo"])

# Print results
print(f"Original tags: 1girl, solo")
print(f"Expanded tags: {', '.join(expanded_tags)}")

Advanced Usage: External Graph Injection

For advanced use cases, you can inject an external DanbooruTagGraph instance from the separate danbooru-tag-graph package:

from danbooru_tag_expander.tag_expander import TagExpander
from danbooru_tag_graph import DanbooruTagGraph

# Create and populate an external graph
graph = DanbooruTagGraph()
graph.add_tag("cat", fetched=True)
graph.add_tag("animal", fetched=True)
graph.add_implication("cat", "animal")

# Use the external graph
expander = TagExpander(
    username="your-username",
    api_key="your-api-key",
    tag_graph=graph  # Inject external graph
)

# This will use the pre-populated graph data
expanded_tags, frequencies = expander.expand_tags(["cat"])

This approach is useful for:

  • Pre-loading tag relationships from external sources
  • Sharing graph instances between multiple expanders
  • Custom caching strategies
  • Integration with external tag management systems

The danbooru-tag-graph package can also be used independently for graph-based tag relationship management.

Configuration

The tool can be configured using environment variables or command-line arguments:

  • DANBOORU_USERNAME: Your Danbooru username
  • DANBOORU_API_KEY: Your Danbooru API key
  • DANBOORU_SITE_URL: Custom Danbooru instance URL (optional)
  • DANBOORU_CACHE_DIR: Custom cache directory location (optional)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

danbooru_tag_expander-0.2.2.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

danbooru_tag_expander-0.2.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file danbooru_tag_expander-0.2.2.tar.gz.

File metadata

  • Download URL: danbooru_tag_expander-0.2.2.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for danbooru_tag_expander-0.2.2.tar.gz
Algorithm Hash digest
SHA256 d4f6600b054b1a7fe6bea66b66d89263ffc46b2fcf8c987217db45115d85c64e
MD5 af44f631a48ef3149ca97dbdcbb2bc99
BLAKE2b-256 aababd66f26aa5269e265a6aa1870a641476cf9adfdf4e12dd69443b8f41939f

See more details on using hashes here.

File details

Details for the file danbooru_tag_expander-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for danbooru_tag_expander-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 30a1f0d326ab94d51162d31db3466ec4bf1221cdf583aa4834925d57ab671873
MD5 9299ce96f16a772bfc6bb5997ab957a0
BLAKE2b-256 9f632a738a4e4262e11c5cb431f7513e4a57e7772f14f36ab4e9dfed7c3a402f

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