From e881401a1055c885981c0bf74d4944cddc1af785 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Tue, 10 Mar 2020 16:06:47 +0100
Subject: [PATCH] Update CQi stuff
---
app/corpora/CQiWrapper/CQiClient.py | 115 ++++++++++++++-------------
app/corpora/CQiWrapper/CQiWrapper.py | 4 +-
2 files changed, 63 insertions(+), 56 deletions(-)
diff --git a/app/corpora/CQiWrapper/CQiClient.py b/app/corpora/CQiWrapper/CQiClient.py
index 761a8739..fc620eca 100644
--- a/app/corpora/CQiWrapper/CQiClient.py
+++ b/app/corpora/CQiWrapper/CQiClient.py
@@ -17,14 +17,14 @@ class CQiClient:
self.__send_WORD(CQi.CTRL_CONNECT)
self.__send_STRING(username)
self.__send_STRING(password)
- self.__recv_response()
+ return self.__recv_response(CQi.STATUS_CONNECT_OK)
def ctrl_bye(self):
# INPUT: ()
# OUTPUT: CQI_STATUS_BYE_OK
# print('CTRL_BYE')
self.__send_WORD(CQi.CTRL_BYE)
- self.__recv_response()
+ return self.__recv_response(CQi.STATUS_BYE_OK)
def ctrl_user_abort(self):
# INPUT: ()
@@ -37,7 +37,7 @@ class CQiClient:
# OUTPUT: CQI_STATUS_PING_OK
# print('CTRL_PING')
self.__send_WORD(CQi.CTRL_PING)
- self.__recv_response()
+ return self.__recv_response(CQi.STATUS_PING_OK)
def ctrl_last_general_error(self):
# INPUT: ()
@@ -46,35 +46,35 @@ class CQiClient:
# CQi server
# print('CTRL_LAST_GENERAL_ERROR')
self.__send_WORD(CQi.CTRL_LAST_GENERAL_ERROR)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING)
def ask_feature_cqi_1_0(self):
# INPUT: ()
# OUTPUT: CQI_DATA_BOOL
# print('ASK_FEATURE_CQI_1_0')
self.__send_WORD(CQi.ASK_FEATURE_CQI_1_0)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_BOOL)
def ask_feature_cl_2_3(self):
# INPUT: ()
# OUTPUT: CQI_DATA_BOOL
# print('ASK_FEATURE_CL_2_3')
self.__send_WORD(CQi.ASK_FEATURE_CL_2_3)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_BOOL)
def ask_feature_cqp_2_3(self):
# INPUT: ()
# OUTPUT: CQI_DATA_BOOL
# print('ASK_FEATURE_CL_2_3')
self.__send_WORD(CQi.ASK_FEATURE_CL_2_3)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_BOOL)
def corpus_list_coprora(self):
# INPUT: ()
# OUTPUT: CQI_DATA_STRING_LIST
# print('CORPUS_LIST_CORPORA')
self.__send_WORD(CQi.CORPUS_LIST_CORPORA)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def corpus_charset(self, corpus):
# INPUT: (STRING corpus)
@@ -82,7 +82,7 @@ class CQiClient:
# print('CORPUS_CHARSET')
self.__send_WORD(CQi.CORPUS_CHARSET)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING)
def corpus_properties(self, corpus):
# INPUT: (STRING corpus)
@@ -90,7 +90,7 @@ class CQiClient:
# print('CORPUS_PROPERTIES')
self.__send_WORD(CQi.CORPUS_PROPERTIES)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def corpus_positional_attributes(self, corpus):
# INPUT: (STRING corpus)
@@ -98,7 +98,7 @@ class CQiClient:
# print('CORPUS_POSITIONAL_ATTRIBUTES')
self.__send_WORD(CQi.CORPUS_POSITIONAL_ATTRIBUTES)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def corpus_structural_attributes(self, corpus):
# INPUT: (STRING corpus)
@@ -106,7 +106,7 @@ class CQiClient:
# print('CORPUS_STRUCTURAL_ATTRIBUTES')
self.__send_WORD(CQi.CORPUS_STRUCTURAL_ATTRIBUTES)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def corpus_structural_attribute_has_values(self, attribute):
# INPUT: (STRING attribute)
@@ -114,7 +114,7 @@ class CQiClient:
# print('CORPUS_STRUCTURAL_ATTRIBUTE_HAS_VALUES')
self.__send_WORD(CQi.CORPUS_STRUCTURAL_ATTRIBUTE_HAS_VALUES)
self.__send_STRING(attribute)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_BOOL)
def corpus_alignment_attributes(self, corpus):
# INPUT: (STRING corpus)
@@ -122,7 +122,7 @@ class CQiClient:
# print('CORPUS_ALIGNMENT_ATTRIBUTES')
self.__send_WORD(CQi.CORPUS_ALIGNMENT_ATTRIBUTES)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def corpus_full_name(self, corpus):
# INPUT: (STRING corpus)
@@ -131,7 +131,7 @@ class CQiClient:
# print('CORPUS_FULL_NAME')
self.__send_WORD(CQi.CORPUS_FULL_NAME)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING)
def corpus_info(self, corpus):
# INPUT: (STRING corpus)
@@ -140,7 +140,7 @@ class CQiClient:
# print('CORPUS_INFO')
self.__send_WORD(CQi.CORPUS_INFO)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def corpus_drop_corpus(self, corpus):
'''
@@ -153,7 +153,7 @@ class CQiClient:
# print('CORPUS_DROP_CORPUS')
self.__send_WORD(CQi.CORPUS_DROP_CORPUS)
self.__send_STRING(corpus)
- self.__recv_response()
+ return self.__recv_response(CQi.STATUS_OK)
def cl_attribute_size(self, attribute):
# INPUT: (STRING attribute)
@@ -165,7 +165,7 @@ class CQiClient:
# print('CL_ATTRIBUTE_SIZE')
self.__send_WORD(CQi.CL_ATTRIBUTE_SIZE)
self.__send_STRING(attribute)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT)
def cl_lexicon_size(self, attribute):
# INPUT: (STRING attribute)
@@ -176,7 +176,7 @@ class CQiClient:
# print('CL_LEXICON_SIZE')
self.__send_WORD(CQi.CL_LEXICON_SIZE)
self.__send_STRING(attribute)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT)
def cl_drop_attribute(self, attribute):
# INPUT: (STRING attribute)
@@ -185,7 +185,7 @@ class CQiClient:
# print('CL_DROP_ATTRIBUTE')
self.__send_WORD(CQi.CL_LEXICON_SIZE)
self.__send_STRING(attribute)
- self.__recv_response()
+ return self.__recv_response(CQi.STATUS_OK)
"""
" NOTE: simple (scalar) mappings are applied to lists (the returned list
@@ -201,7 +201,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_LEXICON_SIZE)
self.__send_STRING(attribute)
self.__send_STRING_LIST(strings)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_id2str(self, attribute, id):
# INPUT: (STRING attribute, INT_LIST id)
@@ -211,7 +211,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_ID2STR)
self.__send_STRING(attribute)
self.__send_INT_LIST(id)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def cl_id2freq(self, attribute, id):
# INPUT: (STRING attribute, INT_LIST id)
@@ -221,7 +221,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_ID2FREQ)
self.__send_STRING(attribute)
self.__send_INT_LIST(id)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_cpos2id(self, attribute, cpos):
# INPUT: (STRING attribute, INT_LIST cpos)
@@ -231,7 +231,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_ID2FREQ)
self.__send_STRING(attribute)
self.__send_INT_LIST(cpos)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_cpos2str(self, attribute, cpos):
# INPUT: (STRING attribute, INT_LIST cpos)
@@ -241,7 +241,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_CPOS2STR)
self.__send_STRING(attribute)
self.__send_INT_LIST(cpos)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def cl_cpos2struc(self, attribute, cpos):
# INPUT: (STRING attribute, INT_LIST cpos)
@@ -251,7 +251,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_CPOS2STRUC)
self.__send_STRING(attribute)
self.__send_INT_LIST(cpos)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
"""
" NOTE: temporary addition for the Euralex2000 tutorial, but should
@@ -267,7 +267,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_CPOS2LBOUND)
self.__send_STRING(attribute)
self.__send_INT_LIST(cpos)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_cpos2rbound(self, attribute, cpos):
# INPUT: (STRING attribute, INT_LIST cpos)
@@ -278,7 +278,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_CPOS2RBOUND)
self.__send_STRING(attribute)
self.__send_INT_LIST(cpos)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_cpos2alg(self, attribute, cpos):
# INPUT: (STRING attribute, INT_LIST cpos)
@@ -288,7 +288,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_CPOS2ALG)
self.__send_STRING(attribute)
self.__send_INT_LIST(cpos)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_struc2str(self, attribute, strucs):
# INPUT: (STRING attribute, INT_LIST strucs)
@@ -300,7 +300,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_STRUC2STR)
self.__send_STRING(attribute)
self.__send_INT_LIST(strucs)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
"""
" NOTE: the following mappings take a single argument and return multiple
@@ -315,7 +315,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_ID2CPOS)
self.__send_STRING(attribute)
self.__send_INT(id)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_idlist2cpos(self, attribute, id_list):
# INPUT: (STRING attribute, INT_LIST id_list)
@@ -326,7 +326,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_IDLIST2CPOS)
self.__send_STRING(attribute)
self.__send_INT_LIST(id_list)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_regex2id(self, attribute, regex):
# INPUT: (STRING attribute, STRING regex)
@@ -337,7 +337,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_REGEX2ID)
self.__send_STRING(attribute)
self.__send_STRING(regex)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_struc2cpos(self, attribute, struc):
# INPUT: (STRING attribute, INT struc)
@@ -347,7 +347,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_STRUC2CPOS)
self.__send_STRING(attribute)
self.__send_INT(struc)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cl_alg2cpos(self, attribute, alg):
# INPUT: (STRING attribute, INT alg)
@@ -357,7 +357,7 @@ class CQiClient:
self.__send_WORD(CQi.CL_ALG2CPOS)
self.__send_STRING(attribute)
self.__send_INT(alg)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_INT_INT_INT)
def cqp_query(self, mother_corpus, subcorpus_name, query):
# INPUT: (STRING mother_corpus, STRING subcorpus_name, STRING query)
@@ -368,7 +368,7 @@ class CQiClient:
self.__send_STRING(mother_corpus)
self.__send_STRING(subcorpus_name)
self.__send_STRING(query)
- self.__recv_response()
+ return self.__recv_response(CQi.STATUS_OK)
def cqp_list_subcorpora(self, corpus):
# INPUT: (STRING corpus)
@@ -376,7 +376,7 @@ class CQiClient:
# print('CQP_LIST_SUBCORPORA')
self.__send_WORD(CQi.CQP_LIST_SUBCORPORA)
self.__send_STRING(corpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_STRING_LIST)
def cqp_subcorpus_size(self, subcorpus):
# INPUT: (STRING subcorpus)
@@ -384,7 +384,7 @@ class CQiClient:
# print('CQP_SUBCORPUS_SIZE')
self.__send_WORD(CQi.CQP_SUBCORPUS_SIZE)
self.__send_STRING(subcorpus)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT)
def cqp_subcorpus_has_field(self, subcorpus, field):
# INPUT: (STRING subcorpus, BYTE field)
@@ -393,7 +393,7 @@ class CQiClient:
self.__send_WORD(CQi.CQP_SUBCORPUS_HAS_FIELD)
self.__send_STRING(subcorpus)
self.__send_BYTE(field)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_BOOL)
def cqp_dump_subcorpus(self, subcorpus, field, first, last):
# INPUT: (STRING subcorpus, BYTE field, INT first, INT last)
@@ -406,7 +406,7 @@ class CQiClient:
self.__send_BYTE(field)
self.__send_INT(first)
self.__send_INT(last)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cqp_drop_subcorpus(self, subcorpus):
# INPUT: (STRING subcorpus)
@@ -415,7 +415,7 @@ class CQiClient:
# print('CQP_DROP_SUBCORPUS')
self.__send_WORD(CQi.CQP_DROP_SUBCORPUS)
self.__send_STRING(subcorpus)
- self.__recv_response()
+ return self.__recv_response(CQi.STATUS_OK)
"""
" NOTE: The following two functions are temporarily included for the
@@ -436,7 +436,7 @@ class CQiClient:
self.__send_INT(cutoff)
self.__send_BYTE(field)
self.__send_STRING(attribute)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
def cqp_fdist_2(self, subcorpus, cutoff, field1, attribute1, field2,
attribute2):
@@ -455,28 +455,35 @@ class CQiClient:
self.__send_STRING(attribute1)
self.__send_BYTE(field2)
self.__send_STRING(attribute2)
- return self.__recv_response()
+ return self.__recv_response(CQi.DATA_INT_LIST)
- def __recv_response(self):
+ def __recv_response(self, expected_response_type=None):
byte_data = self.__recv_WORD()
response_type = byte_data >> 8
- if response_type == CQi.STATUS:
- response = byte_data
- elif response_type == CQi.ERROR:
- raise Exception(CQi.lookup[byte_data])
- elif response_type == CQi.ERROR_SYNTAX_ERROR:
- raise Exception(CQi.lookup[byte_data])
- elif response_type == CQi.DATA:
- response = self.__recv_DATA(byte_data)
- elif response_type == CQi.CL_ERROR:
+ if response_type == CQi.CL_ERROR:
raise Exception(CQi.lookup[byte_data])
elif response_type == CQi.CQP_ERROR:
raise Exception(CQi.lookup[byte_data])
+ elif response_type == CQi.DATA:
+ if (expected_response_type is not None
+ and byte_data != expected_response_type):
+ raise Exception("Expected different response type."
+ + "Received: {}, ".format(byte_data)
+ + "Expected {}".format(expected_response_type))
+ return self.__recv_DATA(byte_data)
+ elif response_type == CQi.ERROR:
+ raise Exception(CQi.lookup[byte_data])
+ elif response_type == CQi.STATUS:
+ if (expected_response_type is not None
+ and byte_data != expected_response_type):
+ raise Exception("Expected different response type."
+ + "Received: {}, ".format(byte_data)
+ + "Expected {}".format(expected_response_type))
+ return byte_data
else:
raise Exception(
'Unknown response type: {}'.format(hex(response_type))
)
- return response
def __recv_DATA(self, data_type):
if data_type == CQi.DATA_BYTE:
diff --git a/app/corpora/CQiWrapper/CQiWrapper.py b/app/corpora/CQiWrapper/CQiWrapper.py
index fb50e15b..dd0d29bc 100644
--- a/app/corpora/CQiWrapper/CQiWrapper.py
+++ b/app/corpora/CQiWrapper/CQiWrapper.py
@@ -20,8 +20,8 @@ class CQiWrapper(CQiClient):
SUBCORPUS_NAMES = []
- def __init__(self, host='127.0.0.1', port=4877, username='opaque',
- password='opaque'):
+ def __init__(self, host='127.0.0.1', port=4877, username='anonymous',
+ password=''):
super(CQiWrapper, self).__init__(host=host, port=port)
self.username = username
self.password = password