lightrag-dedupe 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lightrag_dedupe-0.1.1/.gitignore +47 -0
- lightrag_dedupe-0.1.1/CHANGELOG.md +33 -0
- lightrag_dedupe-0.1.1/LICENSE +21 -0
- lightrag_dedupe-0.1.1/PKG-INFO +354 -0
- lightrag_dedupe-0.1.1/README.ko.md +327 -0
- lightrag_dedupe-0.1.1/README.md +327 -0
- lightrag_dedupe-0.1.1/pyproject.toml +61 -0
- lightrag_dedupe-0.1.1/samples/aliases.csv +5 -0
- lightrag_dedupe-0.1.1/samples/demo_factory.py +16 -0
- lightrag_dedupe-0.1.1/samples/sample_kg/graph_chunk_entity_relation.graphml +257 -0
- lightrag_dedupe-0.1.1/samples/sample_kg.json +272 -0
- lightrag_dedupe-0.1.1/scripts/make_sample.py +19 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/__init__.py +16 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/aliases.py +52 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/apply.py +180 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/apply_http.py +75 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/apply_python.py +95 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/applylog.py +51 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/backup.py +132 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/candidates.py +176 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/cli.py +247 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/demo.py +191 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/errors.py +37 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/graphml.py +107 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/i18n.py +58 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/locales/en.json +160 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/locales/ko.json +160 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/normalize.py +163 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/plan.py +284 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/report.py +63 -0
- lightrag_dedupe-0.1.1/src/lightrag_dedupe/rules.py +69 -0
- lightrag_dedupe-0.1.1/tests/conftest.py +55 -0
- lightrag_dedupe-0.1.1/tests/golden/sample_plan.json +213 -0
- lightrag_dedupe-0.1.1/tests/test_apply_http.py +149 -0
- lightrag_dedupe-0.1.1/tests/test_apply_python.py +151 -0
- lightrag_dedupe-0.1.1/tests/test_demo.py +30 -0
- lightrag_dedupe-0.1.1/tests/test_i18n.py +140 -0
- lightrag_dedupe-0.1.1/tests/test_normalize.py +52 -0
- lightrag_dedupe-0.1.1/tests/test_perf.py +49 -0
- lightrag_dedupe-0.1.1/tests/test_plan_io.py +149 -0
- lightrag_dedupe-0.1.1/tests/test_robustness.py +85 -0
- lightrag_dedupe-0.1.1/tests/test_scan.py +106 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
.venv/
|
|
5
|
+
.pytest_cache/
|
|
6
|
+
.ruff_cache/
|
|
7
|
+
.mypy_cache/
|
|
8
|
+
*.egg-info/
|
|
9
|
+
|
|
10
|
+
# Node
|
|
11
|
+
node_modules/
|
|
12
|
+
dist/
|
|
13
|
+
.next/
|
|
14
|
+
|
|
15
|
+
# 실행 산출물·모델·데이터 캐시
|
|
16
|
+
runs/
|
|
17
|
+
outputs/
|
|
18
|
+
.cache/
|
|
19
|
+
*.safetensors
|
|
20
|
+
*.gguf
|
|
21
|
+
*.bin
|
|
22
|
+
|
|
23
|
+
# 환경·비밀
|
|
24
|
+
.env
|
|
25
|
+
.env.*
|
|
26
|
+
!.env.example
|
|
27
|
+
|
|
28
|
+
# OS·에디터
|
|
29
|
+
.DS_Store
|
|
30
|
+
.vscode/
|
|
31
|
+
.idea/
|
|
32
|
+
|
|
33
|
+
# Flutter·Dart
|
|
34
|
+
.dart_tool/
|
|
35
|
+
build/
|
|
36
|
+
.flutter-plugins
|
|
37
|
+
.flutter-plugins-dependencies
|
|
38
|
+
coverage/
|
|
39
|
+
|
|
40
|
+
# Tool outputs
|
|
41
|
+
plan.json
|
|
42
|
+
plan.csv
|
|
43
|
+
apply-log.jsonl
|
|
44
|
+
*.lightrag-dedupe-backups/
|
|
45
|
+
|
|
46
|
+
# LightRAG working dir created by README step 5
|
|
47
|
+
rag_storage/
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.1] - 2026-11-27
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
- Name normalization no longer takes quadratic time on large graphs, so scanning big graphs stays fast.
|
|
13
|
+
- CSV plan errors now name the file, and an empty CSV gets its own message.
|
|
14
|
+
- The performance test measures CPU time with a 15 s budget so it is stable on shared CI machines.
|
|
15
|
+
|
|
16
|
+
## [0.1.0] - 2026-11-27
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
- `scan`: read-only analysis of a LightRAG working directory (graphml, standard library only) that
|
|
20
|
+
writes a reproducible `plan.json` (and optional `plan.csv`) with per-rule evidence.
|
|
21
|
+
- Deterministic name normalization: NFKC, case folding, repeated corporate suffix/prefix removal
|
|
22
|
+
(`Inc.`, `Co., Ltd.`, `주식회사`, `(주)`, `㈜`), Korean particle removal only when the remaining
|
|
23
|
+
stem exists as another node or alias, spacing/punctuation removal, alias CSV, short-name guard.
|
|
24
|
+
- Groups by identical key (no chains), `type_conflict` groups never applied by default, trigram
|
|
25
|
+
near pairs listed for review only.
|
|
26
|
+
- Deterministic target choice (alias canonical, no surface rules, most source chunks, degree, shorter).
|
|
27
|
+
- `explain`: shows the rule chain for a node and the nodes sharing its key.
|
|
28
|
+
- `apply`: dry-run by default; Python path (`--rag FILE.py:FUNC`) through `amerge_entities` and server
|
|
29
|
+
path (`--server URL`) through `POST /graph/entities/merge`; pre-check of every source name,
|
|
30
|
+
working-directory backup with sha256 manifest, resumable `apply-log.jsonl`, `--limit`.
|
|
31
|
+
- `restore`: verify a backup against its manifest and copy it back.
|
|
32
|
+
- `demo`: practice-mock run of scan, apply and rescan on real LightRAG 1.5.8, fully offline.
|
|
33
|
+
- English and Korean screens (`--lang`, `LIGHTRAG_DEDUPE_LANG`, `LC_ALL`/`LC_MESSAGES`/`LANG`).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LeeGyeongPil
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lightrag-dedupe
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Deterministic, reviewable duplicate-entity merge plans for LightRAG knowledge graphs (unofficial tool).
|
|
5
|
+
Project-URL: Homepage, https://github.com/LeeGyeongPil/lightrag-dedupe
|
|
6
|
+
Project-URL: Issues, https://github.com/LeeGyeongPil/lightrag-dedupe/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/LeeGyeongPil/lightrag-dedupe/blob/main/CHANGELOG.md
|
|
8
|
+
Author: LeeGyeongPil
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: deduplication,entity-resolution,knowledge-graph,korean,lightrag,rag
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Provides-Extra: lightrag
|
|
25
|
+
Requires-Dist: lightrag-hku<1.6,>=1.5.8; extra == 'lightrag'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# lightrag-dedupe
|
|
29
|
+
|
|
30
|
+
**English** | [한국어](https://github.com/LeeGyeongPil/lightrag-dedupe/blob/v0.1.1/README.ko.md)
|
|
31
|
+
|
|
32
|
+
Find the same entity written in different ways in a LightRAG knowledge graph (`OpenAI`, `openai`, `OpenAI Inc.`, `오픈AI의`), get a merge plan you can review, and apply only the groups you approve through LightRAG's own merge API. No LLM calls, no network, no API key: the same graph always gives the same plan. This is an unofficial tool, not part of LightRAG.
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
*`scan` reads a LightRAG working folder and lists groups of nodes that are probably one entity, each with the rules that matched.*
|
|
36
|
+
|
|
37
|
+
## Who it is for
|
|
38
|
+
|
|
39
|
+
- You indexed hundreds of documents with LightRAG and the graph viewer shows one company as five nodes.
|
|
40
|
+
- Your documents mix Korean and English, so names carry particles (`삼성전자의`), company markers (`(주)카카오`, `Co., Ltd.`) or two scripts (`오픈AI`).
|
|
41
|
+
- You want a cleanup step you can re-run in a script and review in a spreadsheet before anything changes.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
Requires Python 3.11+. [uv](https://docs.astral.sh/uv/) is the easiest way to run it (`curl -LsSf https://astral.sh/uv/install.sh | sh`).
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
uv tool install git+https://github.com/LeeGyeongPil/lightrag-dedupe
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`scan`, `explain`, `restore` and `apply --server` use only the Python standard library. `apply --rag` and `demo` need LightRAG itself (`lightrag-hku`, version 1.5.x), which you already have if you run LightRAG from Python.
|
|
52
|
+
|
|
53
|
+
To try it from a clone (this also installs LightRAG for the demo):
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
git clone https://github.com/LeeGyeongPil/lightrag-dedupe
|
|
57
|
+
cd lightrag-dedupe
|
|
58
|
+
uv sync
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
On a minimal Linux image you need `git` and `curl` before the commands above.
|
|
62
|
+
|
|
63
|
+
## Quick start (30 seconds, practice mock)
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
uv run lightrag-dedupe demo --lang en
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
This is a practice mock: real LightRAG 1.5.8 runs offline with a one-token-per-character tokenizer and hash embeddings (no meaning), and an LLM that would raise if called. It works in a temporary folder only.
|
|
71
|
+
1) building a small knowledge graph from the bundled sample (no LLM) ...
|
|
72
|
+
2) scan:
|
|
73
|
+
nodes: 19 edges: 11
|
|
74
|
+
groups: 4 (auto-apply candidates 3, type conflicts 1)
|
|
75
|
+
review: 1 near pairs (never auto-applied)
|
|
76
|
+
G001 skip 'Apple' <- 'apple' (type conflict: entity types differ, not merged by default)
|
|
77
|
+
G002 apply 'OpenAI' <- 'openai', '오픈AI의' [casefold; ko_particle:의; alias], 'OpenAI Inc.' [casefold; suffix:inc]
|
|
78
|
+
G003 apply '삼성전자' <- '삼성전자의' [ko_particle:의], 'Samsung Electronics Co., Ltd.' [casefold; suffix:ltd; suffix:co; punct_space; alias]
|
|
79
|
+
G004 apply '카카오' <- '(주)카카오' [suffix:(주)], '주식회사 카카오' [suffix:주식회사]
|
|
80
|
+
R001 skip 'OpenAI' <- 'OpenAl' [casefold] (near pair: similar spelling, check by hand) J=0.6
|
|
81
|
+
3) applying 3 approved groups through LightRAG's amerge_entities ...
|
|
82
|
+
4) result:
|
|
83
|
+
nodes: 19 -> 12
|
|
84
|
+
entity vector store: 19 -> 12 entries (0 merged-away names left)
|
|
85
|
+
OpenAI -> Sam Altman: 3 relations became 1, weight 2.0 (max rule, not the sum)
|
|
86
|
+
삼성전자 -> HBM: 3 relations became 1, weight 1.0 (max rule, not the sum)
|
|
87
|
+
카카오 -> 카카오톡: 3 relations became 1, weight 1.0 (max rule, not the sum)
|
|
88
|
+
5) scan again: 1 groups left, 0 auto-apply candidates, 1 review pairs
|
|
89
|
+
Never open your real working directory with mock embeddings: LightRAG refuses a different embedding model. Use --rag with your own factory or --server.
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+

