From c128e55e2e1a27f3267053835d7666736d477c8d Mon Sep 17 00:00:00 2001 From: Johannes de Wit <77152231+johannesdewit@users.noreply.github.com> Date: Tue, 12 May 2026 21:43:33 +0200 Subject: [PATCH 1/3] Update yaml validation to allow for list of aliases. --- validation/schema.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/validation/schema.yml b/validation/schema.yml index b4548b2..0eb6136 100644 --- a/validation/schema.yml +++ b/validation/schema.yml @@ -4,6 +4,12 @@ additionalProperties: type: object description: Lemma object properties: + aliases: + type: array + description: List of aliases of lemma. + uniqueItems: True + items: + type: string metadata: type: object description: Metadata object From 896ea8222c5d63eadfbaee8231ea7a3570aa8ec6 Mon Sep 17 00:00:00 2001 From: Johannes de Wit <77152231+johannesdewit@users.noreply.github.com> Date: Tue, 12 May 2026 22:47:50 +0200 Subject: [PATCH 2/3] Add `add-alias` funciton to add alias to lemma --- index/__init__.py | 6 ++++++ index/index.py | 22 ++++++++++++++++++++++ setup.py | 1 + 3 files changed, 29 insertions(+) diff --git a/index/__init__.py b/index/__init__.py index 4f78262..e00b36d 100644 --- a/index/__init__.py +++ b/index/__init__.py @@ -90,6 +90,12 @@ def click_add_metadata(metadata_type, lemma, lemma_type, md_value, overwrite): def click_find_ref(search_term): find_ref(search_term) +@click.command() +@click.argument("lemma", type=str) +@click.argument("alias", type=str) +def click_add_alias(lemma, alias): + add_alias(lemma, alias) + @click.command() def click_format_refs(): diff --git a/index/index.py b/index/index.py index 6847c92..91d0442 100644 --- a/index/index.py +++ b/index/index.py @@ -216,6 +216,28 @@ def find_ref(search_term, max_l_dist=2, num_results=5): print("No matches found") +def add_alias(lemma: str, alias: str): + """Add an alias to a lemma in the index. An alias is regarded as a property of the lemma.""" + + with open(INDEX_FILE, encoding='utf-8') as f: + index = yaml.safe_load(f) + + if lemma not in index: + raise click.BadParameter(f"Lemma '{lemma}' not found in index.") + + if alias in index: + raise click.BadParameter(f"Alias '{alias}' is a lemma in the index. Please use 'add-rel' to add it as a relation between lemmata.") + + lemma_dict = index[lemma] + + index[lemma]["aliases"] = lemma_dict.get("aliases", []) + [ + alias + ] + + with open(INDEX_FILE, "w", encoding='utf-8') as f: + yaml.safe_dump(index, f, allow_unicode=True) + + def add_metadata(md_type, lemma, lemma_type, md_value=None, overwrite=False): """Add metadata to a lemma of type 'author' or 'work'""" diff --git a/setup.py b/setup.py index 984e479..08af3ab 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,7 @@ "add-ref=index:click_add_ref", "add-rel=index:click_add_rel", "add-metadata=index:click_add_metadata", + "add-alias=index:click_add_alias", "find-ref=index:click_find_ref", "format-refs=index:click_format_refs", ] From 3615c4b199e58fb82b71c56bd0bfe0827a210d34 Mon Sep 17 00:00:00 2001 From: Johannes de Wit <77152231+johannesdewit@users.noreply.github.com> Date: Tue, 12 May 2026 23:42:43 +0200 Subject: [PATCH 3/3] `add-ref` searches aliases when no lemma is found --- index/index.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/index/index.py b/index/index.py index 91d0442..a018672 100644 --- a/index/index.py +++ b/index/index.py @@ -75,10 +75,29 @@ def add_ref( ref_dict["type"] = ref_type # Determine whether that lemma is already in the index + lemma_in_index = False try: lemma_entry = index[lemma] except KeyError: # This triggers if the lemma is not present in the index. + + # Check whether lemma is an alias. + for k, v in index.items(): + if "aliases" in v: + if lemma in v["aliases"]: + click.echo(f"'{lemma}' is known to be an alias for '{k}' in this index. Reference will be added to '{k}'") + lemma = k + lemma_entry = index[lemma] + lemma_in_index = True + break + else: + continue + else: + continue + else: + lemma_in_index = True + + if not lemma_in_index: lemma_entry = {"references": {work: [ref_dict]}} if lemma_type: lemma_entry["type"] = lemma_type @@ -228,6 +247,17 @@ def add_alias(lemma: str, alias: str): if alias in index: raise click.BadParameter(f"Alias '{alias}' is a lemma in the index. Please use 'add-rel' to add it as a relation between lemmata.") + # Check whether lemma is an alias. + for k, v in index.items(): + if "aliases" in v: + if lemma in v["aliases"]: + raise click.BadParameter(f"Alias '{alias}' is already an alias for '{k}' in the index. No duplicates are allowed.") + break + else: + continue + else: + continue + lemma_dict = index[lemma] index[lemma]["aliases"] = lemma_dict.get("aliases", []) + [