forked from ad-freiburg/aqqu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_translator.py
64 lines (55 loc) · 2.28 KB
/
console_translator.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
58
59
60
61
62
63
64
"""
Provides a console based translation interface.
Copyright 2015, University of Freiburg.
Elmar Haussmann <[email protected]>
"""
import logging
import globals
import scorer_globals
import sys
from query_translator.translator import QueryTranslator
logging.basicConfig(format="%(asctime)s : %(levelname)s "
": %(module)s : %(message)s",
level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
import argparse
parser = argparse.ArgumentParser(description="Console based translation.")
parser.add_argument("ranker_name",
default="WQ_Ranker",
help="The ranker to use.")
parser.add_argument("--config",
default="config.cfg",
help="The configuration file to use.")
args = parser.parse_args()
globals.read_configuration(args.config)
if args.ranker_name not in scorer_globals.scorers_dict:
logger.error("%s is not a valid ranker" % args.ranker_name)
logger.error("Valid rankers are: %s " % (" ".join(scorer_globals.scorers_dict.keys())))
logger.info("Using ranker %s" % args.ranker_name)
ranker = scorer_globals.scorers_dict[args.ranker_name]
translator = QueryTranslator.init_from_config()
translator.set_scorer(ranker)
while True:
sys.stdout.write("enter question> ")
sys.stdout.flush()
query = sys.stdin.readline().strip()
logger.info("Translating query: %s" % query)
results = translator.translate_and_execute_query(query)
logger.info("Done translating query: %s" % query)
logger.info("#candidates: %s" % len(results))
if len(results) > 0:
best_candidate = results[0].query_candidate
sparql_query = best_candidate.to_sparql_query()
result_rows = results[0].query_result_rows
result = []
# Usually we get a name + mid.
for r in result_rows:
if len(r) > 1:
result.append("%s (%s)" % (r[1], r[0]))
else:
result.append("%s" % r[0])
logger.info("SPARQL query: %s" % sparql_query)
logger.info("Result: %s " % " ".join(result))
if __name__ == "__main__":
main()