-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfp_extract.py
More file actions
259 lines (229 loc) · 8.45 KB
/
rfp_extract.py
File metadata and controls
259 lines (229 loc) · 8.45 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# rfp_extract.py — multi-file extractor (DOCX + XLSX)
# - Strikte requirement-poort + legal/adres-filter
# - Hybride score (rule + semantic)
# - Governance gating (cues + min similarity)
# - Uitleg fail-safe (chat timeout / fallback)
from __future__ import annotations
import argparse, sys, glob
from pathlib import Path
import pandas as pd
import docx
from utils.helpers import (
normalize_ws,
rule_requirement_score,
is_probably_requirement_text,
looks_like_legal_or_address,
classify_requirement,
detect_document_role,
)
from ai.semantic_classifier import SemanticRequirementClassifier
from ai.semantic_governance import SemanticGovernanceMapper
OUTPUT_COLS = [
"id", "text", "type", "source_file", "source_role",
"cobit", "itil", "iso27001",
"governance_explanation", "governance_confidence"
]
ALLOWED_EXTS = {".docx", ".xlsx"}
def collect_input_files(inputs: list[str]) -> list[Path]:
files : list[Path] = []
for arg in inputs:
p = Path(arg)
if p.is_dir():
files += list(p.glob("*.docx")) + list(p.glob("*.xlsx"))
else:
matches = glob.glob(str(p))
if not matches and p.exists():
matches = [str(p)]
for m in matches:
mp = Path(m)
if mp.suffix.lower() in ALLOWED_EXTS and mp.exists():
files.append(mp)
uniq, seen = [], set()
for f in files:
if f not in seen:
uniq.append(f)
seen.add(f)
return uniq
def parse_docx_file(path: Path, clf: SemanticRequirementClassifier, mapper: SemanticGovernanceMapper):
out = []
try:
document = docx.Document(str(path))
except Exception as e:
print(f"[WARN] Cannot open DOCX: {path} ({e})", file=sys.stderr)
return out
# Document role
header_text = []
for p in document.paragraphs[:15]:
t = normalize_ws(p.text)
if t:
header_text.append(t)
role = detect_document_role("\n".join(header_text), path.name)
# PRICING/LEGAL → skip
if role in {"PRICING", "LEGAL"}:
print(f"[INFO] Skipping requirement extraction for {path.name} (role={role})")
return out
for p in document.paragraphs:
txt = normalize_ws(p.text)
if not txt:
continue
# absolute legal/adres skip
if looks_like_legal_or_address(txt):
continue
# strikte poort
if not is_probably_requirement_text(txt):
continue
# hybride score
rule_s = rule_requirement_score(txt)
sem_s = clf.score(txt)
final = 0.5 * rule_s + 0.5 * sem_s
# strengere drempel voor BR uit RFP_MAIN
rtype = classify_requirement(txt, role_hint=role)
if role == "RFP_MAIN" and rtype == "BR":
if final < 0.65:
continue
else:
if final < 0.55:
continue
gov = mapper.map_text(txt)
out.append({
"id": mapper.hash_id(txt),
"text": txt,
"type": rtype,
"source_file": path.name,
"source_role": role,
"cobit": ", ".join(gov.cobit),
"itil": ", ".join(gov.itil),
"iso27001": ", ".join(gov.iso),
"governance_explanation": gov.explanation,
"governance_confidence": gov.confidence,
})
return out
def _pick_text_column(columns):
cand = {c.lower().strip(): c for c in columns}
priority = ["requirement", "requirements", "req", "requirement_text",
"description", "omschrijving", "beschrijving", "text"]
for key in priority:
for k, v in cand.items():
if key == k:
return v
for k, v in cand.items():
if any(x in k for x in ["require", "desc", "text", "eis"]):
return v
return columns[0] if columns else None
def parse_xlsx_file(path: Path, clf: SemanticRequirementClassifier, mapper: SemanticGovernanceMapper):
out = []
import openpyxl # engine
try:
xls = pd.ExcelFile(str(path), engine="openpyxl")
except Exception as e:
print(f"[WARN] Cannot open XLSX: {path} ({e})", file=sys.stderr)
return out
role = "GENERIC"
try:
df0 = pd.read_excel(xls, sheet_name=xls.sheet_names[0], engine="openpyxl", nrows=100)
sample_text = " ".join([str(x) for x in df0.astype(str).values.flatten().tolist()[:200]])
role = detect_document_role(sample_text, path.name)
except Exception:
pass
if role in {"PRICING", "LEGAL"}:
print(f"[INFO] Skipping requirement extraction for {path.name} (role={role})")
return out
for sheet in xls.sheet_names:
try:
df = pd.read_excel(xls, sheet_name=sheet, engine="openpyxl")
except Exception as e:
print(f"[WARN] Cannot read sheet {path.name}:{sheet} ({e})", file=sys.stderr)
continue
if df.empty:
continue
text_col = _pick_text_column(list(df.columns))
if not text_col:
continue
for _, row in df.iterrows():
txt = normalize_ws(str(row.get(text_col, "")))
if not txt:
continue
if looks_like_legal_or_address(txt):
continue
if not is_probably_requirement_text(txt):
continue
rule_s = rule_requirement_score(txt)
sem_s = clf.score(txt)
final = 0.5 * rule_s + 0.5 * sem_s
rtype = classify_requirement(txt, role_hint=role)
if role == "RFP_MAIN" and rtype == "BR":
if final < 0.65:
continue
else:
if final < 0.55:
continue
gov = mapper.map_text(txt)
out.append({
"id": mapper.hash_id(txt),
"text": txt,
"type": rtype,
"source_file": f"{path.name}:{sheet}",
"source_role": role,
"cobit": ", ".join(gov.cobit),
"itil": ", ".join(gov.itil),
"iso27001": ", ".join(gov.iso),
"governance_explanation": gov.explanation,
"governance_confidence": gov.confidence,
})
return out
def dedupe(records: list[dict]) -> list[dict]:
seen, out = set(), []
for r in records:
key = normalize_ws(r["text"]).lower()
if key in seen:
continue
seen.add(key)
out.append(r)
return out
def export_xlsx(records: list[dict], out_path: Path):
df = pd.DataFrame(records)
for c in OUTPUT_COLS:
if c not in df.columns:
df[c] = ""
df = df[OUTPUT_COLS]
tabs = {
"BR": df[df["type"] == "BR"],
"FR": df[df["type"] == "FR"],
"NFR": df[df["type"] == "NFR"],
"RISKS": df[df["type"] == "RISK"],
"SCOPE_IN": df[df["type"] == "SCOPE_IN"],
"SCOPE_OUT": df[df["type"] == "SCOPE_OUT"],
"SCOPE_OTHER": df[df["type"] == "SCOPE_OTHER"],
"ALL": df,
}
with pd.ExcelWriter(out_path, engine="openpyxl") as writer:
for name, sub in tabs.items():
sub.to_excel(writer, sheet_name=name, index=False)
def main():
parser = argparse.ArgumentParser(description="RFP multi-file extractor (hybrid filter + governance gating)")
parser.add_argument("inputs", nargs="+", help="Files/patterns or a directory (e.g., examples/ or 'examples/*.docx' 'examples/*.xlsx')")
parser.add_argument("--xlsx", default="out.xlsx")
parser.add_argument("--ai", default="on", choices=["on", "off"])
parser.add_argument("--embed-model", default=None)
parser.add_argument("--chat-model", default=None)
args = parser.parse_args()
files = collect_input_files(args.inputs)
if not files:
print("[ERR] No input files found.")
sys.exit(2)
print("[INFO] Files:")
for f in files:
print(f" - {f}")
clf = SemanticRequirementClassifier(embed_model=args.embed_model)
mapper = SemanticGovernanceMapper(embed_model=args.embed_model, chat_model=args.chat_model)
all_recs : list[dict] = []
for f in files:
if f.suffix.lower() == ".docx":
all_recs += parse_docx_file(f, clf, mapper)
elif f.suffix.lower() == ".xlsx":
all_recs += parse_xlsx_file(f, clf, mapper)
merged = dedupe(all_recs)
export_xlsx(merged, Path(args.xlsx))
print(f"[OK] Wrote {args.xlsx} (records={len(merged)})")
if __name__ == "__main__":
main()