Skip to main content

Translate natural language queries to Scryfall search syntax

Project description

nlp2scryfall

PyPI version Tests Coverage License: MIT

A Python package that translates natural language queries to Scryfall search syntax.

Installation

pip install nlp2scryfall

For development:

pip install nlp2scryfall[dev]

Usage

from nlp2scryfall import Translator

translator = Translator()

# Simple queries
query = translator.translate("find all red dragons")
print(query)  # "c>=r t:dragon"

# Complex queries
query = translator.translate("show me legendary creatures from dominaria that cost 3 or less")
print(query)  # "t:legendary t:creature s:dom cmc<=3"

# Power/toughness queries
query = translator.translate("creatures with power greater than 5")
print(query)  # "t:creature pow>5"

# Set-specific queries
query = translator.translate("all cards from the latest set")
print(query)  # "s:latest"

# Color identity queries
query = translator.translate("blue and white cards")
print(query)  # "c>=uw"

# Dynamic set queries
query = translator.translate("the most recent 5 sets ordered by price asc")
print(query)  # "s:woe s:mat s:mom s:one s:bro order:usd"

# Land queries (uses color identity)
query = translator.translate("green lands")
print(query)  # "t:land id>=g"

# Mono-color queries
query = translator.translate("mono red creatures")
print(query)  # "c=r t:creature"

Features

  • Natural Language Processing: Converts human-readable queries to Scryfall syntax
  • Comprehensive Coverage: Supports most Scryfall search operators
  • Dynamic Set Recognition: Automatically fetches and recognizes set names and codes from Scryfall API
  • Color Support: Handles color combinations, identities, and mono-color queries
  • Power/Toughness: Translates power and toughness comparisons
  • Type Recognition: Identifies card types, subtypes, and supertypes
  • Mana Cost: Handles mana cost queries and comparisons
  • Order-Agnostic Matching: Color codes are matched regardless of order (e.g., "red and blue" = "blue and red")
  • Smart Land Handling: Automatically uses color identity (id) for land queries instead of card color (c)

Features in Depth

Color Syntax

  • General colors: c>=r (cards that can be played with red)
  • Mono colors: c=r (cards that are only red)
  • Lands: id>=g (lands with green in their color identity)
  • Colorless: c=c (colorless cards)

Dynamic Set Support

  • Set names and codes are fetched dynamically from the Scryfall API
  • Supports "most recent N sets" queries (e.g., "the most recent 5 sets")
  • Caches set data for performance
  • Handles special sets like "latest", "oldest", "The List"

Query Processing

  • Order independence: Components can appear in any order in the query
  • Case insensitive: Queries work regardless of capitalization
  • Filler word removal: Ignores common words like "find", "show", "get", etc.
  • Conflict resolution: Parsers are ordered to avoid conflicts (e.g., type parser runs before color parser)

Supported Query Types

Basic Properties

  • Card names
  • Colors and color identity
  • Card types (creature, instant, sorcery, etc.)
  • Rarities
  • Sets and editions

Advanced Properties

  • Power and toughness
  • Mana cost
  • Oracle text
  • Artist names
  • Flavor text
  • Collector numbers

Complex Queries

  • Multi-color combinations (guilds, shards, five-color)
  • Set ranges and dynamic set queries
  • Date ranges
  • Format legality
  • Price ranges
  • Ordering (by price, power, CMC, name, etc.)

Limitations

  • English-only: Currently only supports English natural language queries
  • Scryfall dependency: Requires internet connection to fetch set data
  • Query complexity: Very complex nested queries may not be fully supported
  • Ambiguous queries: Some queries may have multiple valid interpretations
  • API rate limits: Respects Scryfall's API rate limits (100ms between requests)

Testing

Run the test suite:

pytest

Run with coverage:

pytest --cov=nlp2scryfall

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
  6. Submit a pull request

License

MIT License - see 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

nlp2scryfall-0.1.0.tar.gz (105.7 kB view details)

Uploaded Source

Built Distribution

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

nlp2scryfall-0.1.0-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file nlp2scryfall-0.1.0.tar.gz.

File metadata

  • Download URL: nlp2scryfall-0.1.0.tar.gz
  • Upload date:
  • Size: 105.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for nlp2scryfall-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e36fe4ef5a345e6f461ed7229e4b34d86cb8e2c5666edb84bc7d6599fe96c0a
MD5 0d351d1ba806e8089301b7b7c1aa0d54
BLAKE2b-256 e4b6ca69707aa9926e0ae7e7afcd960afb5b72bae361b1657cd2b20af8b6b99e

See more details on using hashes here.

File details

Details for the file nlp2scryfall-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nlp2scryfall-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for nlp2scryfall-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e4b2fac111894b367a567087c0ca0b9b4527eb1997032e1018539981d470e9f
MD5 b87cec3ed2c4b1bf4b8331aefd4e695e
BLAKE2b-256 119f113070354b7cd2ae6b5aff28b6c2820454eb9d50395e2073423852ce497b

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