[honey] Fix portability to systems without pread()
[xapian.git] / xapian-bindings / lua / docs / examples / simpleexpand.lua
blob394b6c83aec64fc543ec3b372c50a7adbfbe5cbd
1 #!/usr/bin/env lua
2 --
3 -- Simple example script demonstrating query expansion.
4 --
5 -- Copyright (C) 2011 Xiaona Han
6 --
7 -- This program is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation; either version 2 of the
10 -- License, or (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
20 -- USA
22 require("xapian")
24 -- Require at least two command line arguments.
25 if #arg < 2 then
26 io.stderr:write("Usage:" .. arg[0] .. " PATH_TO_DATABASE QUERY [-- [DOCID...]]\n")
27 os.exit()
28 end
30 -- Open the database for searching.
31 database = xapian.Database(arg[1])
33 -- Start an enquire session.
34 enquire = xapian.Enquire(database)
36 -- Combine command line arguments up to "--" with spaces between
37 -- them, so that simple queries don't have to be quoted at the shell level.
38 query_string = arg[2]
39 local index = 3
40 while index <= #arg do
41 local para = arg[index]
42 index = index + 1;
43 if para == '--' then
44 break
45 end
46 query_string = query_string .. ' ' .. para
47 end
49 -- Create an RSet with the listed docids in.
50 reldocs = xapian.RSet()
52 --for index in range(index, #arg) do
53 for i = index, #arg do
54 reldocs:add_document(tonumber(arg[index]))
55 index = index + 1
56 end
58 -- Parse the query string to produce a Xapian::Query object.
59 qp = xapian.QueryParser()
60 stemmer = xapian.Stem("english")
61 qp:set_stemmer(stemmer)
62 qp:set_database(database)
63 qp:set_stemming_strategy(xapian.QueryParser_STEM_SOME)
64 query = qp:parse_query(query_string)
66 if not query:empty() then
67 print("Parsed query is: " .. tostring(query))
69 -- Find the top 10 results for the query.
70 enquire:set_query(query)
71 matches = enquire:get_mset(0, 10)
73 -- Display the size of the results.
74 print(string.format("%i results found.", matches:get_matches_estimated()))
75 print(string.format("Results 1-%i:", matches:size()))
77 -- Display the results
78 for m in matches:items() do
79 print(m:get_rank() + 1, m:get_percent() .. "%", m:get_docid(), m:get_document():get_data())
80 end
82 -- Put the top 5 (at most) docs into the rset if rset is empty
83 if reldocs:empty() then
84 local c = 5
85 for m in matches:items() do
86 reldocs:add_document(m:get_docid())
87 print (c)
88 c = c - 1
89 if c == 0 then
90 break
91 end
92 end
93 end
95 -- Get the suggested expand terms
96 eterms = enquire:get_eset(10, reldocs)
97 print (string.format("%i suggested additional terms", eterms:size()))
98 for m in eterms:terms() do
99 print(string.format("%s: %f", m:get_term(), m:get_weight()))