# Parse a sentence into its dependency tree

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

[Pride and Prejudice, the first sentence](https://www.gutenberg.org/ebooks/1342), Jane Austen. Public domain (first published 1813).

The calls read it as `austen-first-sentence.txt` in the directory they run from:

```bash
curl -fLO https://mox-labs.github.io/matra/example-files/parse-a-sentence/austen-first-sentence.txt
```
## The call

Rust:

```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:

```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))
```

The command line, which prints the output below inside an envelope:

```bash
matra analyze --json austen-first-sentence.txt
```

```jsonc
{
  "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.

```jsonc
[
  {
    "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: https://mox-labs.github.io/matra/example-files/parse-a-sentence/output.json, and what the CLI prints: https://mox-labs.github.io/matra/example-files/parse-a-sentence/cli.json.
The same 25 tokens as a table, then as arcs drawn from each word's head:

<figure-parse input="austen-first-sentence" />

## 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` (the `id` of the word it depends on, 0 for the root) and `dep` (the relation). Here `truth` is the root, and `It` is its `nsubj`.
- `lemma` is the dictionary form: `is` becomes `be`, and `acknowledged` becomes `acknowledge`.
- The parse is the model's, reported as the model returned it. The model reads `that` (token 8) as a pronoun and the object of `want`, where a grammar would call it the conjunction that opens the clause. matra does not correct the parser; [Methodology](../reference/methodology.md#the-parse-layer) names the model and its limits.
- The whole output also carries the structural primitives read off this tree: a modal (`must`, governing `want`) and a clausal complement (`acknowledge`, whose clause is headed by `want`). The [next example](negations-and-modals.md) reads those across a document.
