app/core/derive_edges.py aktualisiert
Some checks failed
Deploy mindnet to llm-node / deploy (push) Failing after 1s
Some checks failed
Deploy mindnet to llm-node / deploy (push) Failing after 1s
This commit is contained in:
parent
1f54f8136b
commit
da2bbf72a6
|
|
@ -1,4 +1,25 @@
|
||||||
# app/core/derive_edges.py
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Name: app/core/derive_edges.py
|
||||||
|
Version: v1.1.0 (2025-09-05)
|
||||||
|
|
||||||
|
Kurzbeschreibung:
|
||||||
|
Leitet Edges aus Wikilinks ([[...]]) ab.
|
||||||
|
- Unterstützt Auflösung über note_id, Title-Slug, File-Slug.
|
||||||
|
- Erzeugt Edges:
|
||||||
|
* "references" (Note->Note) + "backlink" (inverse)
|
||||||
|
* "references_at" (Chunk->Note) mit seq = chunk_index
|
||||||
|
- **NEU**: pro Match ein Occurrence-Zähler 'occ' für eindeutige Edge-IDs.
|
||||||
|
|
||||||
|
Kompatibilität:
|
||||||
|
- Vorherige Payload-Felder bleiben erhalten.
|
||||||
|
- Zusätzliche Felder: 'seq' für Volltext ("body"), 'occ' überall.
|
||||||
|
|
||||||
|
Changelog:
|
||||||
|
v1.1.0: 'occ' eingeführt; 'references' erhalten jetzt seq="body".
|
||||||
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import re
|
import re
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
|
@ -54,42 +75,46 @@ def derive_wikilink_edges(note_payload: dict, chunks_payloads: List[dict], note_
|
||||||
edges: List[dict] = []
|
edges: List[dict] = []
|
||||||
source_note_id = note_payload["note_id"]
|
source_note_id = note_payload["note_id"]
|
||||||
|
|
||||||
def _make_edge(kind: str, src: str, tgt: str, seq=None, extra: dict|None=None):
|
def _make_edge(kind: str, src: str, tgt: str, seq=None, occ=None, extra: dict|None=None):
|
||||||
e = {"edge_id": None, "kind": kind, "source_id": src, "target_id": tgt}
|
e = {"edge_id": None, "kind": kind, "source_id": src, "target_id": tgt}
|
||||||
if seq is not None:
|
if seq is not None:
|
||||||
e["seq"] = seq
|
e["seq"] = seq
|
||||||
|
if occ is not None:
|
||||||
|
e["occ"] = occ
|
||||||
if extra:
|
if extra:
|
||||||
e.update(extra)
|
e.update(extra)
|
||||||
return e
|
return e
|
||||||
|
|
||||||
# Links im Volltext (falls vorhanden)
|
# Links im Volltext (gesamter Body)
|
||||||
fulltext = note_payload.get("fulltext") or note_payload.get("body") or ""
|
fulltext = note_payload.get("fulltext") or note_payload.get("body") or ""
|
||||||
if fulltext:
|
if fulltext:
|
||||||
for m in WIKILINK_RE.finditer(fulltext):
|
for k, m in enumerate(WIKILINK_RE.finditer(fulltext), start=1):
|
||||||
raw_target, heading, alias = m.groups()
|
raw_target, heading, alias = m.groups()
|
||||||
target_id, how = resolve_target(raw_target, note_index)
|
target_id, how = resolve_target(raw_target, note_index)
|
||||||
extra = {"raw": raw_target, "alias": alias, "heading": heading, "resolution": how}
|
extra = {"raw": raw_target, "alias": alias, "heading": heading, "resolution": how}
|
||||||
if target_id:
|
if target_id:
|
||||||
edges.append(_make_edge("references", source_note_id, target_id, extra=extra))
|
edges.append(_make_edge("references", source_note_id, target_id, seq="body", occ=k, extra=extra))
|
||||||
edges.append(_make_edge("backlink", target_id, source_note_id, extra=extra))
|
edges.append(_make_edge("backlink", target_id, source_note_id, seq="body", occ=k, extra=extra))
|
||||||
else:
|
else:
|
||||||
extra["status"] = "unresolved"
|
extra["status"] = "unresolved"
|
||||||
extra["target_label"] = raw_target
|
extra["target_label"] = raw_target
|
||||||
edges.append(_make_edge("references", source_note_id, raw_target, extra=extra))
|
edges.append(_make_edge("references", source_note_id, raw_target, seq="body", occ=k, extra=extra))
|
||||||
|
|
||||||
# Links in Chunks (wenn übergeben)
|
# Links in Chunks (wenn übergeben)
|
||||||
for i, ch in enumerate(chunks_payloads, start=1):
|
for i, ch in enumerate(chunks_payloads, start=1):
|
||||||
txt = ch.get("text") or ch.get("content") or ""
|
txt = ch.get("text") or ch.get("content") or ""
|
||||||
if not txt:
|
if not txt:
|
||||||
continue
|
continue
|
||||||
|
occ = 0
|
||||||
for m in WIKILINK_RE.finditer(txt):
|
for m in WIKILINK_RE.finditer(txt):
|
||||||
|
occ += 1
|
||||||
raw_target, heading, alias = m.groups()
|
raw_target, heading, alias = m.groups()
|
||||||
target_id, how = resolve_target(raw_target, note_index)
|
target_id, how = resolve_target(raw_target, note_index)
|
||||||
extra = {"raw": raw_target, "alias": alias, "heading": heading, "resolution": how}
|
extra = {"raw": raw_target, "alias": alias, "heading": heading, "resolution": how}
|
||||||
if target_id:
|
if target_id:
|
||||||
edges.append(_make_edge("references_at", ch["chunk_id"], target_id, seq=i, extra=extra))
|
edges.append(_make_edge("references_at", ch["chunk_id"], target_id, seq=i, occ=occ, extra=extra))
|
||||||
else:
|
else:
|
||||||
extra["status"] = "unresolved"
|
extra["status"] = "unresolved"
|
||||||
extra["target_label"] = raw_target
|
extra["target_label"] = raw_target
|
||||||
edges.append(_make_edge("references_at", ch["chunk_id"], raw_target, seq=i, extra=extra))
|
edges.append(_make_edge("references_at", ch["chunk_id"], raw_target, seq=i, occ=occ, extra=extra))
|
||||||
return edges
|
return edges
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user