Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Inverted-index/UNIX-Shell/inverted-index-1.sh
Normal file
25
Task/Inverted-index/UNIX-Shell/inverted-index-1.sh
Normal 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
|
||||
}
|
||||
2
Task/Inverted-index/UNIX-Shell/inverted-index-2.sh
Normal file
2
Task/Inverted-index/UNIX-Shell/inverted-index-2.sh
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
index *.txt
|
||||
search hello world
|
||||
49
Task/Inverted-index/UNIX-Shell/inverted-index-3.sh
Normal file
49
Task/Inverted-index/UNIX-Shell/inverted-index-3.sh
Normal 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
|
||||
33
Task/Inverted-index/UNIX-Shell/inverted-index-4.sh
Normal file
33
Task/Inverted-index/UNIX-Shell/inverted-index-4.sh
Normal 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 "$@"
|
||||
Loading…
Add table
Add a link
Reference in a new issue