Skip to content

policies

knowledge_graph.news.policies

Combine spaCy and ReLiK mention spans and assign their entity links.

overlaps(left, right)

Source code in src/knowledge_graph/news/policies.py
6
7
def overlaps(left, right):
    return left[0] < right[1] and right[0] < left[1]

select_without_overlap(ranked, selected=())

Spans taken in order, skipping any that overlaps a span already selected.

Source code in src/knowledge_graph/news/policies.py
10
11
12
13
14
15
16
def select_without_overlap(ranked, selected=()):
    """Spans taken in order, skipping any that overlaps a span already selected."""
    selected = set(selected)
    for candidate in ranked:
        if not any(overlaps(candidate, kept) for kept in selected):
            selected.add(candidate)
    return selected

longest_first(spans)

Source code in src/knowledge_graph/news/policies.py
19
20
def longest_first(spans):
    return select_without_overlap(sorted(spans, key=lambda span: (-(span[1] - span[0]), span)))

first_flat(preferred, other)

Preferred spans longest-first without overlaps, then the other system's spans in document order.

Source code in src/knowledge_graph/news/policies.py
23
24
25
def first_flat(preferred, other):
    """Preferred spans longest-first without overlaps, then the other system's spans in document order."""
    return select_without_overlap(sorted(other - preferred), longest_first(preferred))

length_flat(spacy, relik, longest)

The union longest- or shortest-first, ReLiK first on equal length, without overlaps.

Source code in src/knowledge_graph/news/policies.py
28
29
30
31
32
33
34
35
def length_flat(spacy, relik, longest):
    """The union longest- or shortest-first, ReLiK first on equal length, without overlaps."""

    def rank(span):
        length = span[1] - span[0]
        return -length if longest else length, span not in relik, span

    return select_without_overlap(sorted(spacy | relik, key=rank))

drop_boundary_conflicts(spacy, relik)

Exact agreements and spans that overlap no span of the other system, longest-first without overlaps.

Source code in src/knowledge_graph/news/policies.py
38
39
40
41
42
43
def drop_boundary_conflicts(spacy, relik):
    """Exact agreements and spans that overlap no span of the other system, longest-first without overlaps."""
    exact = spacy & relik
    spacy_safe = {span for span in spacy - exact if not any(overlaps(span, other) for other in relik)}
    relik_safe = {span for span in relik - exact if not any(overlaps(span, other) for other in spacy)}
    return longest_first(exact | spacy_safe | relik_safe)

A single system keeps its own links; combined policies prefer ReLiK, except spaCy-first flat.

Source code in src/knowledge_graph/news/policies.py
67
68
69
70
71
def default_links(policy):
    """A single system keeps its own links; combined policies prefer ReLiK, except spaCy-first flat."""
    return {"spacy_only": "spacy_only", "relik_only": "relik_only", "spacy_first_flat": "spacy_then_relik"}.get(
        policy, "relik_then_spacy"
    )