|
|
93
|
+
*The whole flow on a bundled sample graph: scan, merge through LightRAG, check the result, scan again.*
|
|
94
|
+
|
|
95
|
+
This is a **practice mock** (it is labeled as one in the output): it builds a small graph in a temporary folder with fake hash embeddings and an LLM that is never called. The merge itself is real LightRAG 1.5.8 code. Use it only to see the flow; never open your own working folder with mock embeddings (LightRAG refuses a different embedding model).
|
|
96
|
+
|
|
97
|
+
## Usage
|
|
98
|
+
|
|
99
|
+
The normal flow is `scan`, review `plan.csv`, `apply` (preview), `apply --yes`.
|
|
100
|
+
|
|
101
|
+
| Command | What it does |
|
|
102
|
+
|---|---|
|
|
103
|
+
| `scan PATH [--out plan.json] [--csv plan.csv] [--aliases aliases.csv] [--near 0.6]` | Reads the graph file in a working folder (read-only) and writes the plan. |
|
|
104
|
+
| `explain NAME --in PATH [--aliases aliases.csv]` | Shows the rule chain that gave one node its key, and the other nodes with the same key. |
|
|
105
|
+
| `apply PLAN [--working-dir DIR] [--rag FILE.py:FUNC \| --server URL] [--yes]` | Applies groups marked `apply=true`. Without `--yes` it only prints the calls it would make. |
|
|
106
|
+
| `restore BACKUP --working-dir DIR [--yes]` | Puts back a backup that `apply` made. |
|
|
107
|
+
| `demo` | The practice mock above. |
|
|
108
|
+
| `--lang en\|ko` | Screen language (see Settings). |
|
|
109
|
+
|
|
110
|
+
Exit codes: `0` success, `2` input or usage error, `3` the plan no longer matches the graph, `4` a merge failed.
|
|
111
|
+
|
|
112
|
+
### 1. Scan
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
uv run lightrag-dedupe scan samples/sample_kg --out plan.json --csv plan.csv --aliases samples/aliases.csv --lang en
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```text
|
|
119
|
+
scan: samples/sample_kg/graph_chunk_entity_relation.graphml (read-only)
|
|
120
|
+
nodes: 19 edges: 11
|
|
121
|
+
groups: 4 (auto-apply candidates 3, type conflicts 1)
|
|
122
|
+
review: 1 near pairs (never auto-applied)
|
|
123
|
+
G001 skip 'Apple' <- 'apple' (type conflict: entity types differ, not merged by default)
|
|
124
|
+
G002 apply 'OpenAI' <- 'openai', '오픈AI의' [casefold; ko_particle:의; alias], 'OpenAI Inc.' [casefold; suffix:inc]
|
|
125
|
+
G003 apply '삼성전자' <- '삼성전자의' [ko_particle:의], 'Samsung Electronics Co., Ltd.' [casefold; suffix:ltd; suffix:co; punct_space; alias]
|
|
126
|
+
G004 apply '카카오' <- '(주)카카오' [suffix:(주)], '주식회사 카카오' [suffix:주식회사]
|
|
127
|
+
R001 skip 'OpenAI' <- 'OpenAl' [casefold] (near pair: similar spelling, check by hand) J=0.6
|
|
128
|
+
wrote plan.json
|
|
129
|
+
wrote plan.csv
|
|
130
|
+
next: review plan.csv, then run: lightrag-dedupe apply plan.csv --working-dir <dir> (dry-run)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
How to read it: `G…` lines are groups of nodes that normalize to the same key. `'OpenAI' <- 'openai', …` means "merge the sources into the target". The part in `[...]` is the evidence: the rules that changed that name. `skip` groups are not applied by default (`apply=false` in the plan). The `R001` line is a *near pair* (`OpenAI` vs `OpenAl` with a lowercase L). It is only listed for you to check by hand; the tool never merges near pairs on its own.
|
|
134
|
+
|
|
135
|
+
For your own data, point `scan` at the LightRAG working folder (the one that contains `graph_chunk_entity_relation.graphml`).
|
|
136
|
+
|
|
137
|
+
### 2. Review the plan
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
cat plan.csv
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```text
|
|
144
|
+
group_id,apply,target,member,role,entity_type,degree,chunks,rules
|
|
145
|
+
G001,false,Apple,Apple,target,ORGANIZATION,0,1,casefold
|
|
146
|
+
G001,false,Apple,apple,source,FOOD,0,1,
|
|
147
|
+
G002,true,OpenAI,OpenAI,target,ORGANIZATION,1,2,casefold
|
|
148
|
+
G002,true,OpenAI,openai,source,ORGANIZATION,1,1,
|
|
149
|
+
G002,true,OpenAI,오픈AI의,source,ORGANIZATION,1,1,casefold;ko_particle:의;alias
|
|
150
|
+
G002,true,OpenAI,OpenAI Inc.,source,ORGANIZATION,1,1,casefold;suffix:inc
|
|
151
|
+
G003,true,삼성전자,삼성전자,target,ORGANIZATION,1,1,
|
|
152
|
+
G003,true,삼성전자,삼성전자의,source,ORGANIZATION,1,1,ko_particle:의
|
|
153
|
+
G003,true,삼성전자,"Samsung Electronics Co., Ltd.",source,ORGANIZATION,1,1,casefold;suffix:ltd;suffix:co;punct_space;alias
|
|
154
|
+
G004,true,카카오,카카오,target,ORGANIZATION,1,1,
|
|
155
|
+
G004,true,카카오,(주)카카오,source,ORGANIZATION,1,1,suffix:(주)
|
|
156
|
+
G004,true,카카오,주식회사 카카오,source,ORGANIZATION,1,1,suffix:주식회사
|
|
157
|
+
R001,false,OpenAI,OpenAI,target,ORGANIZATION,1,2,casefold
|
|
158
|
+
R001,false,OpenAI,OpenAl,source,ORGANIZATION,1,1,casefold
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+

