# Path: chat/client.py
# def match(question="question", userid="A0001", key="A0001"):
#     """Match the answers from the semantic knowledge database.
#     从语义知识数据库搜索答案。
# 
#     Args:
#         question: User question. 用户问题。
#             Defaults to "question".
#         userid: User id. 用户唯一标识。
#             Defaults to "userid".
# 
#     Returns:
#         Packaged JSON format data of answer. 打包好的答案json格式数据。
#     """
#     send = question_pack(question, userid, key)
#     mysock.sendall(send.encode("UTF-8"))
#     received = mysock.recv(4096) # 2048->4096(2018-1-5 添加了 xml 后扩充)
#     received = received.decode("UTF-8")
#     return received
# 
# def config(info="", userid="A0001", key="A0001"):
#     """Configure the semantic knowledge database.
#     配置语义知识数据库。
# 
#     Args:
#         info: User config info. 用户配置信息，以空格分隔的知识库名称字符串。
#             Defaults to "". 返回可配置选项信息。
#         userid: User id. 用户唯一标识。
#             Defaults to "userid".
# 
#     Returns:
#         Packaged JSON format data of config result. 打包好的配置结果json格式数据。
#     """
#     send = config_pack(info, userid)
#     mysock.sendall(send.encode("UTF-8"))
#     received = mysock.recv(4096) # 2048->4096(2018-1-5 添加了 xml 后扩充)
#     received = received.decode("UTF-8")
#     return received
# 
# def batch_test(filename, userid="A0001", key="A0001"):
#     """Batch test.
#     """
#     assert filename is not None, "filename can not be None"
#     data = []
#     with open("testresult.txt", 'w', encoding="UTF-8") as file:
#         with open(filename, 'r', encoding="UTF-8") as testcase:
#             for line in testcase:
#                 if not line:
#                     continue
#                 question = line.rstrip()
#                 result = json.loads(match(question=question, userid=userid, key=key))
#                 data.append(result)
#                 for key in ["question", "content", "behavior", "url", "context", "parameter"]:
#                     file.write(key + ": " + str(result[key]) + "\n")
#                 file.write("\n")
#     return data
# 
# def start(userid=None, key=None):
#     """Start Client.
#     启动客户端。
#     """
#     while True:
#         question = input("\n>>question=")
#         if not userid:
#             userid = "A0001"
#             key = "A0001"
#         if question == "config":
#             result = config(info="", userid=userid, key=key)
#         else:
#             result = match(question=question, userid=userid, key=key)
#         # print(json.loads(result))
#         print(json.loads(result)['content'])


import os
import sys
import json
from unittest import TestCase, main
from chat.client import match, config, batch_test, start

        # for sentence in sentences:
            # result = match(question=sentence, userid=self.userid)
            # print(sentence, ':\n', result)
        
        pass
    
    def test_config(self):
        # result = json.loads(config(info="", userid="A0001"))
        # databases = result.setdefault('databases', [])
        # akbs = [item['name'] for item in databases if item['available']==1 ]
        # print('akbs: ', akbs)
        # skbs = [item['name'] for item in databases if item['bselected']==1 ]
        # print('skbs: ', skbs)
        
        # result = config(info=' '.join(skbs[:-1]), userid="A0001")
        # result = config(info=' '.join(akbs), userid="A0001")
        # print('config: ', result)
        # result = json.loads(config(info="", userid="A0001"))
        # databases = result.setdefault('databases', [])
        # akbs = [item['name'] for item in databases if item['available']==1 ]
        # print('akbs: ', akbs)
        # skbs = [item['name'] for item in databases if item['bselected']==1 ]
        # print('skbs: ', skbs)
        
        pass
        
    def test_batch_test(self):
        # batch_test("testcase.txt")
        pass


# Path: chat/client.py
# def match(question="question", userid="A0001", key="A0001"):
#     """Match the answers from the semantic knowledge database.
#     从语义知识数据库搜索答案。
# 
#     Args:
#         question: User question. 用户问题。
#             Defaults to "question".
#         userid: User id. 用户唯一标识。
#             Defaults to "userid".
# 
#     Returns:
#         Packaged JSON format data of answer. 打包好的答案json格式数据。
#     """
#     send = question_pack(question, userid, key)
#     mysock.sendall(send.encode("UTF-8"))
#     received = mysock.recv(4096) # 2048->4096(2018-1-5 添加了 xml 后扩充)
#     received = received.decode("UTF-8")
#     return received
# 
# def config(info="", userid="A0001", key="A0001"):
#     """Configure the semantic knowledge database.
#     配置语义知识数据库。
# 
#     Args:
#         info: User config info. 用户配置信息，以空格分隔的知识库名称字符串。
#             Defaults to "". 返回可配置选项信息。
#         userid: User id. 用户唯一标识。
#             Defaults to "userid".
# 
#     Returns:
#         Packaged JSON format data of config result. 打包好的配置结果json格式数据。
#     """
#     send = config_pack(info, userid)
#     mysock.sendall(send.encode("UTF-8"))
#     received = mysock.recv(4096) # 2048->4096(2018-1-5 添加了 xml 后扩充)
#     received = received.decode("UTF-8")
#     return received
# 
# def batch_test(filename, userid="A0001", key="A0001"):
#     """Batch test.
#     """
#     assert filename is not None, "filename can not be None"
#     data = []
#     with open("testresult.txt", 'w', encoding="UTF-8") as file:
#         with open(filename, 'r', encoding="UTF-8") as testcase:
#             for line in testcase:
#                 if not line:
#                     continue
#                 question = line.rstrip()
#                 result = json.loads(match(question=question, userid=userid, key=key))
#                 data.append(result)
#                 for key in ["question", "content", "behavior", "url", "context", "parameter"]:
#                     file.write(key + ": " + str(result[key]) + "\n")
#                 file.write("\n")
#     return data
# 
# def start(userid=None, key=None):
#     """Start Client.
#     启动客户端。
#     """
#     while True:
#         question = input("\n>>question=")
#         if not userid:
#             userid = "A0001"
#             key = "A0001"
#         if question == "config":
#             result = config(info="", userid=userid, key=key)
#         else:
#             result = match(question=question, userid=userid, key=key)
#         # print(json.loads(result))
#         print(json.loads(result)['content'])


# Path: tests/test_client.py
import os
import sys
import json
from unittest import TestCase, main
from chat.client import match, config, batch_test, start

        # for sentence in sentences:
            # result = match(question=sentence, userid=self.userid)
            # print(sentence, ':\n', result)
        
        pass
    
    def test_config(self):
        # result = json.loads(config(info="", userid="A0001"))
        # databases = result.setdefault('databases', [])
        # akbs = [item['name'] for item in databases if item['available']==1 ]
        # print('akbs: ', akbs)
        # skbs = [item['name'] for item in databases if item['bselected']==1 ]
        # print('skbs: ', skbs)
        
        # result = config(info=' '.join(skbs[:-1]), userid="A0001")
        # result = config(info=' '.join(akbs), userid="A0001")
        # print('config: ', result)
        # result = json.loads(config(info="", userid="A0001"))
        # databases = result.setdefault('databases', [])
        # akbs = [item['name'] for item in databases if item['available']==1 ]
        # print('akbs: ', akbs)
        # skbs = [item['name'] for item in databases if item['bselected']==1 ]
        # print('skbs: ', skbs)
        
        pass
        
    def test_batch_test(self):
        # batch_test("testcase.txt")
        pass


    def test_start(self):
