Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

346 řádky
12KB

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

Powered by TurnKey Linux.