Parse a sentence into its dependency tree
on this page
The first sentence of Pride and Prejudice, parsed: every word with its lemma, its part of speech, the word it depends on, and the relation between them.
Input
The calls read it as austen-first-sentence.txt in the directory you run them from. Save it there:
curl -fLO https://mox-labs.github.io/matra/example-files/parse-a-sentence/austen-first-sentence.txt or download austen-first-sentence.txt.
It is a truth universally acknowledged, that a single man in possession of a good fortune must be in want of a wife.
The call
Rust
// cargo add matra@0.3 serde_json
use matra::{Engine, Input};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Loads the English model, downloading it on first use.
let engine = Engine::new()?;
let doc = engine.analyze(Input::file("austen-first-sentence.txt"))?;
println!("{}", serde_json::to_string_pretty(&doc)?);
Ok(())
} Python
# pip install 'matra>=0.3'
import json
import matra
engine = matra.Engine() # downloads the English model on first use
doc = engine.analyze(file="austen-first-sentence.txt") # a str alone would be text, not a path
print(json.dumps(doc.to_dict(), indent=2, ensure_ascii=False)) CLI
$ matra analyze --json austen-first-sentence.txt It prints the output below, inside an envelope that names the command and the input:
{
"format_version": 2,
"command": "analyze",
"input": "austen-first-sentence.txt",
"result": /* the output below */,
"error": null
}Output
Trimmed: only result.sections[0].paragraphs[0].sentences[0].tokens, lists cut to the first 3.
[
{
"id": 1,
"span": {
"byte_start": 0,
"byte_end": 2
},
"text": "It",
"lemma": "it",
"pos": "PRON",
"xpos": "PRP",
"features": {
"Case": "Nom",
"Gender": "Neut",
"Number": "Sing",
"Person": "3",
"PronType": "Prs"
},
"head": 4,
"dep": "nsubj",
"deps": "_",
"misc": "",
"is_punct": false
},
{
"id": 2,
"span": {
"byte_start": 3,
"byte_end": 5
},
"text": "is",
"lemma": "be",
"pos": "AUX",
"xpos": "VBZ",
"features": {
"Mood": "Ind",
"Number": "Sing",
"Person": "3",
"Tense": "Pres",
"VerbForm": "Fin"
},
"head": 4,
"dep": "cop",
"deps": "_",
"misc": "",
"is_punct": false
},
{
"id": 3,
"span": {
"byte_start": 6,
"byte_end": 7
},
"text": "a",
"lemma": "a",
"pos": "DET",
"xpos": "DT",
"features": {
"Definite": "Ind",
"PronType": "Art"
},
"head": 4,
"dep": "det",
"deps": "_",
"misc": "",
"is_punct": false
}
// … 22 more
] The whole output: output.json, and what the CLI prints, cli.json. The examples gate runs all three calls and compares what each prints with these files.
The same 25 tokens as a table, then as arcs drawn from each word's head:
Dependency parse
1It is a truth universally acknowledged, that a single man in possession of a good fortune must be in want of a wife.
show the data
| # | Word | Lemma | POS | Head | Relation |
|---|---|---|---|---|---|
| 1 | It | it | PRON | 4truth | nsubj |
| 2 | is | be | AUX | 4truth | cop |
| 3 | a | a | DET | 4truth | det |
| 4 | truth | truth | NOUN | 0root | root |
| 5 | universally | universally | ADV | 6acknowledged | advmod |
| 6 | acknowledged | acknowledge | VERB | 4truth | conj |
| 7 | , | , | PUNCT | 6acknowledged | punct |
| 8 | that | that | PRON | 21want | obj |
| 9 | a | a | DET | 11man | det |
| 10 | single | single | ADJ | 11man | amod |
| 11 | man | man | NOUN | 21want | nsubj |
| 12 | in | in | ADP | 13possession | case |
| 13 | possession | possession | NOUN | 11man | nmod |
| 14 | of | of | ADP | 17fortune | case |
| 15 | a | a | DET | 17fortune | det |
| 16 | good | good | ADJ | 17fortune | amod |
| 17 | fortune | fortune | NOUN | 13possession | nmod |
| 18 | must | must | AUX | 21want | aux |
| 19 | be | be | AUX | 21want | cop |
| 20 | in | in | ADP | 21want | case |
| 21 | want | want | NOUN | 6acknowledged | ccomp |
| 22 | of | of | ADP | 24wife | case |
| 23 | a | a | DET | 24wife | det |
| 24 | wife | wife | NOUN | 21want | nmod |
| 25 | . | . | PUNCT | 4truth | punct |
- Core argument
- Modifier
- Function word
- Other
Each arc runs from a word's head to the word, whose arrow it ends at, labelled with the relation. The line groups the relations the way Universal Dependencies does: the core arguments of a predicate, the modifiers, and the function words. Point at or tap a word, here or in the table, to keep its arcs and dim the rest.
Input: Pride and Prejudice, the first sentence, Jane Austen. Public domain (first published 1813).
Produced by matra 0.3.0 with the UDPipe model english-ewt-ud-2.5-191206
(SHA-256 784bd0fa85e3). Data: austen-first-sentence/parse.json.
What to notice
- A document is sections of paragraphs of sentences, even when it is one sentence, so the tokens are at
sections[0].paragraphs[0].sentences[0].tokens. In Rust,doc.sentences()walks every sentence in document order. - Each token carries its CoNLL-U columns under matra's names:
id(from 1 within the sentence),text,lemma,pos(the Universal Dependencies tag),head(theidof the word it depends on, 0 for the root) anddep(the relation). Heretruthis the root, andItis itsnsubj. lemmais the dictionary form:isbecomesbe, andacknowledgedbecomesacknowledge.- The parse is the model's, reported as the model returned it. The model reads
that(token 8) as a pronoun and the object ofwant, where a grammar would call it the conjunction that opens the clause. matra does not correct the parser; Methodology names the model and its limits. - The whole output also carries the structural primitives read off this tree: a modal (
must, governingwant) and a clausal complement (acknowledge, whose clause is headed bywant). The next example reads those across a document.
This page, measured by matra 0.3.0 with english-ewt-ud-2.5-191206: 2 paragraphs, in the margin. the data