Parsers
TypedLogic supports parsing from multiple input formats to create logical theories.
ValidationMessage
dataclass
A message from a parser that indicates the result of a validation.
Source code in src/typedlogic/parser.py
10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Parser
dataclass
Bases: ABC
A parser is a class that can parse a source and return a Theory object.
You can use the registry get_parser method to get a parser for a particular syntax:
>>> from typedlogic.registry import get_parser
>>> parser = get_parser("yaml")
Source code in src/typedlogic/parser.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
parse_file(source, **kwargs)
Parse a file or a file-like object and return a Theory object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[Path, str, TextIO]
|
|
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
Theory
|
|
Source code in src/typedlogic/parser.py
41 42 43 44 45 46 47 48 49 50 51 | |
parse(source, **kwargs)
abstractmethod
Parse a source and return a Theory object.
TODO: in future, if the source is a text representation, use parse_text() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[Path, str, TextIO]
|
A path to a file, a string representation of the source, or a file-like object. |
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
Theory
|
|
Source code in src/typedlogic/parser.py
53 54 55 56 57 58 59 60 61 62 63 64 | |
parse_text(source, **kwargs)
Parse a text string and return a Theory object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
|
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
Theory
|
|
Source code in src/typedlogic/parser.py
66 67 68 69 70 71 72 73 74 | |
parse_to_sentences(source, **kwargs)
Parse a source and return a list of sentences.
.. note::
This method is a convenience method that calls `parse` and returns the sentences from the resulting Theory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[Path, str, TextIO]
|
|
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
List[Sentence]
|
|
Source code in src/typedlogic/parser.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
parse_ground_terms(source, **kwargs)
Parse a source and return a list of ground terms (facts).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[Path, str, TextIO]
|
|
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
List[Term]
|
|
Source code in src/typedlogic/parser.py
91 92 93 94 95 96 97 98 99 100 | |
translate(source, **kwargs)
Translate a source object into a Theory object.
The type of the source object may be constrained by individual subclasses
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
|
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
Theory
|
|
Source code in src/typedlogic/parser.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
validate_iter(source, **kwargs)
Validate a source and return an iterator of validation messages.
Validation might include type checking (for python source), syntax checking (for text source), linting, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[Path, str, TextIO, ModuleType]
|
|
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
Iterator[ValidationMessage]
|
|
Source code in src/typedlogic/parser.py
118 119 120 121 122 123 124 125 126 127 128 129 | |
validate(source, **kwargs)
Validate a source and return a list of validation messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[Path, str, TextIO, ModuleType]
|
|
required |
kwargs
|
|
{}
|
Returns:
| Type | Description |
|---|---|
List[ValidationMessage]
|
|
Source code in src/typedlogic/parser.py
131 132 133 134 135 136 137 138 139 | |
Available Parsers
Core Parsers
- Python Parser - Parse Python modules with TypedLogic decorators
- YAML Parser - Parse YAML files containing logical specifications
Data Parsers
- DataFrame Parser - Parse CSV, TSV, Excel files using pandas
- Catalog Parser - Orchestrate multiple parsers for complex datasets
Integration Parsers
- OWL Python Parser - Parse OWL ontologies expressed in Python
- RDF Parser - Parse RDF/Turtle files and SPARQL endpoints
Usage
Parsers can be used directly in Python or via the CLI:
from typedlogic.parsers import get_parser
# Get parser by format
parser = get_parser("dataframe")
theory = parser.parse("data.csv")
# Or use specific parser class
from typedlogic.parsers.dataframe_parser import DataFrameParser
parser = DataFrameParser()
theory = parser.parse("data.csv")
# Via CLI - format auto-detected
typedlogic dump data.csv -t yaml
# Or explicitly specify format
typedlogic dump data.txt --input-format dataframe -t prolog