diff --git a/common/tsakorpus_query_parser.py b/common/tsakorpus_query_parser.py index f9c7b9bd79305b871be7ec40c1aab0541d9b7599..c1f6c8718c04bd5e58962c942f027a0b480768f3 100644 --- a/common/tsakorpus_query_parser.py +++ b/common/tsakorpus_query_parser.py @@ -1,4 +1,4 @@ -import copy +from urllib.parse import quote import re from .query_parser import QueryParser from .config import ResourceConfig @@ -11,6 +11,22 @@ class TsakorpusQueryParser(QueryParser): rxTsakorpusBool = re.compile('[()|,]') + def build_get_string(self, getParams, config): + """ + Build a GET string (everything after the ?) from a description + of the GET parameters in the getParams list. + """ + nWords = len(self.term_indexes(getParams)) + s = 'n_words=' + str(nWords) + for param in getParams: + if type(param[1]) is list: + index = '_'.join(str(v) for v in param[1]) + else: + index = str(param[1]) + s += '&' + param[0] + index + '=' + quote(param[2]) + s += '&page_size=' + str(config.max_hits) + return s + def term_query(self, query, config): """ Return list of query parameters for one term or sequence of terms. @@ -95,13 +111,17 @@ class TsakorpusQueryParser(QueryParser): The function is recursive and only looks at the part of the string delimited by start and end parameters. """ - print(query, start, end) if end == -1: + # Top-level call, so return a finalized GET string end = len(query) if end == 0: raise Diagnostic(DiagnosticTypes.sru, 27) if self.rxTermQuery.search(query) is not None: - return self.term_query(query, config) + return self.build_get_string(self.term_query(query, config), config) + return self.build_get_string(self.translate_fcsql(query, config, + basicSearch=basicSearch, + start=start, end=end), + config) # if query.count('(') != query.count(')'): # return None if len(query) <= 0: