Inverted index: Difference between revisions

no edit summary
m (→‎{{header|REXX}}: added a wikipedia link for the REXX entry (regarding the data read for the program).)
No edit summary
Line 1,054:
Enter a word to search for: (q to quit): q
quitting.</pre>
 
=={{header|EchoLisp}}==
 
===Indexing===
Index values are sets associates with each word (key). We use the local-put-value function to permanently store the index, in the browser local storage.
<lang lisp>
;; 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))))
</lang>
=== Query ===
Intersect sets values of each word.
<lang lisp>
;; 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))
</lang>
Output :
<lang lisp>
(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
</lang>
 
 
=={{header|Erlang}}==