A declarative DSL for extracting structured information from OCR output, PDFs, reports, and other semi-structured documents.
Instead of writing large, difficult-to-maintain regular expressions, DocDSL lets you describe what to extract using a simple, readable language while keeping extraction rules separate from your application code.
pip install docdslfrom docdsl import DSLTranslator
translator = DSLTranslator()
dsl = """
FIND "Patient:";
SKIP UNTIL NEWLINE;
CAPTURE TARGET [@NAME];
"""
pattern = translator.translate(dsl)
print(pattern)
with open("doc.txt", "r") as fh:
text = fh.read()
matches = pattern.finditer(text)
for matched in matches:
print(f"{matched=}")
matched_groups = matched.groupdict()
name = matched_groups.get("NAME", None)
print(f"Name: {name}")The NAME entity is part of DocDSL's built-in entity library and is available
automatically. Custom entities can still be supplied when additional matching
behaviour is required.
- Declarative, English-like extraction language
- Reusable named entities
- Readable extraction rules
- Conditional extraction using
IF - Multi-line capture
- Capture between delimiters
- Built-in helper tokens
- Friendly syntax and validation errors
- Exact error locations with line and column information
Complete language documentation is available in the DocDSL Documentation.
The reference includes:
- Complete command reference
- DSL syntax
- Built-in helper tokens
- Conditional statements
- Examples
- Best practices
- Python 3.11+
Contributions, bug reports and feature requests are welcome.
If you discover a bug or have an idea for improving DocDSL, please open an issue or submit a pull request.
Released under the MIT License.