Knowledge graph query method and device, electronic equipment and storage medium

The knowledge graph query method is used to automatically generate database query statements, which solves the problem of low efficiency of traditional manual SQL statement writing and realizes an efficient query process.

CN120687475APending Publication Date: 2025-09-23LIAONING COMM TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510772847.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-11
Publication Date
2025-09-23

AI Technical Summary

Technical Problem

Traditional database queries rely on users to manually write SQL statements, resulting in low query efficiency.

Method used

Through the knowledge graph query method, information is automatically extracted from natural query statements to generate database query statements, and target query statements are generated using semantic analysis and triple optimization technology.

Benefits of technology

There is no need to manually write SQL statements, which improves query efficiency and accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120687475A_ABST
    Figure CN120687475A_ABST
Patent Text Reader

Abstract

The invention provides a knowledge graph query method and device, electronic equipment and a storage medium, and the method comprises the steps: in response to a natural query statement input by a query user in a query interface, determining second query information corresponding to first query information in the natural query statement from a pre-constructed knowledge graph, semantic information of the second query information is determined based on the knowledge graph; generating an initial database query statement under the guidance of the semantic information according to the second query information; optimizing the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement; and querying data in the knowledge graph based on the target database query statement. According to the method, the database query statement can be generated based on the natural query statement, then query is carried out, manual SQL statement compiling is not needed, and the query efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of information query, and in particular to a knowledge graph query method, device, electronic device and storage medium. Background Art

[0002] Against the backdrop of today's rapid development of information technology, businesses and organizations are facing the challenge of storing and querying massive amounts of data. Traditional database queries primarily rely on relational databases and perform searches using the user-written SQL language. However, SQL statements are prone to errors, and manual SQL writing is time-consuming, significantly reducing query efficiency. Summary of the Invention

[0003] In view of this, the purpose of this application is to provide a knowledge graph query method, device, electronic device and storage medium, which can generate database query statements based on natural query statements and then perform queries without manually writing SQL statements, thereby improving query efficiency.

[0004] In a first aspect, an embodiment of the present application provides a knowledge graph query method, the knowledge graph query method comprising:

[0005] In response to a natural query statement input by a query user in a query interface, determining second query information corresponding to the first query information in the natural query statement from a pre-constructed knowledge graph, and determining semantic information of the second query information based on the knowledge graph;

[0006] generating an initial database query statement based on the second query information and under the guidance of the semantic information;

[0007] Optimize the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement;

[0008] Query the data in the knowledge graph based on the target database query statement.

[0009] In a possible implementation, the first query information in the natural query statement is determined by the following steps:

[0010] First query information in the natural query sentence is determined based on the first weight of each character and the second weight of each phrase in the natural query sentence.

[0011] In a possible implementation, determining the semantic information of the second query information based on the knowledge graph includes:

[0012] Determining a context vector of the second query information in a pre-built knowledge graph;

[0013] The context vector is fused with the embedding vector of the second query information to obtain semantic information of the second query information.

[0014] In a possible implementation, determining a context vector of the second query information in a pre-built knowledge graph includes:

[0015] Calculate a third weight of the embedding vector corresponding to each triple data in the knowledge graph for the second query information;

[0016] Based on the third weight, a weighted sum is performed on the embedding vectors corresponding to all triple data to obtain a context vector of the second query information in the pre-constructed knowledge graph.

[0017] In a possible implementation, the initial database query statement is optimized according to all triples in the knowledge graph to obtain a target database query statement;

[0018] Input each triple data in the knowledge graph into the recognition model to extract the first entity and attribute information of each triple;

[0019] Determine the triple containing the first entity identical to the second entity in the initial database query statement as the target triple;

[0020] The conditions of the initial database query statement are optimized according to the attribute information in all target triples to obtain a target database query statement.

[0021] In a possible implementation, after optimizing the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement, the method further includes:

