kg 修改

This commit is contained in:
QiyiJiang 2024-07-14 13:04:00 +00:00
parent 39e7e76e58
commit 812ad2cbd8
3 changed files with 35 additions and 27 deletions

View File

@ -1,2 +1,2 @@
from oneke import *
from pdf2txt import *
from plugins.oneke import *
from plugins.pdf2txt import *

View File

@ -117,12 +117,9 @@ class OneKE:
return outputs
def processing_text_to_kg(self, text_or_path, output_path):
if os.path.isfile(text_or_path):
with open(text_or_path, 'r', encoding='utf-8') as f:
text = f.read()
else:
text = text_or_path
for chunk in read_and_process_chars(text_or_path):
text = chunk
schema = [
{
"entity_type": "食品",
@ -136,14 +133,25 @@ class OneKE:
}
}
]
task = "KG"
output = self.predict(text=text, schema=schema, task=task, language="zh")
formatted_output = parse_and_format_output(output=output, task_type=task)
if os.path.exists(output_path):
with open(output_path, 'r', encoding='utf-8') as f:
existing_data = json.load(f)
else:
existing_data = []
with open(output_path, 'a+', encoding='utf-8') as f:
for entry in formatted_output:
f.write(json.dumps(entry, ensure_ascii=False) + '\n')
# 将新的数据添加到现有数据之后
existing_data.extend(formatted_output)
# 将合并后的数据写回 JSON 文件
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(existing_data, f, ensure_ascii=False, indent=4)
# with open(output_path, 'a+', encoding='utf-8') as f:
# for entry in formatted_output:
# f.write(json.dumps(entry, ensure_ascii=False) + '\n')
print(f"预测结果已添加到 {output_path} 文件中。")
return output_path

View File

@ -1,6 +1,6 @@
import fitz
from logging_config import setup_logger, logger
from utils.logging_config import setup_logger, logger
def is_text_pdf(pdf_path):
doc = fitz.open(pdf_path)