Checks whether the generated code is finished.
(
code: str,
language_type: str = None,
dataset: str = None,
)
| 113 | |
| 114 | |
| 115 | def is_code_generation_finished( |
| 116 | code: str, |
| 117 | language_type: str = None, |
| 118 | dataset: str = None, |
| 119 | ): |
| 120 | """ |
| 121 | Checks whether the generated code is finished. |
| 122 | """ |
| 123 | if language_type is None or dataset is None: |
| 124 | return False |
| 125 | |
| 126 | if "humaneval" in dataset.lower(): |
| 127 | if language_type.lower() == "python": |
| 128 | for line in code.split("\n"): |
| 129 | if len(line.strip()) > 0 and line[0] != ' ' and line[0] != '\t': |
| 130 | return True |
| 131 | end_words = ["\ndef", "\nclass", "\nif", "\n#", "\nprint"] |
| 132 | for w in end_words: |
| 133 | if w in code: |
| 134 | return True |
| 135 | elif language_type.lower() == "java": |
| 136 | if code.count("{") + 1 == code.count("}"): |
| 137 | return True |
| 138 | elif language_type.lower() == "go": |
| 139 | if code.count("{") + 1 == code.count("}"): |
| 140 | return True |
| 141 | elif language_type.lower() == "js": |
| 142 | if code.count("{") + 1 == code.count("}"): |
| 143 | return True |
| 144 | elif language_type.lower() == "cpp": |
| 145 | if code.count("{") + 1 == code.count("}"): |
| 146 | return True |
| 147 | |
| 148 | return False |
| 149 | |
| 150 | |
| 151 | def cleanup_code( |
no outgoing calls
no test coverage detected