[0022] Taking the target entity in the target database query statement as the starting point, filtering out the target path in the knowledge graph according to the preset path length and the preset relationship type;

[0023] generating an extended database query statement based on other entities in the target path except the target entity and corresponding attributes;

[0024] The data in the knowledge graph is queried based on the extended database query statement.

[0025] In one possible implementation, the method further includes:

[0026] Calculating the relevance between entities in the queried data and query keywords in the natural query statement;

[0027] Sort the queried data according to the correlation degree to obtain a query result sequence;

[0028] The query result sequence is displayed in the query interface.

[0029] In a second aspect, an embodiment of the present application further provides a knowledge graph query device, the device comprising:

[0030] a response module for determining, in response to a natural query statement input by a query user in a query interface, second query information corresponding to the first query information in the natural query statement from a pre-built knowledge graph, and determining semantic information of the second query information based on the knowledge graph;

[0031] a generating module, configured to generate an initial database query statement based on the second query information and under the guidance of the semantic information;

[0032] An optimization module, configured to optimize the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement;

[0033] A query module is used to query the data in the knowledge graph based on the target database query statement.

[0034] In the third aspect, an embodiment of the present application also provides an electronic device, comprising: a processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor and the storage medium communicate through the bus, and the processor executes the machine-readable instructions to perform the steps of the knowledge graph query method as described in any one of the first aspects.

[0035] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the knowledge graph query method as described in any one of the first aspects are executed.

[0036] The embodiment of the present application provides a knowledge graph query method, device, electronic device and storage medium, the method comprising: in response to a natural query statement input by a query user in a query interface, determining second query information corresponding to the first query information in the natural query statement from a pre-constructed knowledge graph, and determining semantic information of the second query information based on the knowledge graph; generating an initial database query statement based on the second query information under the guidance of the semantic information; optimizing the initial database query statement based on all triples in the knowledge graph to obtain a target database query statement; and querying the data in the knowledge graph based on the target database query statement. Through the present application, it is possible to generate a database query statement based on a natural query statement, and then perform a query, without manually writing an SQL statement, thereby improving query efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without creative work.

[0038] Figure 1 A flow chart of a knowledge graph query method provided by an embodiment of the present application is shown;

[0039] Figure 2 A flowchart of another knowledge graph query method provided by an embodiment of the present application is shown;

[0040] Figure 3 A schematic diagram of the structure of a knowledge graph query device provided in an embodiment of the present application is shown;

[0041] Figure 4 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION

[0042] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. It should be understood that the drawings in the present application only serve the purpose of illustration and description and are not used to limit the scope of protection of the present application. In addition, it should be understood that the schematic drawings are not drawn to scale. The flowcharts used in this application illustrate the operations implemented according to some embodiments of the present application. It should be understood that the operations of the flowcharts can be implemented out of sequence, and steps without logical context can be reversed or implemented simultaneously. In addition, those skilled in the art, under the guidance of the contents of this application, can add one or more other operations to the flowchart, or remove one or more operations from the flowchart.

[0043] In addition, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments. The components of the embodiments of the present application generally described and shown in the drawings here can be arranged and designed in various configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the claimed application, but merely represents selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present application.

[0044] To enable those skilled in the art to utilize the present disclosure, the following embodiments are provided in conjunction with the specific application scenario of "information query field." Those skilled in the art will appreciate that the general principles defined herein can be applied to other embodiments and application scenarios without departing from the spirit and scope of this disclosure. While this disclosure is primarily described in the context of "information query field," it should be understood that this is merely an exemplary embodiment.

[0045] It should be noted that the term "comprising" will be used in the embodiments of the present application to indicate the existence of the features declared thereafter, but does not exclude the addition of other features.

[0046] The following is a detailed description of a knowledge graph query method provided in an embodiment of the present application.

[0047] Reference Figure 1 The figure is a flow chart of a knowledge graph query method provided in an embodiment of the present application. The exemplary steps of the embodiment of the present application are described below:

