-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbm25.py
More file actions
291 lines (239 loc) · 12.2 KB
/
bm25.py
File metadata and controls
291 lines (239 loc) · 12.2 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import math
import logging
import threading
from typing import List
from api import GET_API
# from api_tree_sitter import TreeSitterExtractor
from functools import partial
from datasets import CodeBlock
from rank_bm25 import BM25Okapi
from multiprocessing import Pool
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def process_example(example, language):
try:
api_processor = GET_API([example], language)
return api_processor.get()[0]
except Exception as e:
return ""
def process_example_tree_sitter(example, language):
api_processor = TreeSitterExtractor([example], language)
return api_processor.get()[0]
def split_into_smaller_blocks(code_block, enable_fixed_block):
"""
Split large blocks of code into smaller ones, each containing no more than 12 non-empty lines.
"""
smaller_blocks = []
if enable_fixed_block:
lines = [line for line in code_block.code_content.split('\n') if line.strip() != '']
for i in range(0, min(len(lines), 5000), 12):
start_line_offset = i
end_line_offset = min(i + 12, len(lines))
block_content = '\n'.join(lines[start_line_offset:end_line_offset])
smaller_blocks.append(CodeBlock(code_block.file_path, f"file path: {code_block.file_path}\nlines: {start_line_offset}-{end_line_offset - 1}", block_content, code_block.language, 'fixed_block'))
else:
mini_blocks = []
current_block = []
for line in code_block.code_content.splitlines():
if line.strip() == '':
if current_block:
mini_blocks.append(current_block)
current_block = []
else:
current_block.append(line)
if current_block:
mini_blocks.append(current_block)
max_len = 15
temp_mini_blocks = []
for mini_block in mini_blocks:
if len(mini_block) > max_len:
for idx in range(0, len(mini_block), max_len):
temp_mini_blocks.append(mini_block[idx:idx + max_len])
else:
temp_mini_blocks.append(mini_block)
mini_blocks = temp_mini_blocks
current_content = []
total_lines = 0
for block in mini_blocks:
if total_lines >= 5000:
break
if len(current_content) + len(block) <= 15:
current_content.extend(block)
total_lines += len(block)
else:
if current_content:
smaller_blocks.append(CodeBlock(code_block.file_path, f"file path: {code_block.file_path}\nlines: {total_lines - len(current_content) + 1}-{total_lines}", '\n'.join(current_content), code_block.language, 'mini_block'))
current_content = block
total_lines += len(block)
if current_content:
smaller_blocks.append(CodeBlock(code_block.file_path, f"file path: {code_block.file_path}\nlines: {total_lines - len(current_content) + 1}-{total_lines}", '\n'.join(current_content), code_block.language, 'mini_block'))
return smaller_blocks
def split_api_blocks(api_content, language):
"""
Split api blocks of code into smaller ones, each containing no more than 12 non-empty lines.
"""
smaller_blocks = []
mini_blocks = []
current_block = []
for line in api_content.splitlines():
if line.strip() == '':
if current_block:
mini_blocks.append(current_block)
current_block = []
else:
current_block.append(line)
if current_block:
mini_blocks.append(current_block)
max_len = 15
temp_mini_blocks = []
for mini_block in mini_blocks:
if len(mini_block) > max_len:
for idx in range(0, len(mini_block), max_len):
temp_mini_blocks.append(mini_block[idx:idx + max_len])
else:
temp_mini_blocks.append(mini_block)
mini_blocks = temp_mini_blocks
current_content = []
total_lines = 0
for block in mini_blocks:
if len(current_content) + len(block) <= 15:
current_content.extend(block)
total_lines += len(block)
else:
if current_content:
smaller_blocks.append(CodeBlock('file_path', "Potentially invoked APIs:\n", '\n'.join(current_content), language, 'mini_block', True))
current_content = block
total_lines += len(block)
if current_content:
smaller_blocks.append(CodeBlock('file_path', "Potential invoked APIs:\n", '\n'.join(current_content), language, 'mini_block', True))
return smaller_blocks
class TaskSpecificBM25:
def __init__(self, examples, args):
self.bm25_indices = {}
self.code_blocks = {}
self.args = args
self.api_blocks = {}
self.code_blocks_all = {}
self.bm25_indices_all = {}
self._build_indices(examples, args)
def _build_indices(self, examples, args):
num_processes = 32
num_examples_per_batch = math.ceil(len(examples) / num_processes)
example_batches = [examples[i:i + num_examples_per_batch] for i in range(0, len(examples), num_examples_per_batch)]
with Pool(processes=num_processes) as pool:
partial_process_batch = partial(self._process_batch, enable_fixed_block=args.enable_fixed_block)
results = pool.map(partial_process_batch, example_batches)
for batch_result in results:
for task_id, code_blocks, bm25_index in batch_result:
self.bm25_indices[task_id] = bm25_index
self.code_blocks[task_id] = code_blocks
if args.add_api_blocks:
with Pool(processes=64) as pool:
func = partial(process_example, language=examples[0].language)
# api_contents = pool.map(func, examples)
api_contents = pool.map_async(func, examples).get(timeout=1800)
# with Pool(processes=64) as pool:
# func = partial(process_example_tree_sitter, language=examples[0].language)
# # api_contents = pool.map(func, examples)
# api_contents = pool.map_async(func, examples).get(timeout=1800)
# func = partial(process_example, language=examples[0].language)
# api_contents = [func(example) for example in examples]
# for example in examples:
# logging.info(f'task_id:\n{example.task_id}')
# logging.info(f'path:{example.file_path}')
# logging.info(f'left_context:{example.left_context}')
# for file in example.related_files:
# logging.info(file.file_path)
# logging.info(file.code_content)
# logging.info('==============next============')
# for api, example in zip(api_contents, examples):
# logging.info(f'task_id:\n{example.task_id}')
# logging.info(f'api_info1:\n{api}')
for example, api_content in zip(examples, api_contents):
code_blocks_all = self.code_blocks[example.task_id].copy()
api_blocks = []
if api_content != '':
api_blocks.extend(split_api_blocks(api_content, example.language))
code_blocks_all.extend(api_blocks)
bm25_index_all = None
if len(code_blocks) != 0:
bm25_index_all = BM25Okapi([code_block.code_content.lower().split() for code_block in code_blocks_all])
self.api_blocks[example.task_id] = api_blocks
self.code_blocks_all[example.task_id] = code_blocks_all
self.bm25_indices_all[example.task_id] = bm25_index_all
block_len, block_num = 0, 0
for task_id, code_blocks in self.code_blocks.items():
block_num += len(code_blocks)
for code_block in code_blocks:
block_len += len(code_block.code_content.splitlines())
logger.info(f'Block avg line: {round(block_len / block_num, 2)}')
@staticmethod
def _process_batch(batch, enable_fixed_block):
batch_result = []
for example in batch:
code_blocks = []
for code_block in example.related_files:
code_blocks.extend(split_into_smaller_blocks(code_block, enable_fixed_block))
bm25_index = None
if len(code_blocks) != 0:
bm25_index = BM25Okapi([code_block.code_content.lower().split() for code_block in code_blocks])
batch_result.append((example.task_id, code_blocks, bm25_index))
return batch_result
def query(self, task_ids: List[int], queries: List[str], topk: int):
results = []
for task_id, query in zip(task_ids, queries):
bm25_index = self.bm25_indices.get(task_id)
if bm25_index:
query_tokens = query.split()
scores = bm25_index.get_scores(query_tokens)
sorted_indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)
fixed_block_indices, mini_block_indices = [], []
for i in sorted_indices:
block = self.code_blocks[task_id][i]
if block._type == "fixed_block":
fixed_block_indices.append(i)
elif block._type == "mini_block":
mini_block_indices.append(i)
topk_fixed_blocks = [self.code_blocks[task_id][i] for i in fixed_block_indices[:topk]]
topk_mini_blocks = [self.code_blocks[task_id][i] for i in mini_block_indices[:topk]]
if self.args.enable_fixed_block:
task_results = topk_fixed_blocks
else:
task_results = topk_mini_blocks
results.append(task_results)
else:
results.append([])
return results
def query_with_api(self, task_ids: List[int], queries: List[str], topk: int):
results = []
# for k in self.api_blocks:
# logging.info(len(self.api_blocks[k]))
for task_id, query in zip(task_ids, queries):
bm25_index_all = self.bm25_indices_all.get(task_id)
if bm25_index_all:
query_tokens = query.split()
scores = bm25_index_all.get_scores(query_tokens)
sorted_indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)
fixed_block_indices, mini_block_indices = [], []
api_block_numbers = 0
for i in sorted_indices:
block = self.code_blocks_all[task_id][i]
if block._type == "fixed_block":
fixed_block_indices.append(i)
elif block._type == "mini_block":
if not block._api_block:
mini_block_indices.append(i)
elif block._api_block and api_block_numbers < 2:
mini_block_indices.append(i)
api_block_numbers += 1
topk_fixed_blocks = [self.code_blocks_all[task_id][i] for i in fixed_block_indices[:topk]]
topk_mini_blocks = [self.code_blocks_all[task_id][i] for i in mini_block_indices[:topk]]
if self.args.enable_fixed_block:
task_results = topk_fixed_blocks
else:
task_results = topk_mini_blocks
results.append(task_results)
else:
results.append([])
return results