stl-parser 1.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. stl_parser-1.7.0/LICENSE +201 -0
  2. stl_parser-1.7.0/PKG-INFO +351 -0
  3. stl_parser-1.7.0/README.md +308 -0
  4. stl_parser-1.7.0/pyproject.toml +148 -0
  5. stl_parser-1.7.0/setup.cfg +4 -0
  6. stl_parser-1.7.0/stl_parser/__init__.py +113 -0
  7. stl_parser-1.7.0/stl_parser/_utils.py +524 -0
  8. stl_parser-1.7.0/stl_parser/analyzer.py +309 -0
  9. stl_parser-1.7.0/stl_parser/builder.py +252 -0
  10. stl_parser-1.7.0/stl_parser/cli.py +758 -0
  11. stl_parser-1.7.0/stl_parser/decay.py +294 -0
  12. stl_parser-1.7.0/stl_parser/diff.py +414 -0
  13. stl_parser-1.7.0/stl_parser/emitter.py +231 -0
  14. stl_parser-1.7.0/stl_parser/errors.py +919 -0
  15. stl_parser-1.7.0/stl_parser/grammar.py +232 -0
  16. stl_parser-1.7.0/stl_parser/graph.py +254 -0
  17. stl_parser-1.7.0/stl_parser/llm.py +563 -0
  18. stl_parser-1.7.0/stl_parser/models.py +427 -0
  19. stl_parser-1.7.0/stl_parser/parser.py +607 -0
  20. stl_parser-1.7.0/stl_parser/query.py +340 -0
  21. stl_parser-1.7.0/stl_parser/reader.py +458 -0
  22. stl_parser-1.7.0/stl_parser/schema.py +764 -0
  23. stl_parser-1.7.0/stl_parser/serializer.py +225 -0
  24. stl_parser-1.7.0/stl_parser/validator.py +642 -0
  25. stl_parser-1.7.0/stl_parser.egg-info/PKG-INFO +351 -0
  26. stl_parser-1.7.0/stl_parser.egg-info/SOURCES.txt +48 -0
  27. stl_parser-1.7.0/stl_parser.egg-info/dependency_links.txt +1 -0
  28. stl_parser-1.7.0/stl_parser.egg-info/entry_points.txt +2 -0
  29. stl_parser-1.7.0/stl_parser.egg-info/requires.txt +19 -0
  30. stl_parser-1.7.0/stl_parser.egg-info/top_level.txt +1 -0
  31. stl_parser-1.7.0/tests/test_analyzer.py +190 -0
  32. stl_parser-1.7.0/tests/test_auto_extraction.py +535 -0
  33. stl_parser-1.7.0/tests/test_builder.py +216 -0
  34. stl_parser-1.7.0/tests/test_cli.py +252 -0
  35. stl_parser-1.7.0/tests/test_cli_query.py +231 -0
  36. stl_parser-1.7.0/tests/test_decay.py +288 -0
  37. stl_parser-1.7.0/tests/test_diff.py +459 -0
  38. stl_parser-1.7.0/tests/test_emitter.py +232 -0
  39. stl_parser-1.7.0/tests/test_empty.py +12 -0
  40. stl_parser-1.7.0/tests/test_graph.py +205 -0
  41. stl_parser-1.7.0/tests/test_llm.py +212 -0
  42. stl_parser-1.7.0/tests/test_multiline_merge.py +312 -0
  43. stl_parser-1.7.0/tests/test_parser.py +235 -0
  44. stl_parser-1.7.0/tests/test_query.py +422 -0
  45. stl_parser-1.7.0/tests/test_reader.py +506 -0
  46. stl_parser-1.7.0/tests/test_roundtrip.py +0 -0
  47. stl_parser-1.7.0/tests/test_schema.py +294 -0
  48. stl_parser-1.7.0/tests/test_serializer.py +239 -0
  49. stl_parser-1.7.0/tests/test_utils.py +51 -0
  50. stl_parser-1.7.0/tests/test_validator.py +479 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,351 @@