[0048] S101. In response to a natural query statement input by a query user in a query interface, second query information corresponding to the first query information in the natural query statement is determined from a pre-constructed knowledge graph, and semantic information of the second query information is determined based on the knowledge graph.

[0049] In the embodiments of this application, a natural query statement refers to text entered by a query user to express a query requirement. The first query information and the second query information include entities, relationships, and attributes. Specifically, the first query information in the natural query statement is determined; the second query information corresponding to the first query information is determined from a pre-built knowledge graph. The semantic information of the second query information is then determined based on the knowledge graph.

[0050] Furthermore, the first query information in the natural query sentence is determined by the following steps: based on the first weight of each character in the natural query sentence and the second weight of each phrase, the first query information in the natural query sentence is determined.

[0051] In an embodiment of the present application, a natural query statement is input into a semantic analysis model, which uses an attention mechanism to calculate the first weight of each character of the natural query statement and the second weight of each short sentence, and then performs lexical analysis, syntactic analysis and deep semantic analysis on the natural query statement based on the first weight of each character and the second weight of each short sentence to determine the first query information in the natural query statement.

[0052] Here, the attention mechanism is used to achieve in-depth understanding and processing of natural query sentences. The core idea of ​​the attention mechanism is to enable the model to assign different weights to different parts of the input sequence, allowing the semantic analysis model to focus on the information most relevant to the current query task, rather than treating all input information in the natural query sentence equally. This helps the semantic analysis model focus on important parts, thereby improving the accuracy and efficiency of semantic understanding.

[0053] Specifically, the steps for calculating the first weight of the i-th character of a natural query using the attention mechanism are as follows:

[0054] (1) Convert the i-th character into an embedding vector e i , the embedding vector e of the i-th character i Contains the semantic information of the i-th character.

[0055] (2) Embedding vector e for the i-th character i Perform a linear transformation to generate the query vector q corresponding to the i-th character i , key vector k i Sum value vector v i The specific formula is as follows:

[0056] q i =W Q e i ;

[0057] k i=W K e i ;

[0058] v i =W V e i ;

[0059] Among them, W Q 、W K and W V is a learnable weight matrix.

[0060] (3) The query vector q of the i-th character i Perform dot multiplication with the key vector of each character to obtain the query vector q for the i-th character of each character's key vector i The specific formula is as follows:

[0061] score(q i ,k j )=q i ·k j ;

[0062] Among them, score(q i ,k j ) is the key vector k of the jth character j For the query vector q of the i-th character i The attention score of the query vector q of the i-th character i The key vector k with the jth character j The similarity between them.

[0063] (4) To prevent the attention score from being too large and causing the gradient to vanish or explode, the attention score is scaled; then the scaled attention score is normalized using the softmax function to obtain the attention weight. The specific formula is as follows:

[0064]

[0065] Among them, a i,j is the attention weight of the jth character for the ith character, d k is the dimension of the key vector, and n is the number of characters in the natural query statement.

[0066] (5) According to the attention weights of all characters on the i-th character, the value vectors of all characters are weighted summed to generate the context vector c of the i-th character i The specific formula is as follows:

[0067]

[0068] Among them, the context vector c of the i-th characteri It integrates the information of all characters in the natural query sentence and highlights the parts with high relevance to the i-th character.

[0069] Specifically, the steps for calculating the second weight of each short sentence of a natural query using the attention mechanism are as follows:

[0070] (1) The frequency of each word t in the short sentence in the document d corresponding to the knowledge graph is calculated using the following formula to obtain the word frequency TF(t,d).

[0071]

[0072] (2) The inverse document frequency (IDF) is calculated by the following formula: the inverse of the number of documents in which each word t in the phrase appears in the document set D corresponding to the knowledge graph, and the inverse document frequency IDF(t,D) is obtained.

[0073]

[0074] (3) Calculate the weight TF-IDF(t) of word t using the following formula.

