feat: 导入CMA完整文档(14章)到知识库
This commit is contained in:
@@ -0,0 +1,60 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""将CMA战略地图完整文档导入知识库"""
|
||||||
|
import pymysql, re
|
||||||
|
|
||||||
|
DB = dict(host='127.0.0.1', port=3306, user='cma_user', password='cma_pass_2026', database='cma', charset='utf8mb4')
|
||||||
|
|
||||||
|
DOC = "/root/CMA_Strategy_Map_完整分析.md"
|
||||||
|
|
||||||
|
# 读取文档
|
||||||
|
with open(DOC) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# 按章节分割(## 开头)
|
||||||
|
chapters = re.split(r'\n(?=## \d+\.)', content)
|
||||||
|
|
||||||
|
articles = []
|
||||||
|
for i, ch in enumerate(chapters):
|
||||||
|
if not ch.strip():
|
||||||
|
continue
|
||||||
|
# 提取标题
|
||||||
|
title_match = re.match(r'##\s+(.+?)(?:\n|$)', ch)
|
||||||
|
title = title_match.group(1).strip() if title_match else f"第{i+1}部分"
|
||||||
|
# 提取摘要
|
||||||
|
summary = ""
|
||||||
|
for line in ch.split('\n')[:5]:
|
||||||
|
clean = line.strip()
|
||||||
|
if clean and not clean.startswith('#') and not clean.startswith('>') and len(clean) > 10:
|
||||||
|
summary = clean[:200]
|
||||||
|
break
|
||||||
|
|
||||||
|
articles.append({
|
||||||
|
"title": title[:200],
|
||||||
|
"summary": summary,
|
||||||
|
"content": ch.strip(),
|
||||||
|
"category": "CMA完整文档",
|
||||||
|
"icon": "📘",
|
||||||
|
"related_page": "/knowledge",
|
||||||
|
"sort_order": i
|
||||||
|
})
|
||||||
|
|
||||||
|
conn = pymysql.connect(**DB)
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
# 先删除同分类旧文章
|
||||||
|
cur.execute("DELETE FROM knowledge_articles WHERE category = 'CMA完整文档'")
|
||||||
|
print(f"已清理旧文章")
|
||||||
|
|
||||||
|
inserted = 0
|
||||||
|
for a in articles:
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO knowledge_articles (title, summary, content, category, icon, related_page, sort_order) VALUES (%s,%s,%s,%s,%s,%s,%s)",
|
||||||
|
(a['title'], a['summary'], a['content'], a['category'], a['icon'], a['related_page'], a['sort_order'])
|
||||||
|
)
|
||||||
|
inserted += 1
|
||||||
|
print(f" [{inserted}] {a['title'][:50]}...")
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
print(f"\n✅ 导入完成: {inserted} 篇文章")
|
||||||
Reference in New Issue
Block a user