|
|
162
|
+
*One row per node. Open it in a spreadsheet and change `apply` to `true` or `false`, or edit `target`, before you apply. CSV column names are always English.*
|
|
163
|
+
|
|
164
|
+
Rows with the same `group_id` form one merge. Edit the CSV (or `plan.json`) freely; `apply` checks the result again and rejects empty sources, a target that is also a source, and overlapping groups (exit code 2).
|
|
165
|
+
|
|
166
|
+
### 3. Explain one name
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
uv run lightrag-dedupe explain "오픈AI의" --in samples/sample_kg --aliases samples/aliases.csv --lang en
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```text
|
|
173
|
+
name: 오픈AI의 entity_type: ORGANIZATION
|
|
174
|
+
1. casefold -> '오픈ai의' (upper/lower case ignored)
|
|
175
|
+
2. ko_particle:의 -> '오픈ai' (Korean particle removed) - the remaining stem is in the alias table
|
|
176
|
+
3. alias -> 'openai' (replaced by the alias table's canonical spelling)
|
|
177
|
+
key: openai
|
|
178
|
+
other nodes with the same key:
|
|
179
|
+
- OpenAI
|
|
180
|
+
- OpenAI Inc.
|
|
181
|
+
- openai
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+

|
|
185
|
+
*Each numbered line is one rule and the name it produced. Nodes that end with the same `key` are one group.*
|
|
186
|
+
|
|
187
|
+
### 4. Preview the apply
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
uv run lightrag-dedupe apply plan.csv --working-dir samples/sample_kg --lang en
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
```text
|
|
194
|
+
plan: 3 groups to apply (2 marked apply=false, 0 already done)
|
|
195
|
+
dry-run: nothing was changed.
|
|
196
|
+
[1/3] G002: amerge_entities(['openai', '오픈AI의', 'OpenAI Inc.'] -> 'OpenAI')
|
|
197
|
+
[2/3] G003: amerge_entities(['삼성전자의', 'Samsung Electronics Co., Ltd.'] -> '삼성전자')
|
|
198
|
+
[3/3] G004: amerge_entities(['(주)카카오', '주식회사 카카오'] -> '카카오')
|
|
199
|
+
pre-check passed: every source exists in the graph exactly as written.
|
|
200
|
+
to apply for real, run the same command with --yes.
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+

