Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
52 changes: 52 additions & 0 deletions index/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -216,6 +235,39 @@ 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.")

# 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", []) + [
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'"""

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
6 changes: 6 additions & 0 deletions validation/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading