Document where to install .jar on macOS
[xapian.git] / xapian-bindings / tcl8 / docs / examples / simpleexpand.tcl
blobf3017f80166383fe84bc17868ec6c6f254b31db9
1 #!/usr/bin/env tclsh
2 # Simple example Tcl script demonstrating query expansion.
4 # Copyright (C) 2004,2006,2007 Olly Betts
5 # Copyright (C) 2004 Michael Schlenker
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 # We need at least Tcl version 8.1.
22 package require Tcl 8.1
23 # We need only actually need Xapian 0.9.3 (for the query from list constructor
24 # wrapper), but "package require" doesn't accept differing major versions.
25 package require xapian 1.0.0
27 if {[llength $argv] < 2} {
28 puts "Usage: $argv0 PATH_TO_DATABASE QUERY [-- [DOCID...]]"
29 exit 1
32 if {[catch {
33 xapian::Database database [lindex $argv 0]
35 # Start an enquire session.
36 xapian::Enquire enquire database
38 # Combine command line arguments up to "--" with spaces between
39 # them, so that simple queries don't have to be quoted at the shell
40 # level.
41 set args [lrange $argv 1 end]
42 set sep [lsearch -exact $args "--"]
44 if {$sep == -1} {
45 set sep [llength $args]
46 } else {
47 incr sep -1
50 set query_string [join [lrange $args 0 $sep]]
52 xapian::RSet rset
53 incr sep 2
54 foreach docid [lrange $args $sep end] {
55 puts "$sep $docid"
56 rset add_document $docid
59 xapian::QueryParser qp
60 xapian::Stem stemmer "english"
61 qp set_stemmer stemmer
62 qp set_database database
63 qp set_stemming_strategy $xapian::QueryParser_STEM_SOME
64 set query [qp parse_query $query_string]
65 puts "Parsed query is: [$query get_description]"
67 # Find the top 10 results for the query.
68 enquire set_query $query
69 set matches [enquire get_mset 0 10 rset]
71 # Display the results.
72 puts "[$matches get_matches_estimated] results found:"
74 for {set i [$matches begin]} {![$i equals [$matches end]]} {$i next} {
75 xapian::Document document [$i get_document]
76 set rank [expr [$i get_rank] + 1]
77 puts [format {%s: %s%% docid=%s [%s]} \
78 $rank [$i get_percent] [$i get_docid] [document get_data]]
81 # If no relevant docids were given, invent an RSet containing the top 5
82 # matches (or all the matches if there are less than 5).
83 if {[rset empty]} {
84 set c 5
85 set i [$matches begin]
86 while {$c > 0 && ![$i equals [$matches end]]} {
87 rset add_document [$i get_docid]
88 $i next
89 incr c -1
93 # Generate an ESet containing terms that the user might want to add to
94 # the query.
95 xapian::ESet eset [enquire get_eset 10 rset]
97 # List the terms.
98 for {set t [eset begin]} {![$t equals [eset end]]} {$t next} {
99 puts [format {%s: weight = %f} [$t get_term] [$t get_weight]]
101 } exception]} {
102 puts stderr "Exception: $errorCode $exception"
103 exit 1