Location>code7788 >text

Knowledge graph-based medical Q&A system (dockerfile+docker-compose)

Popularity:898 ℃/2024-12-18 22:58:18

catalogs
  • I. Building the Neo4j graph database
    • 1. Choice of approach
    • 2, Dockerfile + docker-compose deployment neo4j container
      • 2.1 Updating the yum mirror source
      • 2.2 Installing docker-ce community edition
      • 2.3 Configure mirror acceleration
      • 2.4 Installing Docker Compose
        • 2.4.1 Downloading the Docker Compose Binary Package
        • 2.4.2 Setting of executable privileges
        • 2.4.3. Viewing the version
      • 2.5 Create a directory structure
      • 2.6 Preparation of configuration files
      • 2.7 Writing the dockerfile file
      • 2.8. Build ne4j container image
      • 2.9 Preparation of documentation
      • 2.10 Running docker-compose
      • 2.11. Browser login neo4j
  • Neo4j initial configuration
    • 1. Empty the Neo4j database.
  • PyCharm project to install the necessary libraries
    • 1. py2neo library
    • 2. pymongo library
    • 3、lxml library
  • IV. python connection Neo4j
    • 1、Browser browser View Neo4j connection status
    • 2. Modify the Graph connection format in the source file
  • V. PyCharm Importing Medical Knowledge Graphs
    • 1. Read the file
    • 2、Establishment of nodes
    • 3. Creating nodes for diseases at the center of the knowledge graph
    • 4、Create knowledge graph entity node type schema
    • 5. Create entity relationship edges
    • 6. Create entity-associated edges
    • 7、Export data
    • 8. Main entrance to the program
      • 8.1、UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 81: illegal multibyte sequence
      • 8.2. Modify the code: for data in open(self.data_path)::
    • 9. Running results
    • 10、Optimize the time of importing data
  • Six, PyCharm realization of the question and answer system
    • 1. Script for categorizing types of interrogative sentences
    • 2. Question parsing script
    • 3. Question and answer program scripts
    • 4. Question and answer system realization
      • 4.1. Model initialization
      • 4.2. Q&A Master Functions
      • 4.3. Running the main entrance
      • 4.4. Operational results

In advance: reference to Mr. Liu Huanyong's open source project on Github

GitHub Address:A Knowledge Graph-based Medical Q&A System

I. Building the Neo4j graph database

