Skip to main content

Pattern generating DSL for spaCy

Project description

Spatgen: Pattern generator for spaCy

Spatgen is a concise and readable DSL and parser which produces patterns for spaCy which you can use in the Matcher class.

We think Matcher is fantastic but writing patterns can become tedious especially if there are several combinations with the same prefix or suffix and if the rules don't fall into any of the OP operations provided. This package simplifies the creation of large lists of patterns.

Example

Here's an example showing how to match some syntax with optional and either-or patterns (you can combine these too):

from spatgen import parser

patterns = r"""
name = example
anchors = [study survey report research datum article review]

++match++
lemma(anchors) aux? adv? verb adp? <det? noun | date> [adp det? <org | gpe>]
"""

parser.parse_str(str(patterns))

This will produce a Sections object which contains a list of patterns (suitable for Matcher) for each heading found in the input. It also has a get_meta method which allows you to retrieve extra information about the pattern such as its line number and the raw source that led to its creation.

Syntax

Variables

Names can contain lowercase alphabetic characters and underscores only. Values are interpreted as either strings or arrays of strings and quotation marks are not required.

Sections

Sections are demarcated by surrounding an identifier with ++ on either side.

Patterns

Patterns loosely follow command-line syntax format.

  • [a ...] means whatever inside is optional. A pattern will be generated for combinations with and without this option.
  • <a | b | ...> means either a or b (or more). Patterns are generated for every combination.
  • action(param) interprets action to be one of spaCy's token operators (LEMMA, LOWER, etc).
    • If param is a string surrounded by quotes then the pattern is compared directly (e.g. {"LEMMA": "say"}).
    • If param is an identifier and is specified as a variable then the pattern is an IN pattern ( e.g. {"LEMMA": {"IN": var}}).
  • Other terms are interpreted as either POS tags or entity types. These are matched on context so the spaCy pattern will have the correct type.
    • E.g. verb will produce {"POS": "VERB"} but org will produce {"ENT_TYPE": "ORG"}.
  • Every match supports modifiers and will produce the corresponding OP entry:
    • ? will match zero or one
    • * will match zero or more
    • + will match one or more
    • ! will ensure that none match

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

spatgen-0.1.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

spatgen-0.1.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file spatgen-0.1.3.tar.gz.

File metadata

  • Download URL: spatgen-0.1.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for spatgen-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e7da22239d884efbb0bdcf6c23da32fd110427d3602b7bca3b4e1c6853b4a7c2
MD5 6780b5a0cae814241c2e68f20ee100f9
BLAKE2b-256 8f4c2a3dec53b87d93f3e621889dcc320de826283a6e7e4385a5cd476bb6d030

See more details on using hashes here.

File details

Details for the file spatgen-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: spatgen-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for spatgen-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 135c39460297a24c7ca6d4a8d255354df1d6aca5c155886971b805d877d35a0b
MD5 9ef6e9679b47a3604c7853ab5231bd9d
BLAKE2b-256 1c7a976aea7ada1af9eaad047c19113560214b55c2b95511fcbab26f4eb305ff

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