Python builds a question answering system for Jingdong Mall based on knowledge graphs - answer acquisition

In the previous article, we mainly introduced how to extract key information from the received user questions and how to identify the user's intention. Then we will introduce how to query the answer in the knowledge map after obtaining this information. When I dealt with this problem, I thought very directly. Simply put, each question template corresponds to a user intent, so write query statements according to each intent. This is a simple and crude method. The advantage is that Only simple, of course there are many disadvantages, such as not conducive to maintenance and expansion. Now you must have the awareness of maintenance and expansion, but the specific implementation is that if you want to quickly implement a demo, you can use this simple and crude method. Here's how to get answers.

First, I defined a method dictionary for question templates. Each key corresponds to the number of the template, and value is the method to query the answer based on the template. The structure is as follows:

    def __init__(self):
        self.q_template_dict={
            0:self.get_computer_rating,
            1:self.get_computer_price,
            2:self.get_computer_type,
            3:self.get_computer_introduction,
            4:self.get_computer_disposition,
            5:self.get_computer_info,
            6:self.get_computer_weight,
            7:self.get_computer_rating_smaller,
            8:self.get_computer_rating_bigger,
            9:self.get_computer_color,
            10:self.get_computer_cpu,
            11:self.get_computer_gpu,
            12:self.get_computer_postage,
            13:self.get_computer_goods,
            14:self.get_computer_courier,
            15: self.get_computer_refresh,
            16: self.get_computer_ssd,
            17: self.get_computer_origin,
            18: self.get_computer_thickness,
            19: self.get_computer_Screen_color
        }

What work do we need to do when we get the user question and the template of the question?
First, use the method introduced in the previous article to obtain the key information of the problem, and then call the corresponding method according to the template number. The code of the overall framework is as follows:

    def get_question_answer(self,question,template):
        # 如果问题模板的格式不正确则结束
        assert len(str(template).strip().split("\t"))==2
        template_id,template_str=int(str(template).strip().split("\t")[0]),str(template).strip().split("\t")[1]
        self.template_id=template_id
        self.template_str2list=str(template_str).split()

        # 预处理问题
        question_word,question_flag=[],[]
        for one in question:
            word, flag = one.split("/")
            question_word.append(str(word).strip())
            question_flag.append(str(flag).strip())
        assert len(question_flag)==len(question_word)
        self.question_word=question_word
        self.question_flag=question_flag
        self.raw_question=question
        # 根据问题模板来做对应的处理,获取答案
        answer=self.q_template_dict[template_id]()
        return answer

Enter the corresponding method, and use the Cypher language to construct the query statement, and its basic form is:

match(n)-[r] -(b)

It doesn't matter if you don't know much about the Cypher language. You can also directly use the python library py2neo to operate the graph database neo4j. This only involves query operations, so I directly constructed Cypher query statements, and then used the run method of the py2neo library to query. I wrote a separate class for database links and queries, which can be called when needed:

from py2neo import Graph,Node,Relationship,NodeMatcher

class Query():
    def __init__(self):
        self.graph=Graph("http://localhost:7474", username="neo4j",password="123456")

    # 问题类型0,查询电脑得评论
    def run(self,cql):
        # find_rela  = Graph.run("match (n:Comments_jd)-[r:computers]->(p:Genre_jd) where p.name='{computer_name}' return n.acclaim")
        result=[]
        find_rela = self.graph.run(cql)
        for i in find_rela:
            result.append(i.items()[0][1])
        return result

For example, here is an example of querying the profile of a certain computer. The code for querying the answer is:

    # 3:nm 简介
    def get_computer_introduction(self):
        computer_name = self.get_computer_name()
        cql = f"match (n:Price_jd)-[r:computers]->(p:Genre_jd) where p.name='{computer_name}' return n.sell"
        print(cql)
        answer = self.graph.run(cql)
        final_answer = computer_name + "的简介是" + str(answer) + "!"
        return final_answer

It is mainly to construct a Cypher statement, and then call the run method of py2neo to process the obtained answer in these steps, and then return the answer.

Guess you like

Origin blog.csdn.net/weixin_45897172/article/details/131002654