1
+ Metadata-Version: 2.4
2
+ Name: stl-parser
3
+ Version: 1.7.0
4
+ Summary: Semantic Tension Language (STL) Parser - A lightweight parser for structured knowledge representation
5
+ Author-email: STL Working Group <contact@scos-lab.org>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/scos-lab/semantic-tension-language
8
+ Project-URL: Documentation, https://stl-parser.readthedocs.io
9
+ Project-URL: Repository, https://github.com/scos-lab/semantic-tension-language/tree/main/parser
10
+ Project-URL: Issues, https://github.com/scos-lab/semantic-tension-language/issues
11
+ Keywords: stl,semantic-tension-language,parser,knowledge-graph,structured-knowledge
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: lark>=1.1.0
26
+ Requires-Dist: pydantic>=2.0.0
27
+ Requires-Dist: rdflib>=7.0.0
28
+ Requires-Dist: networkx>=3.0
29
+ Requires-Dist: typer>=0.9.0
30
+ Requires-Dist: rich>=13.0.0
31
+ Provides-Extra: dev
32
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
33
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
34
+ Requires-Dist: hypothesis>=6.0.0; extra == "dev"
35
+ Requires-Dist: black>=23.0.0; extra == "dev"
36
+ Requires-Dist: mypy>=1.0.0; extra == "dev"
37
+ Requires-Dist: ruff>=0.0.250; extra == "dev"
38
+ Provides-Extra: docs
39
+ Requires-Dist: sphinx>=5.0.0; extra == "docs"
40
+ Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "docs"
41
+ Requires-Dist: myst-parser>=0.18.0; extra == "docs"
42
+ Dynamic: license-file
43
+
44
+ # STL Parser (v1.7.0)
45
+
46
+ **A comprehensive Python toolkit for Semantic Tension Language (STL) — parse, build, validate, query, diff, stream, and repair structured knowledge.**
47
+
48
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
49
+ [![Python Version](https://img.shields.io/badge/python-3.9%2B-blue)](https://www.python.org/downloads/)
50
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
51
+
52
+ ## Overview
53
+
54
+ `stl-parser` provides feature parity with the JSON ecosystem for STL documents:
55
+
56
+ | Capability | Functions | JSON Equivalent |
57
+ |-----------|-----------|-----------------|
58
+ | **Parse & Serialize** | `parse()`, `to_json()`, `to_stl()`, `to_rdf()` | `json.loads()` / `json.dumps()` |
59
+ | **Build Programmatically** | `stl()`, `stl_doc()`, `StatementBuilder` | Manual dict construction |
60
+ | **Schema Validation** | `load_schema()`, `validate_against_schema()` | JSON Schema |
61
+ | **LLM Output Repair** | `clean()`, `repair()`, `validate_llm_output()` | N/A (STL-unique) |
62
+ | **Query & Filter** | `find()`, `filter_statements()`, `select()`, `stl_pointer()` | `jq` / JSONPath |
63
+ | **Diff & Patch** | `stl_diff()`, `stl_patch()`, `diff_to_text()` | `json-diff` / `json-patch` |
64
+ | **Streaming I/O** | `STLEmitter`, `STLReader`, `stream_parse()` | NDJSON streaming |
65
+ | **Graph Analysis** | `STLGraph`, `STLAnalyzer` | N/A (STL-unique) |
66
+ | **Confidence Decay** | `effective_confidence()`, `decay_report()` | N/A (STL-unique) |
67
+ | **CLI** | 10 commands (validate, convert, query, diff, ...) | `jq` CLI |
68
+
69
+ ## Installation
70
+
71
+ ### From Source
72
+
73
+ ```bash
74
+ git clone https://github.com/scos-lab/semantic-tension-language.git
75
+ cd semantic-tension-language/parser
76
+ pip install -e .
77
+ ```
78
+
79
+ ### Development Installation
80
+
81
+ ```bash
82
+ pip install -e ".[dev]"
83
+ ```
84
+
85
+ ## Quick Start
86
+
87
+ ### Parse STL
88
+
89
+ ```python
90
+ from stl_parser import parse
91
+
92
+ result = parse('[Einstein] -> [Theory_Relativity] ::mod(confidence=0.98, rule="empirical")')
93
+ stmt = result.statements[0]
94
+ print(stmt.source.name) # "Einstein"
95
+ print(stmt.modifiers.confidence) # 0.98
96
+ ```
97
+
98
+ ### Build Programmatically
99
+
100
+ ```python
101
+ from stl_parser import stl
102
+
103
+ stmt = stl("[Rain]", "[Flooding]").mod(confidence=0.85, rule="causal", strength=0.8).build()
104
+ print(str(stmt)) # [Rain] -> [Flooding] ::mod(confidence=0.85, rule="causal", strength=0.8)
105
+ ```
106
+
107
+ ### Build a Multi-Statement Document
108
+
109
+ ```python
110
+ from stl_parser import stl, stl_doc
111
+
112
+ doc = stl_doc(
113
+ stl("[Rain]", "[Flooding]").mod(rule="causal", confidence=0.85),
114
+ stl("[Flooding]", "[Evacuation]").mod(rule="causal", confidence=0.90),
115
+ )
116
+ print(len(doc.statements)) # 2
117
+ ```
118
+
119
+ ### Validate Against a Schema
120
+
121
+ ```python
122
+ from stl_parser import parse, load_schema, validate_against_schema
123
+
124
+ schema = load_schema("docs/schemas/causal.stl.schema")
125
+ result = parse('[Rain] -> [Flooding] ::mod(rule="causal", confidence=0.85, strength=0.8)')
126
+ validation = validate_against_schema(result, schema)
127
+ print(f"Valid: {validation.is_valid}")
128
+ ```
129
+
130
+ ### Clean LLM Output
131
+
132
+ ```python
133
+ from stl_parser import validate_llm_output
134
+
135
+ result = validate_llm_output("A => B mod(confience=1.5)")
136
+ print(f"Valid: {result.is_valid}")
137
+ print(f"Repairs: {len(result.repairs)}")
138
+ # Repairs fix: missing brackets, wrong arrow, missing ::, typo "confience", value 1.5 clamped to 1.0
139
+ ```
140
+
141
+ ### Query Statements
142
+
143
+ ```python
144
+ from stl_parser import parse, find, find_all, filter_statements, select
145
+
146
+ result = parse("""
147
+ [Rain] -> [Flooding] ::mod(rule="causal", confidence=0.85)
148
+ [Sun] -> [Evaporation] ::mod(rule="causal", confidence=0.90)
149
+ [Wind] -> [Erosion] ::mod(rule="causal", confidence=0.70)
150
+ """)
151
+
152
+ # Find first match ("source" resolves to source.name)
153
+ stmt = find(result, source="Rain")
154
+
155
+ # Filter returns a new ParseResult
156
+ high_conf = filter_statements(result, confidence__gte=0.85)
157
+ print(len(high_conf.statements)) # 2
158
+
159
+ # Extract a single field from all statements
160
+ names = select(result, field="source") # ["Rain", "Sun", "Wind"]
161
+ confs = select(result, field="confidence") # [0.85, 0.9, 0.7]
162
+ ```
163
+
164
+ ### Diff & Patch
165
+
166
+ ```python
167
+ from stl_parser import parse, stl_diff, stl_patch
168
+
169
+ before = parse('[A] -> [B] ::mod(confidence=0.8)')
170
+ after = parse('[A] -> [B] ::mod(confidence=0.9)')
171
+
172
+ diff = stl_diff(before, after)
173
+ print(diff.summary) # {added: 0, removed: 0, modified: 1}
174
+
175
+ patched = stl_patch(before, diff)
176
+ ```
177
+
178
+ ### Streaming I/O
179
+
180
+ ```python
181
+ from stl_parser import STLEmitter, stream_parse
182
+
183
+ # Write statements to a file
184
+ with STLEmitter("output.stl") as emitter:
185
+ emitter.emit("[A]", "[B]", confidence=0.9, rule="causal")
186
+ emitter.emit("[C]", "[D]", confidence=0.8, rule="logical")
187
+
188
+ # Stream-parse a file
189
+ for stmt in stream_parse("output.stl"):
190
+ print(f"{stmt.source.name} -> {stmt.target.name}")
191
+ ```
192
+
193
+ ### Confidence Decay
194
+
195
+ ```python
196
+ from stl_parser import parse, effective_confidence
197
+
198
+ # Statement with a timestamp
199
+ result = parse('[Fact_X] -> [Conclusion_Y] ::mod(confidence=0.95, timestamp="2020-01-01T00:00:00Z")')
200
+ stmt = result.statements[0]
201
+
202
+ # Compute decayed confidence (exponential half-life)
203
+ decayed = effective_confidence(stmt, half_life_days=365)
204
+ print(f"Original: {stmt.modifiers.confidence}, Decayed: {decayed:.4f}")
205
+ ```
206
+
207
+ ## CLI Reference
208
+
209
+ The `stl` command provides 10 subcommands:
210
+
211
+ ```bash
212
+ # Validate an STL file
213
+ stl validate input.stl
214
+
215
+ # Parse and output as JSON
216
+ stl parse input.stl --json
217
+
218
+ # Convert to JSON, RDF/Turtle, JSON-LD, N-Triples
219
+ stl convert input.stl --to json --output output.json
220
+ stl convert input.stl --to rdf --format turtle --output output.ttl
221
+
222
+ # Graph analysis (nodes, edges, centrality, cycles)
223
+ stl analyze input.stl
224
+
225
+ # Build a statement from CLI
226
+ stl build "[Rain]" "[Flooding]" --mod "rule=causal,confidence=0.85"
227
+
228
+ # Clean and repair LLM output
229
+ stl clean messy_output.txt --show-repairs
230
+ stl clean messy_output.txt --schema domain.stl.schema
231
+
232
+ # Validate against a domain schema
233
+ stl schema-validate input.stl --schema medical.stl.schema
234
+
235
+ # Query with filters and field selection
236
+ stl query input.stl --where "rule=causal,confidence__gte=0.8" --select "source.name,target.name"
237
+
238
+ # Semantic diff between two files
239
+ stl diff before.stl after.stl
240
+ stl diff before.stl after.stl --format json
241
+
242
+ # Apply a diff patch
243
+ stl patch base.stl changes.json --output patched.stl
244
+ ```
245
+
246
+ ## Architecture
247
+
248
+ ```
249
+ stl_parser/ # 19 modules
250
+
251
+ ├── Core Layer
252
+ │ ├── grammar.py # Lark EBNF grammar definition
253
+ │ ├── parser.py # parse(), parse_file() — core parser with multi-line merge
254
+ │ ├── models.py # Pydantic v2 models (Anchor, Modifier, Statement, ParseResult)
255
+ │ ├── validator.py # Structural and semantic validation
256
+ │ ├── errors.py # Error hierarchy (E001-E969, W001-W099)
257
+ │ └── _utils.py # Shared utilities (sanitize_anchor_name, etc.)
258
+
259
+ ├── Serialization Layer
260
+ │ ├── serializer.py # to_json(), to_dict(), to_stl(), to_rdf(), from_json()
261
+ │ ├── builder.py # stl(), stl_doc(), StatementBuilder — programmatic construction
262
+ │ └── emitter.py # STLEmitter — thread-safe streaming writer
263
+
264
+ ├── Analysis Layer
265
+ │ ├── graph.py # STLGraph — NetworkX-based graph construction
266
+ │ ├── analyzer.py # STLAnalyzer — centrality, cycles, statistics
267
+ │ └── decay.py # Confidence decay with configurable half-life
268
+
269
+ ├── Query Layer
270
+ │ ├── query.py # find(), filter_statements(), select(), stl_pointer()
271
+ │ ├── diff.py # stl_diff(), stl_patch(), diff_to_text()
272
+ │ └── reader.py # stream_parse(), STLReader — streaming input
273
+
274
+ ├── Validation Layer
275
+ │ ├── schema.py # load_schema(), validate_against_schema(), .stl.schema format
276
+ │ └── llm.py # clean(), repair(), validate_llm_output() — 3-stage pipeline
277
+
278
+ └── Interface Layer
279
+ └── cli.py # Typer CLI with 10 commands
280
+ ```
281
+
282
+ ## Schema Ecosystem
283
+
284
+ Six domain-specific schemas are included in `docs/schemas/`:
285
+
286
+ | Schema | Domain | Key Constraints |
287
+ |--------|--------|----------------|
288
+ | `tcm.stl.schema` | Traditional Chinese Medicine | Unicode anchors, `domain` required |
289
+ | `scientific.stl.schema` | Scientific research | `source` required, confidence 0.3-1.0 |
290
+ | `causal.stl.schema` | Causal inference | `strength` required, rule must be "causal" |
291
+ | `historical.stl.schema` | Historical knowledge | `time` + `source` required, multi-script |
292
+ | `medical.stl.schema` | Medical/clinical | Prefixed anchors (Symptom_, Drug_, ...) |
293
+ | `legal.stl.schema` | Legal reasoning | Prefixed anchors (Law_, Regulation_, ...) |
294
+
295
+ Create custom schemas using `docs/schemas/_template.stl.schema`.
296
+
297
+ ## Development
298
+
299
+ ### Setup
300
+
301
+ ```bash
302
+ git clone https://github.com/scos-lab/semantic-tension-language.git
303
+ cd semantic-tension-language/parser
304
+ python -m venv venv
305
+ source venv/bin/activate # On Windows: venv\Scripts\activate
306
+ pip install -e ".[dev]"
307
+ ```
308
+
309
+ ### Run Tests
310
+
311
+ ```bash
312
+ pytest tests/ # Run all 531 tests
313
+ pytest tests/ --cov=stl_parser # With coverage report
314
+ pytest tests/test_builder.py # Single module
315
+ pytest tests/ -k "test_query" # By name pattern
316
+ ```
317
+
318
+ ### Code Quality
319
+
320
+ ```bash
321
+ black stl_parser/ # Format
322
+ ruff stl_parser/ # Lint
323
+ mypy stl_parser/ # Type check
324
+ ```
325
+
326
+ ## Documentation
327
+
328
+ - [STL Core Specification v1.0](../spec/stl-core-spec-v1.0.md)
329
+ - [STL Core Specification Supplement](../spec/stl-core-spec-v1.0-supplement.md)
330
+ - [Project Index](../docs/PROJECT_INDEX.md) — Module reference and architecture overview
331
+ - [Schema Ecosystem](../docs/schemas/) — Domain-specific schema library
332
+
333
+ ## Contributing
334
+
335
+ We welcome contributions. See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
336
+
337
+ ## License
338
+
339
+ Apache License 2.0 — see [LICENSE](../LICENSE).
340
+
341
+ ## Citation
342
+
343
+ ```bibtex
344
+ @software{stl_parser_2025,
345
+ author = {SCOS-Lab},
346
+ title = {STL Parser: A Comprehensive Toolkit for Semantic Tension Language},
347
+ year = {2025},
348
+ version = {1.7.0},
349
+ url = {https://github.com/scos-lab/semantic-tension-language}
350
+ }
351
+ ```