langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,82 @@
TYPE_CONV_PATH "Inverted_index"
type files = string array with sexp
type inverted_index = (string * int list) list with sexp
type t = files * inverted_index with sexp
open Sexplib
let data_file = "data.inv"
let data_path = Filename.concat Filename.temp_dir_name data_file
let get_inv_index() =
if Sys.file_exists data_path
then t_of_sexp(Sexp.load_sexp data_path)
else ([| |], [])
let load_file f =
let ic = open_in f in
let n = in_channel_length ic in
let s = String.create n in
really_input ic s 0 n;
close_in ic;
(s)
let array_push ar v =
let len = Array.length ar in
Array.init (succ len) (fun i ->
if i < len then Array.unsafe_get ar i else v), len
let uniq lst =
let h = Hashtbl.create (List.length lst) in
List.iter (fun x -> Hashtbl.replace h x ()) lst;
Hashtbl.fold (fun x () xs -> x :: xs) h []
let combine words i inv_index =
let h = Hashtbl.create (List.length inv_index) in
List.iter (fun (w, from) -> Hashtbl.replace h w from) inv_index;
List.iter (fun w ->
if Hashtbl.mem h w
then begin
let from = Hashtbl.find h w in
Hashtbl.replace h w (i::from)
end else
Hashtbl.add h w [i]
) words;
Hashtbl.fold (fun w from acc -> (w, from) :: acc) h []
let words_of_file in_file =
let str = load_file in_file in
let words = Str.split (Str.regexp "[ \r\n\t,;.?!:'/\034()]") str in
let words = uniq words in
(words)
let index_file in_file =
let words = words_of_file in_file in
let files, inv_index = get_inv_index() in
let files, i = array_push files in_file in
let inv_index = combine words i inv_index in
let se = sexp_of_t (files, inv_index) in
Sexp.save data_path se
let search_word word =
let files, inv_index = get_inv_index() in
try
let is_in = List.assoc word inv_index in
List.iter (fun i -> print_endline files.(i)) is_in
with Not_found ->
print_endline "# Not Found"
let usage() =
Printf.printf "Usage: %s \
--index-file <file.txt> / \
--search-word <some-word>\n%!" Sys.argv.(0);
exit 1
let () =
let cmd, arg = try (Sys.argv.(1), Sys.argv.(2)) with _ -> usage() in
match cmd, arg with
| "--index-file", in_file -> index_file in_file
| "--search-word", word -> search_word word
| _ -> usage()

View file

@ -0,0 +1,12 @@
sub MAIN (*@files) {
(my %norm).push: do for @files -> $file {
$file X=> slurp($file).lc.words;
}
(my %inv).push: %norm.invert.uniq;
while prompt("Search terms: ").words -> @words {
for @words -> $word {
say "$word => %inv.{$word.lc}";
}
}
}

View file

@ -0,0 +1,30 @@
$$ MODE TUSCRIPT
files="file1'file2'file3"
LOOP file=files
ERROR/STOP CREATE (file,seq-o,-std-)
ENDLOOP
content1="it is what it is"
content2="what is it"
content3="it is a banana"
FILE/ERASE "file1" = content1
FILE/ERASE "file2" = content2
FILE/ERASE "file3" = content3
ASK "search for": search=""
IF (search=="") STOP
BUILD R_TABLE/USER/AND search = *
DATA {search}
LOOP/CLEAR file=files
ACCESS q: READ/RECORDS $file s.z/u,content,count
LOOP
COUNT/NEXT/EXIT q (-; search;-;-)
IF (count!=0) files=APPEND (files," ",file)
ENDLOOP
ENDACCESs q
ENDLOOP
PRINT "-> ",files

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 "$@"