[0075] TF-IDF(t,d,D)=TF(t,d)×IDF(t,D);

[0076] (4) The maximum value of the weights of all words t in the short sentence is determined as the weight of the short sentence.

[0077] Furthermore, determining second query information corresponding to the first query information from a pre-built knowledge graph includes: associating entities in the first query information to unique entity nodes in the knowledge graph to obtain entities in the second query information; mapping relationships in the first query information to relationship types defined in the knowledge graph to obtain relationships in the second query information; and mapping attribute information in the first query information to attributes corresponding to entities in the knowledge graph to obtain attributes in the second query information.

[0078] Here, after the structured query representation of knowledge graph alignment and disambiguation, the natural language elements in the first query information have been replaced by the corresponding identifiers in the knowledge graph.

[0079] Here, the entity in the first query information is associated with the only entity node in the knowledge graph to obtain the entity in the second query information, including: calculating the similarity between the entity in the first query information and each entity node in the knowledge graph; and determining the entity node in the knowledge graph with the highest similarity to the entity in the first query information as the entity in the second query information.

[0080] The similarity between the entity in the first query information and each entity node in the knowledge graph is calculated by the following steps:

[0081] Substitute the entity in the first query information and the entity node in the knowledge graph into the following formula to obtain the similarity between the entity in the first query information and the entity node in the knowledge graph;

[0082]

[0083] Among them, sim(e q ,e kg ) is the entity e in the first query information q and the entity node e in the knowledge graph kg The similarity between them, α is the weight coefficient, Entity e in the first query information q and the entity node e in the knowledge graph kg The semantic embedding similarity between (based on cosine similarity), Entity e in the first query information q The embedding vector of is the entity node e in the knowledge graph kg Embedding vector, Levenshtein(e q .name,e kg .name) is the entity e in the first query information q and the entity node e in the knowledge graph kg The literal similarity between (based on text edit distance), e q .name is the entity e in the first query information q The text, e kg .name is the entity node e in the knowledge graph kg 's text.

[0084] Among them, the optimization strategies of weight coefficients can include weight optimization strategies based on supervised learning, unsupervised adaptive adjustment, grid search based on task performance, multi-objective joint optimization and dynamic weight mechanism.

[0085] Furthermore, the semantic information of the second query information is determined based on the knowledge graph through the following steps:

[0086] Step 1: Determine the context vector of the second query information in the pre-built knowledge graph.

[0087] In an embodiment of the present application, the context vectors of the entities, relationships, and attributes in the second query information in the pre-constructed knowledge graph are determined respectively. Specifically, a third weight of the embedding vector corresponding to each triple data in the knowledge graph to the second query information is calculated (which can be represented by the distance between the embedding vector corresponding to each triple data and the embedding vector of the second query information); based on the third weight, a weighted sum is taken of the embedding vectors corresponding to all triple data to obtain the context vector of the second query information in the pre-constructed knowledge graph.

[0088] Here, the entities, relations, and attributes in each triple data in the knowledge graph are represented by vectors. These vectors can capture the semantic information in the knowledge graph. Assume that the entity set in the knowledge graph is E, the relation set is R, and the attribute set is A. Each entity e m , relationship r m and attribute a m Represented as a vector:

[0089] e m =Embedding(e m ),e m ∈E;r m =Embedding(r m ),r m ∈R;

[0090] a m =Embedding(a m ),a m ∈A.

[0091] Here, the present application may also determine the context vector of the second query information in the pre-built knowledge graph by the following formula:

[0092]

[0093] Among them, MLP is a multi-layer perceptron, N k Entity e in the second query information obtained by sampling the knowledge graph subgraph kg1 The k-hop neighborhood node set, dist(e kg1 ,n) is the entity e in the second query information obtained by sampling the knowledge graph subgraph kg1 The distance between the neighboring node n, v n is the embedding vector of the neighborhood node n.

