lib/vsm: introduce an indexing process based on VSM
[nit.git] / lib / vsm / examples / example_vsm.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Example using a `FileIndex`
16 #
17 # This example shows of to index files from the system and retrieve them
18 # with text queries.
19 module example_vsm
20
21 import vsm
22 import config
23
24 redef class Config
25
26 # --whitelist-exts
27 var opt_white_exts = new OptionArray("Allowed file extensions (default is [])",
28 "-w", "--whitelist-exts")
29
30 # --blacklist-exts
31 var opt_black_exts = new OptionArray("Allowed file extensions (default is [])",
32 "-b", "--blacklist-exts")
33
34 redef init do
35 opts.add_option(opt_white_exts, opt_black_exts)
36 end
37 end
38
39 var config = new Config
40 config.tool_description = "usage: example_vsm <files>"
41 config.parse_options(args)
42
43 if args.length < 1 then
44 config.usage
45 exit 1
46 end
47
48 var index = new FileIndex
49 index.whitelist_exts = config.opt_white_exts.value
50 index.blacklist_exts = config.opt_black_exts.value
51
52 print "Building index..."
53 index.index_files(args, true)
54 print "Indexed {index.documents.length} documents"
55
56 loop
57 print "\nEnter query:"
58 printn "> "
59 var input = sys.stdin.read_line
60 var matches = index.match_string(input)
61 printn ""
62 for match in matches do
63 print match
64 end
65 end