summary refs log tree commit diff stats
path: root/classification/main.py
blob: 3f16a2818c0b22928eb8959ad750937607d9ebcf (plain) (blame)
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
from transformers import pipeline
from os import path, listdir, makedirs

positive_categories = ['semantic', 'mistranslation', 'instruction', 'assembly'] # to add: register
negative_categories = ['other', 'boot', 'network', 'KVM', 'vnc', 'graphic', 'device', 'socket'] # to add: performance
categories = positive_categories + negative_categories

def list_files_recursive(directory):
    result = []
    for entry in listdir(directory):
        full_path = path.join(directory, entry)
        if path.isdir(full_path):
            result = result + list_files_recursive(full_path)
        else:
            result.append(full_path)
    return result

def output(text : str, category : str, labels : list, scores : list, identifier : str):
    file_path = f"output/{category}/{identifier}"
    makedirs(path.dirname(file_path), exist_ok = True)

    with open(file_path, "w") as file:
        for label, score in zip(labels, scores):
            file.write(f"{label}: {score:.3f}\n")

        file.write("\n")
        file.write(text)

def main():
    classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

    bugs = list_files_recursive("../results/scraper/mailinglist")
    bugs = bugs + list_files_recursive("./semantic_issues")
    for bug in bugs:
        print(f"Processing {bug}")
        with open(bug, "r") as file:
            text = file.read()

        result = classifier(text, categories, multi_label=True)
        category = result['labels'][0]

        for label, score in zip(result["labels"], result["scores"]):
            if label in negative_categories and score >= 0.92:
                category = label
                break

        output(text, category, result['labels'], result['scores'], path.basename(bug))

if __name__ == "__main__":
    main()