Natural language-based database query statement intelligent conversion and analysis method

By constructing a joint semantic space and multi-level semantic parsing, combined with a graph attention propagation mechanism, the semantic gap between natural language and database schema is solved, achieving highly accurate transformation of complex queries and enhancing the understanding capabilities of the database query system.

CN121681575BActive Publication Date: 2026-05-08SHANGHAI XIRUAN TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SHANGHAI XIRUAN TECH CO LTD
Filing Date
2026-02-10
Publication Date
2026-05-08

AI Technical Summary

Technical Problem

Existing technologies struggle to effectively understand the semantic alignment between natural language and database schema when processing complex database queries, especially in scenarios involving multi-table joins and nested queries, where accuracy is low and they lack the ability to deeply model the complex relationships between database schema elements.

Method used

We construct a joint semantic space that integrates the language representation subspace and the database schema representation subspace. We achieve semantic alignment through contrastive learning, and employ multi-level semantic parsing and graph attention propagation mechanisms. By combining the co-occurrence statistical features of historical query records, we construct multi-hop inference paths and generate formal query statements that conform to the query language specifications of the target database.

Benefits of technology

It improves the accuracy of semantic understanding between natural language and database schema, enhances the ability to understand complex queries, solves the problems of cross-modal semantic understanding and context dependency mapping, and improves the accuracy of query transformation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121681575B_ABST
    Figure CN121681575B_ABST
Patent Text Reader

Abstract

The application provides a natural language-based database query statement intelligent conversion and analysis method, relates to the natural language processing and database query technical field, and comprises the following steps: constructing a joint semantic space by fusing language and database mode, performing multi-level analysis on the natural language query, constructing a multi-hop reasoning path based on a graph attention propagation mechanism, combining historical query records to establish the mapping of entity relationship and mode elements, and generating a formal query statement under the database constraint. The application realizes accurate conversion from natural language to database query, improves the query semantic understanding accuracy and the relevance of the query result, and reduces the technical threshold for users to use the database.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of natural language processing and database query technology, and in particular to a method for intelligent conversion and analysis of database query statements based on natural language. Background Technology

[0002] With the rapid development of information technology, databases have become the infrastructure for organizations and enterprises to store, manage, and access data. Traditional database queries require users to master structured query languages ​​such as SQL, which poses a high barrier to entry for users without a technical background. To improve the usability and accessibility of databases, natural language query interfaces have become a research hotspot, allowing users to interact with databases using everyday language rather than specialized query languages.

[0003] The technology for converting natural language to database query language has evolved from rule-based methods to semantic parsing-based methods, and more recently, deep learning-based methods. Early systems relied primarily on manually defined conversion rules and templates, while later technologies increasingly utilize machine learning to automatically learn the mapping relationship between language and query statements from data. With advancements in natural language processing, deep learning-based methods have demonstrated strong capabilities in handling complex queries and adapting to different database structures.

[0004] A significant representational gap exists between semantic understanding and database structure. The lack of an effective semantic alignment mechanism between the diversity of natural language expressions and the strict structure of database schemas leads to low accuracy when processing complex query intents, especially when the terminology used by the user differs from the table and field names in the database. Existing technologies lack the ability to deeply model the complex relationships between database schema elements, making it difficult to handle scenarios requiring joins across multiple tables and multi-step reasoning. In particular, when dealing with complex structures such as multi-table joins and nested queries, it is often impossible to construct a reasonable query path. Summary of the Invention

[0005] This invention provides a method for intelligent conversion and analysis of database query statements based on natural language, which can solve the problems in the prior art.

[0006] A first aspect of this invention provides a method for intelligent conversion and analysis of database query statements based on natural language, comprising:

[0007] A joint semantic space is constructed that integrates the language representation subspace and the database schema representation subspace. Through contrastive learning, the semantic vectors of the natural language query expression and the structure vectors of the schema elements in the target database structure description are semantically aligned in the joint semantic space.

[0008] The natural language query expression is subjected to multi-level semantic parsing to extract the intent representation describing the query target and the entity relationship representation describing the query conditions;

[0009] Based on the graph attention propagation mechanism, a multi-hop inference path is constructed between the pattern elements. The entity relation representation annotated with the pattern elements is semantically propagated and aggregated along the multi-hop inference path. The weight of the multi-hop inference path is adaptively adjusted by combining the co-occurrence statistical features of pattern elements in historical query execution records, and a context-dependent mapping relationship is established between the entity relation representation and the pattern elements.

[0010] The mapping relationship whose confidence level meets the preset confidence threshold is selected as the valid mapping;

[0011] The integrity constraints, foreign key dependencies, and query syntax rules of the target database are used as decoding constraints. Based on the intent representation and the effective mapping, a formal query statement that conforms to the query language specification of the target database and satisfies semantic consistency is generated under the limitation of the decoding constraints.

[0012] Constructing a joint semantic space that integrates a language representation subspace and a database schema representation subspace, and through contrastive learning, forming a semantically aligned distribution between the semantic vectors of natural language query expressions and the structure vectors of schema elements in the target database structure description within the joint semantic space, includes:

[0013] The language representation subspace and the database schema representation subspace are initialized, and the vector dimensions of the language representation subspace and the database schema representation subspace are uniformly mapped to a preset dimension through a projection layer with shared parameters to form the joint semantic space;

[0014] The natural language query is encoded into a query semantic vector in the language representation subspace through a bidirectional mapping network, and the pattern elements in the target database structure description are encoded into pattern structure vectors in the database pattern representation subspace. The query semantic vector and the pattern structure vector are mapped to the joint semantic space through cross-space projection transformation.

[0015] In the joint semantic space, the query semantic vector and the pattern structure vector are used as anchor pairs to construct positive sample triples, and the query semantic vector in the anchor pairs is randomly combined with semantically unrelated pattern structure vectors to construct negative sample triples.

[0016] By synchronously adjusting the network parameters of the bidirectional mapping network and the projection layer with shared parameters through a contrastive learning mechanism, the vector distance between the query semantic vector and the pattern structure vector in the joint semantic space of the positive sample triples is less than the vector distance between the query semantic vector and the pattern structure vector in the negative sample triples, thus forming a semantically aligned distribution.

[0017] By synchronously adjusting the network parameters of the bidirectional mapping network and the projection layer with shared parameters through a contrastive learning mechanism, the vector distance between the query semantic vector and the pattern structure vector in the joint semantic space of the positive sample triples is less than the vector distance between the query semantic vector and the pattern structure vector in the negative sample triples, forming a semantically aligned distribution including:

[0018] Calculate the interval between the distances of positive and negative sample vectors to obtain a distance difference metric.

[0019] The partial derivative of the distance difference metric with respect to the encoding parameters of the bidirectional mapping network is used to obtain the encoding parameter gradient. The partial derivative of the distance difference metric with respect to the mapping parameters of the projection layer of the shared parameters is used to obtain the mapping parameter gradient. The encoding parameter gradient and the mapping parameter gradient together constitute the gradient update direction of the network parameters.

[0020] When the distance difference metric indicates that the distance between the positive sample vectors is greater than the distance between the negative sample vectors, the encoding parameters of the bidirectional mapping network are adjusted to reduce the distance between the positive sample vectors, and the mapping parameters of the projection layer with shared parameters are adjusted to increase the distance between the negative sample vectors.

[0021] The gradient update direction of the network parameters is synchronously applied to the bidirectional mapping network and the projection layer of the shared parameters through the backpropagation mechanism. The encoding parameters of the bidirectional mapping network and the mapping parameters of the projection layer of the shared parameters are iteratively updated until the distance difference metric converges to a state where the distance between the positive sample vectors is less than the distance between the negative sample vectors, thus forming a semantically aligned distribution.

[0022] The natural language query expression is subjected to multi-level semantic parsing to extract the intent representation describing the query target and the entity relationship representation describing the query conditions, including:

[0023] Lexical and syntactic analysis are performed on the natural language query expression to identify query action words, target object words, and condition limit words in the natural language query expression;

[0024] Using the main query action word in the query action words as the root node and the auxiliary query action words as child nodes, establish an intent hierarchy association and map it as an intent dominating edge;

[0025] Using the query action word as the central node and the target object word as the associated node, an intent semantic graph describing the query target is constructed by connecting the action-object association edge and the intent domination edge.

[0026] The condition qualifiers are categorized into numerical constraint type, category filtering type, and relation filtering type, and corresponding type labels are assigned to entity-attribute association edges.

