> ## Documentation Index
> Fetch the complete documentation index at: https://ladygraph.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# Entity kinds

> Name the codes and abbreviations in your documents, and turn them into concepts.

An optional `entities:` section of `ladygraph.yaml` names things a deployment's documents contain and explains them: document codes, form codes, decision numbers and abbreviations. It sits next to `sources:` and is read by `sources/entities.py` (`load_entities`, `EntityEngine`, `Lexicon`). A kind is data, so a new kind is a new entry in the config.

```yaml theme={null}
entities:
  order: [form_code, document_code]          # who owns a piece of text first; a composite kind goes before the kind inside it
  kinds:
    document_code:
      answer: title                          # title | expansion
      pattern: '(?<![\w.])(?P<doc>[A-Z]{1,3}[.\-][A-Z]{2,3}[.\-]\d{3})(?![\w])'
      key: [doc]                             # named groups that identify it; F02 of two documents are two entries
      classes: {from: doc, by_prefix: {PR: process}}
      title: {strip: [numbers, extension, kinds], min_words: 2, reject: ['^step\b']}
      resolvers:                             # tried in order; the first one that answers wins, the others are kept for agreement
        - {type: metadata, code: doc_code}
        - {type: text, pattern: '(?P<title>[^.;\n]{10,160}?)\s*\(Code:\s*{self}\)'}
```

Keys fold case, accents and separators the same way keyword matching does, so `REF.AB.010` and `RÉF.AB.010` are one entry. The `metadata` resolver reads `Document.metadata[code]`, so a SQL source supplies it with `map.metadata`.

An answer is always a short title or expansion with a pointer (address, span and fingerprint). It is never copied source text.

The full example is `docs/entity_kinds.example.yaml` in the repository. To inspect the entries:

```bash theme={null}
PYTHONPATH=ladygraph python -m sources.entities --config <yaml> --scope <scope> [--kind K] [--unresolved]
```

## Entries as concepts

With a `concepts:` section, `LexiconExtractor` (`extractors/lexicon.py`) wraps any extractor. To each document's own contribution it adds a concept for every code the document mentions that deserves one, and `REFERS_TO` links from the document's concepts to those codes.

A code's concept has:

* **A title** equal to the code as first written. The title makes the id, so it must not change when a meaning is reworded.
* **A description** of one sentence with the meaning and the code (`describe_resolved`, or `describe_unresolved` when no document spells it out).
* **The code as a user word**, stamped `human:<actor>`, because the person who wrote the pattern defined it.
* **Evidence**: the written meaning first, then up to `evidence_per_document` other mentions.

One code is one concept. Every document's contribution carries only its own evidence, and the store merges them. Contexts are one top-level context per kind, named by the kind's `label`.

### Which entries get a concept

* Not a hub, meaning not in more than `max_share` of passages.
* When no meaning was found, the code must appear in at least `min_passages_unresolved` passages, and must not be an all-caps word that is also written normally `lowercase_words` times.
* A kind can opt out with `concept: false`.

A document concept gets `REFERS_TO` a code its passage mentions in either of two cases:

* The code is in the concept's own title or description.
* The concept is the heading of its passage (its evidence starts in the first `heading_chars`), and the passage mentions at most `max_codes_in_passage` codes.

There are at most `max_links` links per concept. A code with no meaning and no link in a document gets no concept there.

Declare the relation in the ontology with `origin: entities`, which also keeps it from being offered to the LLM:

```yaml theme={null}
relations:
  REFERS_TO: {description: A concept's passage mentions a code that has its own concept, to: [term], origin: entities}
```

```python theme={null}
engine = EntityEngine(load_entities("ladygraph.yaml"))
lexicon = engine.build(documents)                       # every document of the scope: cheap, no LLM
extractor = LexiconExtractor(LlmOkfExtractor(llm, embedder=embedder), engine, lexicon, embedder=embedder)
```

> [!WARNING]
>
> The defaults were tuned on one corpus, so check them on yours. The numbers are in the example config. A known limit: a merged concept's first evidence is the first unit's, not necessarily the one that holds the meaning. A question about a code can therefore find the right concept and show another mention. The extractor puts the meaning first inside its own contribution, but guaranteeing it across units is for the store.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.