[0094] Step 2: Fuse the context vector C′ with the embedding vector of the second query information to obtain the semantic information of the second query information.

[0095] In the embodiment of the present application, the context vector C′ is fused with the embedding vectors of the entities, relationships, and attributes in the second query information, respectively, to obtain the semantic information of the entities, relationships, and attributes in the second query information. This is expressed by the following formula:

[0096] h = LoyerNorm(W+C′);

[0097] Among them, h is the semantic information of the entity, the semantic information of the relationship, or the semantic information of the attribute, W is the embedding vector of the entity, the embedding vector of the relationship, or the embedding vector of the attribute, and LayerNorm (layer normalization) is a normalization technology.

[0098] S102: Generate an initial database query statement based on the second query information under the guidance of semantic information.

[0099] i. Extract the core elements required to generate a database query statement, such as query target, condition, sort, range, etc., from the second query information using the following formula.

[0100] P(y i |s)=softmax(W c s+b c );

[0101] Among them, P(y i |s) is the second query information, s is the i-th core element category y i The probability of , softmax is the activation function, W c and b c is the coefficient.

[0102] Parsing conditional expressions with core elements as conditions: Using a sequence labeling model based on the attention mechanism to parse compound conditions (such as "price > 100 AND inventory < 5"):

[0103] ii. Organize it into a structured semantic framework.

[0104] iii. Determine the corresponding database table, field, and necessary table join (JOIN) conditions based on the entities, attributes, and conditions in the second query information.

[0105] Here, we construct a weighted graph G = (V, E), where the nodes are tables and the edge weights are determined by the knowledge graph relationship types. We use Monte Carlo Tree Search (MCTS) to select the optimal JOIN path:

[0106] Score(p)=∑ e∈p w e -λ·len(p);

[0107] where we is the relationship weight, and λ is the path length penalty coefficient.

[0108] iv. Determine the type of database query statement (such as SELECT) based on the query operation type and build its basic structure.

[0109] v. Based on the semantic information, fill in the SELECT clause (target field), FROM clause (source table), and WHERE clause converted according to the conditions in the semantic framework in the basic structure.

[0110] Here, the SELECT clause (target field) and FROM clause (source table) are filled in the basic structure in sequence according to the semantic information, including: generating an abstract syntax tree (AST) according to the semantic framework, and filling in the clauses according to priority: SELECT {fields} FROM {tables} WHERE {conditions} GROUP BY {groups} HAVING {having} ORDER BY {sort}, where the placeholders {fields}, {tables}, {conditions}, and {groups} are instantiated by the output of step 2.

[0111] Among them, the semantic framework includes query targets (such as "the top 5 products in sales", which usually corresponds to the target field in the SELECT clause of a database query statement), conditions (user-specified filtering conditions, such as "within the past month", which are used to limit query results, usually corresponding to the conditions in the WHERE clause of a database query statement), scope (the context or scope of the query, such as the time range, a specific table or data set, etc., such as "data in the order table", which usually corresponds to the source table in the FROM clause of a database query statement), entities (specific objects or concepts mentioned in natural query statements. Such as "product", "order", etc. This usually corresponds to a table or field in a table), relationships (the relationship between entities, such as "a product belongs to a certain category" or "an order contains a certain product". This usually corresponds to the join (JOIN) condition between tables), and attributes (features or attributes of an entity. Such as "product sales", "order date". This usually corresponds to a field in a table).

[0112] For example, consider the query "Find toll stations in Liaoning Province with more than 1,000 vehicles and more than 500 trucks passing through them in the last hour, sorted in descending order by truck count." Table 1 shows a semantic frame decomposition table obtained by performing semantic frame decomposition on the above content in accordance with this embodiment of the present application.

[0113] Table 1

[0114] Core element type Extract content Query Target Toll station information, total number of passing vehicles, number of trucks Data range Liaoning Province Expressway Time conditions In the last 1 hour Filter conditions Total number of vehicles > 1000 AND number of trucks > 500 Sorting conditions By number of trucks descending