[0027] Using the target object words as entity nodes and the condition qualifier words as attribute nodes, an entity relationship semantic graph describing the query conditions is constructed by connecting the entity nodes and the attribute nodes through entity-attribute association edges with type labels.

[0028] The intent semantic graph is encoded using a heterogeneous graph neural network. Attention weights are set for the action-object association edges and the intent domination edges, respectively, and then aggregated to form an intent representation describing the query target.

[0029] A type-aware graph convolutional network is used to encode the entity relationship semantic graph. The constraint strength, attribution confidence and path connectivity are calculated and aggregated according to the type labels to form an entity relationship representation describing the query conditions.

[0030] Constructing multi-hop reasoning paths between the pattern elements based on the graph attention propagation mechanism, and performing semantic propagation and aggregation of the entity relation representation annotated with the pattern elements along the multi-hop reasoning paths, includes:

[0031] Calculate the semantic association strength between the pattern elements, and calculate the path confidence decay factor based on the number of intermediate pattern elements;

[0032] Based on the semantic association strength and the path confidence decay factor, pattern element pairs are selected, and the connection path between the starting pattern element and the target pattern element in the pattern element pair is identified as the multi-hop inference path.

[0033] The entity relation representation annotated with the pattern elements is used as the initial semantic representation. The semantic representations of intermediate pattern elements are obtained sequentially along the multi-hop reasoning path. The semantic matching degree between the initial semantic representation and the semantic representation of intermediate pattern elements is calculated. The semantic consistency between the initial semantic representation and the semantic representation of the target pattern element is calculated. The attention propagation weight is determined by the weighted combination of the semantic matching degree and the semantic consistency.

[0034] According to the attention propagation weight, the initial semantic representation is propagated hop by hop along the multi-hop inference path from the starting pattern element to the target pattern element. During the propagation process, the initial semantic representation is weighted and fused with the semantic representation of the intermediate pattern element to obtain the propagated semantic representation.

[0035] By aggregating all the propagated semantic representations that reach the same target pattern element through an aggregation operation, the aggregated semantic representation corresponding to the target pattern element is obtained.

[0036] In conjunction with the co-occurrence statistical characteristics of pattern elements in historical query execution records, the weights of the multi-hop inference paths are adaptively adjusted to establish a context-dependent mapping relationship between the entity relation representation and the pattern elements, including:

[0037] The co-occurrence frequency of pattern element pairs in the co-occurrence sequence of pattern elements in the historical query execution records is counted. A time decay factor is calculated based on the timestamp of the historical query execution records. The co-occurrence frequency is scaled by the time decay factor and then accumulated to obtain the historical co-occurrence intensity of the pattern element pairs.

[0038] The historical co-occurrence intensities of each pair of adjacent pattern elements on the multi-hop inference path are multiplied to obtain the historical co-occurrence intensities of the multi-hop inference path. This is then fused with the current weight of the multi-hop inference path to obtain the path adjustment coefficient.

[0039] In the knowledge graph schema layer, query the entity class schema elements and relation class schema elements corresponding to the entity type information and relation type information in the entity relation representation;

[0040] Calculate the weighted type similarity between the entity relationship representation and the entity class pattern element based on the path adjustment coefficient, calculate the weighted relationship similarity between the entity relationship representation and the relationship class pattern element, and concatenate the weighted type similarity and the weighted relationship similarity into a context feature vector;

[0041] The context feature vector and the entity relationship representation are concatenated and fused, and mapped to pattern element annotation vectors through a nonlinear transformation network, thus establishing a context-dependent mapping relationship between the entity relationship representation and the pattern elements.

[0042] Based on the intent representation and the effective mapping, generating a formal query statement that conforms to the target database query language specification and satisfies semantic consistency under the constraints of the decoding includes:

[0043] Based on the intent representation, the query target entity is extracted. Based on the query target entity, the table name and field name are retrieved from the effective mapping to construct a table field mapping table. The foreign key constraint relationship between tables is extracted from the decoded constraint to construct a table connection dependency graph.

[0044] The table set involved is determined based on the table field mapping table. Candidate join paths connecting the table set are searched in the table join dependency graph. Path cost weights are assigned based on the usage frequency and query execution efficiency statistics of each foreign key relationship on the candidate join path in historical queries. The candidate join path with the smallest weighted sum of path length and path cost weights is selected to generate the table join condition clause.

[0045] Extract the attribute mapping relationship between the entity relationship representation and the database field from the effective mapping, convert the attribute constraints in the attribute mapping relationship into field filtering expressions and generate filtering condition clauses, and convert the intent representation into an expected semantic graph structure and into a lexical selection preference vector;

[0046] An autoregressive sequence generator decoder is used. At each time step of decoding, the candidate word space is limited according to the decoding constraints. The generation probability of each word in the candidate word space is weighted and adjusted using the word selection preference vector. The word with the highest generation probability is selected and concatenated to the generated query statement fragment. The table join condition clause is used as the join condition, and the filter condition clause is used as the filter condition to obtain the formal query statement.

[0047] A second aspect of this invention provides a natural language-based intelligent conversion and analysis system for database query statements, comprising:

[0048] The first unit is used to construct a joint semantic space that integrates the language representation subspace and the database schema representation subspace. Through contrastive learning, the semantic vectors of the natural language query expression and the structure vectors of the schema elements in the target database structure description are semantically aligned in the joint semantic space.

[0049] The second unit is used to perform multi-level semantic parsing on the natural language query expression, and extract the intent representation describing the query target and the entity relationship representation describing the query conditions;

[0050] The third unit is used to construct multi-hop inference paths between the pattern elements based on the graph attention propagation mechanism. The entity relation representation annotated with the pattern elements is semantically propagated and aggregated along the multi-hop inference path. The weight of the multi-hop inference path is adaptively adjusted by combining the co-occurrence statistical features of the pattern elements in the historical query execution records, and a context-dependent mapping relationship is established between the entity relation representation and the pattern elements.

[0051] The fourth unit is used to select the mapping relationship whose confidence level meets the preset confidence threshold as a valid mapping;

[0052] The fifth unit is used to take the integrity constraints, foreign key dependencies, and query syntax rules of the target database as decoding constraints, and generate a formal query statement that conforms to the query language specification of the target database and satisfies semantic consistency under the limitation of the decoding constraints, based on the intent representation and the effective mapping.

[0053] A third aspect of the embodiments of the present invention,

[0054] An electronic device is provided, comprising:

[0055] processor;

[0056] Memory used to store processor-executable instructions;

[0057] The processor is configured to invoke instructions stored in the memory to execute the aforementioned method.

[0058] Fourth aspect of the embodiments of the present invention,

[0059] A computer-readable storage medium is provided, on which computer program instructions are stored, which, when executed by a processor, implement the aforementioned method.

[0060] The beneficial effects of this application are as follows:

[0061] By constructing a joint semantic space that integrates the linguistic representation subspace and the database schema representation subspace, and utilizing contrastive learning to achieve semantic alignment, the semantic gap between natural language and database schema elements is resolved, improving the accuracy of cross-modal semantic understanding. A multi-level semantic parsing technique is employed to separate and extract query intent representation and entity relation representation, enabling the system to simultaneously understand "what to search" and "what the query conditions are," enhancing its ability to understand complex queries. Innovatively, a graph attention propagation mechanism and the co-occurrence statistical features of historical query execution records are combined to construct multi-hop inference paths and achieve adaptive weight adjustment, effectively solving the complex context dependency mapping problem between entity relations and database schema elements, and improving the accuracy of query transformation. Attached Figure Description

[0062] Figure 1 This is a flowchart illustrating the intelligent conversion and analysis method for database query statements based on natural language, as described in an embodiment of the present invention.

[0063] Figure 2 A schematic diagram of the process for constructing and aligning a joint semantic space. Detailed Implementation

[0064] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0065] The technical solution of the present invention will be described in detail below with reference to specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.

[0066] Figure 1 This is a flowchart illustrating the intelligent conversion and analysis method for database query statements based on natural language according to an embodiment of the present invention. Figure 1 As shown, the method includes:

[0067] A joint semantic space is constructed that integrates the language representation subspace and the database schema representation subspace. Through contrastive learning, the semantic vectors of the natural language query expression and the structure vectors of the schema elements in the target database structure description are semantically aligned in the joint semantic space.

[0068] The natural language query expression is subjected to multi-level semantic parsing to extract the intent representation describing the query target and the entity relationship representation describing the query conditions;

