Beispiel #1
0
    def handle(self):
        # 初始化语义服务器
        # 从 qa 初始化

        # 从 qa_sql 初始化
        # robot = Robot(path=getConfig("path", "db"), password=None)
        while True:
            # self.request is the TCP socket connected to the client
            self.data = self.request.recv(2048)
            if not self.data:
                break
            print("\n{} wrote:".format(self.client_address[0]))
            self.data = self.data.decode("UTF-8")
            print("Data:\n", self.data)
            # step 1.Bytes to json obj and extract question
            json_data = json.loads(self.data)
            # step 2.Get answer
            if "ask_content" in json_data.keys():
                answer = self.robot.search(question=json_data["ask_content"],
                                           userid=json_data["userid"],
                                           key=json_data["key"])
                info = json_data["ask_content"]
                # 其中 result['picurl'] 为 xml 格式
                result = answer2xml(answer)
            elif "config_content" in json_data.keys():
                answer = self.robot.configure(info=json_data["config_content"],
                                              userid=json_data["userid"],
                                              key=json_data["key"])
                info = json_data["config_content"]
                result = answer
            print(answer)
            print(result)
            # step 3.Send
            try:
                self.request.sendall(json.dumps(result).encode("UTF-8"))
            except:
                with open(logpath, "a", encoding="UTF-8") as file:
                    file.write(
                        get_current_time("%Y-%m-%d %H:%M:%S") + "\n" +
                        "发送失败\n")
            # 追加日志
            with open(logpath, "a", encoding="UTF-8") as file:
                # 写入接收数据中的内容字段
                file.write(
                    get_current_time("%Y-%m-%d %H:%M:%S") + "\n" + info + "\n")
                # 写入正常问答
                if "ask_content" in json_data.keys():
                    for key in [
                            "question", "content", "behavior", "url",
                            "context", "parameter", "picurl"
                    ]:
                        file.write(key + ": " + str(result[key]) + "\n")
                # 写入配置信息
                elif "config_content" in json_data.keys():
                    file.write("Config: " + json.dumps(result) + "\n")
                file.write("\n")
Beispiel #2
0
 def __init__(self, password="******", userid="A0001"):
     self.graph = Graph(getConfig("neo4j", "uri"), password=password)
     print(self.graph)
     self.selector = NodeSelector(self.graph)
     # self.locations = get_navigation_location()
     self.is_scene = False
     self.user = None
     # self.user = self.selector.select("User", userid=userid).first()
     # self.usertopics = self.get_usertopics(userid=userid)
     # self.address = get_location_by_ip(self.user['city'])
     self.topic = ""
     self.behavior = 0  # 场景类型 Add in 2018-6-7
     self.last_step_error = False  # 在场景内上一个问答是否正常 Add in 2018-6-12
     self.qa_id = get_current_time()
     self.qmemory = deque(maxlen=10)
     self.amemory = deque(maxlen=10)
     self.pmemory = deque(maxlen=10)
     # TODO:判断意图是否在当前话题领域内
     self.change_attention = ["换个话题吧", "太无聊了", "没意思", "别说了"]
     self.cmd_end_scene = ["退出业务场景", "退出场景", "退出", "返回", "结束", "发挥"]
     self.cmd_previous_step = ["上一步", "上一部", "上一页", "上一个"]
     self.cmd_next_step = ["下一步", "下一部", "下一页", "下一个"]
     self.cmd_repeat = ["重复", "再来一个", "再来一遍", "你刚说什么", "再说一遍", "重来"]
     self.yes = ["是", "是的", "对", "对的", "好的", "YES", "yes", "结束了"]
     self.no = ["没", "没有", "否", "不", "没有结束", "没结束"]
     self.do_not_know = [
         "这个问题太难了,{robotname}还在学习中", "这个问题{robotname}不会,要么我去问下",
         "您刚才说的是什么,可以再重复一遍吗", "{robotname}刚才走神了,一不小心没听清",
         "{robotname}理解的不是很清楚啦,你就换种方式表达呗", "不如我们换个话题吧", "咱们聊点别的吧",
         "{robotname}正在学习中", "{robotname}正在学习哦", "不好意思请问您可以再说一次吗",
         "额,这个问题嘛。。。", "{robotname}得好好想一想呢", "请问您说什么", "您问的问题好有深度呀",
         "{robotname}没有听明白,您能再说一遍吗"
     ]
Beispiel #3
0
def add_to_memory(Q="Q",
                  words=None,
                  tags=None,
                  content=None,
                  username="******"):
    """
    将用户当前语句的语义分析结果加入信息记忆	
    """
    # 添加到记忆链,每一类型的节点都需要重构
    qaID = username + "_" + get_current_time()
    for semantic_tree in content:
        generate_tree(NodeClass="Memory",
                      Q=Q,
                      words=words,
                      content=semantic_tree,
                      tags=tags,
                      username=username)
