SYSUSELab/RustRepoTrans
Evaluating Large Language Models in Repository-level Code Translation RustRepoTrans is the first repository-level code translation benchmark described in the paper "RustRepoTrans: Repository-level Code Translation Benchmark Targeting Rust". Feel free to contact us to submit new results. Benchmark Dataset RustRepoTrans, the first repository-level code translation benchmark comprising 375 tasks targeting Rust, consists of 122 java-rust function pairs, 145 c-rust… See the full description on the dataset page: https://huggingface.co/datasets/SYSUSELab/RustRepoTrans.
3104
1import os
2import logging
3import time
4import sys
5from generation import generation
6
7source_dir = sys.argv[1]
8target_dir = sys.argv[2]
9project = sys.argv[3]
10
11# 设置日志配置
12logging.basicConfig(filename=f"match_function_throughLLM_{project}.log", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
14
15def process(projects_path, project, target_dir):
16
17
18 lang_pairs = os.listdir(projects_path)
19 for lang_pair in lang_pairs:
20 # print(lang_pair)
21 if not os.path.exists(os.path.join(target_dir, project, lang_pair)):
22 os.makedirs(os.path.join(target_dir, project, lang_pair))
23 query_lang = lang_pair.split("__")[0]
24 corpus_lang = lang_pair.split("__")[1]
25
26 questions_path = os.listdir(os.path.join(projects_path, lang_pair))
27 for question_path in questions_path:
28
29 if os.path.exists(os.path.join(target_dir, project, lang_pair, question_path)):
30 logging.info(f"{lang_pair}'s {question_path} already exist")
31 continue
32
33 # match
34 with open(os.path.join(projects_path, lang_pair, question_path), 'r', encoding='utf-8') as input_file:
35 question = input_file.read()
36 query_func = question[len("<Target function>\n"):question.find("</Target function>")]
37
38 while True:
39
40 try:
41 # match function
42 message = f"You are a professional who is expert in programming language {query_lang} and programming language {corpus_lang}. You will be provided with 1 Target function written in {query_lang} and 10 Possible matching functions written in {corpus_lang}(delimited with XML tags).Please select a function that has the same functionality as the Target function from 10 Possible matching functions.You should only response the serial number of the matching function or \"None\" if it doesn't exit.\n{question}"
43
44 response = generation(message)
45
46 # print(f"successfully get {question_path}")
47 with open(os.path.join(target_dir, project, lang_pair, question_path), 'w', encoding='utf-8', errors='ignore') as output_file:
48 # match
49 if response == "None":
50 output_file.write("None")
51
52 else:
53 start = question.find(f"<Function {response}>") + len(f"<Function {response}>\n")
54 end = question.find(f"</Function {response}>")
55 output_file.write(query_func)
56 output_file.write("------\n")
57 output_file.write(question[start:end])
58 break
59
60 except Exception as e:
61 logging.error(f"error with {lang_pair} {question_path}, detail is: {e}")
62 print(f"error with {lang_pair} {question_path}, detail is: {e}")
63 time.sleep(10)
64 break
65
66
67
68if __name__ == "__main__":
69 projects_path = os.path.join(source_dir, project) # 替换为实际的文件路径
70 process(projects_path, project, target_dir)
71 