-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
57 lines (47 loc) · 1.91 KB
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Copyright 2017, University of Freiburg
Chair of Algorithms and Data Structures.
Hannah Bast <[email protected]>
Claudius Korzen <[email protected]>
Theresa Klumpp <[email protected]>
"""
import re
import readline # NOQA
import argparse
import pickle
from inverted_index import InvertedIndex # NOQA
def main(precomputed_file):
# Create a new inverted index from the given file.
print("Reading from file '%s'." % precomputed_file)
ii = pickle.load(open(precomputed_file, "rb"))
print("Query the inverted index to find the most relevant hits. Enter any "
"amount of keywords. Type 'num_res=<n>' to change the number of "
"results presented to you. Use ctrl+d to leave the program.")
k = 3 # number of results shown
while True:
try:
# Ask the user for a keyword query.
query = input("\nYour keyword query: ")
except (KeyboardInterrupt, EOFError):
print("\nBye!")
break
m = re.match(r"num_res=([0-9]+)$", query)
if m:
k = int(m.group(1))
print(f"Changed the number of results shown to {k}.")
continue
# Split the query into keywords.
keywords = [x.lower().strip() for x in re.split("[^A-Za-z]+", query)]
# Process the keywords.
postings = ii.process_query(keywords)
# Render the output (with ANSI codes to highlight the keywords).
ii.render_output(postings, keywords, k)
if __name__ == "__main__":
# Parse the command line arguments.
parser = argparse.ArgumentParser(description="""Query a precomputed
inverted index to find the most relavent hits.""")
parser.add_argument("precomputed_file", type=str, help="""Pickle file
containing a precomputed inverted index. To generate such a file,
use 'inverted_index.py'.""")
args = parser.parse_args()
main(args.precomputed_file)