Beispiel #4
0
def test_txt():
    current_user = "******"
	
    print("QA测试......") 
    filename = "log/QA_" + get_current_time() + ".md"
    f = open(filename, "w")
    f.write("标签:测试文档\n#QA测试:\n>Enter the QA mode...\n")
    while True:
        try:
            sentence = input("\n>>")
			# 基于语义模式匹配
            answer = search_database(question=sentence, username=current_user)
			# 基于上下文理解
            # answer = understand_context(question=sentence, username=current_user)
            print("R: " + answer)			
            f.write("`>>" + sentence + "`\n")
            f.write("`" + "A: " + answer + "`\n")
        except KeyboardInterrupt:
            f.close()
Beispiel #5
0
def extract_synonym(question, subgraph, pattern="wf"):
    """
    从图形数据库返回的搜索结果列表选取相似度最高的语义Jaccard匹配同义句
    pattern可选'w'-分词, 't'-关键词, 'wf'-分词标签, 'tf-关键词标签'
    """
    similarity = []
    answer = "您好!请问有什么可以帮您的吗?"
    do_not_know = [
        "正在学习中", "小民正在学习哦", "不好意思请问您可以再说一次吗", "额,这个问题嘛。。。", "我得好好想一想呢",
        "请问您说什么", "。。。", "您问的问题好有深度呀"
    ]
    command = {
        "time": ["几点了", "现在几点了", "现在时间", "现在时间是多少"],
        "cmd": ["打电话", "打电话给"]
    }
    if question in command["time"]:
        answer = get_current_time("现在是%Y年%m月%d日%H点%M分%S秒")
        return answer
    elif question in command["cmd"]:
        answer = "正在连接中,请稍候"
        return answer
    sv1 = synonym_cut(question, pattern)
    for node in subgraph:
        sv2 = synonym_cut(node["Q"], pattern)
        ss = semantic_similarity(sv1, sv2, pattern="sj")
        similarity.append(ss)
    max_similarity = max(similarity)
    print("Similarity Score: " + str(max_similarity))
    index = similarity.index(max_similarity)
    Q = subgraph[index]["Q"]
    A = subgraph[index]["A"]
    print("Q: " + Q)
    if max_similarity > 0.2:
        if isinstance(A, list):
            answer = random_item(A)
    else:
        # 随机回答
        answer = random_item(do_not_know)
    return answer
Beispiel #6
0
    def add_to_memory(self, question="question", userid="A0001"):
        """Add user question to memory.
        将用户当前对话加入信息记忆。

        Args:
            question: 用户问题。
                Defaults to "question".
            userid: 用户唯一标识。
                Defaults to "userid".
        """
        previous_node = self.graph.find_one("Memory", "qa_id", self.qa_id)
        self.qa_id = get_current_time()
        node = Node("Memory",
                    question=question,
                    userid=userid,
                    qa_id=self.qa_id)
        if previous_node:
            relation = Relationship(previous_node, "next", node)
            self.graph.create(relation)
        else:
            self.graph.create(node)

    # def extract_navigation(self, question):
        """Extract navigation from question。从问题中抽取导航地点。
Beispiel #7
0
    """
    graph = Graph("http://localhost:7474/db/data/", password="******")
    sv = []
    for word in words:
        word_node = graph.find_one("Synonym", "word", word)
        if word_node:
            tag_node = graph.find_one("SynonymTag", "name", word_node["tag"])
            tag_words = tag_node["words"]
            synonym_random = random_item(tag_words)
            sv.append(synonym_random)
    return sv


if __name__ == '__main__':
    print("语义相似度测试......")
    filename = "log/SemanticSimilarity_" + get_current_time() + ".md"
    f = open(filename, "w")
    f.write("标签:测试文档\n#语义相似度测试:\n>Enter the SemanticSimilarity mode...\n")

    while True:
        try:
            sentence1 = input("\nsentence1\n>>")
            sentence2 = input("sentence2\n>>")
            w1 = synonym_cut(sentence1, 'w')
            w2 = synonym_cut(sentence2, 'w')
            sv1 = synonym_cut(sentence1, 'wf')
            sv2 = synonym_cut(sentence2, 'wf')
            print(w1, w2)
            similarity = semantic_similarity(w1, w2)
            print("similarity: " + str(similarity))
            print(sv1, sv2)
Beispiel #8
0
            score = word_similarity(w1, w2)
            sv_rows.append(score)
        sv_matrix.append(sv_rows)
        sv_rows = []	
    matrix = numpy.mat(sv_matrix)	
    result = sum_cosine(matrix, 0.6)
    total = result["total"]
    total_dif = result["total_dif"]
    m = result["m"]
    similarity = total/(total + m*(1-total_dif))
	
    return similarity
	
if __name__ == '__main__':
    print("向量语义相似度测试......") 
    filename = "log/VecSimilarity_" + get_current_time() + ".md"
    f = open(filename, "w")
    f.write("标签:测试文档\n#向量语义相似度测试:\n>Enter the VecSimilarity mode...\n")

    while True:
        try:
            sentence1 = input("\nsentence1\n>>")
            sentence2 = input("sentence2\n>>")
            similarity = vec_jaccard(sentence1, sentence2, 'w')
            print("similarity: " + str(similarity))
            similarity = vec_jaccard(sentence1, sentence2, 't')
            print("similarity: " + str(similarity))
            
            f.write("`>>" + sentence1 + "`\n")
            f.write("`>>" + sentence2 + "`\n")
            f.write("`" + "similarity: " + str(similarity) + "`\n")