您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

327 行
11KB

  1. <%
  2. '=======================================================================================================================
  3. ' Search support: lexical (SQLite FTS5) indexing for memories and document chunks, and the document chunker.
  4. '
  5. ' FTS rows carry tenant/namespace ids as UNINDEXED columns. A search always filters by tenant in SQL and then by the
  6. ' caller's readable namespaces (and current revision / lifecycle) against the canonical tables, so an index row never
  7. ' discloses anything on its own. Global functions only; module-level arrays hold the chunker output.
  8. '=======================================================================================================================
  9. ' ---------------------------------------------------------------------------------------------------------------------
  10. ' FTS query building
  11. ' ---------------------------------------------------------------------------------------------------------------------
  12. ' Turns free text into a safe FTS5 expression: up to 16 word tokens, each double-quoted, joined with OR so bm25 ranks
  13. ' partial matches. Returns "" when the text has no usable word. Letters/digits of any script are kept.
  14. Function Search_FtsQuery(text)
  15. Dim t, i, c, n, tok, out, count, code
  16. t = "" & text
  17. n = Len(t)
  18. tok = ""
  19. out = ""
  20. count = 0
  21. For i = 1 To n + 1
  22. If i <= n Then
  23. c = Mid(t, i, 1)
  24. code = AscW(c)
  25. Else
  26. c = " "
  27. code = 32
  28. End If
  29. If (c >= "a" And c <= "z") Or (c >= "A" And c <= "Z") Or (c >= "0" And c <= "9") Or c = "_" Or code > 127 Then
  30. tok = tok & c
  31. Else
  32. If Len(tok) > 0 Then
  33. If count < 16 Then
  34. If Len(out) > 0 Then
  35. out = out & " OR "
  36. End If
  37. out = out & """" & Left(tok, 64) & """"
  38. count = count + 1
  39. End If
  40. tok = ""
  41. End If
  42. End If
  43. Next
  44. Search_FtsQuery = out
  45. End Function
  46. ' ---------------------------------------------------------------------------------------------------------------------
  47. ' Memory index (one row per memory: its current revision)
  48. ' ---------------------------------------------------------------------------------------------------------------------
  49. Sub Search_UnindexMemory(memoryId)
  50. DAL().Execute "DELETE FROM memories_fts WHERE memory_id = ?", memoryId
  51. End Sub
  52. ' tagsRaw is the stored JSON array text (or Null); the tag words are indexed as plain text.
  53. Sub Search_IndexMemory(memoryId, revision, tenantId, namespaceId, title, content, tagsRaw)
  54. Dim tagText
  55. Search_UnindexMemory memoryId
  56. tagText = ""
  57. If Not IsNull(tagsRaw) Then
  58. tagText = Replace(Replace(Replace(Replace("" & tagsRaw, "[", " "), "]", " "), """", " "), ",", " ")
  59. End If
  60. DAL().Execute "INSERT INTO memories_fts (title, content, tags, memory_id, revision, tenant_id, namespace_id) VALUES (?, ?, ?, ?, ?, ?, ?)", _
  61. Array(title, content, tagText, memoryId, CLng(revision), tenantId, namespaceId)
  62. End Sub
  63. ' ---------------------------------------------------------------------------------------------------------------------
  64. ' Chunker. State lives in a class instance because ReDim of module-level arrays from inside a Sub does not reach the
  65. ' global array under AxonASP. Output (valid until the next Chunk_Run call), through global accessor functions:
  66. ' Chunk_Count, Chunk_Text(i), Chunk_Heading(i), Chunk_Start(i), Chunk_End(i) (i = 0..Count-1)
  67. ' Offsets are 0-based character offsets into the NORMALIZED text (Chunk_Normalize); Chunk_Text(i) is exactly
  68. ' Mid(normalized, Chunk_Start(i) + 1, Chunk_End(i) - Chunk_Start(i)), so a locator always reproduces the passage.
  69. ' ---------------------------------------------------------------------------------------------------------------------
  70. Const CHUNK_MAX_CHARS = 1200
  71. Class Chunker_Class
  72. Public Count
  73. Private cText(), cHead(), cStart(), cEnd()
  74. Private bS(), bE(), bH(), bIsHead(), bN
  75. Private src
  76. Private Sub Class_Initialize()
  77. Count = 0
  78. bN = 0
  79. ReDim cText(15)
  80. ReDim cHead(15)
  81. ReDim cStart(15)
  82. ReDim cEnd(15)
  83. ReDim bS(31)
  84. ReDim bE(31)
  85. ReDim bH(31)
  86. ReDim bIsHead(31)
  87. End Sub
  88. Public Function TextAt(i)
  89. TextAt = cText(i)
  90. End Function
  91. Public Function HeadAt(i)
  92. HeadAt = cHead(i)
  93. End Function
  94. Public Function StartAt(i)
  95. StartAt = cStart(i)
  96. End Function
  97. Public Function EndAt(i)
  98. EndAt = cEnd(i)
  99. End Function
  100. Private Sub AddChunk(txt, heading, startOff, endOff)
  101. If Count > UBound(cText) Then
  102. ReDim Preserve cText(Count * 2)
  103. ReDim Preserve cHead(Count * 2)
  104. ReDim Preserve cStart(Count * 2)
  105. ReDim Preserve cEnd(Count * 2)
  106. End If
  107. cText(Count) = txt
  108. cHead(Count) = heading
  109. cStart(Count) = startOff
  110. cEnd(Count) = endOff
  111. Count = Count + 1
  112. End Sub
  113. Private Sub PushBlock(s, e, headingPath, isHeading)
  114. If bN > UBound(bS) Then
  115. ReDim Preserve bS(bN * 2)
  116. ReDim Preserve bE(bN * 2)
  117. ReDim Preserve bH(bN * 2)
  118. ReDim Preserve bIsHead(bN * 2)
  119. End If
  120. bS(bN) = s
  121. bE(bN) = e
  122. bH(bN) = headingPath
  123. bIsHead(bN) = isHeading
  124. bN = bN + 1
  125. End Sub
  126. ' Appends a paragraph; one longer than the limit is cut at whitespace into several blocks.
  127. Private Sub AddPara(startOff, endOff, headingPath)
  128. Dim s, e, cut, j, ch
  129. s = startOff
  130. e = endOff
  131. Do While (e - s) > CHUNK_MAX_CHARS
  132. cut = s + CHUNK_MAX_CHARS
  133. For j = cut To cut - 200 Step -1
  134. If j <= s Then
  135. Exit For
  136. End If
  137. ch = Mid(src, j + 1, 1)
  138. If ch = " " Or ch = vbLf Then
  139. cut = j
  140. Exit For
  141. End If
  142. Next
  143. PushBlock s, cut, headingPath, False
  144. s = cut
  145. Do While s < e
  146. ch = Mid(src, s + 1, 1)
  147. If ch = " " Or ch = vbLf Then
  148. s = s + 1
  149. Else
  150. Exit Do
  151. End If
  152. Loop
  153. Loop
  154. If e > s Then
  155. PushBlock s, e, headingPath, False
  156. End If
  157. End Sub
  158. ' Level (1-6) of an ATX heading line ("## Title"), else 0
  159. Private Function HeadingLevel(ln)
  160. Dim n, c
  161. HeadingLevel = 0
  162. n = 0
  163. Do While n < Len(ln) And n < 7
  164. If Mid(ln, n + 1, 1) = "#" Then
  165. n = n + 1
  166. Else
  167. Exit Do
  168. End If
  169. Loop
  170. If n >= 1 And n <= 6 Then
  171. If Len(ln) > n Then
  172. c = Mid(ln, n + 1, 1)
  173. If c = " " Or c = vbTab Then
  174. If Len(Trim(Mid(ln, n + 1))) > 0 Then
  175. HeadingLevel = n
  176. End If
  177. End If
  178. End If
  179. End If
  180. End Function
  181. ' Splits normalized text into passages of about CHUNK_MAX_CHARS. A markdown heading always starts a new chunk and
  182. ' becomes part of the heading path of the chunks below it; blank lines separate paragraphs, grouped greedily.
  183. Public Sub Split(norm, isMarkdown)
  184. Dim lines, i, ln, pos, lineStart, level, title, path, k, g
  185. Dim paraStart, paraEnd, inPara
  186. Dim hstack(5)
  187. Dim gStart, gEnd, gHead, have, bs0, be0
  188. src = norm
  189. Count = 0
  190. bN = 0
  191. For k = 0 To 5
  192. hstack(k) = ""
  193. Next
  194. path = ""
  195. lines = Split_(norm)
  196. pos = 0
  197. inPara = False
  198. paraStart = 0
  199. paraEnd = 0
  200. For i = 0 To UBound(lines)
  201. ln = lines(i)
  202. lineStart = pos
  203. pos = pos + Len(ln) + 1
  204. level = 0
  205. If isMarkdown Then
  206. level = HeadingLevel(ln)
  207. End If
  208. If level > 0 Then
  209. If inPara Then
  210. AddPara paraStart, paraEnd, path
  211. inPara = False
  212. End If
  213. title = Trim(Mid(ln, level + 1))
  214. hstack(level - 1) = title
  215. For k = level To 5
  216. hstack(k) = ""
  217. Next
  218. path = ""
  219. For k = 0 To 5
  220. If Len(hstack(k)) > 0 Then
  221. If Len(path) > 0 Then
  222. path = path & " > "
  223. End If
  224. path = path & hstack(k)
  225. End If
  226. Next
  227. PushBlock lineStart, lineStart + Len(ln), path, True
  228. ElseIf Len(Trim(ln)) = 0 Then
  229. If inPara Then
  230. AddPara paraStart, paraEnd, path
  231. inPara = False
  232. End If
  233. Else
  234. If Not inPara Then
  235. inPara = True
  236. paraStart = lineStart
  237. End If
  238. paraEnd = lineStart + Len(ln)
  239. End If
  240. Next
  241. If inPara Then
  242. AddPara paraStart, paraEnd, path
  243. End If
  244. have = False
  245. gStart = 0
  246. gEnd = 0
  247. gHead = ""
  248. For g = 0 To bN - 1
  249. bs0 = bS(g)
  250. be0 = bE(g)
  251. If have Then
  252. If bIsHead(g) Or (be0 - gStart) > CHUNK_MAX_CHARS Then
  253. AddChunk Mid(norm, gStart + 1, gEnd - gStart), gHead, gStart, gEnd
  254. have = False
  255. End If
  256. End If
  257. If Not have Then
  258. gStart = bs0
  259. gHead = bH(g)
  260. have = True
  261. End If
  262. gEnd = be0
  263. Next
  264. If have Then
  265. AddChunk Mid(norm, gStart + 1, gEnd - gStart), gHead, gStart, gEnd
  266. End If
  267. End Sub
  268. Private Function Split_(t)
  269. Split_ = VBScript_Split(t)
  270. End Function
  271. End Class
  272. Function VBScript_Split(t)
  273. VBScript_Split = Split(t, vbLf)
  274. End Function
  275. Dim Chunk_State
  276. Set Chunk_State = New Chunker_Class
  277. Function Chunk_Normalize(text)
  278. Dim t
  279. t = Replace("" & text, vbCrLf, vbLf)
  280. t = Replace(t, vbCr, vbLf)
  281. Chunk_Normalize = t
  282. End Function
  283. Function Chunk_Count()
  284. Chunk_Count = Chunk_State.Count
  285. End Function
  286. Function Chunk_Text(i)
  287. Chunk_Text = Chunk_State.TextAt(i)
  288. End Function
  289. Function Chunk_Heading(i)
  290. Chunk_Heading = Chunk_State.HeadAt(i)
  291. End Function
  292. Function Chunk_Start(i)
  293. Chunk_Start = Chunk_State.StartAt(i)
  294. End Function
  295. Function Chunk_End(i)
  296. Chunk_End = Chunk_State.EndAt(i)
  297. End Function
  298. ' Entry point: normalizes, splits and returns the normalized text (store this, not the raw input)
  299. Function Chunk_Run(text, isMarkdown)
  300. Dim norm
  301. norm = Chunk_Normalize(text)
  302. Set Chunk_State = New Chunker_Class
  303. Chunk_State.Split norm, isMarkdown
  304. Chunk_Run = norm
  305. End Function
  306. %>

Powered by TurnKey Linux.