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