Is there a way to count repeated text occurrences in all child blocks of one parent block?
I’ve tried for two hours now with ChatGPT, as I can’t code, but to no avail. We got as far as returning the content of the given blocks, but once it gets to counting the return is wrong or invalid query.
Is this actually possible in Logseq MD?
Zoom on the parent block and ctrl+f ?
That would be a viable solution if I want to find one word and count myself.
But I want a list or table returned with about 20 different strings and how many of each.
Sounds doable. Where is your (LLM’s) query?
This is the last working query that ChatGPT came up with:
#+BEGIN_QUERY
{:title “Check content”
:inputs [:parent-block]
:query [:find (pull ?b [:block/content])
:keys block
:in $ ?parent
:where [?b :block/parent ?parent]]
:view (fn [rows]
[:div
(str "Rows: " (count rows))
(for [row rows]
[:div (str (:block/content (:block row)))])])}
#+END_QUERY
But whatever counting view-code it suggests, breaks the query.
In this test code by ChatGPT (“coffee”) I actually got a result, but not from the blocks but from the query text itself:
#+BEGIN_QUERY
{:title “Check content”
:inputs [:parent-block]
:query [:find (pull ?b [:block/content])
:keys block
:in $ ?parent
:where [?b :block/parent ?parent]]
:view (fn [rows]
(let [word “coffee”
count-in
(fn [s]
(count
(re-seq
(re-pattern word)
(clojure.string/lower-case (or s “”)))))
total
(reduce +
(map (fn [row]
(count-in (:block/content (:block row))))
rows))]
[:div (str word ": " total)]))}
#+END_QUERY
Something like this:
#+BEGIN_QUERY
{:title "Check content"
:inputs [:current-block :parent-block]
:query [:find (pull ?b [:block/content])
:keys block
:in $ ?current ?parent
:where
[?b :block/parent ?parent]
[(!= ?b ?current)]
]
:view (fn [rows]
(def text (clojure.string/join " " (map :block/content (map :block rows))))
(def targets ["string1" "string2" "string19" "string20"])
(def pattern (re-pattern (clojure.string/join "|" targets)))
(def counts
(->> text
(re-seq pattern)
frequencies
)
)
[:div (str "result: " counts) ]
)
}
#+END_QUERY
Mind that for this to work:
- should replace string1 to string20 with your own strings
- it should be placed in a block nested one level under the parent block
- Otherwise should query for the parent block in some way.
- the blocks with the content should also be nested one level under the same parent
- It takes extra code to also look into blocks further nested beyond the first level.
It takes extra code to give the output a more advanced format.
You’re a star! That works perfectly, thank you so much!
It takes extra code to give the output a more advanced format.
You mean, if I want it in a table or somehow ordered? That would be perfect, but the main problem you have solved for me and my use case.