Spaces:
Runtime error
Runtime error
File size: 1,432 Bytes
ec99d5d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import networkx as nx
# Global in-memory graph
academic_graph = nx.DiGraph()
def add_paper_relation(source_paper: str, target_paper: str, relation_type: str = "cites"):
"""
Menambahkan hubungan antar paper ke Knowledge Graph.
relation_type: cites, refutes, supports
"""
academic_graph.add_edge(source_paper, target_paper, relation=relation_type)
return f"Berhasil memetakan: '{source_paper}' {relation_type} '{target_paper}'."
def query_graph(paper_name: str):
"""
Mencari informasi paper dan siapa saja yang terkait dengannya di graph.
"""
if paper_name not in academic_graph:
return f"Paper '{paper_name}' belum ada di dalam Knowledge Graph."
successors = list(academic_graph.successors(paper_name))
predecessors = list(academic_graph.predecessors(paper_name))
result = f"Analisis Graph untuk '{paper_name}':\n"
if successors:
result += "Mempengaruhi/Mengutip:\n"
for s in successors:
rel = academic_graph[paper_name][s]['relation']
result += f" - [{rel}] -> {s}\n"
if predecessors:
result += "Dipengaruhi/Dikutip oleh:\n"
for p in predecessors:
rel = academic_graph[p][paper_name]['relation']
result += f" - {p} -> [{rel}]\n"
if not successors and not predecessors:
result += "Belum ada hubungan yang terpetakan."
return result
|