Datasets:
add seed_builder.py
Browse files- seed_builder.py +115 -0
seed_builder.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""seed_builder.py — 四大语料目录扫描 → 种子概念池 seeds.json
|
| 3 |
+
纯标准库:docx=zip+xml抽取,md/txt/html直接读,pdf跳过(内容多为md复本)
|
| 4 |
+
输出:seeds.json {sector:[64], region:[32], instrument:[27], frame:[27],
|
| 5 |
+
sephirot_lex:{16:[...]}, problem_phrases:[...], stats}
|
| 6 |
+
"""
|
| 7 |
+
import os, re, json, zipfile, collections
|
| 8 |
+
|
| 9 |
+
BASE = r"D:\双生天使的怀抱"
|
| 10 |
+
DIRS = [os.path.join(BASE, d) for d in ("爱救人", "爱的拥抱", "爱的文章", "爱的创造")]
|
| 11 |
+
OUT = os.path.join(BASE, "爱的数据集", "worldfix_v2", "seeds.json")
|
| 12 |
+
|
| 13 |
+
STOP = set("的了和与及或在是对于将为被把从向而因由其所这那也都很你我不他她它们我们你们一个这个那个就是但是然后所以如果因为因此因此可以我们会我的你的他们的自己没有已经现在这样那样什么怎么".split())
|
| 14 |
+
WORD_RE = re.compile(r"[\u4e00-\u9fff]{2,6}|[A-Za-z][A-Za-z0-9\-]{3,20}")
|
| 15 |
+
|
| 16 |
+
def docx_text(path):
|
| 17 |
+
try:
|
| 18 |
+
with zipfile.ZipFile(path) as z:
|
| 19 |
+
xml = z.read("word/document.xml").decode("utf-8", "ignore")
|
| 20 |
+
xml = re.sub(r"<[^>]+>", "\u0001", xml)
|
| 21 |
+
return xml.replace("\u0001", "\n")
|
| 22 |
+
except Exception:
|
| 23 |
+
return ""
|
| 24 |
+
|
| 25 |
+
def any_text(path):
|
| 26 |
+
ext = path.lower().rsplit(".", 1)[-1]
|
| 27 |
+
if ext == "docx":
|
| 28 |
+
return docx_text(path)
|
| 29 |
+
if ext in ("txt", "md", "html", "htm", "json", "py", "js", "yml", "yaml", "csv"):
|
| 30 |
+
try:
|
| 31 |
+
return open(path, encoding="utf-8", errors="ignore").read()
|
| 32 |
+
except Exception:
|
| 33 |
+
return ""
|
| 34 |
+
return "" # pdf/doc/xlsx 等跳过
|
| 35 |
+
|
| 36 |
+
texts = []
|
| 37 |
+
stats = collections.Counter()
|
| 38 |
+
for d in DIRS:
|
| 39 |
+
for root, _, files in os.walk(d):
|
| 40 |
+
for fn in files:
|
| 41 |
+
p = os.path.join(root, fn)
|
| 42 |
+
t = any_text(p)
|
| 43 |
+
if t.strip():
|
| 44 |
+
texts.append(t)
|
| 45 |
+
stats[fn.rsplit(".", 1)[-1].lower()] += len(t)
|
| 46 |
+
corpus = "\n".join(texts)
|
| 47 |
+
print(f"语料字符量: {len(corpus):,} 按扩展名: {dict(stats)}")
|
| 48 |
+
|
| 49 |
+
# ── 词频 ──
|
| 50 |
+
JUNK = {"apos","quot","nbsp","amp","lt","gt","mdash","ndash","hellip","rsquo","lsquo",
|
| 51 |
+
"rdquo","ldquo","middot","deg","sup","sub","href","https","http","www","com",
|
| 52 |
+
"div","span","class","style","font","size","color","title","width","height"}
|
| 53 |
+
words = WORD_RE.findall(corpus)
|
| 54 |
+
def has_cjk(w): return any('\u4e00' <= ch <= '\u9fff' for ch in w)
|
| 55 |
+
freq = collections.Counter(
|
| 56 |
+
w for w in words
|
| 57 |
+
if w not in STOP and w.lower() not in JUNK and len(w) >= 2
|
| 58 |
+
and (has_cjk(w) or (w.isalpha() and len(w) >= 5)))
|
| 59 |
+
|
| 60 |
+
# ── 领域词典:世界问题部门(64)候选源 → 归一化到固定64槽 ──
|
| 61 |
+
SECTOR_SEEDS = ["教育","医疗","养老","住房","就业","贫困","饥饿","清洁水","气候","能源",
|
| 62 |
+
"环境","生物多样性","海洋","农业","粮食","交通","城市化","垃圾","污染","水资源",
|
| 63 |
+
"和平","安全","裁军","治理","腐败","法治","人权","移民","难民","性别平等",
|
| 64 |
+
"儿童","残疾人","心理健康","成瘾","疫情","公共卫生","数字鸿沟","人工智能","数据隐私","网络安全",
|
| 65 |
+
"金融普惠","债务","贸易","税收","通胀","供应链","创新","科研","太空","极地",
|
| 66 |
+
"森林","荒漠化","洪水","地震","火灾","热浪","物种灭绝","抗生素","核风险","化学品",
|
| 67 |
+
"体育","文化"]
|
| 68 |
+
sector = SECTOR_SEEDS[:64]
|
| 69 |
+
while len(sector) < 64:
|
| 70 |
+
sector.append(f"复合领域{len(sector)}")
|
| 71 |
+
|
| 72 |
+
REGION_SEEDS = ["超大城市","省会城市","县城","乡镇","农村","牧区","渔村","山区","林区","湿地",
|
| 73 |
+
"海岛","边境","特区","自贸区","工业区","科技园","大学城","老工业区","资源型城市","旅游城市",
|
| 74 |
+
"干旱区","高原","热带","寒带","季风区","三角洲","流域","盆地","绿洲","环礁",
|
| 75 |
+
"贫民窟","棚户区"]
|
| 76 |
+
region = REGION_SEEDS[:32]
|
| 77 |
+
|
| 78 |
+
# 工具/框架只收含CJK的词(杜绝实体残渣);填充词排除常用虚词
|
| 79 |
+
FUNC = {"比如","例如","通过","可以","我们","他们","她们","这个","那个","这些","那些",
|
| 80 |
+
"一个","自己","没有","或者","以及","关于","对于","现在","时候","东西","事情",
|
| 81 |
+
"什么","怎么","如何","还是","就是","但是","然后","所以","因为","因此","如果",
|
| 82 |
+
"而且","并且","不过","只是","其实","真的","感觉","觉得","认为","知道","开始"}
|
| 83 |
+
cand = [w for w, _ in freq.most_common(6000)
|
| 84 |
+
if has_cjk(w) and w not in STOP and w not in FUNC and len(w) >= 2]
|
| 85 |
+
instrument, frame = [], []
|
| 86 |
+
for w in cand:
|
| 87 |
+
if len(instrument) < 60 and re.search(r"(制度|机制|政策|平台|基金|保险|券|补贴|税|许可|标准|法规|条例|公约|联盟|合作社|公私|试点|预算|审计|指数|清单|契约|积分|银行|市场|拍卖|网格|热线|委员会)", w):
|
| 88 |
+
instrument.append(w)
|
| 89 |
+
elif len(frame) < 80 and re.search(r"(指数|评估|指标|报告|地图|图谱|模型|算法|协议|框架|体系|白皮书|标准|认证|评级|监测|预警|仿真|沙盘|账本|仪表盘|闭环|反馈|回路|审计)", w):
|
| 90 |
+
frame.append(w)
|
| 91 |
+
instrument = (instrument + cand[len(instrument):])[:27]
|
| 92 |
+
frame = (frame + cand[200:200+80])[:27]
|
| 93 |
+
|
| 94 |
+
# 十六质点词库
|
| 95 |
+
SEPH = ["凯瑟(王冠)","霍克玛(智慧)","比纳(理解)","哈赛德(慈悲)","格夫拉(力量)","提法莱特(美)",
|
| 96 |
+
"内扎赫(胜利)","霍德(荣光)","耶索德(基础)","马尔胡特(王国)","达特(知识)","克特(创造)",
|
| 97 |
+
"希姆(生命之树)","沙迈(守护)","拉赫曼(恩慈)","谢金尼(临在)"]
|
| 98 |
+
SEPH_LEX_KEYS = ["爱","信任","边界","修复","成长","连接","意义","责任","勇气","希望","智慧","慈悲",
|
| 99 |
+
"共生","秩序","自由","尊严"]
|
| 100 |
+
sephirot_lex = {}
|
| 101 |
+
pool = [w for w,_ in freq.most_common(8000) if len(w)>=2 and w not in STOP and has_cjk(w)]
|
| 102 |
+
for i,k in enumerate(SEPH_LEX_KEYS):
|
| 103 |
+
sephirot_lex[k] = pool[i*37:(i*37)+220] or ["陪伴","倾听","守护"]
|
| 104 |
+
|
| 105 |
+
data = {"sector": sector, "region": region,
|
| 106 |
+
"instrument": instrument, "frame": frame,
|
| 107 |
+
"sephirot_keys": SEPH_LEX_KEYS, "sephirot_lex": sephirot_lex,
|
| 108 |
+
"top_terms": [w for w,_ in freq.most_common(3000)],
|
| 109 |
+
"meta": {"chars": len(corpus), "files": len(texts),
|
| 110 |
+
"sources": DIRS}}
|
| 111 |
+
os.makedirs(os.path.dirname(OUT), exist_ok=True)
|
| 112 |
+
json.dump(data, open(OUT, "w", encoding="utf-8"), ensure_ascii=False)
|
| 113 |
+
print("seeds.json 写出:", OUT)
|
| 114 |
+
print("sector64/instrument27/frame27/lex16:",
|
| 115 |
+
len(sector), len(instrument), len(frame), len(sephirot_lex))
|