-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed_markdown.py
More file actions
229 lines (191 loc) · 7.17 KB
/
embed_markdown.py
File metadata and controls
229 lines (191 loc) · 7.17 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import argparse
import hashlib
import json
import os
import re
from pathlib import Path
import tqdm
from sentence_transformers import SentenceTransformer
def _to_relative(p: Path) -> str:
try:
return os.path.relpath(str(p), start=str(Path.cwd()))
except Exception:
return str(p)
def preprocess_markdown(md_content: str) -> tuple[str, str]:
"""Preprocess markdown content and extract metadata.
Returns:
tuple[str, str]: (processed_content, date_string)
- processed_content: markdown with frontmatter removed and title added
- date_string: ISO 8601 date string from frontmatter, or empty string if not
found
"""
# if title: in frontmatter, convert to # ... and insert right after frontmatter
frontmatter_match = re.match(r"---\n(.*?)\n---\n", md_content, flags=re.DOTALL)
date_str = ""
if frontmatter_match:
# if include "draft: true", skip this file
if re.search(r"draft:\s*true", frontmatter_match.group(1)):
return "", ""
frontmatter = frontmatter_match.group(1)
# Extract date
date_match = re.search(r"date:\s*['\"]?([^'\"]+)['\"]?", frontmatter)
if date_match:
date_str = date_match.group(1).strip()
title_match = re.search(r"title:\s*(.+)", frontmatter)
if title_match:
title = title_match.group(1).strip().strip("'").strip('"')
title_tag = f"# {title}\n"
md_content = re.sub(
r"---\n.*?\n---\n",
f"---\n{frontmatter}\n---\n{title_tag}",
md_content,
count=1,
flags=re.DOTALL,
)
# convert {{< highlight ... >}} ... {{< /highlight >}} to ```` ... ```
md_content = re.sub(r"{{<\s*highlight\s+\w+\s*>}}", "```", md_content)
md_content = re.sub(r"{{</\s*highlight\s*>}}", "```", md_content)
# convert {{< relref link >}} to link
md_content = re.sub(r"{{<\s*relref\s+(.+?)\s*>}}", r"\1", md_content)
# remove {{% ... %}} and {{< ... >}}
md_content = re.sub(r"{{[%<].*?[%>]}}", "", md_content, flags=re.DOTALL)
# remove frontmatter
md_content = re.sub(r"---\n.*?\n---\n", "", md_content, count=1, flags=re.DOTALL)
return md_content, date_str
def convert_clarified_markdown(target: Path) -> list[dict]:
"""Convert markdown file or all markdown files in a directory to clarified text.
Return dict structure is:
```
[
{
"filepath": "absolute path of the file",
"title": "title of the file",
"date": "ISO 8601 date string (e.g., '2025-11-20T06:00:00+09:00')",
"text": "structured text",
},
...,
]
```
"""
def extract_title(structured_text: str) -> str:
# get title from # ...
title_match = re.search(r"#\s*(.*)", structured_text)
return title_match.group(1).strip() if title_match else ""
def get_hash(text: str) -> str:
encoded = text.encode("utf-8")
return hashlib.sha256(encoded).hexdigest()
result_values = []
print("Convert markdown files to structured text...")
# Collect markdown files
md_files = list(target.rglob("*.md")) if target.is_dir() else [target]
with tqdm.tqdm(md_files) as pbar:
for md_file in pbar:
with open(md_file, encoding="utf-8") as f:
md_content = f.read()
md_content, date_str = preprocess_markdown(md_content)
if not md_content:
continue
title = extract_title(md_content)
content_hash = get_hash(md_content)
pbar.set_postfix({"title": title})
result_values.append(
{
"filepath": _to_relative(md_file),
"title": title,
"date": date_str,
"hash": content_hash,
"text": md_content,
}
)
return result_values
def get_embedding(texts: list[dict], model_name: str, prev: Path) -> list[dict]:
"""Get embeddings for a list of texts using SentenceTransformers.
Return dict structure is:
```
[
{
"filepath": "absolute path of the file",
"title": "title of the file",
"date": "ISO 8601 date string",
"vector": [numpy array of the embedding vector]
}, ...
]
```
"""
print(f"Make embeddings using {model_name}...")
vector_list = []
prev_vector_map = {}
if prev and prev.is_file():
with open(prev, encoding="utf-8") as f:
for line in f:
vec = json.loads(line)
prev_vector_map[vec["filepath"]] = vec
text_contents = []
filepaths = []
titles = []
dates = []
hashes = []
for text_dict in texts:
if text_dict["filepath"] in prev_vector_map:
if prev_vector_map[text_dict["filepath"]]["hash"] == text_dict["hash"]:
vector_list.append(prev_vector_map[text_dict["filepath"]])
continue
text_contents.append(text_dict["text"])
filepaths.append(text_dict["filepath"])
titles.append(text_dict["title"])
dates.append(text_dict.get("date", ""))
hashes.append(text_dict["hash"])
if not text_contents:
print("No new or updated files to embed.")
return vector_list
model = SentenceTransformer(model_name, trust_remote_code=True)
embeddings = model.encode(
text_contents, show_progress_bar=True, batch_size=8, convert_to_numpy=True
)
for i, embedding in enumerate(embeddings):
vector_list.append(
{
"filepath": filepaths[i],
"title": titles[i],
"date": dates[i],
"hash": hashes[i],
"vector": [round(float(x), 4) for x in embedding.tolist()],
}
)
return vector_list
def save_jsonl(vectors: list[dict], output_file: str) -> None:
print(f"Saving embedded vectors to JSONL, {output_file} ...")
# sort by filepath
vectors.sort(key=lambda x: x["filepath"])
with open(output_file, "w", encoding="utf-8") as f:
for vec in vectors:
f.write(json.dumps(vec, ensure_ascii=False) + "\n")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Vectorize markdown files.")
parser.add_argument(
"target", type=str, help="The target file or directory to vectorize."
)
parser.add_argument(
"--prev",
type=str,
default="vectors.jsonl",
help="The previous vectors file to compare.",
)
parser.add_argument(
"--embedding-model",
type=str,
default="pfnet/plamo-embedding-1b",
help="The embedding model to use.",
)
parser.add_argument(
"--output",
type=str,
default="vectors.jsonl",
help="The output file to save the embeddings.",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
plain_texts = convert_clarified_markdown(Path(args.target))
vectors = get_embedding(plain_texts, args.embedding_model, Path(args.prev))
save_jsonl(vectors, args.output)