[0115] Knowledge graph mapping: (1) Entity alignment: Liaoning Province highway → knowledge graph node highway:province = Liaoning; toll station → entity type toll_station; truck → vehicle type vehicle_type = freight. (2) Relationship mapping: passing vehicle → relationship has_traffic_volume; belonging highway → relationship belongs_to_highway.

[0116] Placeholders are dynamically filled: SQL template:

[0117] Specific filling content:

[0118]

[0119]

[0120] Finally generate sql:

[0121]

[0122] JOIN highway ON toll_station.highway_id=highway.id

[0123] WHERE

[0124] highway.province = 'Liaoning Province'

[0125] AND traffic_record.pass_time>=NOW()-INTERVAL'1HOUR'

[0126] GROUP BY

[0127] toll_station.id,toll_station.name

[0128] HAVING

[0129] COUNT(traffic_record.vehicle_id)>1000

[0130] AND SUM(CASE WHEN traffic_record.vehicle_type='freight'

[0131] THEN 1ELSE 0END)>500

[0132] ORDER BY

[0133] freight_count DESC

[0134] S103. Optimize the initial database query statement according to all triples in the knowledge graph to obtain the target database query statement.

[0135] Reference Figure 2 As shown, it is a flowchart of another knowledge graph query method provided by an embodiment of the present application:

[0136] S201. Input each triple data in the knowledge graph into the recognition model to extract the first entity and attribute information of each triple.

[0137] S202: Determine the triple containing the first entity identical to the second entity in the initial database query statement as the target triple.

[0138] S203: Optimize the conditions of the initial database query statement according to the attribute information in all target triples to obtain a target database query statement.

[0139] For example, the attributes corresponding to a supplier include region and supply quantity, and the condition of the initial database query statement is supply price, which means that the condition of the initial database query statement is incorrect. Then, the attribute information of the supplier most frequently used by the query user is used as the condition of the initial database query statement to obtain the target database query statement.

[0140] Here, the recognition model is trained based on corpus labeled with entities and attributes, using a long short-term memory network model.

[0141] Furthermore, after optimizing the initial database query statement based on all triples in the knowledge graph to obtain the target database query statement, the method also includes: taking the target entity in the target database query statement as the starting point, and filtering out the target path in the knowledge graph based on a preset path length and a preset relationship type; generating an extended database query statement based on other entities other than the target entity in the target path and the corresponding attributes; and querying the data in the knowledge graph based on the extended database query statement.

[0142] S104. Query the data in the knowledge graph based on the target database query statement.

[0143] In the embodiments of the present application, the target database query statement is an SQL statement. The target database query statement is converted into a graph query statement, and data is queried from the knowledge graph based on the graph query statement. Alternatively, the data in the knowledge graph is stored in a relational database; and the data in the knowledge graph is queried from the relational database based on the target database query statement.

[0144] Furthermore, the method also includes: calculating the association between entities in the queried data and query keywords in the natural query statement; sorting the queried data according to the association to obtain a query result sequence; and displaying the query result sequence in the query interface.

[0145] Here, the association degree SemanticAssociation(S,U) between the entity S in the queried data and the query keyword U in the natural query statement is calculated by the following formula:

[0146]

[0147] Here, ∈ is a small constant used to avoid division by zero. L(S,U) is the shortest path length between S and U in the knowledge graph.

[0148] In addition, the embodiments of the present application also utilize statistical information in the knowledge graph and user personal data to perform reasoning and personalized queries, and recommend personalized query results based on the user's query history and preferences.

