Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,25 @@
#!/bin/ksh
typeset -A INDEX
function index {
typeset num=0
for file in "$@"; do
tr -s '[:punct:]' ' ' < "$file" | while read line; do
for token in $line; do
INDEX[$token][$num]=$file
done
done
((++num))
done
}
function search {
for token in "$@"; do
for file in "${INDEX[$token][@]}"; do
echo "$file"
done
done | sort | uniq -c | while read count file; do
(( count == $# )) && echo $file
done
}

View file

@ -0,0 +1,2 @@
index *.txt
search hello world

View file

@ -0,0 +1,49 @@
#!/bin/sh
# index.sh - create an inverted index
unset IFS
: ${INDEX:=index}
# Prohibit '\n' in filenames (because '\n' is
# the record separator for $INDEX/all.tab).
for file in "$@"; do
# Use printf(1), not echo, because "$file" might start with
# a hyphen and become an option to echo.
test 0 -eq $(printf %s "$file" | wc -l) || {
printf '%s\n' "$file: newline in filename" >&2
exit 1
}
done
# Make a new directory for the index, or else
# exit with the error message from mkdir(1).
mkdir "$INDEX" || exit $?
fi=1
for file in "$@"; do
printf %s "Indexing $file." >&2
# all.tab maps $fi => $file
echo "$fi $file" >> "$INDEX/all.tab"
# Use punctuation ([:punct:]) and whitespace (IFS)
# to split tokens.
ti=1
tr -s '[:punct:]' ' ' < "$file" | while read line; do
for token in $line; do
# Index token by position ($fi, $ti). Ignore
# error from mkdir(1) if directory exists.
mkdir "$INDEX/$token" 2>/dev/null
echo $ti >> "$INDEX/$token/$fi"
: $((ti += 1))
# Show progress. Print a dot per 1000 tokens.
case "$ti" in
*000) printf .
esac
done
done
echo >&2
: $((fi += 1))
done

View file

@ -0,0 +1,33 @@
#!/bin/sh
# search.sh - search an inverted index
unset IFS
: ${INDEX:=index}
want=sequence
while getopts aos name; do
case "$name" in
a) want=all;;
o) want=one;;
s) want=sequence;;
*) exit 2;;
esac
done
shift $((OPTIND - 1))
all() {
echo "TODO"
exit 2
}
one() {
echo "TODO"
exit 2
}
sequence() {
echo "TODO"
exit 2
}
$want "$@"