xruben136x / SZZ_unisannio

Implementation of SZZ Algorithm
Mozilla Public License 2.0
4 stars 0 forks source link

Integration of ChatGPT code to ignore single comment lines #4

Closed GuidoAnnicchiarico closed 10 months ago

GuidoAnnicchiarico commented 10 months ago

QUESTION: concentrati sulla funzione generate_chages_dict(diff_output) e fai in modo che non consideri il numero delle linee che sono commenti. Per farlo crea una nuova funzione che ,tramite un'espressione regolare, riesca a determinare se una linea è un commento o no. Le linee di commento possono iniziare per i seguenti simboli : #, /, // <!--, preceduti da zero o più spazi bianchi. Una linea è considerata di commento anche nel caso in cui ci sia qualsiasi carattere e la linea termina per --!> oppure /, seguiti da zero o più spazi bianchi. Quando confronti line con l’espressione regolare salta il primo carattere. Se una linea è di commento, non deve essere aggiunto il suo numero di linea alla lista numbers_list.

get the dictionary where the key is the file path and the value is a list of numbers of the changed lines

def generate_changes_dict(diff_output): file_path_pattern = re.compile(r'^+++ b/(.*)$') line_number_pattern = re.compile(r'^@@ -(\d+)(,(\d+))? +(\d+)(,(\d+))? @@')

result_dict = {}
current_file_path = None
numbers_list = []

diff_lines = diff_output.split('\n')

for line in diff_lines:
    file_path_match = file_path_pattern.match(line)
    line_number_match = line_number_pattern.match(line)

    if file_path_match:
        if current_file_path and numbers_list:
            result_dict[current_file_path] = numbers_list
            numbers_list = []

        current_file_path = file_path_match.group(1)
    elif line_number_match:
        start_line = int(line_number_match.group(1))
        num_lines = 1 if line_number_match.group(3) is None else int(line_number_match.group(3))                           

       numbers_list.extend(range(start_line, start_line + num_lines))

if current_file_path and numbers_list:
    result_dict[current_file_path] = numbers_list

return result_dict

ANSWER: Per implementare la funzione match_comment che verifica se una linea è un commento o meno, puoi utilizzare il seguente codice. def match_comment(line):

Regular expression pattern for matching comment lines

comment_pattern = re.compile(r'^\s*(#|/\*|//|<!--).*?(\*\/|--!>)?\s*$')

return bool(comment_pattern.match(line[1:]))

Aggiorna la funzione generate_changes_dict

def generate_changes_dict(diff_output): file_path_pattern = re.compile(r'^+++ b/(.*)$') line_number_pattern = re.compile(r'^@@ -(\d+)(,(\d+))? +(\d+)(,(\d+))? @@')

result_dict = {}
current_file_path = None
numbers_list = []

diff_lines = diff_output.split('\n')

for line in diff_lines:
    file_path_match = file_path_pattern.match(line)
    line_number_match = line_number_pattern.match(line)

    if file_path_match:
        if current_file_path and numbers_list:
            result_dict[current_file_path] = numbers_list
            numbers_list = []

        current_file_path = file_path_match.group(1)
    elif line_number_match:
        start_line = int(line_number_match.group(1))
        num_lines = 1 if line_number_match.group(3) is None else int(line_number_match.group(3))

        # Aggiungi le linee modificate solo se non sono commenti
        if not match_comment(line):
            numbers_list.extend(range(start_line, start_line + num_lines))

if current_file_path and numbers_list:
    result_dict[current_file_path] = numbers_list

return result_dict