Debian Codegraph @

proselint/score.py

raw
1 """Compute the lintscore on the corpus."""
2
3
4 import os
5 import re
6 import subprocess
7
8 proselint_path = os.path.dirname(os.path.realpath(__file__))
9
10
11 def score(check=None):
12 """Compute the linter's score on the corpus.
13
14 Proselint's score reflects the desire to have a linter that catches many
15 errors, but which takes false alarms seriously. It is better not to say
16 something than to say the wrong thing, and the harm from saying the wrong
17 thing is greater than the benefit of saying the right thing. Thus our score
18 metric is defined as:
19
20 TP * (TP / (FP + TP)) ^ k,
21
22 where TP is the number of true positives (hits), FP is the number of
23 false positives (false alarms), and k > 0 is a temperature parameter that
24 determines the penalty for imprecision. In general, we should choose a
25 large value of k, one that strongly discourages the creation of rules that
26 can't be trusted. Suppose that k = 2. Then if the linter detects 100
27 errors, of which 10 are false positives, the score is 81.
28 """
29 tp = 0
30 fp = 0
31
32 parent_directory = os.path.dirname(proselint_path)
33 path_to_corpus = os.path.join(parent_directory, "corpora", "0.1.0")
34 for root, _, files in os.walk(path_to_corpus):
35 files = [f for f in files if f.endswith(".md")]
36 for f in files:
37 fullpath = os.path.join(root, f)
38
39 # Run the linter.
40 print(f"Linting {f}")
41 out = subprocess.check_output(["proselint", fullpath])
42
43 # Determine the number of errors.
44 regex = r".+?:(?P<line>\d+):(?P<col>\d+): (?P<message>.+)"
45 num_errors = len(tuple(re.finditer(regex, out)))
46 print(f"Found {num_errors} errors.")
47
48 # Open the document.
49 subprocess.call(["open", fullpath])
50
51 # Ask the scorer how many of the errors were false alarms?
52 input_val = None
53 while not isinstance(input_val, int):
54 try:
55 input_val = input("# of false alarms? ")
56 if input_val == "exit":
57 return
58 else:
59 input_val = int(input_val)
60 fp += input_val
61 tp += (num_errors - input_val)
62 except ValueError:
63 pass
64
65 print(f"Currently {tp} hits and {fp} false alarms\n---")
66
67 if (tp + fp) > 0:
68 return tp * (1.0 * tp / (tp + fp)) ** 2
69 else:
70 return 0