]> git.plutz.net Git - shellwiki/blob - parsers/40_indexer.sh
introducing text indexer
[shellwiki] / parsers / 40_indexer.sh
1 #!/bin/sh
2
3 # Copyright 2023 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 DOC="${PATH_INFO%/}/" DOC="${DOC%/\[*\]/}" DOC="${DOC%/}/"
18 P="$_DATA/pages${DOC}" I="$_DATA/index/"
19
20 if [ -f "$P/#index.flag" -a ! "$P/#page.md" -nt "$P/#index.flag" ]; then
21   cat
22   exit 0
23 fi
24
25 . "$_EXEC/cgilite/storage.sh"
26
27 exec 3>&1
28
29 touch "$P/#index.flag"
30 mkdir -p "$I"
31
32 { cat; printf \\n; } \
33 | while IFS='' read -r line; do
34   printf '%s\n' "$line" >&3
35   printf '%s\n' "$line"
36 done \
37 | awk '
38   BEGIN { FS = "[] \t\n\r!\"#'\''()*+,./:;<=>?\\^_`{|}~[-]+" }
39         { for (n = 1; n <= NF; n++) if ( $n != "" ) { words[tolower($n)]++; total++; } }
40     END { for (w in words) printf "%i %i %f %s\n", words[w], total, words[w] / total, w; }
41 ' \
42 | while read num total freq word; do
43   [ "$word" ] || continue
44   F="$I/$word"
45   L="$(STRING "$DOC")"
46
47   if LOCK "$F"; then
48     touch "$F"
49     { while read d l f n t; do
50       [ "$l" = "$L" ] \
51       || printf "%i     %s      %f      %i      %i\n" \
52                 "$d" "$l" "$f" "$n" "$t"
53       done <"$F"
54       printf "%i        %s      %f      %i      %i\n" \
55              "$_DATE" "$L" "$freq" "$num" "$total"
56     } >"$F.$$"
57     mv -- "$F.$$" "$F"
58     RELEASE "$F"
59   fi
60 done