|
- <%
- '-----------------------------------------------------------------------------------------------------------------------
- ' Consolidation of experiences (spec section 12: candidates without server-side generative inference; AC-16).
- ' Groups the experiences of one namespace that describe the same situation and assembles a draft candidate per group.
- '
- ' Similarity is lexical and deterministic: each experience becomes the set of its significant words (task, problem,
- ' action, result; lower case, stopwords and words shorter than 3 characters dropped, a plural "s" stripped). Words that
- ' occur in more than half of the experiences carry no signal and are ignored. Two experiences are similar when they
- ' share at least 2 words and their Jaccard similarity (shared / union) reaches the threshold; groups are the connected
- ' components of that relation (union-find). The draft only rearranges the experiences' own text (what failed, what
- ' worked); it is a starting point for a reviewer, never accepted automatically.
- '
- ' Usage: Set c = New Consolidator_Class : c.Load tenantId, nsId, since, maxRows : c.Cluster 0.35 : groups = c.Groups(2)
- ' Each group is a comma-separated list of row indexes (newest first); use the accessors and Draft* functions on it.
- '-----------------------------------------------------------------------------------------------------------------------
- Const CONS_MAX_GROUPS = 20
- Const CONS_MIN_SHARED = 2
- Const CONS_DRAFT_LINES = 6
-
- Class Consolidator_Class
- Private m_n, m_id(), m_session(), m_task(), m_problem(), m_action(), m_result(), m_outcome(), m_created()
- Private m_tokens(), m_len(), m_parent()
- Private m_df, m_re
-
- Private Sub Class_Initialize()
- m_n = 0
- Set m_df = Server.CreateObject("Scripting.Dictionary")
- Set m_re = New RegExp
- m_re.Pattern = "[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+"
- m_re.Global = True
- End Sub
-
- Public Property Get Count()
- Count = m_n
- End Property
-
- Public Function Id(i)
- Id = m_id(i)
- End Function
-
- Public Function SessionId(i)
- SessionId = m_session(i)
- End Function
-
- Public Function Outcome(i)
- Outcome = m_outcome(i)
- End Function
-
- Public Function Created(i)
- Created = m_created(i)
- End Function
-
- ' Loads up to maxRows experiences of the namespace created at or after `since` (text timestamp), newest first.
- Public Sub Load(tenantId, nsId, since, maxRows)
- Dim rs, rows, i, t
- m_n = 0
- Set rs = DAL().Query("SELECT id, session_id, task, problem, action, result, outcome, created_at FROM experiences " & _
- "WHERE tenant_id = ? AND namespace_id = ? AND created_at >= ? ORDER BY created_at DESC, id LIMIT " & CLng(maxRows), _
- Array(tenantId, nsId, since))
- rows = Empty
- If Not rs.EOF Then
- rows = rs.GetRows()
- End If
- rs.Close
- If Not IsArray(rows) Then
- Exit Sub
- End If
- m_n = UBound(rows, 2) + 1
- ReDim m_id(m_n - 1)
- ReDim m_session(m_n - 1)
- ReDim m_task(m_n - 1)
- ReDim m_problem(m_n - 1)
- ReDim m_action(m_n - 1)
- ReDim m_result(m_n - 1)
- ReDim m_outcome(m_n - 1)
- ReDim m_created(m_n - 1)
- ReDim m_tokens(m_n - 1)
- ReDim m_len(m_n - 1)
- For i = 0 To m_n - 1
- m_id(i) = "" & rows(0, i)
- m_session(i) = "" & rows(1, i)
- m_task(i) = "" & rows(2, i)
- m_problem(i) = "" & rows(3, i)
- m_action(i) = "" & rows(4, i)
- m_result(i) = "" & rows(5, i)
- m_outcome(i) = "" & rows(6, i)
- m_created(i) = "" & rows(7, i)
- Set m_tokens(i) = Tokens(m_task(i) & " " & m_problem(i) & " " & m_action(i) & " " & m_result(i))
- For Each t In m_tokens(i).Keys
- If m_df.Exists(t) Then
- m_df.Item(t) = m_df.Item(t) + 1
- Else
- m_df.Add t, 1
- End If
- Next
- Next
- End Sub
-
- Private Function Tokens(text)
- Dim d, parts, i, t
- Set d = Server.CreateObject("Scripting.Dictionary")
- parts = Split(m_re.Replace(LCase(text), " "), " ")
- For i = 0 To UBound(parts)
- t = parts(i)
- If Len(t) >= 3 Then
- If InStr(SEARCH_STOPWORDS, " " & t & " ") = 0 Then
- If Len(t) > 4 Then
- If Right(t, 1) = "s" And Right(t, 2) <> "ss" Then
- t = Left(t, Len(t) - 1)
- End If
- End If
- If Not d.Exists(t) Then
- d.Add t, True
- End If
- End If
- End If
- Next
- Set Tokens = d
- End Function
-
- ' Builds the groups for a Jaccard threshold (0..1).
- Public Sub Cluster(threshold)
- Dim post, maxDf, i, j, k, t, ids, counts, shared, jac
- If m_n = 0 Then
- Exit Sub
- End If
- ReDim m_parent(m_n - 1)
- For i = 0 To m_n - 1
- m_parent(i) = i
- Next
- maxDf = m_n \ 2
- If maxDf < 2 Then
- maxDf = 2
- End If
- Set post = Server.CreateObject("Scripting.Dictionary")
- For i = 0 To m_n - 1
- m_len(i) = 0
- For Each t In m_tokens(i).Keys
- If m_df.Item(t) <= maxDf Then
- m_len(i) = m_len(i) + 1
- If post.Exists(t) Then
- post.Item(t) = post.Item(t) & i & ","
- Else
- post.Add t, i & ","
- End If
- End If
- Next
- Next
- For i = 0 To m_n - 1
- Set counts = Server.CreateObject("Scripting.Dictionary")
- For Each t In m_tokens(i).Keys
- If post.Exists(t) Then
- ids = Split(post.Item(t), ",")
- For k = 0 To UBound(ids) - 1
- j = CLng(ids(k))
- If j > i Then
- If counts.Exists(j) Then
- counts.Item(j) = counts.Item(j) + 1
- Else
- counts.Add j, 1
- End If
- End If
- Next
- End If
- Next
- For Each j In counts.Keys
- shared = counts.Item(j)
- If shared >= CONS_MIN_SHARED Then
- jac = shared / (m_len(i) + m_len(j) - shared)
- If jac >= threshold Then
- Union i, j
- End If
- End If
- Next
- Set counts = Nothing
- Next
- End Sub
-
- Private Function Find(i)
- Dim r, nxt, cur
- r = i
- Do While m_parent(r) <> r
- r = m_parent(r)
- Loop
- cur = i
- Do While m_parent(cur) <> r
- nxt = m_parent(cur)
- m_parent(cur) = r
- cur = nxt
- Loop
- Find = r
- End Function
-
- Private Sub Union(a, b)
- Dim ra, rb
- ra = Find(a)
- rb = Find(b)
- If ra <> rb Then
- If ra < rb Then
- m_parent(rb) = ra
- Else
- m_parent(ra) = rb
- End If
- End If
- End Sub
-
- ' Groups of at least minSize experiences, largest first (ties: most recent first), at most CONS_MAX_GROUPS.
- ' Returns an array of "i,j,k" strings (row indexes, newest first), or an empty array.
- Public Function Groups(minSize)
- Dim byRoot, i, r, keys, n, out(), sizes(), a, b, tmp, tsz, cnt
- Groups = Array()
- If m_n = 0 Then
- Exit Function
- End If
- Set byRoot = Server.CreateObject("Scripting.Dictionary")
- For i = 0 To m_n - 1
- r = Find(i)
- If byRoot.Exists(r) Then
- byRoot.Item(r) = byRoot.Item(r) & "," & i
- Else
- byRoot.Add r, CStr(i)
- End If
- Next
- n = 0
- ReDim out(byRoot.Count)
- ReDim sizes(byRoot.Count)
- For Each r In byRoot.Keys
- cnt = UBound(Split(byRoot.Item(r), ",")) + 1
- If cnt >= minSize Then
- out(n) = byRoot.Item(r)
- sizes(n) = cnt
- n = n + 1
- End If
- Next
- If n = 0 Then
- Exit Function
- End If
- ' Selection sort by size (descending); rows are newest first, so a smaller first index is more recent.
- For a = 0 To n - 2
- For b = a + 1 To n - 1
- If sizes(b) > sizes(a) Or (sizes(b) = sizes(a) And FirstIndex(out(b)) < FirstIndex(out(a))) Then
- tmp = out(a)
- out(a) = out(b)
- out(b) = tmp
- tsz = sizes(a)
- sizes(a) = sizes(b)
- sizes(b) = tsz
- End If
- Next
- Next
- If n > CONS_MAX_GROUPS Then
- n = CONS_MAX_GROUPS
- End If
- ReDim Preserve out(n - 1)
- Groups = out
- End Function
-
- Private Function FirstIndex(group)
- FirstIndex = CLng(Left(group, InStr(group & ",", ",") - 1))
- End Function
-
- ' The n words shared by most members of the group (ties: rarer overall first, then alphabetical).
- Public Function TopTerms(group, n)
- Dim members, i, t, freq, keys, a, b, tmp, out, cnt, take
- members = Split(group, ",")
- Set freq = Server.CreateObject("Scripting.Dictionary")
- For i = 0 To UBound(members)
- For Each t In m_tokens(CLng(members(i))).Keys
- If freq.Exists(t) Then
- freq.Item(t) = freq.Item(t) + 1
- Else
- freq.Add t, 1
- End If
- Next
- Next
- keys = freq.Keys
- cnt = UBound(keys) + 1
- For a = 0 To cnt - 2
- For b = a + 1 To cnt - 1
- If Better(keys(b), keys(a), freq) Then
- tmp = keys(a)
- keys(a) = keys(b)
- keys(b) = tmp
- End If
- Next
- Next
- out = ""
- take = 0
- For a = 0 To cnt - 1
- If take >= n Then
- Exit For
- End If
- If freq.Item(keys(a)) >= 2 Then
- If Len(out) > 0 Then
- out = out & ","
- End If
- out = out & keys(a)
- take = take + 1
- End If
- Next
- TopTerms = out
- End Function
-
- Private Function Better(x, y, freq)
- Better = False
- If freq.Item(x) > freq.Item(y) Then
- Better = True
- ElseIf freq.Item(x) = freq.Item(y) Then
- If m_df.Item(x) < m_df.Item(y) Then
- Better = True
- ElseIf m_df.Item(x) = m_df.Item(y) Then
- Better = (x < y)
- End If
- End If
- End Function
-
- ' success|failure|partial|unknown counts of a group, as "s,f,p,u"
- Public Function OutcomeCounts(group)
- Dim members, i, s, f, p, u
- members = Split(group, ",")
- s = 0
- f = 0
- p = 0
- u = 0
- For i = 0 To UBound(members)
- Select Case m_outcome(CLng(members(i)))
- Case "success"
- s = s + 1
- Case "failure"
- f = f + 1
- Case "partial"
- p = p + 1
- Case Else
- u = u + 1
- End Select
- Next
- OutcomeCounts = s & "," & f & "," & p & "," & u
- End Function
-
- Public Function SessionCount(group)
- Dim members, i, d
- members = Split(group, ",")
- Set d = Server.CreateObject("Scripting.Dictionary")
- For i = 0 To UBound(members)
- If Not d.Exists(m_session(CLng(members(i)))) Then
- d.Add m_session(CLng(members(i))), True
- End If
- Next
- SessionCount = d.Count
- End Function
-
- ' "solution" when every member succeeded, otherwise "lesson" (failures and what eventually worked).
- Public Function DraftType(group)
- Dim oc
- oc = Split(OutcomeCounts(group), ",")
- If CLng(oc(0)) = UBound(Split(group, ",")) + 1 Then
- DraftType = "solution"
- Else
- DraftType = "lesson"
- End If
- End Function
-
- Public Function DraftTitle(group)
- Dim t
- t = Trim(m_task(FirstIndex(group)))
- If Len(t) > 180 Then
- t = Left(t, 177) & "..."
- End If
- DraftTitle = "Recurring: " & t
- End Function
-
- ' Plain text built only from the members' own fields: tasks, then what failed and what worked (newest first).
- Public Function DraftContent(group)
- Dim members, i, k, oc, sb, worked, failed, nw, nf, lastSeen, firstSeen, line
- members = Split(group, ",")
- oc = Split(OutcomeCounts(group), ",")
- lastSeen = m_created(CLng(members(0)))
- firstSeen = m_created(CLng(members(UBound(members))))
- worked = ""
- failed = ""
- nw = 0
- nf = 0
- For i = 0 To UBound(members)
- k = CLng(members(i))
- line = Excerpt(m_action(k), 200)
- If Len(m_result(k)) > 0 Then
- line = line & " -> " & Excerpt(m_result(k), 200)
- End If
- If Len(m_problem(k)) > 0 And Len(m_action(k)) = 0 Then
- line = Excerpt(m_problem(k), 200) & line
- End If
- line = "- " & line & " (" & Left(m_created(k), 10) & ")"
- If m_outcome(k) = "success" Then
- If nw < CONS_DRAFT_LINES Then
- worked = worked & line & vbLf
- End If
- nw = nw + 1
- ElseIf m_outcome(k) = "failure" Or m_outcome(k) = "partial" Then
- If nf < CONS_DRAFT_LINES Then
- failed = failed & line & vbLf
- End If
- nf = nf + 1
- End If
- Next
- sb = "Seen " & (UBound(members) + 1) & " times in " & SessionCount(group) & " session(s) between " & firstSeen & " and " & lastSeen & _
- " (" & oc(0) & " succeeded, " & oc(1) & " failed, " & oc(2) & " partial, " & oc(3) & " unknown)." & vbLf & vbLf & _
- "Task: " & Excerpt(m_task(CLng(members(0))), 300) & vbLf
- If Len(m_problem(CLng(members(0)))) > 0 Then
- sb = sb & "Problem: " & Excerpt(m_problem(CLng(members(0))), 300) & vbLf
- End If
- If nf > 0 Then
- sb = sb & vbLf & "What did not work:" & vbLf & failed
- If nf > CONS_DRAFT_LINES Then
- sb = sb & "- ... and " & (nf - CONS_DRAFT_LINES) & " more" & vbLf
- End If
- End If
- If nw > 0 Then
- sb = sb & vbLf & "What worked:" & vbLf & worked
- If nw > CONS_DRAFT_LINES Then
- sb = sb & "- ... and " & (nw - CONS_DRAFT_LINES) & " more" & vbLf
- End If
- End If
- sb = sb & vbLf & "Common terms: " & Replace(TopTerms(group, 6), ",", ", ") & "." & vbLf & vbLf & _
- "Draft assembled automatically from the linked experiences. Check it, and generalise it, before accepting."
- DraftContent = sb
- End Function
-
- Private Function Excerpt(s, n)
- Dim t
- t = Trim(Replace(Replace("" & s, vbCr, " "), vbLf, " "))
- If Len(t) > n Then
- t = Left(t, n - 3) & "..."
- End If
- Excerpt = t
- End Function
- End Class
- %>
|