[0069] Based on the graph attention propagation mechanism, a multi-hop inference path is constructed between the pattern elements. The entity relation representation annotated with the pattern elements is semantically propagated and aggregated along the multi-hop inference path. The weight of the multi-hop inference path is adaptively adjusted by combining the co-occurrence statistical features of pattern elements in historical query execution records, and a context-dependent mapping relationship is established between the entity relation representation and the pattern elements.

[0070] The mapping relationship whose confidence level meets the preset confidence threshold is selected as the valid mapping;

[0071] The integrity constraints, foreign key dependencies, and query syntax rules of the target database are used as decoding constraints. Based on the intent representation and the effective mapping, a formal query statement that conforms to the query language specification of the target database and satisfies semantic consistency is generated under the limitation of the decoding constraints.

[0072] In one optional implementation, a joint semantic space is constructed that integrates the language representation subspace and the database schema representation subspace. Through contrastive learning, the semantic vectors of the natural language query expression and the structure vectors of the schema elements in the target database structure description are semantically aligned in the joint semantic space, including:

[0073] The language representation subspace and the database schema representation subspace are initialized, and the vector dimensions of the language representation subspace and the database schema representation subspace are uniformly mapped to a preset dimension through a projection layer with shared parameters to form the joint semantic space;

[0074] The natural language query is encoded into a query semantic vector in the language representation subspace through a bidirectional mapping network, and the pattern elements in the target database structure description are encoded into pattern structure vectors in the database pattern representation subspace. The query semantic vector and the pattern structure vector are mapped to the joint semantic space through cross-space projection transformation.

[0075] In the joint semantic space, the query semantic vector and the pattern structure vector are used as anchor pairs to construct positive sample triples, and the query semantic vector in the anchor pairs is randomly combined with semantically unrelated pattern structure vectors to construct negative sample triples.

[0076] By synchronously adjusting the network parameters of the bidirectional mapping network and the projection layer with shared parameters through a contrastive learning mechanism, the vector distance between the query semantic vector and the pattern structure vector in the joint semantic space of the positive sample triples is less than the vector distance between the query semantic vector and the pattern structure vector in the negative sample triples, thus forming a semantically aligned distribution.

[0077] like Figure 2 As shown, the method includes:

[0078] The joint semantic space is initialized by creating separate language representation subspaces and database schema representation subspaces as initial independent semantic representation regions. The language representation subspace uses a pre-trained language model (such as BERT or RoBERTa) as the basic encoder, with an output dimension typically of 768 or 1024. The database schema representation subspace uses a graph neural network (such as GCN or GAT) as the basic structural encoder, representing structural information such as tables and fields in the database schema, with an output dimension different from the language model. To enable comparison of the representations from the two subspaces within the same semantic space, a projection layer with shared parameters is designed. This projection layer, implemented using a fully connected neural network, maps the vectors from both subspaces to a joint semantic space of a preset dimension (e.g., 256 dimensions). The projection layer uses the same weight matrix W and bias vector b for both language and database representations, ensuring that the two heterogeneous representations are projected into the same semantic metric space.

[0079] A bidirectional mapping network is used to encode and map natural language queries to database schema elements. For the input natural language query, word segmentation converts it into a token sequence, which is then input into a pre-trained language model to obtain context-sensitive token embeddings. An attention pooling mechanism aggregates token-level representations to obtain the semantic vector q of the entire query statement, located in the language representation subspace. For the target database structure, a database schema graph containing tables, fields, foreign key constraints, etc., is constructed. Nodes in the graph represent database elements, and edges represent relationships between elements. The database schema graph is input into a graph neural network, and a message-passing mechanism captures the structural relationships between elements, generating a structure vector s for each schema element (such as a table or column), located in the database schema representation subspace. Finally, through the aforementioned projection layer with shared parameters, the query semantic vector q and the schema structure vector s are mapped to the joint semantic space, respectively, to obtain q' and s'.

