Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

438 lignes
15KB

  1. <%
  2. '-----------------------------------------------------------------------------------------------------------------------
  3. ' Consolidation of experiences (spec section 12: candidates without server-side generative inference; AC-16).
  4. ' Groups the experiences of one namespace that describe the same situation and assembles a draft candidate per group.
  5. '
  6. ' Similarity is lexical and deterministic: each experience becomes the set of its significant words (task, problem,
  7. ' action, result; lower case, stopwords and words shorter than 3 characters dropped, a plural "s" stripped). Words that
  8. ' occur in more than half of the experiences carry no signal and are ignored. Two experiences are similar when they
  9. ' share at least 2 words and their Jaccard similarity (shared / union) reaches the threshold; groups are the connected
  10. ' components of that relation (union-find). The draft only rearranges the experiences' own text (what failed, what
  11. ' worked); it is a starting point for a reviewer, never accepted automatically.
  12. '
  13. ' Usage: Set c = New Consolidator_Class : c.Load tenantId, nsId, since, maxRows : c.Cluster 0.35 : groups = c.Groups(2)
  14. ' Each group is a comma-separated list of row indexes (newest first); use the accessors and Draft* functions on it.
  15. '-----------------------------------------------------------------------------------------------------------------------
  16. Const CONS_MAX_GROUPS = 20
  17. Const CONS_MIN_SHARED = 2
  18. Const CONS_DRAFT_LINES = 6
  19. Class Consolidator_Class
  20. Private m_n, m_id(), m_session(), m_task(), m_problem(), m_action(), m_result(), m_outcome(), m_created()
  21. Private m_tokens(), m_len(), m_parent()
  22. Private m_df, m_re
  23. Private Sub Class_Initialize()
  24. m_n = 0
  25. Set m_df = Server.CreateObject("Scripting.Dictionary")
  26. Set m_re = New RegExp
  27. m_re.Pattern = "[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+"
  28. m_re.Global = True
  29. End Sub
  30. Public Property Get Count()
  31. Count = m_n
  32. End Property
  33. Public Function Id(i)
  34. Id = m_id(i)
  35. End Function
  36. Public Function SessionId(i)
  37. SessionId = m_session(i)
  38. End Function
  39. Public Function Outcome(i)
  40. Outcome = m_outcome(i)
  41. End Function
  42. Public Function Created(i)
  43. Created = m_created(i)
  44. End Function
  45. ' Loads up to maxRows experiences of the namespace created at or after `since` (text timestamp), newest first.
  46. Public Sub Load(tenantId, nsId, since, maxRows)
  47. Dim rs, rows, i, t
  48. m_n = 0
  49. Set rs = DAL().Query("SELECT id, session_id, task, problem, action, result, outcome, created_at FROM experiences " & _
  50. "WHERE tenant_id = ? AND namespace_id = ? AND created_at >= ? ORDER BY created_at DESC, id LIMIT " & CLng(maxRows), _
  51. Array(tenantId, nsId, since))
  52. rows = Empty
  53. If Not rs.EOF Then
  54. rows = rs.GetRows()
  55. End If
  56. rs.Close
  57. If Not IsArray(rows) Then
  58. Exit Sub
  59. End If
  60. m_n = UBound(rows, 2) + 1
  61. ReDim m_id(m_n - 1)
  62. ReDim m_session(m_n - 1)
  63. ReDim m_task(m_n - 1)
  64. ReDim m_problem(m_n - 1)
  65. ReDim m_action(m_n - 1)
  66. ReDim m_result(m_n - 1)
  67. ReDim m_outcome(m_n - 1)
  68. ReDim m_created(m_n - 1)
  69. ReDim m_tokens(m_n - 1)
  70. ReDim m_len(m_n - 1)
  71. For i = 0 To m_n - 1
  72. m_id(i) = "" & rows(0, i)
  73. m_session(i) = "" & rows(1, i)
  74. m_task(i) = "" & rows(2, i)
  75. m_problem(i) = "" & rows(3, i)
  76. m_action(i) = "" & rows(4, i)
  77. m_result(i) = "" & rows(5, i)
  78. m_outcome(i) = "" & rows(6, i)
  79. m_created(i) = "" & rows(7, i)
  80. Set m_tokens(i) = Tokens(m_task(i) & " " & m_problem(i) & " " & m_action(i) & " " & m_result(i))
  81. For Each t In m_tokens(i).Keys
  82. If m_df.Exists(t) Then
  83. m_df.Item(t) = m_df.Item(t) + 1
  84. Else
  85. m_df.Add t, 1
  86. End If
  87. Next
  88. Next
  89. End Sub
  90. Private Function Tokens(text)
  91. Dim d, parts, i, t
  92. Set d = Server.CreateObject("Scripting.Dictionary")
  93. parts = Split(m_re.Replace(LCase(text), " "), " ")
  94. For i = 0 To UBound(parts)
  95. t = parts(i)
  96. If Len(t) >= 3 Then
  97. If InStr(SEARCH_STOPWORDS, " " & t & " ") = 0 Then
  98. If Len(t) > 4 Then
  99. If Right(t, 1) = "s" And Right(t, 2) <> "ss" Then
  100. t = Left(t, Len(t) - 1)
  101. End If
  102. End If
  103. If Not d.Exists(t) Then
  104. d.Add t, True
  105. End If
  106. End If
  107. End If
  108. Next
  109. Set Tokens = d
  110. End Function
  111. ' Builds the groups for a Jaccard threshold (0..1).
  112. Public Sub Cluster(threshold)
  113. Dim post, maxDf, i, j, k, t, ids, counts, shared, jac
  114. If m_n = 0 Then
  115. Exit Sub
  116. End If
  117. ReDim m_parent(m_n - 1)
  118. For i = 0 To m_n - 1
  119. m_parent(i) = i
  120. Next
  121. maxDf = m_n \ 2
  122. If maxDf < 2 Then
  123. maxDf = 2
  124. End If
  125. Set post = Server.CreateObject("Scripting.Dictionary")
  126. For i = 0 To m_n - 1
  127. m_len(i) = 0
  128. For Each t In m_tokens(i).Keys
  129. If m_df.Item(t) <= maxDf Then
  130. m_len(i) = m_len(i) + 1
  131. If post.Exists(t) Then
  132. post.Item(t) = post.Item(t) & i & ","
  133. Else
  134. post.Add t, i & ","
  135. End If
  136. End If
  137. Next
  138. Next
  139. For i = 0 To m_n - 1
  140. Set counts = Server.CreateObject("Scripting.Dictionary")
  141. For Each t In m_tokens(i).Keys
  142. If post.Exists(t) Then
  143. ids = Split(post.Item(t), ",")
  144. For k = 0 To UBound(ids) - 1
  145. j = CLng(ids(k))
  146. If j > i Then
  147. If counts.Exists(j) Then
  148. counts.Item(j) = counts.Item(j) + 1
  149. Else
  150. counts.Add j, 1
  151. End If
  152. End If
  153. Next
  154. End If
  155. Next
  156. For Each j In counts.Keys
  157. shared = counts.Item(j)
  158. If shared >= CONS_MIN_SHARED Then
  159. jac = shared / (m_len(i) + m_len(j) - shared)
  160. If jac >= threshold Then
  161. Union i, j
  162. End If
  163. End If
  164. Next
  165. Set counts = Nothing
  166. Next
  167. End Sub
  168. Private Function Find(i)
  169. Dim r, nxt, cur
  170. r = i
  171. Do While m_parent(r) <> r
  172. r = m_parent(r)
  173. Loop
  174. cur = i
  175. Do While m_parent(cur) <> r
  176. nxt = m_parent(cur)
  177. m_parent(cur) = r
  178. cur = nxt
  179. Loop
  180. Find = r
  181. End Function
  182. Private Sub Union(a, b)
  183. Dim ra, rb
  184. ra = Find(a)
  185. rb = Find(b)
  186. If ra <> rb Then
  187. If ra < rb Then
  188. m_parent(rb) = ra
  189. Else
  190. m_parent(ra) = rb
  191. End If
  192. End If
  193. End Sub
  194. ' Groups of at least minSize experiences, largest first (ties: most recent first), at most CONS_MAX_GROUPS.
  195. ' Returns an array of "i,j,k" strings (row indexes, newest first), or an empty array.
  196. Public Function Groups(minSize)
  197. Dim byRoot, i, r, keys, n, out(), sizes(), a, b, tmp, tsz, cnt
  198. Groups = Array()
  199. If m_n = 0 Then
  200. Exit Function
  201. End If
  202. Set byRoot = Server.CreateObject("Scripting.Dictionary")
  203. For i = 0 To m_n - 1
  204. r = Find(i)
  205. If byRoot.Exists(r) Then
  206. byRoot.Item(r) = byRoot.Item(r) & "," & i
  207. Else
  208. byRoot.Add r, CStr(i)
  209. End If
  210. Next
  211. n = 0
  212. ReDim out(byRoot.Count)
  213. ReDim sizes(byRoot.Count)
  214. For Each r In byRoot.Keys
  215. cnt = UBound(Split(byRoot.Item(r), ",")) + 1
  216. If cnt >= minSize Then
  217. out(n) = byRoot.Item(r)
  218. sizes(n) = cnt
  219. n = n + 1
  220. End If
  221. Next
  222. If n = 0 Then
  223. Exit Function
  224. End If
  225. ' Selection sort by size (descending); rows are newest first, so a smaller first index is more recent.
  226. For a = 0 To n - 2
  227. For b = a + 1 To n - 1
  228. If sizes(b) > sizes(a) Or (sizes(b) = sizes(a) And FirstIndex(out(b)) < FirstIndex(out(a))) Then
  229. tmp = out(a)
  230. out(a) = out(b)
  231. out(b) = tmp
  232. tsz = sizes(a)
  233. sizes(a) = sizes(b)
  234. sizes(b) = tsz
  235. End If
  236. Next
  237. Next
  238. If n > CONS_MAX_GROUPS Then
  239. n = CONS_MAX_GROUPS
  240. End If
  241. ReDim Preserve out(n - 1)
  242. Groups = out
  243. End Function
  244. Private Function FirstIndex(group)
  245. FirstIndex = CLng(Left(group, InStr(group & ",", ",") - 1))
  246. End Function
  247. ' The n words shared by most members of the group (ties: rarer overall first, then alphabetical).
  248. Public Function TopTerms(group, n)
  249. Dim members, i, t, freq, keys, a, b, tmp, out, cnt, take
  250. members = Split(group, ",")
  251. Set freq = Server.CreateObject("Scripting.Dictionary")
  252. For i = 0 To UBound(members)
  253. For Each t In m_tokens(CLng(members(i))).Keys
  254. If freq.Exists(t) Then
  255. freq.Item(t) = freq.Item(t) + 1
  256. Else
  257. freq.Add t, 1
  258. End If
  259. Next
  260. Next
  261. keys = freq.Keys
  262. cnt = UBound(keys) + 1
  263. For a = 0 To cnt - 2
  264. For b = a + 1 To cnt - 1
  265. If Better(keys(b), keys(a), freq) Then
  266. tmp = keys(a)
  267. keys(a) = keys(b)
  268. keys(b) = tmp
  269. End If
  270. Next
  271. Next
  272. out = ""
  273. take = 0
  274. For a = 0 To cnt - 1
  275. If take >= n Then
  276. Exit For
  277. End If
  278. If freq.Item(keys(a)) >= 2 Then
  279. If Len(out) > 0 Then
  280. out = out & ","
  281. End If
  282. out = out & keys(a)
  283. take = take + 1
  284. End If
  285. Next
  286. TopTerms = out
  287. End Function
  288. Private Function Better(x, y, freq)
  289. Better = False
  290. If freq.Item(x) > freq.Item(y) Then
  291. Better = True
  292. ElseIf freq.Item(x) = freq.Item(y) Then
  293. If m_df.Item(x) < m_df.Item(y) Then
  294. Better = True
  295. ElseIf m_df.Item(x) = m_df.Item(y) Then
  296. Better = (x < y)
  297. End If
  298. End If
  299. End Function
  300. ' success|failure|partial|unknown counts of a group, as "s,f,p,u"
  301. Public Function OutcomeCounts(group)
  302. Dim members, i, s, f, p, u
  303. members = Split(group, ",")
  304. s = 0
  305. f = 0
  306. p = 0
  307. u = 0
  308. For i = 0 To UBound(members)
  309. Select Case m_outcome(CLng(members(i)))
  310. Case "success"
  311. s = s + 1
  312. Case "failure"
  313. f = f + 1
  314. Case "partial"
  315. p = p + 1
  316. Case Else
  317. u = u + 1
  318. End Select
  319. Next
  320. OutcomeCounts = s & "," & f & "," & p & "," & u
  321. End Function
  322. Public Function SessionCount(group)
  323. Dim members, i, d
  324. members = Split(group, ",")
  325. Set d = Server.CreateObject("Scripting.Dictionary")
  326. For i = 0 To UBound(members)
  327. If Not d.Exists(m_session(CLng(members(i)))) Then
  328. d.Add m_session(CLng(members(i))), True
  329. End If
  330. Next
  331. SessionCount = d.Count
  332. End Function
  333. ' "solution" when every member succeeded, otherwise "lesson" (failures and what eventually worked).
  334. Public Function DraftType(group)
  335. Dim oc
  336. oc = Split(OutcomeCounts(group), ",")
  337. If CLng(oc(0)) = UBound(Split(group, ",")) + 1 Then
  338. DraftType = "solution"
  339. Else
  340. DraftType = "lesson"
  341. End If
  342. End Function
  343. Public Function DraftTitle(group)
  344. Dim t
  345. t = Trim(m_task(FirstIndex(group)))
  346. If Len(t) > 180 Then
  347. t = Left(t, 177) & "..."
  348. End If
  349. DraftTitle = "Recurring: " & t
  350. End Function
  351. ' Plain text built only from the members' own fields: tasks, then what failed and what worked (newest first).
  352. Public Function DraftContent(group)
  353. Dim members, i, k, oc, sb, worked, failed, nw, nf, lastSeen, firstSeen, line
  354. members = Split(group, ",")
  355. oc = Split(OutcomeCounts(group), ",")
  356. lastSeen = m_created(CLng(members(0)))
  357. firstSeen = m_created(CLng(members(UBound(members))))
  358. worked = ""
  359. failed = ""
  360. nw = 0
  361. nf = 0
  362. For i = 0 To UBound(members)
  363. k = CLng(members(i))
  364. line = Excerpt(m_action(k), 200)
  365. If Len(m_result(k)) > 0 Then
  366. line = line & " -> " & Excerpt(m_result(k), 200)
  367. End If
  368. If Len(m_problem(k)) > 0 And Len(m_action(k)) = 0 Then
  369. line = Excerpt(m_problem(k), 200) & line
  370. End If
  371. line = "- " & line & " (" & Left(m_created(k), 10) & ")"
  372. If m_outcome(k) = "success" Then
  373. If nw < CONS_DRAFT_LINES Then
  374. worked = worked & line & vbLf
  375. End If
  376. nw = nw + 1
  377. ElseIf m_outcome(k) = "failure" Or m_outcome(k) = "partial" Then
  378. If nf < CONS_DRAFT_LINES Then
  379. failed = failed & line & vbLf
  380. End If
  381. nf = nf + 1
  382. End If
  383. Next
  384. sb = "Seen " & (UBound(members) + 1) & " times in " & SessionCount(group) & " session(s) between " & firstSeen & " and " & lastSeen & _
  385. " (" & oc(0) & " succeeded, " & oc(1) & " failed, " & oc(2) & " partial, " & oc(3) & " unknown)." & vbLf & vbLf & _
  386. "Task: " & Excerpt(m_task(CLng(members(0))), 300) & vbLf
  387. If Len(m_problem(CLng(members(0)))) > 0 Then
  388. sb = sb & "Problem: " & Excerpt(m_problem(CLng(members(0))), 300) & vbLf
  389. End If
  390. If nf > 0 Then
  391. sb = sb & vbLf & "What did not work:" & vbLf & failed
  392. If nf > CONS_DRAFT_LINES Then
  393. sb = sb & "- ... and " & (nf - CONS_DRAFT_LINES) & " more" & vbLf
  394. End If
  395. End If
  396. If nw > 0 Then
  397. sb = sb & vbLf & "What worked:" & vbLf & worked
  398. If nw > CONS_DRAFT_LINES Then
  399. sb = sb & "- ... and " & (nw - CONS_DRAFT_LINES) & " more" & vbLf
  400. End If
  401. End If
  402. sb = sb & vbLf & "Common terms: " & Replace(TopTerms(group, 6), ",", ", ") & "." & vbLf & vbLf & _
  403. "Draft assembled automatically from the linked experiences. Check it, and generalise it, before accepting."
  404. DraftContent = sb
  405. End Function
  406. Private Function Excerpt(s, n)
  407. Dim t
  408. t = Trim(Replace(Replace("" & s, vbCr, " "), vbLf, " "))
  409. If Len(t) > n Then
  410. t = Left(t, n - 3) & "..."
  411. End If
  412. Excerpt = t
  413. End Function
  414. End Class
  415. %>

Powered by TurnKey Linux.