From d03486a825e4278dc92a8e69914fd68ffc0925f3 Mon Sep 17 00:00:00 2001 From: Thorsten Vitt Date: Wed, 16 Oct 2019 12:31:44 +0200 Subject: [PATCH] inscription method copy-orphans --- src/macrogen/graph.py | 12 ++++++++---- src/macrogen/graphutils.py | 4 ++++ src/macrogen/report.py | 2 ++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/macrogen/graph.py b/src/macrogen/graph.py index 10cf081..63efba1 100644 --- a/src/macrogen/graph.py +++ b/src/macrogen/graph.py @@ -10,7 +10,7 @@ from io import TextIOWrapper from operator import itemgetter from pathlib import Path -from typing import List, Any, Dict, Tuple, Union, Sequence, Optional, Set, Iterable, FrozenSet +from typing import List, Any, Dict, Tuple, Union, Sequence, Optional, Set, Iterable from warnings import warn from zipfile import ZipFile, ZIP_DEFLATED @@ -18,6 +18,8 @@ import pandas as pd from dataclasses import dataclass +from macrogen.graphutils import is_orphan + from .graphutils import mark_edges_to_delete, remove_edges, in_path, first from .bibliography import BiblSource from .config import config @@ -87,6 +89,8 @@ def handle_inscriptions(base): inscriptions_inline(base) if 'copy' in methods: datings_from_inscriptions(base) + if 'copy-orphans' in methods: + datings_from_inscriptions(base, orphans_only=True) if 'orphans' in methods: adopt_orphans(base) @@ -797,7 +801,7 @@ def resolve_ambiguities(graph: nx.MultiDiGraph): graph.remove_node(ambiguity) -def datings_from_inscriptions(base: nx.MultiDiGraph): +def datings_from_inscriptions(base: nx.MultiDiGraph, orphans_only=False): """ Copy datings from inscriptions to witnesses. @@ -817,10 +821,10 @@ def datings_from_inscriptions(base: nx.MultiDiGraph): before = [edge for edge in iin if isinstance(edge[0], date)] iout = [edge for i in inscriptions for edge in base.out_edges(i, data=True, keys=True)] after = [edge for edge in iout if isinstance(edge[1], date)] - if before and not any(isinstance(pred, date) for pred in base.predecessors(witness)): + if before and not any(isinstance(pred, date) for pred in base.predecessors(witness)) and not (orphans_only and not is_orphan(witness, base)): for d, i, k, attr in before: base.add_edge(d, witness, copy=(d, i, k), **attr) - if after and not any(isinstance(succ, date) for succ in base.successors(witness)): + if after and not any(isinstance(succ, date) for succ in base.successors(witness)) and not (orphans_only and not is_orphan(witness, base)): for i, d, k, attr in after: base.add_edge(witness, d, copy=(d, i, k), **attr) diff --git a/src/macrogen/graphutils.py b/src/macrogen/graphutils.py index bfbb392..6313c87 100644 --- a/src/macrogen/graphutils.py +++ b/src/macrogen/graphutils.py @@ -277,3 +277,7 @@ def base_n(number: int, base: int = 10, neg: Optional[str] = '-') -> str: digits.append(neg) return "".join(reversed(digits)) + + +def is_orphan(node, graph: nx.DiGraph): + return node not in graph.nodes or graph.in_degree[node] == 0 and graph.out_degree[node] == 0 \ No newline at end of file diff --git a/src/macrogen/report.py b/src/macrogen/report.py index 20353d0..0ab468e 100644 --- a/src/macrogen/report.py +++ b/src/macrogen/report.py @@ -1434,6 +1434,8 @@ def report_config(info: MacrogenesisInfo): 'Inskription wi von w, so wird für jede Inskription von w' 'eine Kante wi → w eingezogen.', 'copy': 'Alle Aussagen über Inskriptionen werden auf die zugehörigen Zeugen kopiert.', + 'copy-orphans': 'Alle Aussagen über Inskriptionen werden auf die zugehörigen Zeugen kopiert, wenn' + 'für die Zeugen keine eigenen Aussagen vorliegen.', 'inline': 'Eine Inskription wi eines Zeugen w wird so eingebunden, dass ' 'sie nach dem Beginn von w beginnt und vor dem Ende von w endet.', }