lynn-twinkl commited on
Commit
0944554
·
1 Parent(s): 3679914

Now assigns usage score based on lenght of line items

Browse files
Files changed (1) hide show
  1. functions/shortlist.py +33 -10
functions/shortlist.py CHANGED
@@ -4,13 +4,14 @@ def shortlist_applications(
4
  df: pd.DataFrame,
5
  k: int = None,
6
  threshold: float = None,
7
- weight_necessity: float = 0.5,
8
- weight_length: float = 0.2,
9
- weight_usage: float = 0.3
10
  ) -> pd.DataFrame:
11
  """
12
  Automatically shortlist grant applications by combining necessity index,
13
- application length (favoring longer submissions), and whether usage was specified.
 
14
 
15
  Args:
16
  df: Processed DataFrame including columns 'necessity_index', 'word_count', and 'Usage'.
@@ -18,7 +19,7 @@ def shortlist_applications(
18
  threshold: Score threshold above which to select applications. Mutually exclusive with k.
19
  weight_necessity: Weight for necessity_index (0 to 1).
20
  weight_length: Weight for length score (0 to 1).
21
- weight_usage: Weight for usage inclusion (0 to 1).
22
 
23
  Returns:
24
  DataFrame of shortlisted applications sorted by descending combined score.
@@ -38,14 +39,36 @@ def shortlist_applications(
38
  else:
39
  length_score = pd.Series([0.5] * len(df), index=df.index)
40
 
41
- # Compute usage score: 1 if any usage items specified, else 0
42
- def has_usage(items):
43
- return any(
44
- item and isinstance(item, str) and item.strip().lower() != 'none'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  for item in items
 
46
  )
47
 
48
- usage_score = df['Usage'].apply(has_usage).astype(float)
 
 
 
 
 
 
 
49
 
50
  # Combine scores with normalized weights
51
  total_weight = weight_necessity + weight_length + weight_usage
 
4
  df: pd.DataFrame,
5
  k: int = None,
6
  threshold: float = None,
7
+ weight_necessity: float = 0.55,
8
+ weight_length: float = 0.1,
9
+ weight_usage: float = 0.35
10
  ) -> pd.DataFrame:
11
  """
12
  Automatically shortlist grant applications by combining necessity index,
13
+ application length (favoring longer submissions), and the specificity of the
14
+ requested usage list.
15
 
16
  Args:
17
  df: Processed DataFrame including columns 'necessity_index', 'word_count', and 'Usage'.
 
19
  threshold: Score threshold above which to select applications. Mutually exclusive with k.
20
  weight_necessity: Weight for necessity_index (0 to 1).
21
  weight_length: Weight for length score (0 to 1).
22
+ weight_usage: Weight for usage specificity (0 to 1).
23
 
24
  Returns:
25
  DataFrame of shortlisted applications sorted by descending combined score.
 
39
  else:
40
  length_score = pd.Series([0.5] * len(df), index=df.index)
41
 
42
+ # Compute usage score based on *how many* concrete usage items were extracted
43
+ # (previously this was a simple binary flag). Longer lists are taken as a
44
+ # signal of greater specificity → higher score. We first count the number
45
+ # of non‑empty items, then min‑max normalise the counts so the resulting
46
+ # score is between 0 and 1 (mirroring the approach used for
47
+ # `length_score`).
48
+
49
+ def count_valid_usage(items):
50
+ """Return the number of meaningful usage entries in *items*.
51
+
52
+ The Usage column is expected to contain a list of strings (output of
53
+ `extract_usage.extract_usage`). We treat empty strings and the literal
54
+ "None" (case‑insensitive) as non‑entries.
55
+ """
56
+ if not isinstance(items, (list, tuple, set)):
57
+ return 0
58
+ return sum(
59
+ 1
60
  for item in items
61
+ if isinstance(item, str) and item.strip() and item.strip().lower() != "none"
62
  )
63
 
64
+ usage_counts = df["Usage"].apply(count_valid_usage)
65
+
66
+ min_uc, max_uc = usage_counts.min(), usage_counts.max()
67
+ if max_uc != min_uc:
68
+ usage_score = (usage_counts - min_uc) / (max_uc - min_uc)
69
+ else:
70
+ # If all rows have identical counts (e.g. all zero), assign a neutral 0.5
71
+ usage_score = pd.Series([0.5] * len(df), index=df.index)
72
 
73
  # Combine scores with normalized weights
74
  total_weight = weight_necessity + weight_length + weight_usage