Upload 3 files
Browse files- division.py +125 -0
- new_triple_processed.json +3 -0
- old_triple_processed.json +3 -0
division.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
from transformers import LlamaTokenizer
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
import argparse
|
| 7 |
+
|
| 8 |
+
def build_entity_triples(triples):
|
| 9 |
+
entity_triples = defaultdict(list)
|
| 10 |
+
for triple in triples:
|
| 11 |
+
subj, relation, obj = triple
|
| 12 |
+
# 构建完整的三元组字符串
|
| 13 |
+
triple_str = f"{subj} - {relation} - {obj}"
|
| 14 |
+
entity_triples[subj].append(triple_str)
|
| 15 |
+
return entity_triples
|
| 16 |
+
|
| 17 |
+
def create_paragraphs_with_alignment(subj, old_triples_list, new_triples_list, tokenizer, max_tokens=512):
|
| 18 |
+
paragraphs_old = []
|
| 19 |
+
paragraphs_new = []
|
| 20 |
+
boundaries = [] # 记录段落边界的索引
|
| 21 |
+
|
| 22 |
+
index = 0
|
| 23 |
+
start_idx = 0
|
| 24 |
+
while index < len(old_triples_list):
|
| 25 |
+
# 累积令牌长度
|
| 26 |
+
candidate_old_triples = old_triples_list[start_idx:index+1]
|
| 27 |
+
paragraph_text_old = ', '.join(candidate_old_triples)
|
| 28 |
+
tokens_in_paragraph = len(tokenizer.encode(paragraph_text_old, add_special_tokens=False))
|
| 29 |
+
|
| 30 |
+
if tokens_in_paragraph > max_tokens:
|
| 31 |
+
if index == start_idx:
|
| 32 |
+
# 单个三元组就超出限制,强制添加
|
| 33 |
+
boundaries.append(index+1)
|
| 34 |
+
start_idx = index+1
|
| 35 |
+
index += 1
|
| 36 |
+
else:
|
| 37 |
+
boundaries.append(index)
|
| 38 |
+
start_idx = index
|
| 39 |
+
else:
|
| 40 |
+
index += 1
|
| 41 |
+
|
| 42 |
+
# 添加最后的边界
|
| 43 |
+
if start_idx < len(old_triples_list):
|
| 44 |
+
boundaries.append(len(old_triples_list))
|
| 45 |
+
|
| 46 |
+
# 根据边界划分段落
|
| 47 |
+
start = 0
|
| 48 |
+
for end in boundaries:
|
| 49 |
+
# 旧的段落
|
| 50 |
+
old_paragraph_triples = old_triples_list[start:end]
|
| 51 |
+
paragraph_text_old = ', '.join(old_paragraph_triples)
|
| 52 |
+
paragraphs_old.append({
|
| 53 |
+
"title": subj,
|
| 54 |
+
"contents": paragraph_text_old
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
# 新的段落
|
| 58 |
+
new_paragraph_triples = new_triples_list[start:end]
|
| 59 |
+
paragraph_text_new = ', '.join(new_paragraph_triples)
|
| 60 |
+
paragraphs_new.append({
|
| 61 |
+
"title": subj,
|
| 62 |
+
"contents": paragraph_text_new
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
start = end
|
| 66 |
+
|
| 67 |
+
return paragraphs_old, paragraphs_new
|
| 68 |
+
|
| 69 |
+
def main(args):
|
| 70 |
+
os.makedirs(os.path.dirname(args.old_output_file), exist_ok=True)
|
| 71 |
+
os.makedirs(os.path.dirname(args.new_output_file), exist_ok=True)
|
| 72 |
+
|
| 73 |
+
tokenizer = LlamaTokenizer.from_pretrained(args.model_path)
|
| 74 |
+
|
| 75 |
+
with open(args.old_input_json_file, 'r') as f:
|
| 76 |
+
old_triples = json.load(f)
|
| 77 |
+
|
| 78 |
+
with open(args.new_input_json_file, 'r') as f:
|
| 79 |
+
new_triples = json.load(f)
|
| 80 |
+
|
| 81 |
+
old_entity_triples = build_entity_triples(old_triples)
|
| 82 |
+
new_entity_triples = build_entity_triples(new_triples)
|
| 83 |
+
|
| 84 |
+
# 处理所有实体
|
| 85 |
+
all_paragraphs_old = []
|
| 86 |
+
all_paragraphs_new = []
|
| 87 |
+
entities = set(old_entity_triples.keys()).union(new_entity_triples.keys())
|
| 88 |
+
|
| 89 |
+
for subj in tqdm(entities, desc="Processing entities", unit="entity"):
|
| 90 |
+
old_triples_list = old_entity_triples.get(subj, [])
|
| 91 |
+
new_triples_list = new_entity_triples.get(subj, [])
|
| 92 |
+
|
| 93 |
+
# 确保新旧三元组列表长度一致
|
| 94 |
+
max_length = max(len(old_triples_list), len(new_triples_list))
|
| 95 |
+
if len(old_triples_list) < max_length:
|
| 96 |
+
old_triples_list.extend([''] * (max_length - len(old_triples_list)))
|
| 97 |
+
if len(new_triples_list) < max_length:
|
| 98 |
+
new_triples_list.extend([''] * (max_length - len(new_triples_list)))
|
| 99 |
+
|
| 100 |
+
paragraphs_old, paragraphs_new = create_paragraphs_with_alignment(
|
| 101 |
+
subj, old_triples_list, new_triples_list, tokenizer, args.max_tokens
|
| 102 |
+
)
|
| 103 |
+
all_paragraphs_old.extend(paragraphs_old)
|
| 104 |
+
all_paragraphs_new.extend(paragraphs_new)
|
| 105 |
+
|
| 106 |
+
with open(args.old_output_file, 'w') as out_f:
|
| 107 |
+
json.dump(all_paragraphs_old, out_f, ensure_ascii=False, indent=4)
|
| 108 |
+
|
| 109 |
+
with open(args.new_output_file, 'w') as out_f:
|
| 110 |
+
json.dump(all_paragraphs_new, out_f, ensure_ascii=False, indent=4)
|
| 111 |
+
|
| 112 |
+
print(f"old {args.old_output_file}")
|
| 113 |
+
print(f"new {args.new_output_file}")
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
parser = argparse.ArgumentParser(description="Process old and new triples")
|
| 117 |
+
parser.add_argument('--old_input_json_file', type=str, required=True, help='旧的三元组JSON文件的路径。')
|
| 118 |
+
parser.add_argument('--new_input_json_file', type=str, required=True, help='新的三元组JSON文件的路径。')
|
| 119 |
+
parser.add_argument('--old_output_file', type=str, required=True, help='保存处理后的旧三元组输出文件的完整路径。')
|
| 120 |
+
parser.add_argument('--new_output_file', type=str, required=True, help='保存处理后的新三元组输出文件的完整路径。')
|
| 121 |
+
parser.add_argument('--model_path', type=str, required=True, help='预训练的Tokenizer模型的路径。')
|
| 122 |
+
parser.add_argument('--max_tokens', type=int, default=512, help='每个段落的最大令牌数。')
|
| 123 |
+
|
| 124 |
+
args = parser.parse_args()
|
| 125 |
+
main(args)
|
new_triple_processed.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d91388a2bfdbc4074123b00d475859b92fb61e3d07e256309d18eef9b81216f9
|
| 3 |
+
size 1080845964
|
old_triple_processed.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:57c88a61b0b5e8442c3508fa203a86c283dd7de153a254d0ffaa8522e37c1001
|
| 3 |
+
size 1080796696
|