[0080] For each natural language query, the correct database schema element (such as a table, column, or a combination thereof) is determined based on the labeled data. A positive sample pair (q', s'+) is formed by pairing the query semantic vector q' with its corresponding schema structure vector s'. Simultaneously, a schema element semantically irrelevant to the current query is randomly selected from the current batch or the global database schema element set; its structure vector is denoted as s'-, and it forms a negative sample pair (q', s'-) with q'. In this way, triples (q', s'+, s'-) containing positive and negative samples are constructed for contrastive learning. Multiple such triples are typically constructed in each batch to ensure the model can learn to distinguish between semantically relevant and irrelevant query-schema pairs.

[0081] The contrastive loss for each triple is calculated using the InfoNCE loss function, which aims to maximize the similarity of positive sample pairs (q', s'+) in the joint semantic space while minimizing the similarity of negative sample pairs (q', s'-). Similarity is calculated using cosine similarity or a dot product. Specifically, for each query vector q', its similarity with the positive sample pattern vector s'+ should be significantly higher than its similarity with all negative sample vectors s'-. The model synchronously updates the parameters of the language encoder, structure encoder, and shared projection layer through backpropagation, ensuring that semantically related query-pattern pairs are closer in the joint space and semantically unrelated pairs are farther apart. A dynamically adjusted learning rate strategy and gradient pruning technique are employed during training to ensure stable model convergence.

[0082] Through the above steps, a joint semantic space integrating language representation and database schema representation is gradually constructed, achieving semantic alignment between natural language queries and database schema elements. In practical applications, when a user submits a new natural language query, it is mapped to the joint semantic space, and then the closest database schema element in the semantic space is retrieved, thus realizing the conversion from natural language to database query. This joint semantic space-based method overcomes the semantic gap between language expression and database structure in traditional methods, improving the accuracy and robustness of text-to-SQL conversion.

[0083] In one optional implementation, the network parameters of the bidirectional mapping network and the projection layer with shared parameters are synchronously adjusted through a contrastive learning mechanism, such that the vector distance between the query semantic vector and the pattern structure vector in the positive sample triples in the joint semantic space is less than the vector distance between the query semantic vector and the pattern structure vector in the negative sample triples, forming a semantically aligned distribution including:

[0084] Calculate the interval between the distances of positive and negative sample vectors to obtain a distance difference metric.

[0085] The partial derivative of the distance difference metric with respect to the encoding parameters of the bidirectional mapping network is used to obtain the encoding parameter gradient. The partial derivative of the distance difference metric with respect to the mapping parameters of the projection layer of the shared parameters is used to obtain the mapping parameter gradient. The encoding parameter gradient and the mapping parameter gradient together constitute the gradient update direction of the network parameters.

[0086] When the distance difference metric indicates that the distance between the positive sample vectors is greater than the distance between the negative sample vectors, the encoding parameters of the bidirectional mapping network are adjusted to reduce the distance between the positive sample vectors, and the mapping parameters of the projection layer with shared parameters are adjusted to increase the distance between the negative sample vectors.

[0087] The gradient update direction of the network parameters is synchronously applied to the bidirectional mapping network and the projection layer of the shared parameters through the backpropagation mechanism. The encoding parameters of the bidirectional mapping network and the mapping parameters of the projection layer of the shared parameters are iteratively updated until the distance difference metric converges to a state where the distance between the positive sample vectors is less than the distance between the negative sample vectors, thus forming a semantically aligned distribution.

[0088] After constructing positive and negative sample triples, the margin between the positive and negative sample vector distances is calculated. Specifically, Euclidean distance is used to calculate the distance between the query semantic vector and the pattern structure vector in the joint semantic space. The distance d_pos between the query semantic vector q and the pattern structure vector p_pos in the positive sample triple is calculated: d_pos = ||q - p_pos||2. Similarly, the distance d_neg between the query semantic vector q and the pattern structure vector p_neg in the negative sample triple is calculated: d_neg = ||q - p_neg||2. The distance difference metric margin_diff is then calculated, representing the difference between the positive and negative sample vector distances: margin_diff = d_pos - d_neg + margin, where margin is a preset boundary constant, usually set to a positive value to ensure that the positive sample distance is at least margin units smaller than the negative sample distance. When margin_diff is greater than 0, it indicates that the positive sample vector distance is greater than the negative sample vector distance, and the network parameters need to be adjusted.

[0089] The gradient of the encoding parameters is obtained by taking the partial derivative of the distance difference metric with respect to the encoding parameters of the bidirectional mapping network. Specifically, let the encoding parameters of the bidirectional mapping network be θ_enc, and calculate them using the chain rule: grad_θ_enc = margin_diff / θ_enc.

[0090] The gradient of the mapping parameters is obtained by taking the partial derivative of the distance difference metric with respect to the mapping parameters of the projection layer with shared parameters. Let the mapping parameters of the projection layer with shared parameters be θ_proj, and calculate: grad_θ_proj = margin_diff / θ_proj, the gradient of the encoding parameters and the gradient of the mapping parameters together constitute the gradient update direction of the network parameters.

[0091] When the distance difference metric `margin_diff` is greater than 0, indicating that the distance between positive sample vectors is greater than the distance between negative sample vectors, it is necessary to adjust the encoding parameters of the bidirectional mapping network to reduce the distance between positive sample vectors, and simultaneously adjust the mapping parameters of the projection layer with shared parameters to increase the distance between negative sample vectors. Specifically, gradient descent is used to update the parameters: `θ_enc = θ_enc - learning_rate × grad_θ_enc`; `θ_proj = θ_proj - learning_rate × grad_θ_proj`, where `learning_rate` is the learning rate, controlling the step size of each parameter update.

[0092] The gradient update direction of the network parameters is synchronously applied to the bidirectional mapping network and the projection layer with shared parameters through backpropagation. In each iteration, forward propagation is performed to compute the representations of the query semantic vector and pattern structure vector in the joint semantic space, calculate the distance difference metric, and update the network parameters through backpropagation.

[0093] The encoding parameters of the bidirectional mapping network and the mapping parameters of the projection layer with shared parameters are iteratively updated until the distance difference metric converges to a state where the distance between positive sample vectors is less than the distance between negative sample vectors, forming a semantically aligned distribution. In the actual implementation, an upper limit of the number of iterations, max_iter, and a convergence threshold, threshold, are set. Training is considered to have converged when the upper limit of the number of iterations is reached or when the change in the distance difference metric is less than the threshold after multiple consecutive iterations.

[0094] In specific application scenarios, such as the mapping task between natural language queries and structured queries, the query semantic vector can be a vector representation of the user-input natural language query generated by a semantic encoder, while the pattern structure vector can be a vector representation of the database schema or API structure generated by a structure encoder. Through the above contrastive learning mechanism, natural language queries and corresponding structured queries can be brought closer in the joint semantic space, while irrelevant structured queries are pushed away, thereby achieving an accurate mapping from natural language to structured queries.

[0095] Experiments show that this bidirectional mapping method based on contrastive learning can effectively capture the correspondence between semantics and structure, and performs excellently in tasks such as cross-modal retrieval, natural language processing, and code generation. Especially when dealing with complex semantic structure mappings, the model performance can be further improved by adjusting the ratio of positive and negative samples and the sampling strategy, adapting to the application needs of different domains.

[0096] In one optional implementation, multi-level semantic parsing is performed on the natural language query representation to extract the intent representation describing the query target and the entity relationship representation describing the query conditions, including:

[0097] Lexical and syntactic analysis are performed on the natural language query expression to identify query action words, target object words, and condition limit words in the natural language query expression;

[0098] Using the main query action word in the query action words as the root node and the auxiliary query action words as child nodes, establish an intent hierarchy association and map it as an intent dominating edge;

[0099] Using the query action word as the central node and the target object word as the associated node, an intent semantic graph describing the query target is constructed by connecting the action-object association edge and the intent domination edge.

[0100] The condition qualifiers are categorized into numerical constraint type, category filtering type, and relation filtering type, and corresponding type labels are assigned to entity-attribute association edges.

[0101] Using the target object words as entity nodes and the condition qualifier words as attribute nodes, an entity relationship semantic graph describing the query conditions is constructed by connecting the entity nodes and the attribute nodes through entity-attribute association edges with type labels.

[0102] The intent semantic graph is encoded using a heterogeneous graph neural network. Attention weights are set for the action-object association edges and the intent domination edges, respectively, and then aggregated to form an intent representation describing the query target.

[0103] A type-aware graph convolutional network is used to encode the entity relationship semantic graph. The constraint strength, attribution confidence and path connectivity are calculated and aggregated according to the type labels to form an entity relationship representation describing the query conditions.

[0104] The system performs lexical and syntactic analysis on the input natural language query. In the lexical analysis stage, a pre-trained language model is used to segment the query text, perform part-of-speech tagging, and name entity recognition. In the syntactic analysis stage, a dependency syntax tree is constructed to identify the dependency relationships between sentence components. Through these analyses, three types of keywords are identified from the query: query action words (such as "query," "search," and "statistics"), target object words (such as "order," "product," and "user"), and conditional words (such as "greater than," "belongs to," and "related to"). For example, for the query "find electronic products with sales exceeding 10,000 yuan in the last three months," "find" is identified as the query action word, "electronic products" as the target object word, and "last three months" and "sales exceeding 10,000 yuan" as conditional words.

[0105] A hierarchical structure is established with the main query action term as the root node and auxiliary query action terms as child nodes. For example, in the query "statistically analyze last year's sales data", "statistically" is the main query action term, and "analyze" is the auxiliary query action term. They are connected by intent domination edges, indicating the dominance relationship between "statistically" and "analyze". Intent domination edges are assigned weight values ​​to reflect the strength of dominance. The weight calculation is based on the distance between words and semantic relevance.

[0106] Using query action words as the central node and target object words as associated nodes, they are connected by action-object association edges. For example, for "find active users and count their consumption records", an intent semantic graph is constructed containing two action word nodes, "find" and "count", and two object word nodes, "active users" and "consumption records". "Find" is connected to "count" through intent dominance edges, and each is connected to its respective target object word through action-object association edges.

[0107] Conditional qualifiers are categorized into three types: numerical constraint types (e.g., "greater than", "less than"), category filtering types (e.g., "belongs to", "type is"), and relational filtering types (e.g., "associated with", "contains"). Corresponding type labels are assigned to entity-attribute association edges to differentiate the processing logic for different conditional qualifiers.

[0108] Based on the partitioned condition qualifiers, an entity relation semantic graph describing the query conditions is constructed, with target object words as entity nodes and condition qualifiers as attribute nodes, connected by entity-attribute association edges with type labels. For example, for "querying smartphones with prices higher than 1000 yuan", an entity relation semantic graph is constructed with "smartphones" as entity nodes and "prices higher than 1000 yuan" as attribute nodes, and the connecting edges are assigned the type label "numerical constraint".

[0109] The constructed intent semantic graph is encoded, and a heterogeneous graph neural network is used to process different types of nodes and edges. Different attention mechanisms are set to calculate weights for action-object association edges and intent dominance edges. The attention weights for association edges are calculated based on the semantic similarity of nodes, while the attention weights for dominance edges are based on the strength of the hierarchical relationship between action words. Information from different types of edges is aggregated through a multi-head attention mechanism to obtain updated node representations. After iterative updates by a multi-layer graph neural network, the node representations are aggregated to form an intent representation vector describing the query target.

[0110] The entity relationship semantic graph is encoded using a type-aware graph convolutional network. Processing logic is differentiated based on the edge type label: for edges with numerical constraints, constraint strength is calculated; for edges with category filtering, attribution confidence is calculated; and for edges with relationship filtering, path connectivity is calculated. These three metrics are then weighted and aggregated to form the final entity relationship representation vector. In the processing, the constraint strength is calculated based on a quantitative evaluation of the span of the numerical interval and the clarity of the boundaries. Specifically, for entity-attribute association edges of numerical constraint type, the numerical boundary values ​​and comparison operators in the attribute nodes are extracted, the normalized span of the numerical interval is calculated, and boundary clarity weights are assigned according to the explicitness of the comparison operators (strict inequality and loose inequality). The normalized span and boundary clarity weights are then weighted and summed to obtain the constraint strength value. The attribution confidence is calculated based on a comprehensive evaluation of the depth and semantic similarity of the category concept in the ontology hierarchy. Specifically, for entity-attribute association edges of category filtering type, the category concept corresponding to the attribute node is retrieved from the pre-built domain ontology knowledge base, the depth normalized value of the category concept in the ontology hierarchy tree is calculated as a measure of concept specificity, and the relationship between the category concept and the entity node is also calculated. In semantic space, cosine similarity is used as a semantic matching metric. The concept specificity metric and the semantic matching metric are weighted and aggregated to obtain the attribution confidence value. Path connectivity is calculated based on a comprehensive measure of the hop count of the inter-entity association path and the association strength of the edges on the path. Specifically, for entity-attribute association edges of the relation filtering type, all reachable paths from the source entity node to the target entity node are searched in the entity relation semantic graph. The reciprocal of the hop count for each path is calculated as the path density, and the weight values ​​of all edges on that path are accumulated as the path association strength. The path density and path association strength are multiplied and summed over all paths to obtain the path connectivity value. Finally, the constraint strength value, attribution confidence value, and path connectivity value are weighted and summed according to a preset type weight coefficient to form a comprehensive entity relation representation vector representing the query conditions. Finally, through the weighted aggregation of these metrics, a comprehensive entity relation representation representing the query conditions is obtained.

[0111] Through the steps described above, natural language queries are transformed into structured intent representations and entity relation representations, providing a precise semantic understanding foundation for subsequent query execution and result retrieval. This multi-level semantic parsing method can handle complex nested queries and multiple conditional constraints, significantly improving the understanding ability and response accuracy of natural language query systems.

[0112] In practical applications, such as enterprise data analysis platforms, when users input complex queries like "find products whose sales exceeded expectations last quarter and whose customer satisfaction was higher than the industry average," this method can accurately identify the query intent as "find products" and extract two key conditions: "sales exceeded expectations" (numerical constraint type) and "customer satisfaction was higher than the industry average" (numerical constraint type). This allows the method to correctly understand and execute the query, providing accurate data support for decision-makers.

[0113] In one optional implementation, constructing a multi-hop reasoning path between the pattern elements based on a graph attention propagation mechanism, and performing semantic propagation and aggregation of the entity relation representation annotated with the pattern elements along the multi-hop reasoning path includes:

[0114] Calculate the semantic association strength between the pattern elements, and calculate the path confidence decay factor based on the number of intermediate pattern elements;

[0115] Based on the semantic association strength and the path confidence decay factor, pattern element pairs are selected, and the connection path between the starting pattern element and the target pattern element in the pattern element pair is identified as the multi-hop inference path.

[0116] The entity relation representation annotated with the pattern elements is used as the initial semantic representation. The semantic representations of intermediate pattern elements are obtained sequentially along the multi-hop reasoning path. The semantic matching degree between the initial semantic representation and the semantic representation of intermediate pattern elements is calculated. The semantic consistency between the initial semantic representation and the semantic representation of the target pattern element is calculated. The attention propagation weight is determined by the weighted combination of the semantic matching degree and the semantic consistency.

[0117] According to the attention propagation weight, the initial semantic representation is propagated hop by hop along the multi-hop inference path from the starting pattern element to the target pattern element. During the propagation process, the initial semantic representation is weighted and fused with the semantic representation of the intermediate pattern element to obtain the propagated semantic representation.

[0118] By aggregating all the propagated semantic representations that reach the same target pattern element through an aggregation operation, the aggregated semantic representation corresponding to the target pattern element is obtained.

[0119] Calculate the semantic association strength between pattern elements. For any two pattern elements mi and mj, the semantic association strength Sij is measured by calculating their cosine similarity: Sij = cosine(vi, vj), where vi and vj are the vector representations of pattern elements mi and mj, respectively. The semantic association strength ranges from [-1, 1], with a larger value indicating a stronger semantic association between the two pattern elements.

[0120] The path confidence decay factor is calculated based on the number of intermediate pattern elements. For a path p from pattern element mi to mj, if the path contains n intermediate pattern elements, the path confidence decay factor γp is defined as: γp = α n Where α is the attenuation base, with a value between (0, 1), which can be set according to the specific application scenario, such as α = 0.8. The larger n is, the smaller the attenuation factor γp is, indicating a longer path and a lower confidence level.

[0121] Based on semantic association strength and path confidence decay factor, pattern element pairs are selected. For any two pattern elements mi and mj, when the semantic association strength Sij between them is greater than the preset threshold θs (e.g., θs=0.5) and the path confidence decay factor γp is greater than the preset threshold θγ (e.g., θγ=0.3), the connection path between mi and mj in the pattern element pair (mi, mj) is identified as a multi-hop inference path.

[0122] The entity relation representation annotated with pattern elements is used as the initial semantic representation. For the relation r between entities ei and ej, if the relation is annotated with pattern element mk, then the initial semantic representation is h0k.

[0123] Sequentially obtain the semantic representations of intermediate pattern elements along the multi-hop inference path. Assume the multi-hop inference path from the initial pattern element ms to the target pattern element mt is p = (ms, m1, m2, ..., mn, mt), where m1, m2, ..., mn are intermediate pattern elements. The semantic representation of the pattern element mi is hmi.

[0124] The semantic matching degree between the initial semantic representation and the semantic representation of the intermediate pattern element is calculated. For the intermediate pattern element mi in path p, the semantic matching degree Mki between it and the initial semantic representation h0k is calculated as: Mki = tanh(h0k · hmi), where tanh is the hyperbolic tangent function that maps the matching degree to the interval [-1, 1].

[0125] The semantic consistency between the initial semantic representation and the target pattern element semantic representation is calculated. For the target pattern element mt in path p, its semantic consistency Ckt with the initial semantic representation h0k is calculated as: Ckt = sigmoid(h0k ·hmt), where the sigmoid function maps the consistency to the interval (0, 1).

[0126] The attention propagation weight is determined by a weighted combination of semantic matching degree and semantic consistency. For a hop from pattern element mi to mj in path p, the attention propagation weight αij is calculated as: αij = β·Mki + (1-β)·Ckj, where β is a hyperparameter that weighs semantic matching degree and semantic consistency, and its value ranges between [0, 1]. It can be set to β=0.5.

[0127] Based on the attention propagation weights, the initial semantic representation is propagated hop-by-hop along the multi-hop inference path from the initial pattern element to the target pattern element. For each hop in path p from pattern element mi to mj, the propagated semantic representation hjk is calculated as: hjk = αij·h0k + (1-αij)·hmj, which means that the initial semantic representation is weighted and fused with the semantic representations of the intermediate pattern elements to obtain the propagated semantic representation.

[0128] By aggregating all propagated semantic representations leading to the same target pattern element, an aggregated semantic representation corresponding to the target pattern element is obtained. Assuming there are P paths leading to the target pattern element mt, producing propagated semantic representations htk1, htk2, ..., htkP respectively, the aggregated semantic representation htk corresponding to the target pattern element mt is obtained. Calculated as: htk =maxpool(htk1, htk2, ..., htkP), where maxpool is an element-wise max pooling operation that selects the maximum value in each dimension to form the final aggregate semantic representation.

[0129] Through the above process, a multi-hop reasoning path based on the graph attention propagation mechanism is constructed, along with the semantic propagation and aggregation process along this path. In practical applications, such as knowledge graph completion tasks, this mechanism can be used to uncover implicit relationships between pattern elements, improving the ability to reason about entity relationships. When it is necessary to predict the relationship between entity e1 and entity e2, a multi-hop reasoning path can be constructed, starting from relevant pattern elements, to perform semantic propagation and aggregation, ultimately obtaining high-quality relationship prediction results.

[0130] In one optional implementation, and in conjunction with the co-occurrence statistical characteristics of pattern elements in historical query execution records, the weights of the multi-hop inference paths are adaptively adjusted to establish a context-dependent mapping relationship between the entity relation representation and the pattern elements, including:

[0131] The co-occurrence frequency of pattern element pairs in the co-occurrence sequence of pattern elements in the historical query execution records is counted. A time decay factor is calculated based on the timestamp of the historical query execution records. The co-occurrence frequency is scaled by the time decay factor and then accumulated to obtain the historical co-occurrence intensity of the pattern element pairs.

[0132] The historical co-occurrence intensities of each pair of adjacent pattern elements on the multi-hop inference path are multiplied to obtain the historical co-occurrence intensities of the multi-hop inference path. This is then fused with the current weight of the multi-hop inference path to obtain the path adjustment coefficient.

[0133] In the knowledge graph schema layer, query the entity class schema elements and relation class schema elements corresponding to the entity type information and relation type information in the entity relation representation;

[0134] Calculate the weighted type similarity between the entity relationship representation and the entity class pattern element based on the path adjustment coefficient, calculate the weighted relationship similarity between the entity relationship representation and the relationship class pattern element, and concatenate the weighted type similarity and the weighted relationship similarity into a context feature vector;

[0135] The context feature vector and the entity relationship representation are concatenated and fused, and mapped to pattern element annotation vectors through a nonlinear transformation network, thus establishing a context-dependent mapping relationship between the entity relationship representation and the pattern elements.

[0136] In practical applications, historical query execution records are obtained in the following ways: First, by deploying a query log recording module in a database management system or knowledge graph query engine, detailed information about each query execution is automatically captured and stored, including key data such as the sequence of pattern elements involved in the query statement, the co-occurrence relationship of pattern element pairs, the query execution timestamp, and query execution efficiency metrics. This recording module operates in a non-intrusive manner and does not affect normal query processing performance. Second, historical query log data is imported from existing business systems or data analysis platforms. Pattern element information is extracted by parsing the query statements, and a co-occurrence sequence of pattern elements is established according to time sequence. This method is suitable for system migration or upgrade scenarios. The system can quickly accumulate initial historical data. During the initial deployment phase, simulated historical query execution records can be constructed using synthetic data generation methods. Specifically, based on the structural features of the knowledge graph pattern layer and typical query patterns provided by domain experts, query samples that conform to actual business scenarios are automatically generated, and these samples are assigned simulated timestamps and co-occurrence frequencies as initial seed data for historical records. An incremental learning strategy is adopted to continuously collect real-time query execution records during system operation, dynamically updating the co-occurrence statistics of pattern element pairs. As real historical data accumulates, the initial synthetic data or imported data is gradually replaced or diluted to ensure the accuracy and timeliness of historical co-occurrence intensity calculation. Through the flexible combination of the above multiple acquisition methods, the system can effectively acquire and utilize historical query execution records in different deployment stages and application scenarios, supporting adaptive adjustment of multi-hop inference path weights and the establishment of mapping relationships.

[0137] Extract the occurrence of pattern element pairs from the historical query execution record database of the knowledge graph. For example, suppose there are pattern element pairs...<Person, worksAt, Organization> and<Organization, locatedIn,Location> If two pattern elements appear together 5 times in historical queries, then the co-occurrence frequency of these two pattern element pairs is recorded as 5.

[0138] The time decay factor is calculated based on the timestamps of historical query execution records. This factor reflects the timeliness of historical queries, making newer queries have a greater impact on current inference. The specific calculation method can use an exponential decay function: for a historical record, its time decay factor can be expressed as e^(-1 / 2). -λΔt Where λ is the attenuation coefficient, and Δt is the difference between the current time and the historical timestamp. For example, for a historical record from 30 days ago, if λ is set to 0.05, then the attenuation factor is e. -0.05×30 ≈0.223.

[0139] The historical co-occurrence intensity of the pattern element pair is obtained by scaling the co-occurrence frequency with a time decay factor and then summing them. Taking the above example, if these 5 co-occurrences occurred 10 days, 15 days, 20 days, 25 days and 30 days ago, respectively, the corresponding decay factors are 0.607, 0.472, 0.368, 0.287 and 0.223, respectively. Then the historical co-occurrence intensity is 5×(0.607+0.472+0.368+0.287+0.223) / 5≈1.957.

[0140] The historical co-occurrence intensity of each adjacent pattern element pair on a multi-hop inference path is obtained by multiplying the historical co-occurrence intensity of the multi-hop inference path. For example, for path<Person, worksAt, Organization> →<Organization, locatedIn, Location> →<Location, hasEvent, Event> Assuming the historical co-occurrence intensities of the three pattern element pairs are 1.957, 2.345, and 1.682, respectively, the historical co-occurrence intensities of this path are approximately 1.957 × 2.345 × 1.682 ≈ 7.725.

[0141] The path adjustment coefficient is obtained by fusing the historical co-occurrence intensity of multi-hop inference paths with the current weight. The fusion method can be a weighted average: Path adjustment coefficient = α × current weight + (1-α) × historical co-occurrence intensity, where α is a balancing parameter. For example, if the current weight is 0.8 and α is 0.7, then the path adjustment coefficient = 0.7 × 0.8 + (1-0.7) × 7.725 ≈ 2.878.

[0142] In the knowledge graph schema layer, query the entity type information and relation type information in the entity relation representation to find the corresponding entity class schema elements and relation class schema elements. For example, for the entity "Chairman", the query finds that its type is "Person"; for the relation "establishment", the query finds that its type is "foundCompany".

[0143] The weighted type similarity between the entity relation representation and the entity class pattern element is calculated based on the path adjustment coefficient. Specifically, the similarity (e.g., cosine similarity) between the entity's vector representation and the entity class pattern element's vector representation is calculated and multiplied by the path adjustment coefficient. For example, if the cosine similarity between the entity "Steve Jobs" and the "Person" class is 0.9 and the path adjustment coefficient is 2.878, then the weighted type similarity is 0.9 × 2.878 ≈ 2.590.

[0144] Calculate the weighted similarity between the entity relation representation and the relation class schema element. For example, if the cosine similarity between the relation "create" and "foundCompany" is 0.85, then the weighted similarity is 0.85 × 2.878 ≈ 2.446.

[0145] The weighted type similarity and the weighted relation similarity are concatenated to form a context feature vector. In this example, the context feature vector is [2.590, 2.446].

[0146] The context feature vector and entity relation representation are concatenated and fused, and then mapped to pattern element annotation vectors through a nonlinear transformation network, establishing a context-dependent mapping relationship between entity relation representations and pattern elements. The nonlinear transformation network can be a multilayer perceptron (MLP), taking the concatenated vector as input and outputting the pattern element annotation vector. For example, if the entity relation representation is a 300-dimensional vector and the context feature vector is 2-dimensional, the concatenated vector will be 302-dimensional. After passing through the MLP, the corresponding pattern element annotation vector is obtained, which is used for subsequent pattern matching and inference.

[0147] In one optional implementation, generating a formal query statement that conforms to the target database query language specification and satisfies semantic consistency, based on the intent representation and the effective mapping, under the constraints of the decoding, includes:

[0148] Based on the intent representation, the query target entity is extracted. Based on the query target entity, the table name and field name are retrieved from the effective mapping to construct a table field mapping table. The foreign key constraint relationship between tables is extracted from the decoded constraint to construct a table connection dependency graph.

[0149] The table set involved is determined based on the table field mapping table. Candidate join paths connecting the table set are searched in the table join dependency graph. Path cost weights are assigned based on the usage frequency and query execution efficiency statistics of each foreign key relationship on the candidate join path in historical queries. The candidate join path with the smallest weighted sum of path length and path cost weights is selected to generate the table join condition clause.

[0150] Extract the attribute mapping relationship between the entity relationship representation and the database field from the effective mapping, convert the attribute constraints in the attribute mapping relationship into field filtering expressions and generate filtering condition clauses, and convert the intent representation into an expected semantic graph structure and into a lexical selection preference vector;

[0151] An autoregressive sequence generator decoder is used. At each time step of decoding, the candidate word space is limited according to the decoding constraints. The generation probability of each word in the candidate word space is weighted and adjusted using the word selection preference vector. The word with the highest generation probability is selected and concatenated to the generated query statement fragment. The table join condition clause is used as the join condition, and the filter condition clause is used as the filter condition to obtain the formal query statement.

[0152] Based on intent representation, the query target entity is extracted. The intent representation typically contains the core semantic elements of the user's query, where the query target entity refers to the main object for which the user hopes to obtain information. For example, when a user asks "Find all customers with sales exceeding 10,000 yuan", "customers" is the query target entity. The semantic parsing module analyzes the semantic structure of the intent representation to identify the type and attribute information of the query target entity.

[0153] After obtaining the target entity for the query, the table name and field name are retrieved from the valid mapping based on this entity, and a table field mapping table is constructed. The valid mapping stores the correspondence between natural language concepts and database structures, including the mapping information from entities to tables and attributes to fields. For example, the "customer" entity maps to the "customer" table in the database, while the "sales amount" attribute maps to the "amount" field in the "orders" table. The table field mapping table records the correspondence between all tables and fields involved in the query, providing a foundation for subsequent query generation.

[0154] Foreign key constraints between tables are extracted from the decoded constraints, and a table join dependency graph is constructed. The decoded constraints typically contain structural information about the database schema, where the foreign key relationships describe the association between different tables. The table join dependency graph is a weighted directed graph where nodes represent database tables, edges represent foreign key relationships between tables, and weights reflect the priority or complexity of the joins.

[0155] After determining the set of tables involved based on the table field mapping table, candidate join paths connecting these tables are searched in the table join dependency graph. Shortest path algorithms such as Dijkstra's algorithm or A* are employed. The algorithm identifies paths that join all necessary tables. For each candidate path, a path cost weight is assigned based on the frequency of use of each foreign key relationship along the path in historical queries and query execution efficiency statistics. For example, frequently used and efficient join paths are assigned lower cost weights, and vice versa.

[0156] By calculating the weighted sum of path length and path cost weights, the candidate join path with the minimum total cost is selected, and a table join condition clause is generated. The join condition clause uses the SQL JOIN syntax and is constructed based on the foreign key relationship on the selected path. For example, if the selected path contains a foreign key relationship of customer_id between the customer and orders tables, then the join condition "customer JOIN orders ON customer.id = orders.customer_id" is generated.

[0157] Extract the attribute mapping relationships between entity relationship representations and database fields from the valid mappings. These attribute mapping relationships include information such as attribute name, data type, and value range. Convert these attribute constraints into field filter expressions to generate filter condition clauses. For example, the constraint "sales exceeding 10,000 yuan" will be converted into the filter condition "orders.amount>10000".

[0158] The intent representation is transformed into a desired semantic graph structure. A semantic graph structure is a formal representation of query semantics, containing elements such as entities, attributes, and operators, and their relationships. Query action nodes and target object nodes are decoded from the intent representation vector, with query action nodes serving as operator nodes and target object nodes as entity nodes in the semantic graph. Attribute nodes and their constraints are extracted from the entity relationship representation, connecting attribute nodes to their corresponding entity nodes to form entity-attribute association edges. Based on the intent-dominant edges between query action nodes, dependencies between operator nodes are established in the semantic graph, representing the order of operation execution. Constraint types (numerical constraints, category filtering, or relationship filtering) are labeled for entity-attribute association edges based on the type labels of attribute constraints, and measures such as constraint strength, attribution confidence, and path connectivity are used as edge weight attributes. Operator nodes, entity nodes, attribute nodes, and their connecting edges are integrated to form a complete desired semantic graph structure, which formally expresses the hierarchical relationship between the query's semantic intent and constraints. Based on the semantic graph structure, it is further transformed into a vocabulary selection preference vector, which represents the priority weight of each vocabulary word during query generation.

[0159] An autoregressive sequence generator decoder is used to generate the final query statement. At each time step of decoding, the type of clause to be generated is determined based on the context of the generated query statement, and the corresponding syntax rule constraints are extracted from the decoding constraints. Based on the syntax rule constraints, the legal vocabulary category for the current time step is identified, such as keywords, table names, field names, or operators. All words belonging to this legal vocabulary category are selected from the vocabulary table of the target database query language to form an initial candidate vocabulary space. Furthermore, based on semantic consistency constraints, the semantic compatibility of each word in the candidate vocabulary space with the generated query fragment is checked, and words that conflict with the context semantics are excluded. The process involves several steps. For example, if a specific table has been selected, field names that do not belong to that table are excluded. For the remaining candidate words, the original generation probability of each word is calculated based on the current hidden state of the decoder. Preference weights corresponding to the candidate word space are extracted from the word selection preference vector; these preference weights reflect the semantic preference of the desired semantic graph structure for each word. The original generation probabilities are then multiplied element-wise by the corresponding preference weights to obtain a weighted probability distribution. This weighted probability distribution is then normalized to ensure that the sum of the probabilities of all candidate words is 1. Through these steps, the candidate word space is limited and the generation probabilities are weighted. For example, if a SELECT clause is currently being generated, the generation probabilities of table names and field names will be increased.

[0160] By selecting the words with the highest generation probability, they are concatenated into the generated query statement fragments. During the generation process, table join clauses are used as join conditions, and filter clauses are used as filter conditions. When the decoder generates the JOIN clause, it extracts the pre-constructed join condition expression from the table join condition clause. The table name, field names, and join operators in this expression are used as candidate words for the current time step. The decoder is forced to generate words according to the structural order of the table join condition clause, ensuring that the generated JOIN statement is consistent with the selected optimal join path. When the decoder generates the WHERE clause, it extracts the pre-transformed field filtering expression from the filter condition clause. The field names, comparison operators, and constraint values ​​in this expression are used as candidate words for the current time step. Based on the logical relationships (AND, OR) between multiple constraints in the filter condition clause, the decoder is guided to generate a WHERE clause that conforms to the constraint logic. During the generation process, the table join condition clause and filter condition clause are inserted as hard constraints into the decoder's candidate word space. By limiting the range and generation order of candidate words, the decoder ensures that the final generated query statement contains correct join and filter conditions in its syntactic structure, thereby guaranteeing the semantic consistency and execution correctness of the query statement.

[0161] This query generation method, based on intent representation and effective mapping, can effectively improve the accuracy and execution efficiency of generated query statements by making full use of database structure information and historical query statistics, thus solving the problem of converting natural language into database queries.

[0162] This invention relates to a natural language-based intelligent database query statement conversion and analysis system, the system comprising:

[0163] The first unit is used to construct a joint semantic space that integrates the language representation subspace and the database schema representation subspace. Through contrastive learning, the semantic vectors of the natural language query expression and the structure vectors of the schema elements in the target database structure description are semantically aligned in the joint semantic space.

[0164] The second unit is used to perform multi-level semantic parsing on the natural language query expression, and extract the intent representation describing the query target and the entity relationship representation describing the query conditions;

[0165] The third unit is used to construct multi-hop inference paths between the pattern elements based on the graph attention propagation mechanism. The entity relation representation annotated with the pattern elements is semantically propagated and aggregated along the multi-hop inference path. The weight of the multi-hop inference path is adaptively adjusted by combining the co-occurrence statistical features of the pattern elements in the historical query execution records, and a context-dependent mapping relationship is established between the entity relation representation and the pattern elements.

[0166] The fourth unit is used to select the mapping relationship whose confidence level meets the preset confidence threshold as a valid mapping;

[0167] The fifth unit is used to take the integrity constraints, foreign key dependencies, and query syntax rules of the target database as decoding constraints, and generate a formal query statement that conforms to the query language specification of the target database and satisfies semantic consistency under the limitation of the decoding constraints, based on the intent representation and the effective mapping.

[0168] A third aspect of the present invention provides an electronic device, comprising:

[0169] processor;

[0170] Memory used to store processor-executable instructions;

[0171] The processor is configured to invoke instructions stored in the memory to execute the aforementioned method.

[0172] A fourth aspect of the present invention provides a computer-readable storage medium having stored thereon computer program instructions that, when executed by a processor, implement the aforementioned method.

[0173] This invention can be a method, apparatus, system, and / or computer program product. The computer program product may include a computer-readable storage medium having computer-readable program instructions loaded thereon for performing various aspects of the invention.

[0174] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for intelligent conversion and analysis of database query statements based on natural language, characterized in that, include: A joint semantic space is constructed that integrates the language representation subspace and the database schema representation subspace. Through contrastive learning, the semantic vectors of the natural language query expression and the structure vectors of the schema elements in the target database structure description are semantically aligned and distributed within the joint semantic space. This includes: The language representation subspace and the database schema representation subspace are initialized, and the vector dimensions of the language representation subspace and the database schema representation subspace are uniformly mapped to a preset dimension through a projection layer with shared parameters to form the joint semantic space; The natural language query is encoded into a query semantic vector in the language representation subspace through a bidirectional mapping network, and the pattern elements in the target database structure description are encoded into pattern structure vectors in the database pattern representation subspace. The query semantic vector and the pattern structure vector are mapped to the joint semantic space through cross-space projection transformation. In the joint semantic space, the query semantic vector and the pattern structure vector are used as anchor pairs to construct positive sample triples, and the query semantic vector in the anchor pairs is randomly combined with semantically unrelated pattern structure vectors to construct negative sample triples. By synchronously adjusting the network parameters of the bidirectional mapping network and the projection layer of the shared parameters through a contrastive learning mechanism, the vector distance between the query semantic vector and the pattern structure vector in the joint semantic space of the positive sample triples is less than the vector distance between the query semantic vector and the pattern structure vector in the negative sample triples, thus forming a semantically aligned distribution. The natural language query expression is subjected to multi-level semantic parsing to extract the intent representation describing the query target and the entity relationship representation describing the query conditions, including: Lexical and syntactic analysis are performed on the natural language query expression to identify query action words, target object words, and condition limit words in the natural language query expression; Using the main query action word in the query action words as the root node and the auxiliary query action words as child nodes, establish an intent hierarchy association and map it as an intent dominating edge; Using the query action word as the central node and the target object word as the associated node, an intent semantic graph describing the query target is constructed by connecting the action-object association edge and the intent domination edge. The condition qualifiers are categorized into numerical constraint type, category filtering type, and relation filtering type, and corresponding type labels are assigned to entity-attribute association edges. Using the target object words as entity nodes and the condition qualifier words as attribute nodes, an entity relationship semantic graph describing the query conditions is constructed by connecting the entity nodes and the attribute nodes through entity-attribute association edges with type labels. The intent semantic graph is encoded using a heterogeneous graph neural network. Attention weights are set for the action-object association edges and the intent domination edges, respectively, and then aggregated to form an intent representation describing the query target. A type-aware graph convolutional network is used to encode the entity relationship semantic graph. The constraint strength, attribution confidence and path connectivity are calculated and aggregated according to the type labels to form an entity relationship representation describing the query conditions. Based on the graph attention propagation mechanism, a multi-hop inference path is constructed between the pattern elements. The entity relation representation annotated with the pattern elements is semantically propagated and aggregated along the multi-hop inference path. The weight of the multi-hop inference path is adaptively adjusted by combining the co-occurrence statistical features of pattern elements in historical query execution records, and a context-dependent mapping relationship is established between the entity relation representation and the pattern elements. The mapping relationship whose confidence level meets the preset confidence threshold is selected as the valid mapping; The integrity constraints, foreign key dependencies, and query syntax rules of the target database are used as decoding constraints. Based on the intent representation and the effective mapping, a formal query statement that conforms to the query language specification of the target database and satisfies semantic consistency is generated under the limitation of the decoding constraints.

2. The method according to claim 1, characterized in that, By synchronously adjusting the network parameters of the bidirectional mapping network and the projection layer with shared parameters through a contrastive learning mechanism, the vector distance between the query semantic vector and the pattern structure vector in the joint semantic space of the positive sample triples is less than the vector distance between the query semantic vector and the pattern structure vector in the negative sample triples, forming a semantically aligned distribution including: Calculate the interval between the distances of positive and negative sample vectors to obtain a distance difference metric. The partial derivative of the distance difference metric with respect to the encoding parameters of the bidirectional mapping network is used to obtain the encoding parameter gradient. The partial derivative of the distance difference metric with respect to the mapping parameters of the projection layer of the shared parameters is used to obtain the mapping parameter gradient. The encoding parameter gradient and the mapping parameter gradient together constitute the gradient update direction of the network parameters. When the distance difference metric indicates that the distance between the positive sample vectors is greater than the distance between the negative sample vectors, the encoding parameters of the bidirectional mapping network are adjusted to reduce the distance between the positive sample vectors, and the mapping parameters of the projection layer with shared parameters are adjusted to increase the distance between the negative sample vectors. The gradient update direction of the network parameters is synchronously applied to the bidirectional mapping network and the projection layer of the shared parameters through the backpropagation mechanism. The encoding parameters of the bidirectional mapping network and the mapping parameters of the projection layer of the shared parameters are iteratively updated until the distance difference metric converges to a state where the distance between the positive sample vectors is less than the distance between the negative sample vectors, thus forming a semantically aligned distribution.

3. The method according to claim 1, characterized in that, Constructing multi-hop reasoning paths between the pattern elements based on the graph attention propagation mechanism, and performing semantic propagation and aggregation of the entity relation representation annotated with the pattern elements along the multi-hop reasoning paths, includes: Calculate the semantic association strength between the pattern elements, and calculate the path confidence decay factor based on the number of intermediate pattern elements; Based on the semantic association strength and the path confidence decay factor, pattern element pairs are selected, and the connection path between the starting pattern element and the target pattern element in the pattern element pair is identified as the multi-hop inference path. The entity relation representation annotated with the pattern elements is used as the initial semantic representation. The semantic representations of intermediate pattern elements are obtained sequentially along the multi-hop reasoning path. The semantic matching degree between the initial semantic representation and the semantic representation of intermediate pattern elements is calculated. The semantic consistency between the initial semantic representation and the semantic representation of the target pattern element is calculated. The attention propagation weight is determined by the weighted combination of the semantic matching degree and the semantic consistency. According to the attention propagation weight, the initial semantic representation is propagated hop by hop along the multi-hop inference path from the starting pattern element to the target pattern element. During the propagation process, the initial semantic representation is weighted and fused with the semantic representation of the intermediate pattern element to obtain the propagated semantic representation. By aggregating all the propagated semantic representations that reach the same target pattern element through an aggregation operation, the aggregated semantic representation corresponding to the target pattern element is obtained.

4. The method according to claim 1, characterized in that, In conjunction with the co-occurrence statistical characteristics of pattern elements in historical query execution records, the weights of the multi-hop inference paths are adaptively adjusted to establish a context-dependent mapping relationship between the entity relation representation and the pattern elements, including: The co-occurrence frequency of pattern element pairs in the co-occurrence sequence of pattern elements in the historical query execution records is counted. A time decay factor is calculated based on the timestamp of the historical query execution records. The co-occurrence frequency is scaled by the time decay factor and then accumulated to obtain the historical co-occurrence intensity of the pattern element pairs. The historical co-occurrence intensities of each pair of adjacent pattern elements on the multi-hop inference path are multiplied to obtain the historical co-occurrence intensities of the multi-hop inference path. This is then fused with the current weight of the multi-hop inference path to obtain the path adjustment coefficient. In the knowledge graph schema layer, query the entity class schema elements and relation class schema elements corresponding to the entity type information and relation type information in the entity relation representation; Calculate the weighted type similarity between the entity relationship representation and the entity class pattern element based on the path adjustment coefficient, calculate the weighted relationship similarity between the entity relationship representation and the relationship class pattern element, and concatenate the weighted type similarity and the weighted relationship similarity into a context feature vector; The context feature vector and the entity relationship representation are concatenated and fused, and mapped to pattern element annotation vectors through a nonlinear transformation network, thus establishing a context-dependent mapping relationship between the entity relationship representation and the pattern elements.

5. The method according to claim 1, characterized in that, Based on the intent representation and the effective mapping, generating a formal query statement that conforms to the target database query language specification and satisfies semantic consistency under the constraints of the decoding includes: Based on the intent representation, the query target entity is extracted. Based on the query target entity, the table name and field name are retrieved from the effective mapping to construct a table field mapping table. The foreign key constraint relationship between tables is extracted from the decoded constraint to construct a table connection dependency graph. The table set involved is determined based on the table field mapping table. Candidate join paths connecting the table set are searched in the table join dependency graph. Path cost weights are assigned based on the usage frequency and query execution efficiency statistics of each foreign key relationship on the candidate join path in historical queries. The candidate join path with the smallest weighted sum of path length and path cost weights is selected to generate the table join condition clause. Extract the attribute mapping relationship between the entity relationship representation and the database field from the effective mapping, convert the attribute constraints in the attribute mapping relationship into field filtering expressions and generate filtering condition clauses, and convert the intent representation into an expected semantic graph structure and into a lexical selection preference vector; An autoregressive sequence generator decoder is used. At each time step of decoding, the candidate word space is limited according to the decoding constraints. The generation probability of each word in the candidate word space is weighted and adjusted using the word selection preference vector. The word with the highest generation probability is selected and concatenated to the generated query statement fragment. The table join condition clause is used as the join condition, and the filter condition clause is used as the filter condition to obtain the formal query statement.

6. A natural language-based intelligent database query statement conversion and analysis system, used to implement the method as described in any one of claims 1-5, characterized in that, include: The first unit is used to construct a joint semantic space that integrates the language representation subspace and the database schema representation subspace. Through contrastive learning, the semantic vectors of the natural language query expression and the structure vectors of the schema elements in the target database structure description are semantically aligned in the joint semantic space. The second unit is used to perform multi-level semantic parsing on the natural language query expression, and extract the intent representation describing the query target and the entity relationship representation describing the query conditions; The third unit is used to construct multi-hop inference paths between the pattern elements based on the graph attention propagation mechanism. The entity relation representation annotated with the pattern elements is semantically propagated and aggregated along the multi-hop inference path. The weight of the multi-hop inference path is adaptively adjusted by combining the co-occurrence statistical features of the pattern elements in the historical query execution records, and a context-dependent mapping relationship is established between the entity relation representation and the pattern elements. The fourth unit is used to select the mapping relationship whose confidence level meets the preset confidence threshold as a valid mapping; The fifth unit is used to take the integrity constraints, foreign key dependencies, and query syntax rules of the target database as decoding constraints, and generate a formal query statement that conforms to the query language specification of the target database and satisfies semantic consistency under the limitation of the decoding constraints, based on the intent representation and the effective mapping.

7. An electronic device, characterized in that, include: processor; Memory used to store processor-executable instructions; The processor is configured to invoke instructions stored in the memory to execute the method according to any one of claims 1 to 5.

8. A computer-readable storage medium having computer program instructions stored thereon, characterized in that, When the computer program instructions are executed by the processor, they implement the method described in any one of claims 1 to 5.

Citation Information

Patent Citations

  • Multi-hop reasoning question and answer method based on cross-language knowledge graph

    CN114780754A

  • Large model reasoning enhancement method based on knowledge graph sub-graph matching

    CN120633855A