new-codigoparallevar/scripts/brain-query
2022-06-04 19:24:02 +02:00

41 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import xapian
import sys
import os
import json
def main(path, query):
db = xapian.Database(path, xapian.DB_OPEN)
docid_map_path = os.path.join(path, "docid_map.json")
with open(docid_map_path, 'rt') as f:
docid_to_node = json.load(f)
qp = xapian.QueryParser()
stemmer = xapian.Stem("english")
qp.set_stemmer(stemmer)
qp.set_database(db)
qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME)
xap_query = qp.parse_query(query)
print("Parsed query is: {}".format(xap_query))
enquire = xapian.Enquire(db)
enquire.set_query(xap_query)
matches = enquire.get_mset(0, 10)
for match in matches:
print(
"ID {} {}% | DocId: {}".format(
match.rank + 1,
match.percent,
docid_to_node[str(match.document.get_docid())],
)
)
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Brain-Query")
print("Usage: {} <path> <query>".format(sys.argv[0]))
exit(0)
main(sys.argv[1], sys.argv[2])