[0149] It should be noted that the application of knowledge graph combined with database is the infrastructure and execution environment that supports the operation of this method, providing information on how to effectively integrate knowledge graph data with the database system to ensure that the target database query statement of the method can be correctly executed and return query results. a) Implementation of application infrastructure construction includes: (1) Data modeling: Define the data model according to business needs and the structure of the knowledge graph. Map the entities, relationships and attributes in the knowledge graph to the tables and fields of the database. The data in the knowledge graph is usually stored in the form of triples (subject, predicate, object). In the database, three tables can be used to store the subject, predicate and object respectively, or the triples can be stored as rows in a single table. (2) Database integration: Select the database type or relational database according to the characteristics of the actual data type. Use the appropriate driver or API to connect the knowledge graph to the database. (3) Data import: Before importing the knowledge graph data into the database, data preprocessing is required. This includes data cleaning (removing duplicate and erroneous data), data conversion (converting the data format to a format suitable for database storage), etc. Write scripts or use tools to batch import knowledge graph data into the database. (4) Data storage and management: Optimize the storage structure of the database based on data access patterns and query requirements. Regularly back up the knowledge graph data in the database to prevent data loss. At the same time, establish a data recovery mechanism so that data can be quickly restored when it is damaged or lost. b) Execute queries and analysis, including: (1) Query execution: In this integrated environment, receive the target database query statement and use the corresponding query language (such as graph query language or SQL) to execute the query in the database to retrieve data. (2) Result processing and presentation: Fusion analysis of query results with data from other data sources, and perform post-processing such as filtering, sorting, highlighting or visualization on the original results returned by the database, and finally present them in a user-friendly form. c) Through the implementation of this application, a data foundation and execution capabilities are provided, so that the entire intelligent query processing process from user natural language input to final result presentation can be closed-loop, thereby achieving a knowledge graph intelligent query method that integrates Text2SQL.

[0150] An embodiment of the present application provides a knowledge graph query method, which includes: in response to a natural query statement input by a query user in a query interface, determining second query information corresponding to the first query information in the natural query statement from a pre-constructed knowledge graph, and determining semantic information of the second query information based on the knowledge graph; generating an initial database query statement based on the second query information under the guidance of the semantic information; optimizing the initial database query statement based on all triples in the knowledge graph to obtain a target database query statement; and querying data in the knowledge graph based on the target database query statement. Through the present application, it is possible to generate a database query statement based on a natural query statement and then perform a query without manually writing an SQL statement, thereby improving query efficiency.

[0151] Based on the same inventive concept, the embodiments of the present application also provide a knowledge graph query device corresponding to the knowledge graph query method. Since the principle of solving the problem by the device in the embodiments of the present application is similar to the above-mentioned knowledge graph query method in the embodiments of the present application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.

[0152] Reference Figure 3 FIG. 1 is a schematic diagram of a knowledge graph query device provided in an embodiment of the present application, wherein the knowledge graph query device includes:

[0153] A response module 301 is configured to, in response to a natural query statement input by a query user in a query interface, determine, from a pre-built knowledge graph, second query information corresponding to the first query information in the natural query statement, and determine semantic information of the second query information based on the knowledge graph;

[0154] A generating module 302 is configured to generate an initial database query statement based on the second query information and under the guidance of the semantic information;

[0155] An optimization module 303 is configured to optimize the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement;

[0156] The query module 304 is used to query the data in the knowledge graph based on the target database query statement.

[0157] An embodiment of the present application provides a knowledge graph query device, through which a database query statement can be generated based on a natural query statement, and then a query can be performed without manually writing an SQL statement, thereby improving query efficiency.

[0158] like Figure 4As shown, an electronic device 400 provided in an embodiment of the present application includes: a processor 401, a memory 402 and a bus, wherein the memory 402 stores machine-readable instructions executable by the processor 401. When the electronic device is running, the processor 401 communicates with the memory 402 through the bus, and the processor 401 executes the machine-readable instructions to perform the steps of the above-mentioned knowledge graph query method.

[0159] Specifically, the above-mentioned memory 402 and processor 401 can be general-purpose memory and processor, which are not specifically limited here. When the processor 401 runs the computer program stored in the memory 402, it can execute the above-mentioned knowledge graph query method.

