Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,23 @@
;; set of input files
(define FILES {T0.txt T1.txt T2.txt})
;; store name for permanent inverted index
(define INVERT "INVERTED-INDEX")
;; get text for each file, and call (action filename text)
(define (map-files action files)
(for ((file files))
(file->string action file)))
;; create store
(local-make-store INVERT)
; invert-word : word -> set of files
(define (invert-word word file store)
(local-put-value word
(make-set (cons file (local-get-value word store))) store))
; parse file text and invert each word
(define (invert-text file text)
(writeln 'Inverting file text)
(let ((text (text-parse text)))
(for ((word text)) (invert-word (string-downcase word) file INVERT))))

View file

@ -0,0 +1,11 @@
;; usage : (inverted-search w1 w2 ..)
(define-syntax-rule (inverted-search w ...)
(and-get-invert (quote w )))
;; intersects all sets referenced by words
;; returns the intersection
(define (and-get-invert words)
(foldl
(lambda(word res)
(set-intersect res (local-get-value word INVERT)))
FILES words))

View file

@ -0,0 +1,9 @@
(map-files invert-text FILES)
(inverted-search is it)
[0]→ { T0.txt T1.txt T2.txt }
(inverted-search is banana)
[1]→ { T2.txt }
(inverted-search is what)
[2]→ { T0.txt T1.txt }
(inverted-search boule)
[3]→ null

View file

@ -0,0 +1,81 @@
integer word_count = 0
sequence filenames = {}
function is_ascii(string txt)
for i=1 to length(txt) do
integer ch = txt[i]
if ch='\0' or ch>=#7F then return 0 end if
end for
return 1
end function
procedure add_words(string name, sequence words)
filenames = append(filenames,name)
integer fn = length(filenames)
for i=1 to length(words) do
string word = words[i]
if word[1]>='a' -- skip numbers
and word[1]<='z' then
integer node = getd_index(word)
if node=0 then -- not present
setd(word,{fn})
word_count += 1
else
sequence val = getd_by_index(node)
if find(fn,val)=0 then
setd(word,append(val,fn))
end if
end if
end if
end for
end procedure
procedure load_directory()
sequence d = dir(".")
for i=1 to length(d) do
if not find('d',d[i][D_ATTRIBUTES]) -- skip directories
and d[i][D_SIZE]<1024*1024*1024 then -- and files > 1GB
string name = d[i][D_NAME]
integer fn = open(name,"rb")
string txt = lower(get_text(fn))
close(fn)
if is_ascii(txt) then -- skip any bitmaps etc
sequence words = split_any(txt,"\0\r\n\t !\"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~",0,1)
add_words(name,words)
end if
end if
end for
end procedure
function lookup(sequence words)
sequence files = {} -- indexes to filenames
for i=1 to length(words) do
string word = words[i]
integer node = getd_index(word)
if node=0 then return {} end if
sequence val = getd_by_index(node)
if i=1 then
files = val
else
for j=length(files) to 1 by -1 do
if not find(files[j],val) then
files[j..j] = {}
end if
end for
if length(files)=0 then return {} end if
end if
end for
for i=1 to length(files) do
files[i] = filenames[files[i]]
end for
return files
end function
load_directory()
?word_count
?lookup({"load_directory"}) -- should only be this file
?lookup({"dir"}) -- currently two use this
?lookup({"lower"}) -- currently four use this
?lookup({"lower","dir"}) -- currently two use both
?lookup({"dir","lower"}) -- should be the same two
?lookup({"ban"&"anafish"}) -- should be none ({})

View file

@ -0,0 +1,18 @@
# Given an array of [ doc, array_of_distinct_words ]
# construct a lookup table: { word: array_of_docs }
def inverted_index:
reduce .[] as $pair
({};
$pair[0] as $doc
| reduce $pair[1][] as $word
(.; .[$word] += [$doc]));
def search(words):
def overlap(that): . as $this
| reduce that[] as $item ([]; if $this|index($item) then . + [$item] else . end);
. as $dict
| if (words|length) == 0 then []
else reduce words[1:][] as $word
( $dict[words[0]]; overlap( $dict[$word] ) )
end ;

View file

@ -0,0 +1,8 @@
def prompt_search:
"Enter a string or an array of strings to search for, quoting each string, or 0 to exit:",
( (input | if type == "array" then . elif type == "string" then [.]
else empty
end) as $in
| search($in), prompt_search ) ;
$in | inverted_index | prompt_search

View file

@ -0,0 +1,10 @@
$ jq -r -c -n --argfile in <(jq -R 'split(" ") | select(length>0) | [input_filename, unique]' T?.txt) -f Inverted_index.jq
Enter a string or an array of strings to search for, quoting each string, or 0 to exit:
"is"
["T0.txt","T1.txt","T2.txt"]
Enter a string or an array of strings to search for, quoting each string, or 0 to exit:
["is", "banana"]
["T2.txt"]
Enter a string or an array of strings to search for, quoting each string, or 0 to exit:
0
$