From d8f76456a15f58f67248a3305bd91bd16a73cc18 Mon Sep 17 00:00:00 2001
From: Timofey Arkhangelskiy <timofey.arkhangelskiy@uni-hamburg.de>
Date: Sun, 11 Dec 2022 21:20:10 +0100
Subject: [PATCH] Build GET string for simple Tsakorpus queries

---
 common/tsakorpus_query_parser.py | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/common/tsakorpus_query_parser.py b/common/tsakorpus_query_parser.py
index f9c7b9b..c1f6c87 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:
-- 
GitLab