File size: 4,382 Bytes
f0f4f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import logging
import time
import sys
import re

from generation import generation

source_dir = sys.argv[1]
target_dir = sys.argv[2]
llm = sys.argv[3]
dependencies_path = sys.argv[4]

# 设置日志配置
logging.basicConfig(filename=f"translate_throughLLM_{llm}.log", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def process(projects_path, project, target_dir):

    lang_pairs = os.listdir(projects_path)
    for lang_pair in lang_pairs:
        # print(lang_pair)
        if not os.path.exists(os.path.join(target_dir, project, lang_pair)):
            os.makedirs(os.path.join(target_dir, project, lang_pair))
        query_lang = lang_pair.split("__")[0]
        corpus_lang = lang_pair.split("__")[1]

        questions_path = os.listdir(os.path.join(projects_path, lang_pair))
        for question_path in questions_path:

            if os.path.exists(os.path.join(target_dir, project, lang_pair, question_path)):
                logging.info(f"{lang_pair}'s {question_path} already exists")
                continue
            else:
                print(os.path.join(target_dir, project, lang_pair, question_path))

            # translte
            with open(os.path.join(projects_path, lang_pair, question_path), 'r', encoding='utf-8',  errors='ignore') as input_file:
                question = input_file.read().split("------")

                pattern = r'<function>(.*?)</function>'
                query_func = re.findall(pattern, question[0], re.DOTALL)[0].strip()

                corpus_func = re.findall(pattern, question[1], re.DOTALL)[0].strip()
                
                query_func_signature = query_func.split("{")[0]

            # with use package
            try:
                with open(os.path.join(dependencies_path, project, lang_pair, "rust", question_path), 'r', encoding='utf-8',  errors='ignore') as input_file:
                    content = input_file.read()

                content = content.split("------")
                related_function_and_datatype = content[0]
                use_package = content[1]
            except:
                logging.info(f"{lang_pair}'s {question_path} without dependencies file")
                continue
            
            while True:
                try:
                    message = f"Translate the given {corpus_lang} function to {query_lang} according to the {query_lang} function signature, {query_lang} function dependencies(including function and variable dependencies), and data type declarations and {query_lang} function dependency libraries I provide(delimited with XML tags).Make sure to call the relevant dependencies as much as possible in the translated function Only response the translated function results.\n<{corpus_lang} function>\n{corpus_func}\n</{corpus_lang} function>\n<{query_lang} function signature>\n{query_func_signature}\n</{query_lang} function signature>\n<{query_lang} function dependencies, and data type declarations>\n{related_function_and_datatype}\n</{query_lang} function dependencies and data type declarations>\n<{query_lang} function dependency libraries>{use_package}\n</{query_lang} function dependency libraries>\n"

                    response = generation(message)

                    print(f"successfully get {question_path}")
                    with open(os.path.join(target_dir, project, lang_pair, question_path), 'w', encoding='utf-8', errors='ignore') as output_file:
                        # translate
                        output_file.write(f"<message>\n{message}\n</message>\n")
                        output_file.write(f"<function>\n{query_func}\n</function>\n<translated function>\n{response}</translated function>")
                        output_file.write(response)
                    break
                
                except Exception as e:
                    logging.error(f"error with {lang_pair} {question_path}, detail is: {e}")
                    print(f"error with {lang_pair} {question_path}, detail is: {e}")
                    time.sleep(10)
                    break



if __name__ == "__main__":

    projects = os.listdir(source_dir)
    for project in projects:
        process(os.path.join(source_dir, project), project, os.path.join(target_dir, f"translate_by_{llm}"))