"""Issue-tracker triage on the NLBSE'23 issue report classification set (142,320 real GitHub issues labelled bug,
feature, question or documentation). Time-ordered split on the global GitHub issue id."""
import json, re, numpy as np, pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score, precision_recall_fscore_support, confusion_matrix
d = pd.read_csv('/workspace/cs7/data/nlbse23-issue-classification-test.csv').dropna(subset=['title'])
d['body'] = d.body.fillna(''); d = d.sort_values('id').reset_index(drop=True)
def clean(b):
    b = re.sub(r'```.*?```', ' CODEBLOCK ', b, flags=re.S); b = re.sub(r'https?://\S+', ' URL ', b); b = re.sub(r'<!--.*?-->', ' ', b, flags=re.S)
    return b[:2000]
d['text'] = d.title + ' . ' + d.title + ' . ' + d.body.map(clean)
cut = int(len(d) * 0.8); tr, te = d.iloc[:cut], d.iloc[cut:]
L = ['bug', 'feature', 'question', 'documentation']
vec = TfidfVectorizer(ngram_range=(1, 2), min_df=3, max_features=400000, sublinear_tf=True, stop_words=None)
Xtr = vec.fit_transform(tr.text); Xte = vec.transform(te.text)
clf = LogisticRegression(C=4, max_iter=2000, class_weight='balanced'); clf.fit(Xtr, tr.labels)
P = clf.predict_proba(Xte); cl = list(clf.classes_); pred = np.array(cl)[P.argmax(1)]; conf = P.max(1)
# rules a maintainer would write: template words in the title/body
def rule(t, b):
    s = (t + ' ' + b[:600]).lower()
    if re.search(r'\b(docs?|documentation|typo|readme|tutorial)\b', s): return 'documentation'
    if re.search(r'\?\s*$', t.strip()) or re.search(r'^(how|what|why|is there|can i|does)\b', t.lower()): return 'question'
    if re.search(r'\b(feature request|add support|support for|would be nice|proposal|enhancement|allow|option to)\b', s): return 'feature'
    return 'bug'
rb = np.array([rule(t, b) for t, b in zip(te.title, te.body)])
y = te.labels.values; rng = np.random.default_rng(3)
def boot(a):
    m = len(y); v = [f1_score(y[i], a[i], average='macro') for i in (rng.integers(0, m, m) for _ in range(300))]; return [float(np.percentile(v, 2.5)), float(np.percentile(v, 97.5))]
R = dict(n=len(d), n_train=len(tr), n_test=len(te), mix={k: int(v) for k, v in te.labels.value_counts().items()})
for nm, a in [('model', pred), ('rules', rb), ('all_bug', np.array(['bug'] * len(y)))]:
    pr, rc, f1, _ = precision_recall_fscore_support(y, a, labels=L, zero_division=0)
    R[nm] = dict(acc=float((a == y).mean()), macro_f1=float(f1_score(y, a, average='macro', labels=L)), per={l: [float(pr[i]), float(rc[i]), float(f1[i])] for i, l in enumerate(L)})
R['model']['f1_ci'] = boot(pred); R['rules']['f1_ci'] = boot(rb)
R['cm'] = confusion_matrix(y, pred, labels=L).tolist(); R['cm_rules'] = confusion_matrix(y, rb, labels=L).tolist()
# engineering time: questions and docs reaching the bug queue
def leak(a): m = np.isin(y, ['question', 'documentation']); return float((a[m] == 'bug').mean())
R['leak_model'] = leak(pred); R['leak_rules'] = leak(rb); R['leak_none'] = 1.0
# bugs missed (sent away from engineering)
R['bug_recall_model'] = R['model']['per']['bug'][1]; R['bug_recall_rules'] = R['rules']['per']['bug'][1]
cov = []
for th in np.round(np.arange(0.4, 0.96, 0.05), 2):
    m = conf >= th; cov.append([float(th), float(m.mean()), float((pred[m] == y[m]).mean())])
R['coverage'] = cov
th = 0.75; m = conf >= th; R['bar'] = dict(th=th, auto=float(m.mean()), acc=float((pred[m] == y[m]).mean()), held=int((~m).sum()))
# by author association
te2 = te.assign(ok=(pred == y))
R['by_assoc'] = [[k, int(g.shape[0]), float(g.ok.mean())] for k, g in te2.groupby('author_association') if g.shape[0] > 300]
# by body length
te2['blen'] = pd.cut(te2.body.str.len(), [-1, 0, 200, 1000, 5000, 1e9], labels=['No body', 'Under 200 chars', '200 to 1,000', '1,000 to 5,000', 'Over 5,000'])
R['by_len'] = [[str(k), int(g.shape[0]), float(g.ok.mean())] for k, g in te2.groupby('blen', observed=True)]
# learning curve
lc = []
for k in [2000, 5000, 10000, 30000, 60000, len(tr)]:
    c2 = LogisticRegression(C=4, max_iter=2000, class_weight='balanced').fit(Xtr[-k:], tr.labels.values[-k:]); lc.append([k, float(f1_score(y, c2.predict(Xte), average='macro'))])
R['lc'] = lc
fn = np.array(vec.get_feature_names_out()); R['words'] = {c: fn[np.argsort(-clf.coef_[i])[:10]].tolist() for i, c in enumerate(cl)}
json.dump(R, open('/workspace/cs7/out/issues.json', 'w'), indent=1)
print(json.dumps({k: R[k] for k in ['n_train', 'n_test', 'mix', 'leak_model', 'leak_rules', 'bar', 'by_assoc', 'by_len', 'lc']}, indent=0))
for k in ['model', 'rules', 'all_bug']: print(k, R[k]['acc'], R[k]['macro_f1'], R[k]['per'])
print(R['words'])
