基于笔者之前写的博客基础上:https://blog.csdn.net/zhanghan11366/article/details/142139488【基于开源WQ装备知识图谱的智能问答全流程构建】进行优化。新增处理基于特定格式下的WQ文档,抽取文档的WQ属性和关系,并抽取对应WQt图片存储至minio中。
1 文档格式如下:
2 提取文档中的WQ信息
- 配置如下:
import argparse
class Args:
@staticmethod
def parse():
parser = argparse.ArgumentParser()
return parser
@staticmethod
def initialize(parser):
parser.add_argument('--weapon_realtion_api', default='http://0.0.0.0:6410/weapon',
help='Weapons and equipment relationship extraction API interface')
parser.add_argument('--word_extraction_api', default='http://1.95.39.242:2011/attribute_all',
help='Parse word documents and extract weapons and equipment attributes api interface')
parser.add_argument('--neo4j_url', default='bolt://localhost:7687',
help='neo4j login website')
parser.add_argument('--neo4j_usename', default='neo4j',
help='neo4j login username')
parser.add_argument('--neo4j_password', default='neo4jZH',
help='neo4j login password')
parser.add_argument('--unstr_file_path', default='./data/unstr/武器装备-test.docx',
help='Unstructured document parsing path')
parser.add_argument('--unstr_save_file_path', default='./data/unstr/word_weapon_basic_info.txt',
help='The path to save the unstructured document after parsing')
parser.add_argument('--weapon_input_file', default='./data/weapon/weapon_data.txt',
help='Weapons and equipment relationship extraction input address')
return parser
def get_parser(self):
parser = self.parse()
parser = self.initialize(parser)
return parser.parse_args()
- 提取代码
import requests, config
# 调用
args = config.Args().get_parser()
def get_word_weapon(file_path):
# 发送 POST 请求并上传文件
with open(file_path, 'rb') as file:
files = {'file': file}
response = requests.post(args.word_extraction_api, files=files)
# 检查响应状态码
if response.status_code == 200:
try:
# 尝试以 JSON 格式解析响应
response_json = response.json()
return response_json.get('data')
except ValueError:
print("响应不是 JSON 格式:")
print(response.text)
else:
print(f"请求失败,状态码: {response.status_code}")
print(f"响应内容: {response.text}")
def save_word_weapon_basic_info(weapon_data, file_path):
# 循环每个武器数据,将其基本情况写入TXT文件
with open(file_path, 'w', encoding='utf-8') as file:
for weapon in weapon_data:
basic_info = weapon['基本情况'].replace('\n', ' ')
file.write(basic_info + '\n') # 换行区分不同武器的信息
print(f"武器的基本情况已保存至 {file_path}")
if __name__ == "__main__":
# 文件路径
weapon_data = get_word_weapon(args.unstr_file_path)
save_word_weapon_basic_info(weapon_data, args.unstr_save_file_path)
- 其中接口信息如下:
3 图文匹配
抽取对应WQt图片存储至minio中,结果如下。
后续流程与https://blog.csdn.net/zhanghan11366/article/details/142139488【基于开源WQ装备知识图谱的智能问答全流程构建】一致。