Guest User

weird issue

a guest
Jun 19th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1.  
  2. (load "random-snippets.lisp") ;; o compoising operator rcurry partial
  3. (ql:quickload :cl-ppcre)
  4.  
  5. (defparameter batch1 '(wonder tranquil mystic serenity luminary twilight soothing bedtime restful celestial moonlit drowsy calming whisper nebula zephyr radiant peaceful shimmer repose ethereal comets aurora placid somber galactic musing starlit gentle dreamy hushed glowing nimbus serene wistful cosmic starlight moondust stardom phantom silken lucid moonrise stardew quietude stargaze restive silencer starshine moonscape cometary moondrift silentium sparklet starblow starblick starflick starglim starglint starglow glowstreak nimbuster serenitude moonglide silentful sparklen glowshine starfield cosmical silentiary nebulary starthrone mooncraft stardrift glowspark tranquilness starlighten restfulness moonscapist quietness starbloom dreamscape silenthush glowficker nebulist starwhisper moonshadow resthaven cosmicray velvetine stardancer slumberpath mystichaze quietrealm glowbeacon moonhaze restdream cosmicveil velvettouch stardweller slumberhaze mysticwand quietshade glowfairy starlumen moonflicker restglade cosmicdrift velvetmist starflame slumberwind mysticglobe quietblaze glowphantom starvowel moonchaser restcloud cosmicflame velvetshade stardolphin slumbertide mysticflair quietforge glowsprite starclimb moonvapor restmeadow cosmicgleam velvetdream starfountain slumbercave mysticvapor quietstone glowbeast sleep dream night stars moon cloud mist haze))
  6. (defun rem-dup-batch (x)
  7. (remove-duplicates x))
  8. (defun word-no-dups (x)
  9. (remove-duplicates x :test #'string=))
  10. (defun dup* (x)
  11. (not (string= (word-no-dups x) x)))
  12. (defun <x (word x)
  13. (< (length word) x))
  14. (defun =x (word x)
  15. (= (length word) x))
  16. (defun constainsweird (word)
  17. (not (string= (remove-if (complement #'alpha-char-p) word) word)))
  18. (defun invalidstring (word x)
  19. (or (dup* word)
  20. (not (=x word x))
  21. (cl-ppcre:all-matches "[A-Z]" word)
  22. (constainsweird word)))
  23. (defun remove-invalid-string (batch)
  24. (remove-if (rcurry #'invalidstring 4) batch))
  25. (defun <6 (word)
  26. (< (length word) 6) )
  27. (defun invalid (x)
  28. (setf x (write-to-string x))
  29. (or(dup* x) (<6 x)))
  30. (defun zzz ()
  31. (remove-if #'invalid batch1))
  32. (print (zzz))
  33. (ql:quickload :rutils)
  34. ;(in-package :rtl-user)
  35. ;(named-readtables:in-readtable rutils-readtable)
  36.  
  37.  
  38. ;; /usr/share/dict/words has a word on each line
  39. ;; (awk "usr/share/dict/words" " " thunk)
  40.  
  41. (defstruct (tn-node (:conc-name nil))
  42. v
  43. (c (list)))
  44. (defparameter *trie* (make-tn-node))
  45. (defparameter *xxx* 2)
  46. (defun tr-lookup (key root)
  47. (rtl:dovec (ch key (v root))
  48. (rtl:if-it (rtl:assoc1 ch (c root))
  49. (setf root rtl:it)
  50. (return))))
  51. (defun tr-add (key val root)
  52. (let ((i 0))
  53. (rtl:dovec (ch key)
  54. (rtl:if-it (rtl:assoc1 ch (c root))
  55. (progn(setf root rtl:it)
  56. (incf i))
  57. (return)))
  58. (if (= i (length key))
  59. (cerror "Assign a new value"
  60. "There was already a value at key ~A" (v root))
  61. (rtl:dovec (ch (rtl:slice key i))
  62. (let ((child (make-tn-node)))
  63. (push (cons ch child) (c root))
  64. (setf root child))))
  65. (setf (v root) val)))
  66. (tr-add "moon" :moon *trie*)
  67. (tr-add "dust" :dust *trie*)
  68. (tr-add "star" :star *trie*)
  69. (defun tr-composite-p (word trie)
  70. "Returns T if WORD is a composite word in TRIE, NIL otherwise.
  71. A composite word is formed by concatenating two or more words stored in the trie."
  72. (labels
  73. ((check-from (i root)
  74. ;; Helper: Checks if substring from index i is composite.
  75. ;; Returns T if composite, NIL otherwise.
  76. (let ((current root))
  77. ;; Try to find prefixes from index i
  78. (loop for j from i below (length word)
  79. ;; increases until length word
  80. for ch = (char word j)
  81. for child = (rtl:assoc1 ch (c current))
  82. do
  83. (unless child
  84. (return nil)) ; No path for this character
  85. (setf current child)
  86. when (and (< j (1- (length word))) ; Not at end
  87. (v current)) ; Prefix is a word
  88. do
  89. (let ((suffix-composite? (or (tr-lookup (rtl:slice word (1+ j)) trie)
  90. (check-from (1+ j) trie))))
  91. (when suffix-composite?
  92. (return t))) ; Found a valid split
  93. finally
  94. ;; If we reach here and the whole suffix is a word
  95. (return (and (= j (length word))
  96. (v current)))))))
  97. (and (> (length word) 1) ; Single-char words can't be composite
  98. (check-from 0 trie))))
  99. (defun add-word (word)
  100. (tr-add word (intern (string-upcase word) "KEYWORD") *trie*))
  101. (defparameter *small-words* nil)
  102. (defparameter *small-words2* nil)
  103. (defparameter *bigger-words* nil)
  104. (defparameter *prefixes* (cl-ppcre:split " "
  105. (concatenate 'string "dis mega un mini in micro non macro anti co pre"
  106. "post counter re pro ex trans sub super inter")) )
  107. (defparameter *suffixes* (cl-ppcre:split " "
  108. (concatenate 'string "er or ist ian ment ation ing ure ful less able ling"
  109. "ville ish ton bury ship hood dom ry let kin")))
  110. (defparameter *saved-trie* *trie*)
  111. ;; bigger words seem to have some words like disarm and so on
  112. (with-open-file (file "/usr/share/dict/words" :direction :input)
  113. (loop for i from 1
  114. for line = (read-line file nil nil)
  115. while line
  116. when (not (invalidstring line 4))
  117. do (push line *bigger-words*)
  118. ))
  119. (time (progn (setf *trie* (make-tn-node))
  120. (mapcar #'add-word *small-words*)))
  121. (defparameter test
  122. (funcall (o (partial #'remove-if #'null)
  123. (partial #'remove-if (rcurry
  124. #'tr-lookup *trie*))) *prefixes*))
  125. (defun cartesian-product (s1 s2)
  126. "Compute the cartesian product of two sets represented as lists"
  127. (loop for x in s1
  128. nconc (loop for y in s2 collect (list x y))))
  129. (defun each-element-cond (s1 s2 func)
  130. (let ((prod (cartesian-product s1 s2)))
  131. (mapcar (lambda (a b)
  132. (list a b))
  133. (mapcar (o #'eval (partial #'cons `,func )) prod)
  134. prod)))
  135. (defparameter *small-words-save* *small-words*)
  136. (setf *small-words*
  137. (remove-if
  138. #'null
  139. (remove-duplicates
  140. (funcall (o (partial #'remove-if #'null)
  141. (partial #'remove-if (rcurry
  142. #'tr-lookup *trie*)))
  143. (remove-if #'null (remove-duplicates *small-words*))))))
  144.  
  145. (defun add-words (batch)
  146. (let ((doctored-words
  147. (remove-if
  148. #'null
  149. (remove-duplicates
  150. (funcall (o (partial #'remove-if #'null)
  151. (partial #'remove-if (rcurry
  152. #'tr-lookup *trie*)))
  153. (remove-if #'null (remove-duplicates batch)))))))
  154. (mapcar #'add-word doctored-words)))
  155. (time (mapcar #'add-word
  156. (funcall (o (partial #'remove-if #'null)
  157. (partial #'remove-if (rcurry
  158. #'tr-lookup *trie*))) *prefixes*)))
  159. ;; this form has been verified to be correct
  160. ;; prefixes doesn't have duplicates
  161. ;; adding prefixes leads to "There was already a value at key nil"
  162. ;; *prefixes* is bigger than test
  163.  
  164. (mapcar #'add-word test)
  165. ;; idea - start over with the trie and at first add the prefixes and suffexes
  166. ;; after adding suffixes and prefixes small words cannot be added
  167. ;; anti and mega are in both prefixes and small words
  168. ;; and can be seen with tr-lookup
  169. ;; when we delete the extra words in small words it still says
  170. ;; the same error
Advertisement
Add Comment
Please, Sign In to add comment