|
|
204
|
+
*Nothing changed. The pre-check confirms every source name still exists in the graph exactly as written; if not, nothing is touched and the exit code is 3.*
|
|
205
|
+
|
|
206
|
+
### 5. Apply for real, and undo
|
|
207
|
+
|
|
208
|
+
The Python path needs a small factory function that builds your LightRAG instance (not yet initialized) exactly as in your indexing script, with **your** embedding function. `samples/demo_factory.py` is a template. To try it safely, first create a throwaway LightRAG working folder from the bundled sample (offline, from a clone):
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
uv run python -c "from lightrag_dedupe.demo import create_sample_working_dir; create_sample_working_dir('rag_storage')" 2>/dev/null
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Then apply with the bundled mock factory:
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
RAG_WORKING_DIR=rag_storage uv run lightrag-dedupe apply plan.csv --rag samples/demo_factory.py:build_rag --yes --lang en
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
```text
|
|
221
|
+
plan: 3 groups to apply (2 marked apply=false, 0 already done)
|
|
222
|
+
backup: /…/rag_storage.lightrag-dedupe-backups/20261127T005706Z
|
|
223
|
+
[1/3] G002: merging 3 nodes into 'OpenAI'
|
|
224
|
+
[2/3] G003: merging 2 nodes into '삼성전자'
|
|
225
|
+
[3/3] G004: merging 2 nodes into '카카오'
|
|
226
|
+
done: 3 groups merged.
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
LightRAG prints many INFO log lines to stderr while merging; add `2>/dev/null` to hide them.
|
|
230
|
+
|
|
231
|
+
Before merging, the whole working folder is copied to `<working-dir>.lightrag-dedupe-backups/<timestamp>` with a sha256 manifest. To undo (stop any LightRAG process using that folder first):
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
uv run lightrag-dedupe restore rag_storage.lightrag-dedupe-backups/20261127T005706Z --working-dir rag_storage --yes --lang en
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
```text
|
|
238
|
+
restored 12 files into /…/rag_storage.
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
The timestamp folder name is the one printed on the `backup:` line of your own `apply` run (use that, not the one shown here). Without `--yes`, `restore` only verifies the backup and says how many files would be restored. Running `apply` again is safe: groups already done are skipped (recorded in `apply-log.jsonl`), and when you pass `--working-dir`, groups whose sources reappear (for example after a restore) are applied again.
|
|
242
|
+
|
|
243
|
+
For a running LightRAG server use `--server http://localhost:9621` (calls `POST /graph/entities/merge`). Pass `--working-dir` for the pre-check and backup, or `--no-backup` to say you accept no backup. If the server has an API key, set `LIGHTRAG_API_KEY`.
|
|
244
|
+
|
|
245
|
+
### Use your own LightRAG
|
|
246
|
+
|
|
247
|
+
Run the tool from the same Python environment as your LightRAG app, because your factory imports your own embedding and LLM packages (`openai`, `ollama`, ...). Install it into that environment instead of an isolated tool environment:
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
uv pip install "lightrag-dedupe @ git+https://github.com/LeeGyeongPil/lightrag-dedupe"
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Then write a factory like your indexing script, and point `--rag` at it (`my_factory.py`):
|
|
254
|
+
|
|
255
|
+
```python
|
|
256
|
+
from lightrag import LightRAG
|
|
257
|
+
|
|
258
|
+
def build_rag():
|
|
259
|
+
# same working_dir, embedding_func and llm_model_func as when you indexed
|
|
260
|
+
return LightRAG(working_dir="./rag_storage", embedding_func=my_embedding, llm_model_func=my_llm)
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
lightrag-dedupe apply plan.csv --rag my_factory.py:build_rag --working-dir ./rag_storage
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Gotchas:
|
|
268
|
+
|
|
269
|
+
- Stop any LightRAG server or process that uses the working folder before `apply --rag`; its in-memory state could overwrite the merge. (With `--server` the server stays up.)
|
|
270
|
+
- Do not apply while documents are being indexed; LightRAG answers with a conflict (409) and the group fails. Wait for indexing to finish and run `apply` again.
|
|
271
|
+
- LightRAG downloads the tiktoken encoding file when it is created. Offline, prepare it first (`lightrag-download-cache`) and set `TIKTOKEN_CACHE_DIR`, or pass your own tokenizer in the factory.
|
|
272
|
+
- Names in the plan must be written exactly as stored (case, spaces, punctuation); the pre-check rejects anything else.
|
|
273
|
+
|
|
274
|
+
## Settings
|
|
275
|
+
|
|
276
|
+
| Setting | Meaning |
|
|
277
|
+
|---|---|
|
|
278
|
+
| `--lang en\|ko` | Screen language. Order: `--lang`, then `LIGHTRAG_DEDUPE_LANG`, then `LC_ALL` / `LC_MESSAGES` / `LANG`; anything not starting with `ko` gives English. Rule codes, JSON keys and CSV columns are never translated. |
|
|
279
|
+
| `LIGHTRAG_API_KEY` | Sent as `X-API-Key` for `apply --server`, only if your server has a key. See `.env.example`. The tool does not load `.env` itself. |
|
|
280
|
+
| `--aliases FILE` | CSV with columns `variant,canonical` for spellings no rule can derive (`오픈AI` → `OpenAI`). See `samples/aliases.csv`. |
|
|
281
|
+
| `--near 0.6` | Trigram similarity needed to list a near pair for review (0 to 1). |
|
|
282
|
+
| `--workspace NAME` | LightRAG workspace sub-folder inside the working folder. |
|
|
283
|
+
|
|
284
|
+
`scan` supports the default NetworkX storage (a `.graphml` file). Neo4j or PostgreSQL graphs are not scanned yet, though `apply --server` works with any storage the server uses.
|
|
285
|
+
|
|
286
|
+
## Language
|
|
287
|
+
|
|
288
|
+
Screens are in English or Korean. Pick one with `--lang en|ko`, or set `LIGHTRAG_DEDUPE_LANG=ko`; without either, `LC_ALL` / `LC_MESSAGES` / `LANG` decide (anything not starting with `ko` gives English). Rule codes, JSON keys and CSV columns stay in English. Save `plan.csv` as "CSV UTF-8" if you edit it in a spreadsheet; the tool writes it with a BOM so Excel shows Korean names correctly.
|
|
289
|
+
|
|
290
|
+
## How it decides
|
|
291
|
+
|
|
292
|
+
```mermaid
|
|
293
|
+
flowchart LR
|
|
294
|
+
G[graphml file] --> N[normalize names]
|
|
295
|
+
N --> K[same key = one group]
|
|
296
|
+
K --> P[plan.json / plan.csv]
|
|
297
|
+
P --> R[you review]
|
|
298
|
+
R --> A[apply via LightRAG merge API]
|
|
299
|
+
A --> B[backup + log]
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
- **normalize names**: each name goes through fixed rules in order: Unicode NFKC, case folding, repeated company markers (`Inc.`, `Co., Ltd.`, `주식회사`, `(주)`, `㈜`), a Korean particle (`의`, ...), spaces and punctuation, then your alias table. Every rule that changed the name is recorded as evidence.
|
|
303
|
+
- **Korean particles are careful**: `삼성전자의` loses `의` only if `삼성전자` exists as another node or alias. `마이크로` is left alone because there is no `마이크` node (so `로` is not mistaken for a particle).
|
|
304
|
+
- **same key = one group**: no chains, so one wrong link cannot pull unrelated names together. Nodes with different entity types (`Apple` the company and `apple` the food) are marked `type_conflict` and skipped by default.
|
|
305
|
+
- **the target name** is chosen without any model: the alias canonical name, then a name with no surface rules applied, then the most source chunks, then the highest degree, then the shorter name.
|
|
306
|
+
- **near pairs** (similar but different keys) are listed for you only; the tool never merges them.
|
|
307
|
+
- **apply goes through LightRAG**: the graph file is never edited directly, because the vector store and chunk tracking would then disagree with it. LightRAG merges relations by keeping the maximum weight, not the sum.
|
|
308
|
+
|
|
309
|
+
## When to use it
|
|
310
|
+
|
|
311
|
+
- A good fit: you already have a LightRAG graph with spelling variants, and you want a repeatable, explainable, offline cleanup with a file to review.
|
|
312
|
+
- Not covered: a name written in another script (`오픈AI` vs `OpenAI`) is not guessed; put it in the alias CSV. Deciding that two *different-looking* names are the same thing by meaning needs an LLM or a human.
|
|
313
|
+
- Other ways to merge in LightRAG itself: its WebUI graph editor and `merge_entities` / `POST /graph/entities/merge` ([LightRAG](https://github.com/HKUDS/LightRAG)). They merge what you name; they do not look for candidates. This tool finds candidates and then calls that same API.
|
|
314
|
+
|
|
315
|
+
## Compared with
|
|
316
|
+
|
|
317
|
+
Checked 2026-11-27; see each project for its current features.
|
|
318
|
+
|
|
319
|
+
- **LightRAG's own merge** (WebUI editor, `merge_entities`, `POST /graph/entities/merge`): merges the names you give it. This tool produces the candidates and calls it. [LightRAG](https://github.com/HKUDS/LightRAG)
|
|
320
|
+
- **nodecanon** ([PyPI](https://pypi.org/project/nodecanon/), 0.1.1 when checked): another way to find duplicate nodes in a graph file. Read its README for how it scores, what it downloads and how it writes results back.
|
|
321
|
+
- **lightragmcp** ([GitHub](https://github.com/lalitsuryan/lightragmcp)): an MCP server around the LightRAG REST API that exposes a merge tool, so an agent decides what to merge.
|
|
322
|
+
|
|
323
|
+
## Limits & roadmap
|
|
324
|
+
|
|
325
|
+
- `scan` reads the default NetworkX `.graphml` storage only. Neo4j and PostgreSQL graphs are not scanned yet (`apply --server` works with them).
|
|
326
|
+
- Spellings in another script are not guessed; add them to the alias CSV.
|
|
327
|
+
- Near pairs are listed, never merged.
|
|
328
|
+
- Planned: scan through a running server (non-NetworkX storages), and a switch to turn rules on and off. After that: ranking of review pairs with your own embeddings (never auto-merged) and `scan --diff` to show only duplicates that appeared after new documents. Plans can change.
|
|
329
|
+
|
|
330
|
+
## Troubleshooting
|
|
331
|
+
|
|
332
|
+
| Symptom | Cause | Fix |
|
|
333
|
+
|---|---|---|
|
|
334
|
+
| Exit code 3, "nothing was changed" | The graph changed after `scan`, so a source name is missing or different. | Run `scan` again and re-apply your edits. |
|
|
335
|
+
| Exit code 4 while applying | LightRAG or the server refused a merge (for example a 409). | Fix the cause, run the same `apply` again; finished groups are skipped. |
|
|
336
|
+
| `demo` or `apply --rag` asks for LightRAG | `lightrag-hku` is not installed in this environment. | Install the extra: `uv tool install "lightrag-dedupe[lightrag] @ git+https://github.com/LeeGyeongPil/lightrag-dedupe"`, or use `uv sync` in a clone. |
|
|
337
|
+
| LightRAG refuses to open the folder | Your factory uses a different embedding model than the one that built the data. | Build LightRAG in the factory exactly like your indexing script. |
|
|
338
|
+
| `apply --server` refuses `--yes` | No backup path given. | Add `--working-dir DIR`, or `--no-backup`. |
|
|
339
|
+
| `plan.csv` is rejected as not UTF-8 (exit 2) | A spreadsheet saved it as CP949 or another encoding. | Save as "CSV UTF-8" and run again. |
|
|
340
|
+
| `graph file not found: rag_storage/...` | The working folder does not exist yet. | Create the sample folder first (step 5) or point to your own LightRAG folder. |
|
|
341
|
+
| A group you expected is missing | The names differ by more than the rules cover. | Add an alias row, check `R…` review lines, or lower `--near`. |
|
|
342
|
+
|
|
343
|
+
## Development
|
|
344
|
+
|
|
345
|
+
```bash
|
|
346
|
+
uv sync
|
|
347
|
+
uv run pytest -q
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
Tests run offline and merge with real `lightrag-hku` 1.5.8. Bugs and suggestions are welcome as issues. Changes are listed in [CHANGELOG.md](https://github.com/LeeGyeongPil/lightrag-dedupe/blob/v0.1.1/CHANGELOG.md).
|
|
351
|
+
|
|
352
|
+
## License
|
|
353
|
+
|
|
354
|
+
MIT. The bundled sample graph is synthetic.
|