huhan3000/ai-tools/scripts/neo4j_queries.cypher

143 lines
4.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 圐圙文化网络 - Neo4j Cypher 查询集合
// ===== 基础查询 =====
// 1. 查看所有词汇节点
MATCH (w:Word)
RETURN w.name, w.category, w.meaning, w.region, w.dynasty
ORDER BY w.category, w.name;
// 2. 查看所有关系类型
MATCH ()-[r]-()
RETURN DISTINCT type(r) as relationship_types;
// ===== 圐圙词根网络分析 =====
// 3. 以"昆仑"为中心的词汇网络
MATCH (center:Word {name: '昆仑'})-[r]-(connected:Word)
RETURN center, r, connected;
// 4. 查找所有音转关系
MATCH (source:Word)-[r:SOUND_SHIFT]->(target:Word)
RETURN source.name as 源词, target.name as 目标词, r.type as 关系类型;
// 5. 查找音转路径最多3步
MATCH path = (start:Word {name: '昆仑'})-[:SOUND_SHIFT*1..3]-(end:Word)
RETURN
start.name as 起点,
[node in nodes(path) | node.name] as 路径,
end.name as 终点,
length(path) as 步数
ORDER BY 步数;
// ===== 语义分析 =====
// 6. 按类别分组的词汇分布
MATCH (w:Word)
RETURN w.category as 类别, collect(w.name) as 词汇列表, count(w) as 数量
ORDER BY 数量 DESC;
// 7. 按朝代分组的词汇演化
MATCH (w:Word)
RETURN w.dynasty as 朝代, collect(w.name) as 词汇, count(w) as 数量
ORDER BY
CASE w.dynasty
WHEN '先秦' THEN 1
WHEN '汉' THEN 2
WHEN '唐' THEN 3
WHEN '宋' THEN 4
WHEN '元' THEN 5
WHEN '明' THEN 6
WHEN '清' THEN 7
ELSE 8
END;
// 8. 按地区分布的词汇地理分析
MATCH (w:Word)
WHERE w.region IS NOT NULL
RETURN w.region as 地区, collect(w.name) as 词汇, count(w) as 数量
ORDER BY 数量 DESC;
// ===== 高级分析查询 =====
// 9. 查找"圐圙"的完整关联网络
MATCH (kulue:Word {name: '圐圙'})-[r*1..2]-(related:Word)
RETURN kulue, r, related;
// 10. 查找同时具有音转和语义关系的词汇对
MATCH (w1:Word)-[:SOUND_SHIFT]-(w2:Word)
MATCH (w1)-[:SEMANTIC]-(w2)
RETURN w1.name, w2.name, '既有音转又有语义关系' as 关系特征;
// 11. 查找每个类别中的核心词汇(关联最多的)
MATCH (w:Word)-[r]-(connected:Word)
WITH w, count(r) as connections
WHERE connections > 1
RETURN w.category as 类别, w.name as 核心词汇, w.meaning as 含义, connections as 关联数
ORDER BY 类别, connections DESC;
// 12. 查找跨类别的关联模式
MATCH (w1:Word)-[r]-(w2:Word)
WHERE w1.category <> w2.category
RETURN w1.category as 类别1, w2.category as 类别2, count(r) as 关联数
ORDER BY 关联数 DESC;
// ===== 历史演化分析 =====
// 13. 三个帝国的词汇分布
MATCH (w:Word)
WITH w,
CASE
WHEN w.dynasty IN ['先秦', '秦汉', '汉', '三国', '南北朝'] THEN '长城帝国'
WHEN w.dynasty IN ['隋唐', '隋', '唐', '五代', '辽', '金', '夏', '宋'] THEN '运河帝国'
WHEN w.dynasty IN ['元', '明', '清', '民国'] THEN '圐圙帝国'
ELSE '其他'
END as empire
RETURN empire as 帝国, collect(w.name) as 词汇, count(w) as 数量;
// 14. 查找词汇的时空分布模式
MATCH (w:Word)
RETURN w.dynasty as 朝代, w.region as 地区, collect(w.name) as 词汇
ORDER BY w.dynasty, w.region;
// ===== 可视化查询 =====
// 15. 生成完整的圐圙网络图(用于可视化)
MATCH (w:Word)-[r]-(connected:Word)
RETURN w, r, connected;
// 16. 生成核心概念的星形图
MATCH (core:Word {category: '核心'})-[r]-(related:Word)
RETURN core, r, related;
// 17. 生成音转关系的有向图
MATCH (source:Word)-[r:SOUND_SHIFT]->(target:Word)
RETURN source, r, target;
// ===== 数据统计 =====
// 18. 网络统计信息
MATCH (w:Word)
WITH count(w) as total_words
MATCH ()-[r]-()
WITH total_words, count(r)/2 as total_relationships
MATCH (w:Word)-[r]-(connected:Word)
WITH total_words, total_relationships, w, count(r) as degree
RETURN
total_words as 总词汇数,
total_relationships as 总关系数,
avg(degree) as 平均度数,
max(degree) as 最大度数,
min(degree) as 最小度数;
// 19. 查找孤立节点(没有关系的词汇)
MATCH (w:Word)
WHERE NOT (w)-[]-()
RETURN w.name as 孤立词汇, w.category as 类别;
// 20. 查找关联度最高的词汇(网络中心)
MATCH (w:Word)-[r]-(connected:Word)
WITH w, count(r) as degree
ORDER BY degree DESC
LIMIT 5
RETURN w.name as 中心词汇, w.meaning as 含义, degree as 关联度;