Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

LexCQL for Python

A query parser for LexCQL, the query language for lexical resources in the CLARIN Federated Content Search (FCS).

Installation

Install from PyPI:

python3 -m pip install lexcql-parser

Or install from source:

git clone https://github.com/Querela/lexcql-python.git
cd lexcql-python
uv build

# built package
python3 -m pip install dist/lexcql_parser-<version>-py3-none-any.whl
# or
python3 -m pip install dist/lexcql_parser-<version>.tar.gz

# for local development
python3 -m pip install -e .

Usage

The high-level interface lexcql.parser.QueryParser wraps the ANTLR4 parse tree into a simplified query node tree that is easier to work with. The lexcql-parser exposes a simple parsing function with lexcql.parse(input: str, enableSourceLocations: bool = True) -> lexcql.parser.QueryNode:

import lexcql

## parsing a valid query into a query node tree
# our query input string
input = "Banane Or lemma =/lang=eng apple"
# parse into QueryNode tree
sc = lexcql.parse(input)
# print stringified tree
print(str(sc))

## handling possibly invalid queries
input = "broken query"
try:
    lexcql.parse(input)
except lexcql.QueryParserException as ex:
    print(f"Error: {ex}")

You can also use the more low-level ANTLR4 framework to parse the query string. A handy wrapper is provided with lexcql.antlr_parse(input: str) -> LexParser.QueryContext.

from antlr4 import CommonTokenStream, InputStream
from lexcql.parser import LexLexer, LexParser

input = "example"
input_stream = InputStream(input)
lexer = LexLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = LexParser(stream)
tree: LexParser.QueryContext = parser.query()

Parsed queries can also be checked against their specification conformance.

from lexcql import QueryParser
from lexcql.validation import LexCQLValidatorV1_0, SpecificationValidationError

parser = QueryParser(enableSourceLocations=True)

query = """Banane"""
node = parser.parse(query)
validator = LexCQLValidatorV1_0()
validator.validate(node, query=query)
len(validator.errors) == 0  # no errors

# or to raise an error on first violation
query = """post = NOUN"""
node = parser.parse(query)
validator = LexCQLValidatorV1_0(raise_at_first_violation=True)
validator.validate(node, query=query)  # raises SpecificationValidationError

A convenience method is provded with lexcql.validate(query: str):

from lexcql import validate

# simple boolean returns
validate("lemma = apple")  # => True
validate("lemmas = apple")  # => False ("lemmas" is unknown field name)
validate("lemma =")  # => False (parse error, missing search term)

# or with list of errors
error = validate("post = NOUN", return_errors=True)[0]  # has one error
assert error.message == "Unknown index 'post'!"
# error is the full query
assert error.fragment == "post = NOUN"
assert error.position.start == 0
assert error.position.stop == 11
assert error.type == "validation-error"

Development

Fetch (or update) grammar files:

git clone https://github.com/clarin-eric/fcs-ql.git
cp fcs-ql/src/main/antlr4/eu/clarin/sru/fcs/qlparser/lex/*.g4 src/lexcql/

(Re-)Generate python parser code:

# setup environment
uv sync --extra antlr
# NOTE: you can activate the environment (if you do not want to prefix everything with `uv run`)
# NOTE: `uv` does not play nicely with `pyenv` - if you use `pyenv`, sourcing does NOT work!
source .venv/bin/activate

cd src/lexcql
uv run antlr4 -Dlanguage=Python3 *.g4 -listener -visitor

Run style checks:

# setup environment
uv sync --extra style

uv run isort --check --diff .
uv run black --check .
uv run flake8 . --show-source --statistics

uv run mypy src

Run tests:

# setup environment
uv sync --extra test

uv run pytest
# to see output and run a specific test file
uv run pytest -v -rP tests/validation/test_validation.py

Run check before publishing:

# setup environment
uv sync --extra build

# build the package
uv build
# run metadata check
uv run twine check --strict dist/*
# (manual) check of package contents
tar tvf dist/lexcql_parser-*.tar.gz

See also

Download files

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

Source Distribution

lexcql_parser-1.4.0a1.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

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

lexcql_parser-1.4.0a1-py3-none-any.whl (31.0 kB view details)

Uploaded Python 3

File details

Details for the file lexcql_parser-1.4.0a1.tar.gz.

File metadata

  • Download URL: lexcql_parser-1.4.0a1.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for lexcql_parser-1.4.0a1.tar.gz
Algorithm Hash digest
SHA256 9e8a05c0ec4852f656998f33b00b3efe3edca71ec64733a032c8125f31013666
MD5 a6cea8b6322910e50d5adc53167c3df9
BLAKE2b-256 3cf0bb7ba186c6bef26612e8ff7d68281e40988eea6ce665af352b0b8c4935c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for lexcql_parser-1.4.0a1.tar.gz:

Publisher: publish-pypi.yml on Querela/lexcql-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lexcql_parser-1.4.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for lexcql_parser-1.4.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 73ff7d47c82186d36d7215a525c5308e0e2662c4a95def73d825bfa609635cb2
MD5 60efe93632341af1580f8d9fbca778c5
BLAKE2b-256 de17915b51fd0edeb7d10d40ac46b6bfccdb79c301ed6834b01fb95de7c9e011

See more details on using hashes here.

Provenance

The following attestation bundles were made for lexcql_parser-1.4.0a1-py3-none-any.whl:

Publisher: publish-pypi.yml on Querela/lexcql-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.4.0a1 This release

2 files

1.3.5

2 files

1.3.4

2 files

1.3.3

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.0

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page