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
|
from argparse import ArgumentParser
from os import path, listdir, makedirs
parser = ArgumentParser()
parser.add_argument('-b', '--bugs', required = True)
parser.add_argument('-d', '--search_directory', required = True)
parser.add_argument('-o', '--output')
args = parser.parse_args()
def list_files_recursive(directory, basename = False):
result = []
for entry in listdir(directory):
full_path = path.join(directory, entry)
if path.isdir(full_path):
result = result + list_files_recursive(full_path, basename)
else:
if basename:
result.append(path.basename(full_path))
else:
result.append(full_path)
return result
def output_csv(dictionary, full_path):
with open(path.join(full_path, 'analysis.csv'), "w") as file:
file.write("category, count\n")
for key, value in dictionary.items():
file.write(f"{key}, {value}\n")
def duplicate_bug(file_path, category):
output_path = path.join(args.output, category)
makedirs(output_path, exist_ok = True)
with open(file_path, "r") as file:
text = file.read()
with open(path.join(output_path, path.basename(file_path)), "w") as file:
file.write(text)
def main():
result = {}
mistranslation_bugs = list_files_recursive(args.bugs, True)
bugs = list_files_recursive(args.search_directory, False)
for mistranslation_bug in mistranslation_bugs:
for bug in bugs:
if mistranslation_bug == path.basename(bug):
category = path.basename(path.dirname(bug))
if args.output:
duplicate_bug(bug, category)
if category in result:
result[category] += 1
else:
result[category] = 1
continue
output_csv(result, args.search_directory)
if __name__ == "__main__":
main()
|