[0160] Corresponding to the above-mentioned knowledge graph query method, an embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is run by a processor, the steps of the above-mentioned knowledge graph query method are executed.

[0161] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and device described above can refer to the corresponding process in the method embodiment, and will not be repeated in this application. In the several embodiments provided in this application, it should be understood that the disclosed system, device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation. For example, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.

[0162] The modules described as separate components may or may not be physically separate, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed across multiple network elements. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.

[0163] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0164] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the information processing method described in each embodiment of the present application. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a ROM, a RAM, a magnetic disk, or an optical disk.

[0165] The above are only specific embodiments of the present application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.

Claims

1. A knowledge graph query method, characterized in that: The method comprises: In response to a natural query statement input by a query user in a query interface, determining second query information corresponding to the first query information in the natural query statement from a pre-constructed knowledge graph, and determining semantic information of the second query information based on the knowledge graph; generating an initial database query statement based on the second query information and under the guidance of the semantic information; Optimize the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement; Query the data in the knowledge graph based on the target database query statement.

2. The knowledge graph query method according to claim 1, characterized in that: Determine the first query information in the natural query statement by the following steps: First query information in the natural query sentence is determined based on the first weight of each character and the second weight of each phrase in the natural query sentence.

3. The knowledge graph query method according to claim 1, characterized in that: The determining the semantic information of the second query information based on the knowledge graph includes: Determining a context vector of the second query information in a pre-built knowledge graph; The context vector is fused with the embedding vector of the second query information to obtain semantic information of the second query information.

4. The knowledge graph query method according to claim 3, characterized in that: Determining a context vector of the second query information in a pre-built knowledge graph includes: Calculate a third weight of the embedding vector corresponding to each triple data in the knowledge graph for the second query information; Based on the third weight, a weighted sum is performed on the embedding vectors corresponding to all triple data to obtain a context vector of the second query information in the pre-constructed knowledge graph.

5. The knowledge graph query method according to any one of claims 1 to 4, characterized in that: Optimizing the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement; Input each triple data in the knowledge graph into the recognition model to extract the first entity and attribute information of each triple; Determine the triple containing the first entity identical to the second entity in the initial database query statement as the target triple; The conditions of the initial database query statement are optimized according to the attribute information in all target triples to obtain a target database query statement.

6. The knowledge graph query method according to claim 1, characterized in that: After optimizing the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement, the method further includes: Taking the target entity in the target database query statement as the starting point, filtering out the target path in the knowledge graph according to the preset path length and the preset relationship type; generating an extended database query statement based on other entities in the target path except the target entity and corresponding attributes; The data in the knowledge graph is queried based on the extended database query statement.

7. The knowledge graph query method according to claim 1 or 6, characterized in that: The method further comprises: Calculating the relevance between entities in the queried data and query keywords in the natural query statement; Sort the queried data according to the correlation degree to obtain a query result sequence; The query result sequence is displayed in the query interface.

8. A knowledge graph query device, characterized in that: The device comprises: a response module for determining, in response to a natural query statement input by a query user in a query interface, second query information corresponding to the first query information in the natural query statement from a pre-built knowledge graph, and determining semantic information of the second query information based on the knowledge graph; a generating module, configured to generate an initial database query statement based on the second query information and under the guidance of the semantic information; An optimization module, configured to optimize the initial database query statement according to all triples in the knowledge graph to obtain a target database query statement; A query module is used to query the data in the knowledge graph based on the target database query statement.

9. An electronic device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor and the storage medium communicate through the bus, and the processor executes the machine-readable instructions to perform the steps of the knowledge graph query method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, which, when executed by a processor, executes the steps of the knowledge graph query method according to any one of claims 1 to 7.

Citation Information

Cited By

  • Intelligent data query generation method based on large model and knowledge graph

    CN121597840A