1. Choice of approach

  • windows using Neo4j Desktop (from 2024-12-09 Neo4j desktop can not be opened manifested as three/four zombie processes, check the local logs will find [403] can not get to https://dist./neo4j-desktop/win/Resources for this path. Solution: Open Neo4j Desktop without internet connection.Neo4j Desktop 1.5.8 Launches Zombie Processes Only - Neo4j Graph Platform / Desktop - Neo4j Online Community
  • Cloud environment dockerfile + docker-compose (easy to understand deployment builds without focusing on jdk version, preferred)
  • Final Idealization: kubernetes deployment (in line with mainstream technology direction, although deployment is more complex and pitched, but enterprise and industry dominance are factors that make k8s deployment a best practice)

First time deployments are prioritized with dockerfile + docker-compose

2, Dockerfile + docker-compose deployment neo4j container

2.1 Updating the yum mirror source

rm -rf /etc//*
wget -O /etc// /repo/
wget -O /etc// /repo/
wget -O /etc// /docker-ce/linux/centos/

2.2 Installing docker-ce community edition

yum install -y docker-ce

2.3 Configure mirror acceleration

cat > /etc/docker/ << EOF
{
  "exec-opts": ["=systemd"],
  "registry-mirrors": [
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
  ]
}
EOF

systemctl daemon-reload && systemctl restart docker && systemctl enable docker

2.4 Installing Docker Compose

Releases · docker/compose

2.4.1 Downloading the Docker Compose Binary Package

curl -L "/docker/compose/releases/download/v2.5.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  • -L: YescurlAn option that indicates follow redirection. If the download link is redirected, this option will make thecurlAutomatic tracking to the last destination address.
  • "/docker/compose/releases/download/v2.5.1/docker-compose-$(uname -s)-$(uname -m)": This is the download URL for Docker Compose, where thev2.5.1Specifies the version number of Docker Compose to download.$(uname -s) respond in singing$(uname -m) are shell commands that respectively return the current system type (e.g.Linux) and the hardware architecture of the machine (e.g.x86_64), this ensures that the Docker Compose binaries matching the current system architecture are downloaded.
  • -o /usr/local/bin/docker-compose: -o maybe--output Specifies the location and name where the downloaded file will be saved. Here, the file will be saved as/usr/local/bin/docker-composeThis is a common installation path for Docker Compose, and placing it here puts it in the PATH environment variable so that it can be accessed directly from the command line via thedocker-composecommand invocation.

2.4.2 Setting of executable privileges

chmod +x /usr/local/bin/docker-compose

2.4.3. Viewing the version

docker-compose -v

2.5 Create a directory structure

mkdir -p neo4j-docker/{conf,data,import,logs} && touch neo4j-docker/conf/

chown -R neo4j:neo4j ./{conf,data,import,logs}

chmod 755 ./{conf,data,logs,import}

tree -L 2 neo4j-docker
neo4j-docker
├── conf
│   └── 
├── data
├── import
└── logs

2.6 Preparation of configuration files

cat > /root/neo4j-docker/conf/ <<  EOF
=/var/lib/neo4j/import
=512M

server.default_listen_address=0.0.0.0
.allow_csv_import_from_file_urls=true
=/logs
EOF

2.7 Writing the dockerfile file

cat > /root/neo4j-docker/Dockerfile << EOF
# Use the latest version of the official Neo4j image as the base image
FROM neo4j:latest

# Set environment variables, only used to configure Neo4j authentication
ENV NEO4J_AUTH=neo4j/neo4jpassword

# Copy the local configuration file into the container
COPY . /conf/ /var/lib/neo4j/conf/

# Define the command to be executed when the container starts
CMD ["neo4j"]
EOF

2.8. Build ne4j container image

# The command location needs to be the same level as the Dockerfile location
docker build -t my_neo4j:v1 .

image-20241210102548272

2.9 Preparation of documentation

Pitfall: the neo4j version requires more than 8 digits.

version: '3'
services:
  neo4j:
    build: .
    image: my_neo4j:v1
    container_name: neo4j_container
    restart: always
    ports:
      - "7474:7474"
      - "7687:7687"
    environment:
      - NEO4J_AUTH=neo4j/neo4jpassword
    volumes:
      - ./data:/data
      - ./logs:/logs
      - ./import:/var/lib/neo4j/import
      - ./conf:/var/lib/neo4j/conf
    command: ["neo4j"]

2.10 Running docker-compose

docker-compose -f  up -d

2.11. Browser login neo4j

http://192.168.112.30:7474

# Enter username:neo4j
# Enter password:neo4jpassword

Neo4j initial configuration

1. Empty the Neo4j database.

MATCH (n) DETACH DELETE n

image-20241217225238581

PyCharm project to install the necessary libraries

1. py2neo library

pip install py2neo
  • Simplifying Neo4j Connections and Queries

    • Connect to Neo4jpy2neo Provides an easy-to-use interface to connect to the Neo4j database , supports HTTP and Bolt protocols .
    • Execute Cypher Querypy2neo Allows you to directly executeCypher query (Neo4j's graph query language) and returns the result as a Python object.
  • Creating and managing graph data

    • Creating nodes and relationshipspy2neo provides high-level abstractions that allow you to create and manage nodes and relationships in Neo4j as if they were Python objects. You can create and manage nodes and relationships in Neo4j as if they were Python objects using theNode respond in singingRelationship class to represent the entities in the diagram and save them to the database.
    • batch operationpy2neo Supports batch creation of nodes and relationships to improve performance and reduce network round trips.

2. pymongo library

pip install pymongo
  • Used to connect to and manipulate MongoDB databases to read, process, and reinsert medical data.
  • Provides efficient CRUD operations and supports batch data processing.

3、lxml library

pip install lxml
  • Used to parse HTML documents stored in MongoDB to extract useful medical examination information (e.g., disease name, description, etc.).
  • Extract the data via XPath and perform the necessary cleanup and formatting.

IV. python connection Neo4j

1、Browser browser View Neo4j connection status

:server status

image-20241217231334624

Remember the URL (not http:// in the traditional sense, and the default port number 7474)

2. Modify the Graph connection format in the source file

import os
import json
from py2neo import Graph,Node

class MedicalGraph:
    def __init__(self):
        cur_dir = '/'.join((__file__).split('/')[:-1])
        self.data_path = (cur_dir, 'data/')
         = Graph("neo4j://192.168.112.30:7687", auth=("neo4j", "neo4jpassword"))

build_medicalgraph.py respond in singinganswer_search.py The two original files in the = Graph() of the connection format are changed to the format in the above code.

V. PyCharm Importing Medical Knowledge Graphs

1. Read the file

# Read file
    def read_nodes(self):
        # common7类节点
        drugs = [] # 药品
        foods = [] # 食物
        checks = [] # 检查
        departments = [] #科室
        producers = [] #药品大类
        diseases = [] #illnesses
        symptoms = []#症状

        disease_infos = []#illnesses信息

        # 构建节点实体关系
        rels_department = [] # 科室-科室关系
        rels_noteat = [] # illnesses-忌吃食物关系
        rels_doeat = [] # illnesses-Food Relationships
        rels_recommandeat = [] # illnesses-Recommended Food Relationships
        rels_commonddrug = [] # illnesses-Generic drug relationship
        rels_recommanddrug = [] # illnesses-Top Drug Relationships
        rels_check = [] # illnesses-Check the relationship
        rels_drug_producer = [] # company-Drug Relationship

        rels_symptom = [] #illnesses症状关系
        rels_acompany = [] # illnesses并发关系
        rels_category = [] # illnesses与科室之间的关系


        count = 0
        for data in open(self.data_path, encoding='utf8', mode='r'):
            disease_dict = {}
            count += 1
            print(count)
            data_json = (data)
            disease = data_json['name']
            disease_dict['name'] = disease
            (disease)
            disease_dict['desc'] = ''
            disease_dict['prevent'] = ''
            disease_dict['cause'] = ''
            disease_dict['easy_get'] = ''
            disease_dict['cure_department'] = ''
            disease_dict['cure_way'] = ''
            disease_dict['cure_lasttime'] = ''
            disease_dict['symptom'] = ''
            disease_dict['cured_prob'] = ''

            if 'symptom' in data_json:
                symptoms += data_json['symptom']
                for symptom in data_json['symptom']:
                    rels_symptom.append([disease, symptom])

            if 'acompany' in data_json:
                for acompany in data_json['acompany']:
                    rels_acompany.append([disease, acompany])

            if 'desc' in data_json:
                disease_dict['desc'] = data_json['desc']

            if 'prevent' in data_json:
                disease_dict['prevent'] = data_json['prevent']

            if 'cause' in data_json:
                disease_dict['cause'] = data_json['cause']

            if 'get_prob' in data_json:
                disease_dict['get_prob'] = data_json['get_prob']

            if 'easy_get' in data_json:
                disease_dict['easy_get'] = data_json['easy_get']

            if 'cure_department' in data_json:
                cure_department = data_json['cure_department']
                if len(cure_department) == 1:
                     rels_category.append([disease, cure_department[0]])
                if len(cure_department) == 2:
                    big = cure_department[0]
                    small = cure_department[1]
                    rels_department.append([small, big])
                    rels_category.append([disease, small])

                disease_dict['cure_department'] = cure_department
                departments += cure_department

            if 'cure_way' in data_json:
                disease_dict['cure_way'] = data_json['cure_way']

            if 'cure_lasttime' in data_json:
                disease_dict['cure_lasttime'] = data_json['cure_lasttime']

            if 'cured_prob' in data_json:
                disease_dict['cured_prob'] = data_json['cured_prob']

            if 'common_drug' in data_json:
                common_drug = data_json['common_drug']
                for drug in common_drug:
                    rels_commonddrug.append([disease, drug])
                drugs += common_drug

            if 'recommand_drug' in data_json:
                recommand_drug = data_json['recommand_drug']
                drugs += recommand_drug
                for drug in recommand_drug:
                    rels_recommanddrug.append([disease, drug])

            if 'not_eat' in data_json:
                not_eat = data_json['not_eat']
                for _not in not_eat:
                    rels_noteat.append([disease, _not])

                foods += not_eat
                do_eat = data_json['do_eat']
                for _do in do_eat:
                    rels_doeat.append([disease, _do])

                foods += do_eat
                recommand_eat = data_json['recommand_eat']

                for _recommand in recommand_eat:
                    rels_recommandeat.append([disease, _recommand])
                foods += recommand_eat

            if 'check' in data_json:
                check = data_json['check']
                for _check in check:
                    rels_check.append([disease, _check])
                checks += check
            if 'drug_detail' in data_json:
                drug_detail = data_json['drug_detail']
                producer = [('(')[0] for i in drug_detail]
                rels_drug_producer += [[('(')[0], ('(')[-1].replace(')', '')] for i in drug_detail]
                producers += producer
            disease_infos.append(disease_dict)
        return set(drugs), set(foods), set(checks), set(departments), set(producers), set(symptoms), set(diseases), disease_infos,\
               rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,\
               rels_symptom, rels_acompany, rels_category

2、Establishment of nodes

# Creating nodes
    def create_node(self, label, nodes):
        count = 0
        for node_name in nodes:
            node = Node(label, name=node_name)
            (node)
            count += 1
            print(count, len(nodes))
        return

3. Creating nodes for diseases at the center of the knowledge graph

# Creating nodes for diseases at the center of the knowledge graph
    def create_diseases_nodes(self, disease_infos):
        count = 0
        for disease_dict in disease_infos:
            node = Node("Disease", name=disease_dict['name'], desc=disease_dict['desc'],
                        prevent=disease_dict['prevent'] ,cause=disease_dict['cause'],
                        easy_get=disease_dict['easy_get'],cure_lasttime=disease_dict['cure_lasttime'],
                        cure_department=disease_dict['cure_department']
                        ,cure_way=disease_dict['cure_way'] , cured_prob=disease_dict['cured_prob'])
            (node)
            count += 1
            print(count)
        return

4、Create knowledge graph entity node type schema

# Creating Knowledge Graph Entity Node Typesschema
    def create_graphnodes(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos,rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,rels_symptom, rels_acompany, rels_category = self.read_nodes()
        self.create_diseases_nodes(disease_infos)
        self.create_node('Drug', Drugs)
        print(len(Drugs))
        self.create_node('Food', Foods)
        print(len(Foods))
        self.create_node('Check', Checks)
        print(len(Checks))
        self.create_node('Department', Departments)
        print(len(Departments))
        self.create_node('Producer', Producers)
        print(len(Producers))
        self.create_node('Symptom', Symptoms)
        return

5. Create entity relationship edges

# 创建实体关系边
    def create_graphrels(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,rels_symptom, rels_acompany, rels_category = self.read_nodes()
        self.create_relationship('Disease', 'Food', rels_recommandeat, 'recommand_eat', 'Recommended Recipes')
        self.create_relationship('Disease', 'Food', rels_noteat, 'no_eat', 'avoid eating')
        self.create_relationship('Disease', 'Food', rels_doeat, 'do_eat', 'apt eat')
        self.create_relationship('Department', 'Department', rels_department, 'belongs_to', 'be part of')
        self.create_relationship('Disease', 'Drug', rels_commonddrug, 'common_drug', 'Commonly used medicines')
        self.create_relationship('Producer', 'Drug', rels_drug_producer, 'drugs_of', 'Pharmaceutical production')
        self.create_relationship('Disease', 'Drug', rels_recommanddrug, 'recommand_drug', 'Favorable Drugs')
        self.create_relationship('Disease', 'Check', rels_check, 'need_check', 'diagnostic examination')
        self.create_relationship('Disease', 'Symptom', rels_symptom, 'has_symptom', 'symptomatic')
        self.create_relationship('Disease', 'Disease', rels_acompany, 'acompany_with', 'complications (undesired side-effect of medical procedure)')
        self.create_relationship('Disease', 'Department', rels_category, 'belongs_to', 'Section')

6. Create entity-associated edges

# Creating Entity Association Edges
    def create_relationship(self, start_node, end_node, edges, rel_type, rel_name):
        count = 0
        # de-duplication
        set_edges = []
        for edge in edges:
            set_edges.append('###'.join(edge))
        all = len(set(set_edges))
        for edge in set(set_edges):
            edge = ('###')
            p = edge[0]
            q = edge[1]
            query = "match(p:%s),(q:%s) where ='%s'and ='%s' create (p)-[rel:%s{name:'%s'}]->(q)" % (
                start_node, end_node, p, q, rel_type, rel_name)
            try:
                (query)
                count += 1
                print(rel_type, count, all)
            except Exception as e:
                print(e)
        return

7、Export data

# Export data
    def export_data(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug, rels_symptom, rels_acompany, rels_category = self.read_nodes()
        f_drug = open('', 'w+')
        f_food = open('', 'w+')
        f_check = open('', 'w+')
        f_department = open('', 'w+')
        f_producer = open('', 'w+')
        f_symptom = open('', 'w+')
        f_disease = open('', 'w+')

        f_drug.write('\n'.join(list(Drugs)))
        f_food.write('\n'.join(list(Foods)))
        f_check.write('\n'.join(list(Checks)))
        f_department.write('\n'.join(list(Departments)))
        f_producer.write('\n'.join(list(Producers)))
        f_symptom.write('\n'.join(list(Symptoms)))
        f_disease.write('\n'.join(list(Diseases)))

        f_drug.close()
        f_food.close()
        f_check.close()
        f_department.close()
        f_producer.close()
        f_symptom.close()
        f_disease.close()

        return

8. Main entrance to the program

if __name__ == '__main__'.
    handler = MedicalGraph()
    print("step1: importing into graph nodes")
    handler.create_graphnodes()
    print("step2: importing graph edges")
    handler.create_graphrels()
# create knowledge nodes and edges (nodes + rels)
# handler.create_graphnodes()
# handler.create_graphrels()
Shortcut: Ctrl + Shift + F10

8.1、UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 81: illegal multibyte sequence

Running it directly will report an error:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 81: illegal multibyte sequence

8.2. Modify the code: for data in open(self.data_path)::

for data in open(self.data_path, encoding='utf8', mode='r'):
  • 需要确保文件的编码格式为 utf8
  • Open files in read-only mode

9. Running results

image-20241217234528761

10、Optimize the time of importing data

import concurrent
import 
import json
import multiprocessing
import os

from py2neo import Graph, Node, Subgraph
from tqdm import tqdm


class MedicalGraph:
    def __init__(self):
        pass

    def clear(self):
        ("MATCH (n) DETACH DELETE n")

    '''Read file'''

    def read_nodes(self):
        # common7class node
        drugs = []  # medicaments
        foods = []  # foods
        checks = []  # probe
        departments = []  # administrative division
        producers = []  # medicaments大类
        diseases = []  # illnesses
        symptoms = []  # symptomatic

        disease_infos = []  # illnesses信息

        # Constructing node entity relationships
        rels_department = []  # administrative division-administrative division关系
        rels_noteat = []  # illnesses-avoid eatingfoods关系
        rels_doeat = []  # illnesses-apt eatfoods关系
        rels_recommandeat = []  # illnesses-推荐吃foods关系
        rels_commonddrug = []  # illnesses-通用medicaments关系
        rels_recommanddrug = []  # illnesses-热门medicaments关系
        rels_check = []  # illnesses-probe关系
        rels_drug_producer = []  # company-Drug Relationship

        rels_symptom = []  # illnessessymptomatic关系
        rels_acompany = []  # illnesses并发关系
        rels_category = []  # illnesses与administrative division之间的关系

        for data in open(self.data_path):
            disease_dict = {}
            data_json = (data)
            disease = data_json['name']
            disease_dict['name'] = disease
            (disease)
            disease_dict['desc'] = ''
            disease_dict['prevent'] = ''
            disease_dict['cause'] = ''
            disease_dict['easy_get'] = ''
            disease_dict['cure_department'] = ''
            disease_dict['cure_way'] = ''
            disease_dict['cure_lasttime'] = ''
            disease_dict['symptom'] = ''
            disease_dict['cured_prob'] = ''

            if 'symptom' in data_json:
                symptoms += data_json['symptom']
                for symptom in data_json['symptom']:
                    rels_symptom.append([disease, symptom])

            if 'acompany' in data_json:
                for acompany in data_json['acompany']:
                    rels_acompany.append([disease, acompany])

            if 'desc' in data_json:
                disease_dict['desc'] = data_json['desc']

            if 'prevent' in data_json:
                disease_dict['prevent'] = data_json['prevent']

            if 'cause' in data_json:
                disease_dict['cause'] = data_json['cause']

            if 'get_prob' in data_json:
                disease_dict['get_prob'] = data_json['get_prob']

            if 'easy_get' in data_json:
                disease_dict['easy_get'] = data_json['easy_get']

            if 'cure_department' in data_json:
                cure_department = data_json['cure_department']
                if len(cure_department) == 1:
                    rels_category.append([disease, cure_department[0]])
                if len(cure_department) == 2:
                    big = cure_department[0]
                    small = cure_department[1]
                    rels_department.append([small, big])
                    rels_category.append([disease, small])

                disease_dict['cure_department'] = cure_department
                departments += cure_department

            if 'cure_way' in data_json:
                disease_dict['cure_way'] = data_json['cure_way']

            if 'cure_lasttime' in data_json:
                disease_dict['cure_lasttime'] = data_json['cure_lasttime']

            if 'cured_prob' in data_json:
                disease_dict['cured_prob'] = data_json['cured_prob']

            if 'common_drug' in data_json:
                common_drug = data_json['common_drug']
                for drug in common_drug:
                    rels_commonddrug.append([disease, drug])
                drugs += common_drug

            if 'recommand_drug' in data_json:
                recommand_drug = data_json['recommand_drug']
                drugs += recommand_drug
                for drug in recommand_drug:
                    rels_recommanddrug.append([disease, drug])

            if 'not_eat' in data_json:
                not_eat = data_json['not_eat']
                for _not in not_eat:
                    rels_noteat.append([disease, _not])

                foods += not_eat
                do_eat = data_json['do_eat']
                for _do in do_eat:
                    rels_doeat.append([disease, _do])

                foods += do_eat
                recommand_eat = data_json['recommand_eat']

                for _recommand in recommand_eat:
                    rels_recommandeat.append([disease, _recommand])
                foods += recommand_eat

            if 'check' in data_json:
                check = data_json['check']
                for _check in check:
                    rels_check.append([disease, _check])
                checks += check
            if 'drug_detail' in data_json:
                drug_detail = data_json['drug_detail']
                producer = [('(')[0] for i in drug_detail]
                rels_drug_producer += [[('(')[0], ('(')[-1].replace(')', '')] for i in drug_detail]
                producers += producer
            disease_infos.append(disease_dict)
        return set(drugs), set(foods), set(checks), set(departments), set(producers), set(symptoms), set(diseases), disease_infos, \
            rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug, \
            rels_symptom, rels_acompany, rels_category

    '''Creating nodes'''

    def create_node(self, label, nodes):
        batch_size = 1000
        batches = [list(nodes)[i:i + batch_size] for i in range(0, len(nodes), batch_size)]
        for batch in tqdm(batches, desc=f"Creating {label} Nodes", unit="batch"):
            batch_nodes = [Node(label, name=node_name) for node_name in batch]
            (Subgraph(batch_nodes))

    '''创建知识图谱中心illnesses的节点'''

    def create_diseases_nodes(self, disease_infos):
        batch_size = 1000
        batches = [disease_infos[i:i + batch_size] for i in range(0, len(disease_infos), batch_size)]
        for batch in tqdm(batches, desc="Importing Disease Nodes", unit="batch"):
            batch_nodes = [
                Node("Disease", name=disease_dict['name'], desc=disease_dict['desc'],
                     prevent=disease_dict['prevent'], cause=disease_dict['cause'],
                     easy_get=disease_dict['easy_get'], cure_lasttime=disease_dict['cure_lasttime'],
                     cure_department=disease_dict['cure_department'], cure_way=disease_dict['cure_way'],
                     cured_prob=disease_dict['cured_prob']) for disease_dict in batch
            ]
            (Subgraph(batch_nodes))

    '''Creating Knowledge Graph Entity Node Typesschema'''

    def create_graphnodes(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug, rels_symptom, rels_acompany, rels_category = self.read_nodes()
        self.create_diseases_nodes(disease_infos)
        self.create_node('Drug', Drugs)
        self.create_node('Food', Foods)
        self.create_node('Check', Checks)
        self.create_node('Department', Departments)
        self.create_node('Producer', Producers)
        self.create_node('Symptom', Symptoms)

    '''Creating Entity Relationship Edges'''

    def create_graphrels(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug, rels_symptom, rels_acompany, rels_category = self.read_nodes()
        self.create_relationship('Disease', 'Food', rels_recommandeat, 'recommand_eat', 'Recommended Recipes')
        self.create_relationship('Disease', 'Food', rels_noteat, 'no_eat', 'avoid eating')
        self.create_relationship('Disease', 'Food', rels_doeat, 'do_eat', 'apt eat')
        self.create_relationship('Department', 'Department', rels_department, 'belongs_to', 'be part of')
        self.create_relationship('Disease', 'Drug', rels_commonddrug, 'common_drug', '常用medicaments')
        self.create_relationship('Producer', 'Drug', rels_drug_producer, 'drugs_of', '生产medicaments')
        self.create_relationship('Disease', 'Drug', rels_recommanddrug, 'recommand_drug', '好评medicaments')
        self.create_relationship('Disease', 'Check', rels_check, 'need_check', '诊断probe')
        self.create_relationship('Disease', 'Symptom', rels_symptom, 'has_symptom', 'symptomatic')
        self.create_relationship('Disease', 'Disease', rels_acompany, 'acompany_with', 'complications (undesired side-effect of medical procedure)')
        self.create_relationship('Disease', 'Department', rels_category, 'belongs_to', '所属administrative division')

    '''Creating Entity Association Edges'''

    def create_relationship(self, start_node, end_node, edges, rel_type, rel_name):
        batch_size = 10000
        set_edges = set(['###'.join(edge) for edge in edges])
        batches = [list(set_edges)[i:i + batch_size] for i in range(0, len(set_edges), batch_size)]
        executor = (max_workers=min(multiprocessing.cpu_count(), 4))
        tasks = [
            lambda: (
                tx := (),
                [
                    (
                        f"MATCH (p:{start_node}), (q:{end_node}) "
                        f"WHERE ='{p}' AND ='{q}' "
                        f"CREATE (p)-[rel:{rel_type} {{name:'{rel_name}'}}]->(q)"
                    ) for edge in batch for p, q in [('###')]
                ],
                (tx)
            ) for batch in tqdm(batches, desc=f"Creating {rel_type} Relationships", unit="batch")
        ]
        (lambda task: task(), tasks)
        ()

    '''Export data'''

    def export_data(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug, rels_symptom, rels_acompany, rels_category = self.read_nodes()
        f_drug = open('', 'w+')
        f_food = open('', 'w+')
        f_check = open('', 'w+')
        f_department = open('', 'w+')
        f_producer = open('', 'w+')
        f_symptom = open('', 'w+')
        f_disease = open('', 'w+')

        f_drug.write('\n'.join(list(Drugs)))
        f_food.write('\n'.join(list(Foods)))
        f_check.write('\n'.join(list(Checks)))
        f_department.write('\n'.join(list(Departments)))
        f_producer.write('\n'.join(list(Producers)))
        f_symptom.write('\n'.join(list(Symptoms)))
        f_disease.write('\n'.join(list(Diseases)))

        f_drug.close()
        f_food.close()
        f_check.close()
        f_department.close()
        f_producer.close()
        f_symptom.close()
        f_disease.close()


if __name__ == '__main__':
    handler = MedicalGraph()
    ()
    print("step1:Imported into the graph node")
    handler.create_graphnodes()
    print("step2:Imported into the mapping edge")
    handler.create_graphrels()

Six, PyCharm realization of the question and answer system

1. Script for categorizing types of interrogative sentences

here areLoad multiple feature word lists You need to ensure that the file encoding format isutf8

i.e. add content: encoding='utf8'

import os
import ahocorasick

class QuestionClassifier:
    def __init__(self):
        cur_dir = '/'.join((__file__).split('/')[:-1])
        # Trait word path
        self.disease_path = (cur_dir, 'dict/')
        self.department_path = (cur_dir, 'dict/')
        self.check_path = (cur_dir, 'dict/')
        self.drug_path = (cur_dir, 'dict/')
        self.food_path = (cur_dir, 'dict/')
        self.producer_path = (cur_dir, 'dict/')
        self.symptom_path = (cur_dir, 'dict/')
        self.deny_path = (cur_dir, 'dict/')
        # Load Feature Words
        self.disease_wds= [() for i in open(self.disease_path,encoding='utf8') if ()]
        self.department_wds= [() for i in open(self.department_path,encoding='utf8') if ()]
        self.check_wds= [() for i in open(self.check_path,encoding='utf8') if ()]
        self.drug_wds= [() for i in open(self.drug_path,encoding='utf8') if ()]
        self.food_wds= [() for i in open(self.food_path,encoding='utf8') if ()]
        self.producer_wds= [() for i in open(self.producer_path,encoding='utf8') if ()]
        self.symptom_wds= [() for i in open(self.symptom_path,encoding='utf8') if ()]
        self.region_words = set(self.department_wds + self.disease_wds + self.check_wds + self.drug_wds + self.food_wds + self.producer_wds + self.symptom_wds)
        self.deny_words = [() for i in open(self.deny_path,encoding='utf8') if ()]
        # Structural areasactree
        self.region_tree = self.build_actree(list(self.region_words))
        # construct a lexicon
        self.wdtype_dict = self.build_wdtype_dict()
        # interrogative sentence
        self.symptom_qwds = ['symptomatic', 'characterize', 'impunity', 'disease', 'manifestations']
        self.cause_qwds = ['rationale','cause of formation', 'for what reason?', 'How could...?', 'What's the best way to', 'what should I do?', 'why would', 'how will', 'why?', 'why?', 'How can I', 'What's going on?', 'lead to', 'result in']
        self.acompany_qwds = ['complications (undesired side-effect of medical procedure)', 'erupt simultaneously', 'come together', '一erupt simultaneously生', 'come together', 'occur together', 'alongside', 'alongside', 'concomitant', 'concomitant', 'co-occurring']
        self.food_qwds = ['catering', 'drinking or drinkable (water)', 'stammer', 'animal feed', '伙animal feed', '膳animal feed', 'shout loudly', 'dish (type of food)' ,'abstain from certain food (as when ill)', 'restorative', 'healthcare product', 'animal feed谱', 'dish (type of food)谱', 'animal feed用', 'animal feed物','restorative']
        self.drug_qwds = ['leaf of the iris', 'leaf of the iris品', '用leaf of the iris', 'a capsule (medical or pharmaceutical)', 'oral liquid', 'sulfanilamide (used in TCM)']
        self.prevent_qwds = ['take precautions against', 'be on guard', 'refuse (to cooperate)', 'withstand', 'take precautions','avoid (difficulties)','circumvent','keep away from','so as not to','get away','keep away from','avoid','stay out of (hot water, trouble, awkward situation etc)','avoid (sb)','bypass',
                             'What's the best way to能不', 'How do you not', 'what should I do?能不','How do I not', 'How can I not',
                             'What's the best way to不', 'How do I not?', 'what should I do?不','Why not?', 'How do you not',
                             'What's the best way to可以不', 'How can I not?', 'what should I do?可以不', 'How can I not?', 'How can I not',
                             'What's the best way to可不', 'What's to stop it?', 'what should I do?可不', 'How can I not?', 'How can I not?']
        self.lasttime_qwds = ['cyclicality', 'how long?', 'long', 'How much time?', 'several days', 'how many years', 'how many days', 'How many hours?', 'few hours', 'how many years']
        self.cureway_qwds = ['What's the treatment?', 'How to cure', 'How do you cure it?', 'What's the cure?', 'how to cure', 'how to cure', 'treatment method', 'naturopathy', 'how to cure', 'what's be done', 'what to do', 'how to cure']
        self.cureprob_qwds = ['What are the chances of being cured?', 'What are the chances of a cure?', 'What are the chances of a cure?', 'odds', 'quite a few', 'proportions', 'likelihood', 'cure', 'treatable', 'curable', 'treatable']
        self.easyget_qwds = ['susceptible population', 'susceptible', 'vulnerable population', 'who?', 'who?', 'infections', 'get (a bad habit)', 'suitable']
        self.check_qwds = ['probe', 'probe项目', 'find out', 'probe', 'detect', 'test out']
        self.belong_qwds = ['What's the family?', 'be part of', 'what branch?', 'administrative division']
        self.cure_qwds = ['What's the treatment?', 'treat what?', 'Treat what?', '医treat what?', 'Cure what?', '主treat what?', 'What's it for?', 'What's the point?', 'what good is it?', 'use', 'use',
                          'What's in it for you?', 'What's in it for you?', 'what good is it?', 'be used for', 'be used for做啥', 'be used for作甚', 'need', 'coerce']

        print('model init finished ......')

        return

    '''Categorical Master Functions'''
    def classify(self, question):
        data = {}
        medical_dict = self.check_medical(question)
        if not medical_dict:
            return {}
        data['args'] = medical_dict
        #Collecting the types of entities involved in the questioning
        types = []
        for type_ in medical_dict.values():
            types += type_
        question_type = 'others'

        question_types = []

        # symptomatic
        if self.check_words(self.symptom_qwds, question) and ('disease' in types):
            question_type = 'disease_symptom'
            question_types.append(question_type)

        if self.check_words(self.symptom_qwds, question) and ('symptom' in types):
            question_type = 'symptom_disease'
            question_types.append(question_type)

        # rationale
        if self.check_words(self.cause_qwds, question) and ('disease' in types):
            question_type = 'disease_cause'
            question_types.append(question_type)
        # complications (undesired side-effect of medical procedure)
        if self.check_words(self.acompany_qwds, question) and ('disease' in types):
            question_type = 'disease_acompany'
            question_types.append(question_type)

        # 推荐animal feed品
        if self.check_words(self.food_qwds, question) and 'disease' in types:
            deny_status = self.check_words(self.deny_words, question)
            if deny_status:
                question_type = 'disease_not_food'
            else:
                question_type = 'disease_do_food'
            question_types.append(question_type)

        #已知animal feed物找疾病
        if self.check_words(self.food_qwds+self.cure_qwds, question) and 'food' in types:
            deny_status = self.check_words(self.deny_words, question)
            if deny_status:
                question_type = 'food_not_disease'
            else:
                question_type = 'food_do_disease'
            question_types.append(question_type)

        # 推荐leaf of the iris品
        if self.check_words(self.drug_qwds, question) and 'disease' in types:
            question_type = 'disease_drug'
            question_types.append(question_type)

        # leaf of the iris品treat what?病
        if self.check_words(self.cure_qwds, question) and 'drug' in types:
            question_type = 'drug_disease'
            question_types.append(question_type)

        # 疾病接受probe项目
        if self.check_words(self.check_qwds, question) and 'disease' in types:
            question_type = 'disease_check'
            question_types.append(question_type)

        # 已知probe项目查相应疾病
        if self.check_words(self.check_qwds+self.cure_qwds, question) and 'check' in types:
            question_type = 'check_disease'
            question_types.append(question_type)

        # symptomatic防御
        if self.check_words(self.prevent_qwds, question) and 'disease' in types:
            question_type = 'disease_prevent'
            question_types.append(question_type)

        # 疾病医疗cyclicality
        if self.check_words(self.lasttime_qwds, question) and 'disease' in types:
            question_type = 'disease_lasttime'
            question_types.append(question_type)

        # Disease treatment modalities
        if self.check_words(self.cureway_qwds, question) and 'disease' in types:
            question_type = 'disease_cureway'
            question_types.append(question_type)

        # 疾病治愈likelihood
        if self.check_words(self.cureprob_qwds, question) and 'disease' in types:
            question_type = 'disease_cureprob'
            question_types.append(question_type)

        # 疾病易infections人群
        if self.check_words(self.easyget_qwds, question) and 'disease' in types :
            question_type = 'disease_easyget'
            question_types.append(question_type)

        # If no relevant external search information is found,Then the description of the disease is returned
        if question_types == [] and 'disease' in types:
            question_types = ['disease_desc']

        # If no relevant external search information is found,Then the description of the disease is returned
        if question_types == [] and 'symptom' in types:
            question_types = ['symptom_disease']

        # Combining multiple classification results,Assembling a dictionary
        data['question_types'] = question_types

        return data

    '''Types corresponding to constructors'''
    def build_wdtype_dict(self):
        wd_dict = dict()
        for wd in self.region_words:
            wd_dict[wd] = []
            if wd in self.disease_wds:
                wd_dict[wd].append('disease')
            if wd in self.department_wds:
                wd_dict[wd].append('department')
            if wd in self.check_wds:
                wd_dict[wd].append('check')
            if wd in self.drug_wds:
                wd_dict[wd].append('drug')
            if wd in self.food_wds:
                wd_dict[wd].append('food')
            if wd in self.symptom_wds:
                wd_dict[wd].append('symptom')
            if wd in self.producer_wds:
                wd_dict[wd].append('producer')
        return wd_dict

    '''tectonic (geology)actree,Accelerated filtration'''
    def build_actree(self, wordlist):
        actree = ()
        for index, word in enumerate(wordlist):
            actree.add_word(word, (index, word))
        actree.make_automaton()
        return actree

    '''Question Filtering'''
    def check_medical(self, question):
        region_wds = []
        for i in self.region_tree.iter(question):
            wd = i[1][1]
            region_wds.append(wd)
        stop_wds = []
        for wd1 in region_wds:
            for wd2 in region_wds:
                if wd1 in wd2 and wd1 != wd2:
                    stop_wds.append(wd1)
        final_wds = [i for i in region_wds if i not in stop_wds]
        final_dict = {i:self.wdtype_dict.get(i) for i in final_wds}

        return final_dict

    '''Classification based on feature words'''
    def check_words(self, wds, sent):
        for wd in wds:
            if wd in sent:
                return True
        return False


if __name__ == '__main__':
    handler = QuestionClassifier()
    while 1:
        question = input('input an question:')
        data = (question)
        print(data)

2. Question parsing script

class QuestionPaser:

    '''Constructing Entity Nodes'''
    def build_entitydict(self, args):
        entity_dict = {}
        for arg, types in ():
            for type in types:
                if type not in entity_dict:
                    entity_dict[type] = [arg]
                else:
                    entity_dict[type].append(arg)

        return entity_dict

    '''Analyze the main function'''
    def parser_main(self, res_classify):
        args = res_classify['args']
        entity_dict = self.build_entitydict(args)
        question_types = res_classify['question_types']
        sqls = []
        for question_type in question_types:
            sql_ = {}
            sql_['question_type'] = question_type
            sql = []
            if question_type == 'disease_symptom':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'symptom_disease':
                sql = self.sql_transfer(question_type, entity_dict.get('symptom'))

            elif question_type == 'disease_cause':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_acompany':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_not_food':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_do_food':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'food_not_disease':
                sql = self.sql_transfer(question_type, entity_dict.get('food'))

            elif question_type == 'food_do_disease':
                sql = self.sql_transfer(question_type, entity_dict.get('food'))

            elif question_type == 'disease_drug':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'drug_disease':
                sql = self.sql_transfer(question_type, entity_dict.get('drug'))

            elif question_type == 'disease_check':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'check_disease':
                sql = self.sql_transfer(question_type, entity_dict.get('check'))

            elif question_type == 'disease_prevent':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_lasttime':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_cureway':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_cureprob':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_easyget':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            elif question_type == 'disease_desc':
                sql = self.sql_transfer(question_type, entity_dict.get('disease'))

            if sql:
                sql_['sql'] = sql

                (sql_)

        return sqls

    '''For different issues,Handle separately'''
    def sql_transfer(self, question_type, entities):
        if not entities:
            return []

        # query statement
        sql = []
        # Enquire about the cause of the disease
        if question_type == 'disease_cause':
            sql = ["MATCH (m:Disease) where  = '{0}' return , ".format(i) for i in entities]

        # Inquire about defenses against diseases
        elif question_type == 'disease_prevent':
            sql = ["MATCH (m:Disease) where  = '{0}' return , ".format(i) for i in entities]

        # Check the duration of the disease
        elif question_type == 'disease_lasttime':
            sql = ["MATCH (m:Disease) where  = '{0}' return , m.cure_lasttime".format(i) for i in entities]

        # Check the probability of cure of the disease
        elif question_type == 'disease_cureprob':
            sql = ["MATCH (m:Disease) where  = '{0}' return , m.cured_prob".format(i) for i in entities]

        # Enquire about the treatment of the disease
        elif question_type == 'disease_cureway':
            sql = ["MATCH (m:Disease) where  = '{0}' return , m.cure_way".format(i) for i in entities]

        # Check the susceptibility of the disease
        elif question_type == 'disease_easyget':
            sql = ["MATCH (m:Disease) where  = '{0}' return , m.easy_get".format(i) for i in entities]

        # Check the introduction of the disease
        elif question_type == 'disease_desc':
            sql = ["MATCH (m:Disease) where  = '{0}' return , ".format(i) for i in entities]

        # What are the symptoms of the query disease
        elif question_type == 'disease_symptom':
            sql = ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where  = '{0}' return , , ".format(i) for i in entities]

        # What diseases can the query symptoms lead to
        elif question_type == 'symptom_disease':
            sql = ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where  = '{0}' return , , ".format(i) for i in entities]

        # Check for complications of the disease
        elif question_type == 'disease_acompany':
            sql1 = ["MATCH (m:Disease)-[r:acompany_with]->(n:Disease) where  = '{0}' return , , ".format(i) for i in entities]
            sql2 = ["MATCH (m:Disease)-[r:acompany_with]->(n:Disease) where  = '{0}' return , , ".format(i) for i in entities]
            sql = sql1 + sql2
        # Enquire about disease contraindications
        elif question_type == 'disease_not_food':
            sql = ["MATCH (m:Disease)-[r:no_eat]->(n:Food) where  = '{0}' return , , ".format(i) for i in entities]

        # Check what the disease recommends to eat
        elif question_type == 'disease_do_food':
            sql1 = ["MATCH (m:Disease)-[r:do_eat]->(n:Food) where  = '{0}' return , , ".format(i) for i in entities]
            sql2 = ["MATCH (m:Disease)-[r:recommand_eat]->(n:Food) where  = '{0}' return , , ".format(i) for i in entities]
            sql = sql1 + sql2

        # Known taboos to check for diseases
        elif question_type == 'food_not_disease':
            sql = ["MATCH (m:Disease)-[r:no_eat]->(n:Food) where  = '{0}' return , , ".format(i) for i in entities]

        # Known Recommended Diseases
        elif question_type == 'food_do_disease':
            sql1 = ["MATCH (m:Disease)-[r:do_eat]->(n:Food) where  = '{0}' return , , ".format(i) for i in entities]
            sql2 = ["MATCH (m:Disease)-[r:recommand_eat]->(n:Food) where  = '{0}' return , , ".format(i) for i in entities]
            sql = sql1 + sql2

        # Search for common medicines for diseases-Remember to expand drug aliases
        elif question_type == 'disease_drug':
            sql1 = ["MATCH (m:Disease)-[r:common_drug]->(n:Drug) where  = '{0}' return , , ".format(i) for i in entities]
            sql2 = ["MATCH (m:Disease)-[r:recommand_drug]->(n:Drug) where  = '{0}' return , , ".format(i) for i in entities]
            sql = sql1 + sql2

        # Diseases that are known to be treated by the drug query
        elif question_type == 'drug_disease':
            sql1 = ["MATCH (m:Disease)-[r:common_drug]->(n:Drug) where  = '{0}' return , , ".format(i) for i in entities]
            sql2 = ["MATCH (m:Disease)-[r:recommand_drug]->(n:Drug) where  = '{0}' return , , ".format(i) for i in entities]
            sql = sql1 + sql2
        # Check the tests that should be performed for the disease
        elif question_type == 'disease_check':
            sql = ["MATCH (m:Disease)-[r:need_check]->(n:Check) where  = '{0}' return , , ".format(i) for i in entities]

        # Known Check Query Disease
        elif question_type == 'check_disease':
            sql = ["MATCH (m:Disease)-[r:need_check]->(n:Check) where  = '{0}' return , , ".format(i) for i in entities]

        return sql



if __name__ == '__main__':
    handler = QuestionPaser()

3. Question and answer program scripts

from py2neo import Graph

class AnswerSearcher:
    def __init__(self):
         = Graph("neo4j://192.168.112.30:7687", auth=("neo4j", "neo4jpassword"))
        self.num_limit = 20

    '''fulfillmentcypherconsult (a document etc),and return the corresponding result'''
    def search_main(self, sqls):
        final_answers = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            queries = sql_['sql']
            answers = []
            for query in queries:
                ress = (query).data()
                answers += ress
            final_answer = self.answer_prettify(question_type, answers)
            if final_answer:
                final_answers.append(final_answer)
        return final_answers

    '''According to the correspondingqustion_type,Call the appropriate reply template'''
    def answer_prettify(self, question_type, answers):
        final_answer = []
        if not answers:
            return ''
        if question_type == 'disease_symptom':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}Symptoms include:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'symptom_disease':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = 'symptomatic{0}Diseases that can be contracted are:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_cause':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}Possible causes are:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_prevent':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}Preventive measures include:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_lasttime':
            desc = [i['m.cure_lasttime'] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}The treatment may last for a period of:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_cureway':
            desc = [';'.join(i['m.cure_way']) for i in answers]
            subject = answers[0]['']
            final_answer = '{0}The following treatments can be tried:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_cureprob':
            desc = [i['m.cured_prob'] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}The probability of cure is(for reference only):{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_easyget':
            desc = [i['m.easy_get'] for i in answers]
            subject = answers[0]['']

            final_answer = '{0}The susceptible populations include:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_desc':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0},Familiarize yourself.:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_acompany':
            desc1 = [i[''] for i in answers]
            desc2 = [i[''] for i in answers]
            subject = answers[0]['']
            desc = [i for i in desc1 + desc2 if i != subject]
            final_answer = '{0}Symptoms include:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_not_food':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}Foods to avoid include the following:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_do_food':
            do_desc = [i[''] for i in answers if i[''] == 'apt eat']
            recommand_desc = [i[''] for i in answers if i[''] == 'Recommended Recipes']
            subject = answers[0]['']
            final_answer = '{0}Foods that are good to eat include the following:{1}\nRecommended Recipes包括有:{2}'.format(subject, ';'.join(list(set(do_desc))[:self.num_limit]), ';'.join(list(set(recommand_desc))[:self.num_limit]))

        elif question_type == 'food_not_disease':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = 'contract (an illness){0}It's best to avoid people who{1}'.format(';'.join(list(set(desc))[:self.num_limit]), subject)

        elif question_type == 'food_do_disease':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = 'contract (an illness){0}The people who do are advised to try more{1}'.format(';'.join(list(set(desc))[:self.num_limit]), subject)

        elif question_type == 'disease_drug':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}Commonly used medications include:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'drug_disease':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}The main diseases treated are{1},You can try.'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'disease_check':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = '{0}This can usually be checked out by:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'check_disease':
            desc = [i[''] for i in answers]
            subject = answers[0]['']
            final_answer = 'This can usually be done by{0}Diseases detected are{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))

        return final_answer


if __name__ == '__main__':
    searcher = AnswerSearcher()

4. Question and answer system realization

4.1. Model initialization

from answer_search import *
from question_classifier import *
from question_parser import *
 
 
class ChatBotGraph:
    def __init__(self):
         = QuestionClassifier()
         = QuestionPaser()
         = AnswerSearcher()

4.2. Q&A Master Functions

    def chat_main(self, sent):
        answer = 'hello (polite),I'm an intelligent medical assistant.,I hope this helps.。If you don't get it.,contactable/。I wish you the best of health.!'
        res_classify = (sent)
        if not res_classify:
            return answer
        res_sql = .parser_main(res_classify)
        final_answers = .search_main(res_sql)
        if not final_answers:
            return answer
        else:
            return '\n'.join(final_answers)

4.3. Running the main entrance

Run the chatbot_graph.py file

if __name__ == '__main__':
    handler = ChatBotGraph()
    while 1:
        question = input('subscribers:')
        answer = handler.chat_main(question)
        print('Pharmaceutical Intelligent Assistant:', answer)

4.4. Operational results

image-20241218113923894