Pure functions for search result highlighting and snippet extraction.
This namespace provides functions for highlighting matching terms in search results and extracting relevant snippets. All functions are pure (no side effects).
Architecture: Functional Core (Pure)
Pure functions for search result highlighting and snippet extraction. This namespace provides functions for highlighting matching terms in search results and extracting relevant snippets. All functions are pure (no side effects). Architecture: Functional Core (Pure)
(calculate-match-density text search-terms)Calculate density of search term matches in text.
Density = (total characters in matches) / (total text length)
Args: text - Text to analyze search-terms - Terms to find
Returns: Float between 0-1 (higher = more dense with matches)
Example: (calculate-match-density "John Doe" ["John"]) ;=> 0.5 ; "John" is 4 chars out of 8 total
Pure: true
Calculate density of search term matches in text. Density = (total characters in matches) / (total text length) Args: text - Text to analyze search-terms - Terms to find Returns: Float between 0-1 (higher = more dense with matches) Example: (calculate-match-density "John Doe" ["John"]) ;=> 0.5 ; "John" is 4 chars out of 8 total Pure: true
(count-matches text search-terms)(count-matches text search-terms case-sensitive?)Count how many times search terms appear in text.
Args: text - Text to search search-terms - Terms to count case-sensitive? - Match case exactly (default: false)
Returns: Map of term->count
Example: (count-matches "John and Jane met John" ["John" "Jane"]) ;=> {"John" 2 "Jane" 1}
Pure: true
Count how many times search terms appear in text.
Args:
text - Text to search
search-terms - Terms to count
case-sensitive? - Match case exactly (default: false)
Returns:
Map of term->count
Example:
(count-matches "John and Jane met John" ["John" "Jane"])
;=> {"John" 2 "Jane" 1}
Pure: true(extract-multiple-snippets text search-terms)(extract-multiple-snippets text search-terms max-snippets snippet-length)Extract multiple snippets from text (one per unique match).
Useful when search terms appear in different parts of a long document.
Args: text - Full text search-terms - Terms to find max-snippets - Maximum number of snippets (default: 3) snippet-length - Length per snippet (default: 150)
Returns: Vector of snippets
Example: (extract-multiple-snippets "John works in NY...lots of text...John lives in LA" ["John"] 2 30) ;=> ["John works in NY..." "...John lives in LA"]
Pure: true
Extract multiple snippets from text (one per unique match).
Useful when search terms appear in different parts of a long document.
Args:
text - Full text
search-terms - Terms to find
max-snippets - Maximum number of snippets (default: 3)
snippet-length - Length per snippet (default: 150)
Returns:
Vector of snippets
Example:
(extract-multiple-snippets
"John works in NY...lots of text...John lives in LA"
["John"]
2
30)
;=> ["John works in NY..." "...John lives in LA"]
Pure: true(extract-snippet text search-terms)(extract-snippet text search-terms max-length)(extract-snippet text search-terms max-length context-chars)Extract relevant snippet from text around search terms.
Extracts text centered around the first matching search term. Adds ellipsis (...) when text is truncated.
Args: text - Full text search-terms - Terms to find max-length - Maximum snippet length (default: 200) context-chars - Characters of context around match (default: max-length/2)
Returns: Snippet string with context around first match
Example: (extract-snippet "This is a long text about John Doe who works..." ["John"] 50) ;=> "...a long text about John Doe who works..."
Pure: true
Extract relevant snippet from text around search terms.
Extracts text centered around the first matching search term.
Adds ellipsis (...) when text is truncated.
Args:
text - Full text
search-terms - Terms to find
max-length - Maximum snippet length (default: 200)
context-chars - Characters of context around match (default: max-length/2)
Returns:
Snippet string with context around first match
Example:
(extract-snippet "This is a long text about John Doe who works..."
["John"]
50)
;=> "...a long text about John Doe who works..."
Pure: true(extract-snippet-with-highlight text search-terms max-length)(extract-snippet-with-highlight text search-terms max-length highlight-fn)Extract snippet and highlight matches in one step.
Args: text - Full text search-terms - Terms to find and highlight max-length - Maximum snippet length highlight-fn - Highlight function (optional)
Returns: Highlighted snippet
Example: (extract-snippet-with-highlight "This is a long text about John Doe who works as an engineer" ["John" "engineer"] 50) ;=> "...long text about <mark>John</mark> Doe who works as an <mark>engineer</mark>"
Pure: true
Extract snippet and highlight matches in one step.
Args:
text - Full text
search-terms - Terms to find and highlight
max-length - Maximum snippet length
highlight-fn - Highlight function (optional)
Returns:
Highlighted snippet
Example:
(extract-snippet-with-highlight
"This is a long text about John Doe who works as an engineer"
["John" "engineer"]
50)
;=> "...long text about <mark>John</mark> Doe who works as an <mark>engineer</mark>"
Pure: true(find-first-match-position text search-terms)(find-first-match-position text search-terms case-sensitive?)Find position of first search term match in text.
Args: text - Text to search search-terms - Terms to find case-sensitive? - Match case exactly (default: false)
Returns: {:match-pos int :matched-term string} or nil if no match
Example: (find-first-match-position "Hello John Doe" ["John" "Jane"]) ;=> {:match-pos 6 :matched-term "John"}
Pure: true
Find position of first search term match in text.
Args:
text - Text to search
search-terms - Terms to find
case-sensitive? - Match case exactly (default: false)
Returns:
{:match-pos int :matched-term string} or nil if no match
Example:
(find-first-match-position "Hello John Doe" ["John" "Jane"])
;=> {:match-pos 6 :matched-term "John"}
Pure: true(highlight-field result field search-terms)(highlight-field result field search-terms highlight-fn)Highlight matches in a specific field of a result.
Args: result - Result map field - Field to highlight search-terms - Terms to highlight highlight-fn - Highlight function (optional)
Returns: Result map with highlighted field in :_highlights
Example: (highlight-field {:name "John Doe"} :name ["John"]) ;=> {:name "John Doe" ; :_highlights {:name "<mark>John</mark> Doe"}}
Pure: true
Highlight matches in a specific field of a result.
Args:
result - Result map
field - Field to highlight
search-terms - Terms to highlight
highlight-fn - Highlight function (optional)
Returns:
Result map with highlighted field in :_highlights
Example:
(highlight-field {:name "John Doe"} :name ["John"])
;=> {:name "John Doe"
; :_highlights {:name "<mark>John</mark> Doe"}}
Pure: true(highlight-matches text search-terms)(highlight-matches text search-terms highlight-fn)(highlight-matches text search-terms highlight-fn options)Highlight matching terms in text.
Wraps matching terms with highlight function (default: <mark> tags). Case-insensitive matching by default.
Args: text - Original text to highlight search-terms - List of terms to highlight highlight-fn - Function to wrap matches (optional) Default: (fn [term] (str "<mark>" term "</mark>")) options - Map of options: :case-sensitive? - Match case exactly (default: false) :whole-words? - Only match whole words (default: true)
Returns: Text with highlighted terms
Example: (highlight-matches "John Doe is a software engineer" ["John" "engineer"]) ;=> "<mark>John</mark> Doe is a software <mark>engineer</mark>"
(highlight-matches "john doe" ["john"] (fn [term] (str "[" term "]"))) ;=> "[john] doe"
Pure: true
Highlight matching terms in text.
Wraps matching terms with highlight function (default: <mark> tags).
Case-insensitive matching by default.
Args:
text - Original text to highlight
search-terms - List of terms to highlight
highlight-fn - Function to wrap matches (optional)
Default: (fn [term] (str "<mark>" term "</mark>"))
options - Map of options:
:case-sensitive? - Match case exactly (default: false)
:whole-words? - Only match whole words (default: true)
Returns:
Text with highlighted terms
Example:
(highlight-matches "John Doe is a software engineer"
["John" "engineer"])
;=> "<mark>John</mark> Doe is a software <mark>engineer</mark>"
(highlight-matches "john doe" ["john"]
(fn [term] (str "[" term "]")))
;=> "[john] doe"
Pure: true(highlight-multiple-fields result fields search-terms)(highlight-multiple-fields result fields search-terms highlight-fn)Highlight matches in multiple fields.
Args: result - Result map fields - List of fields to highlight search-terms - Terms to highlight highlight-fn - Highlight function (optional)
Returns: Result map with highlighted fields in :_highlights
Example: (highlight-multiple-fields {:name "John Doe" :bio "Software engineer"} [:name :bio] ["John" "engineer"]) ;=> {:name "John Doe" ; :bio "Software engineer" ; :_highlights {:name "<mark>John</mark> Doe" ; :bio "Software <mark>engineer</mark>"}}
Pure: true
Highlight matches in multiple fields.
Args:
result - Result map
fields - List of fields to highlight
search-terms - Terms to highlight
highlight-fn - Highlight function (optional)
Returns:
Result map with highlighted fields in :_highlights
Example:
(highlight-multiple-fields {:name "John Doe" :bio "Software engineer"}
[:name :bio]
["John" "engineer"])
;=> {:name "John Doe"
; :bio "Software engineer"
; :_highlights {:name "<mark>John</mark> Doe"
; :bio "Software <mark>engineer</mark>"}}
Pure: true(strip-html text)Strip HTML tags from text.
Useful for cleaning text before highlighting or snippet extraction.
Args: text - Text with HTML tags
Returns: Text without HTML tags
Example: (strip-html "<p>Hello <strong>world</strong></p>") ;=> "Hello world"
Pure: true
Strip HTML tags from text. Useful for cleaning text before highlighting or snippet extraction. Args: text - Text with HTML tags Returns: Text without HTML tags Example: (strip-html "<p>Hello <strong>world</strong></p>") ;=> "Hello world" Pure: true
(truncate-text text max-length)(truncate-text text max-length ellipsis)Truncate text to maximum length with ellipsis.
Args: text - Text to truncate max-length - Maximum length ellipsis - Ellipsis string (default: "...")
Returns: Truncated text
Example: (truncate-text "This is a long text" 10) ;=> "This is..."
Pure: true
Truncate text to maximum length with ellipsis. Args: text - Text to truncate max-length - Maximum length ellipsis - Ellipsis string (default: "...") Returns: Truncated text Example: (truncate-text "This is a long text" 10) ;=> "This is..." Pure: true
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |