Analyzing syntax with a language model
Configure dspy for your LM:
import dspy
dspy.configure(lm=dspy.LM(model="litellm_proxy/anthropic/Claude Opus 5",
api_base="https://api_url/litellm",
api_key="your-key-here"))Analyze a string
from arsgrammatica import analyze_string
sentences, results = analyze_string("Gallia est omnis divisa in partes tres.")Analyze citable passages
A CitedText has an identifier (such as a CTS URN) and a text value. Use analyze_sources to analyze a list of CitedText objects.
from arsgrammatica import analyze_sources, combined_tokengraph
from arsgrammatica.models import CitedText
aeneid = "urn:cts:latinLit:phi0690.ph003.perseus:"
sources = [
CitedText(citation=f"{aeneid}1.1", text="Arma virumque canō, Trōiae quī prīmus ab ōrīs"),
CitedText(citation=f"{aeneid}1.2", text="Ītaliam, fātō profugus, Lāvīniaque vēnit"),
]
sentences, results = analyze_sources(sources)Note that sentence boundaries don’t need to respect citation-unit boundaries (one sentence may span multiple source lines, as above)
Validation: the
validatefunction checks the results of an analysis for internal consistency, and compiles a list of problems it detects. Bothanalyze_string()andanalyze_sourcescallvalidate, and print a print a warning if any are found.
Analyze a corpus
from arsgrammatica import analyze_ctsdata
sentences, results = analyze_ctsdata("path/to/cexfile")Working with results of an analysis
analyze_string() and analyze_sources() return a pair of lists:
- a list of
Sentenceobjects - a corresponding list of
SentenceAnalysisobjects, one for each sentence.
A SentenceAnalysis has a list of VerbalExpression objects in its verbalunits property, and an ordered list of TokenAnalysis objects in its tokengraph property.
For examples of things you can do with lists of VerbalExpression and TokenAnalysis objects, such as visualizing and analyzing syntax graphs, see further recipes