Files
Mofox-Core/raw_data_preprocessor.py
墨梓柒 2b07c9e81b feat: 新增LPMM知识库模块及工具支持
- 新增LPMM知识库模块,包括实体提取、RDF构建、Embedding存储、KG管理等功能
- 新增`lpmm_get_knowledge`工具,支持从LPMM知识库中检索相关信息
- 新增OpenIE数据处理模块,支持信息提取、数据导入等功能
- 新增知识库初始化脚本,支持从原始数据到知识库的完整处理流程
- 新增配置文件`lpmm_config.toml`,支持自定义知识库相关参数
- 新增日志模块`LPMM_STYLE_CONFIG`,支持知识库相关日志输出
- 新增`raw_data_preprocessor.py`、`info_extraction.py`、`import_openie.py`等脚本,支持知识库数据预处理
2025-04-23 10:28:05 +08:00

89 lines
2.6 KiB
Python
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.

import json
import os
from pathlib import Path
import sys # 新增系统模块导入
from src.common.logger import get_module_logger
logger = get_module_logger("LPMM数据库-原始数据处理")
def check_and_create_dirs():
"""检查并创建必要的目录"""
required_dirs = ["data/lpmm_raw_data", "data/imported_lpmm_data"]
for dir_path in required_dirs:
if not os.path.exists(dir_path):
os.makedirs(dir_path)
logger.info(f"已创建目录: {dir_path}")
def process_text_file(file_path):
"""处理单个文本文件,返回段落列表"""
with open(file_path, "r", encoding="utf-8") as f:
raw = f.read()
paragraphs = []
paragraph = ""
for line in raw.split("\n"):
if line.strip() == "":
if paragraph != "":
paragraphs.append(paragraph.strip())
paragraph = ""
else:
paragraph += line + "\n"
if paragraph != "":
paragraphs.append(paragraph.strip())
return paragraphs
def main():
# 新增用户确认提示
print("=== 重要操作确认 ===")
print("如果你并非第一次导入知识")
print("请先删除data/import.json文件备份data/openie.json文件")
print("在进行知识库导入之前")
print("请修改config/lpmm_config.toml中的配置项")
confirm = input("确认继续执行?(y/n): ").strip().lower()
if confirm != "y":
logger.error("操作已取消")
sys.exit(1)
print("\n" + "=" * 40 + "\n")
# 检查并创建必要的目录
check_and_create_dirs()
# 检查输出文件是否存在
if os.path.exists("data/import.json"):
logger.error("错误: data/import.json 已存在,请先处理或删除该文件")
sys.exit(1)
if os.path.exists("data/openie.json"):
logger.error("错误: data/openie.json 已存在,请先处理或删除该文件")
sys.exit(1)
# 获取所有原始文本文件
raw_files = list(Path("data/lpmm_raw_data").glob("*.txt"))
if not raw_files:
logger.warning("警告: data/lpmm_raw_data 中没有找到任何 .txt 文件")
sys.exit(1)
# 处理所有文件
all_paragraphs = []
for file in raw_files:
logger.info(f"正在处理文件: {file.name}")
paragraphs = process_text_file(file)
all_paragraphs.extend(paragraphs)
# 保存合并后的结果
output_path = "data/import.json"
with open(output_path, "w", encoding="utf-8") as f:
json.dump(all_paragraphs, f, ensure_ascii=False, indent=4)
logger.info(f"处理完成,结果已保存到: {output_path}")
if __name__ == "__main__":
main()