Knowledge graph query statement generation method and related apparatus
Patent Information
- Application Number
- CN202611189685.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-06
- Publication Date
- 2026-10-30
AI Technical Summary
尽管该方法显著提升了RAG系统对结构化知识源的访问能力,但仍面临若干技术挑战:首先,大语言模型在生成复杂嵌套查询、多表连接或多条件过滤时容易出现语法、语义和实际业务需求的理解偏差;其次,现有方案通常缺乏对目标数据库模式的实时感知,导致生成的查询无法执行或返回无关结果;再者,大语言模型直接生成查询语句,业务调整时需高昂成本微调模型,难以快速适应变化
本发明知识图谱查询语句生成方法,通过利用大语言模型将查询问题进行要素解析并填充至预设结构化要素模板得到结构化要素文件,能够将查询构建逻辑与业务逻辑解耦,使大语言模型仅聚焦于业务需求中的关键约束条件的解析,无需掌握查询语句细节,避免了直接生成查询语句时需要设计冗长提示词模板的痛点,从而精准把握业务场景中的关键约束条件。通过基于结构化要素文件提取结构化的实体要素、关系要素和目的要素并根据预设映射规则分别映射为结构化的实体条件语句、关系条件语句和目的语句,结合实时数据库模式感知机制,能够确保生成的查询语句的可执行性与相关性,消除大语言模型直接生成查询语句时因缺乏对目标数据库模式的实时感知而导致的无法执行或返回无关结果的问题。通过根据结构化的目的要素确定目的实体并结合知识图谱的本体邻接表得到查询路径,以及根据查询路径将结构化的要素实体条件语句、关系条件语句和目的语句进行拼接得到知识图谱查询语句,具备路径自适应调整能力,能够显著提升查询性能。同时,本发明通过预定义模板与大语言模型解析的灵活适配架构,在业务调整时可减少大语言模型微调成本并减少提示词工程的投入,新增实体类型仅需预定义模板及对应映射规则即可完成功能迭代,具备横向扩展能力,既支持查询语言的平滑迁移,又能在业务逻辑变更时通过调整映射规则实现快速适配,无需重构大语言模型提示词工程体系,显著降低系统维护复杂度和成本。本发明方法能够广泛应用于各类领域的知识图谱问答系统中,为用户提供一个高效、准确且易于维护的知识图谱查询平台。
Smart Images

Figure CN123012182A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of knowledge graphs and relates to a method and related apparatus for generating knowledge graph query statements. Background Technology
[0002] In recent years, with the rapid development of Large Language Models (LLMs), they have demonstrated powerful capabilities in natural language understanding and generation tasks. However, general-purpose LLMs are usually built based on static training data, which leads to problems such as lagging knowledge updates, inability to access domain-specific private data, and potential lack of factual basis in generated content. To overcome these limitations, researchers have proposed Retrieval-Augmented Generation (RAG) technology. RAG combines external knowledge retrieval mechanisms with language generation models, dynamically retrieving information related to the user query from a specified knowledge base before generating a response, and using the retrieval results as context input to the generation model, thereby guiding the model to output more accurate, reliable, and traceable answers. A typical RAG system usually contains two core modules: a retrieval unit, responsible for mapping the user query to a vector and performing similarity matching in the knowledge base; and a generator, usually a pre-trained language model, used to fuse the retrieved documents with the original query to generate the final answer.
[0003] In recent years, researchers have leveraged the powerful semantic understanding and code generation capabilities of large language models (RAGs) to explore their applications in natural language to structured query language tasks. In this type of RAG architecture, the large language model acts as a query compiler, receiving natural language questions from users and generating corresponding structured query statements. These structured queries are then submitted to a backend database or API (Application Programming Interface) for execution. The returned structured results are then used as context input to the generative model for synthesizing the final answer. While this approach significantly improves the RAG system's access to structured knowledge sources, it still faces several technical challenges: First, large language models are prone to grammatical, semantic, and actual business requirement discrepancies when generating complex nested queries, multi-table joins, or multi-condition filtering. Second, existing solutions often lack real-time awareness of the target database schema, leading to queries that cannot be executed or return irrelevant results. Third, since large language models directly generate query statements, fine-tuning the model requires significant costs when adjusting for business changes, making it difficult to adapt quickly to changes. Summary of the Invention
[0004] The purpose of this invention is to overcome the shortcomings of the prior art and provide a method and related apparatus for generating knowledge graph query statements.
[0005] To achieve the above objectives, the present invention employs the following technical solution: In a first aspect, the present invention provides a method for generating query statements in a knowledge graph, comprising: obtaining a query question; parsing the query question into elements using a large language model and filling the element parsing results into a preset structured element template to obtain a structured element file; extracting structured entity elements, relation elements, and target elements from the structured element file, and mapping them into structured element entity condition statements, relation condition statements, and target statements according to preset mapping rules; determining the target entity based on the structured target elements, and obtaining the query path of the target entity in the knowledge graph by combining the ontology adjacency list of the knowledge graph; and concatenating the structured element entity condition statements, relation condition statements, and target statements according to the query path to obtain a structured knowledge graph query statement for the query question.
[0006] Optionally, the preset structured element template adopts JSON format; the element parsing includes parsing entity elements, relationship elements, and target elements; the preset structured element template includes entity element template, relationship element template, and target element template; wherein, the relationship element template is embedded in the entity element template.
[0007] Optionally, the step of determining the target entity based on structured target elements and obtaining the query path of the target entity in the knowledge graph by combining the ontology adjacency list of the knowledge graph includes: determining the target entity based on structured target elements; traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity to obtain several traversal paths; reversing the several traversal paths to obtain several candidate query paths; and selecting one candidate query path from the several candidate query paths as the query path of the target entity in the knowledge graph.
[0008] Optionally, before traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, the method further includes: determining the mentioned entity based on structured entity elements; traversing each entity in the ontology adjacency list of the knowledge graph, and adding the current entity to the tail of a preset candidate queue when the degree of the current entity is less than or equal to 1 and it is neither the target entity nor the mentioned entity; repeating the marking step until the candidate queue is empty; wherein the marking step includes: selecting the first entity in the candidate queue as the marked entity, setting the degree of the marked entity to 0 and marking it as unnecessary in the ontology adjacency list, traversing the entities connected to the marked entity in the ontology adjacency list to obtain several connected entities, deleting adjacent entities with a degree of 0, and decrementing the degree of each remaining connected entity by one, and adding the current connected entity to the tail of the candidate queue when the degree of the current connected entity is 1 and it is neither the mentioned entity nor the target entity; when traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, entities marked as unnecessary are excluded.
[0009] Optionally, the step of traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity includes: traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity using a depth-first traversal algorithm.
[0010] Optionally, the step of reversing several traversal paths to obtain several candidate query paths, and selecting one candidate query path from the several candidate query paths as the query path of the target entity in the knowledge graph includes: reversing several traversal paths to obtain several candidate query paths; obtaining the priority of the starting entity of each candidate query path, and selecting the candidate query path with the highest priority of the starting entity as the query path of the target entity in the knowledge graph; wherein, the higher the number of entities, the lower the priority of the entities.
[0011] Optionally, the step of concatenating the structured element entity condition statements, relation condition statements, and target statements according to the query path to obtain a structured knowledge graph query statement for the query question includes: taking the starting entity of the query path as the starting point, performing a concatenation operation based on the ontology adjacency table; wherein, the concatenation operation includes: concatenating the entity condition statement corresponding to the current entity to the main body of the query statement; determining the neighboring entities of the current entity according to the ontology adjacency table, and removing visited entities and entities marked as unnecessary from the neighboring entities, and taking the entity located in the query path among the remaining neighboring entities as the main entity, and the remaining neighboring entities as... For each branch entity, the entity condition statement and relational condition statement corresponding to the branch entity are concatenated into a nested statement and attached to the entity condition statement corresponding to the current entity in the main body of the query statement. For the main entity, when the main entity is the target entity, the entity condition statement and relational condition statement corresponding to the main entity are concatenated into a nested statement and attached to the entity condition statement corresponding to the current entity in the main body of the query statement, and the concatenation operation ends. After the concatenation operation ends, the target statement is concatenated at the end of the final main body of the query statement to obtain the structured knowledge graph query statement for the query question. Otherwise, the recursive concatenation operation continues with the main entity.
[0012] In a second aspect, the present invention provides a knowledge graph query statement generation system, comprising: an element parsing module for obtaining a query question, parsing the query question into elements using a large language model, and filling the element parsing results into a preset structured element template to obtain a structured element file; a statement generation module for extracting structured entity elements, relation elements, and target elements from the structured element file, and mapping them into structured element entity condition statements, relation condition statements, and target statements according to preset mapping rules; a path determination module for determining the target entity based on the structured target elements, and obtaining the query path of the target entity in the knowledge graph by combining the ontology adjacency list of the knowledge graph; and a statement concatenation module for concatenating the structured element entity condition statements, relation condition statements, and target statements according to the query path to obtain a structured knowledge graph query statement for the query question.
[0013] Optionally, the preset structured element template adopts JSON format; the element parsing includes parsing entity elements, relationship elements, and target elements; the preset structured element template includes entity element template, relationship element template, and target element template; wherein, the relationship element template is embedded in the entity element template.
[0014] Optionally, the path determination module is specifically used to: determine the target entity based on the structured target elements; traverse each entity in the ontology adjacency table of the knowledge graph starting from the target entity to obtain several traversal paths; reverse the several traversal paths to obtain several candidate query paths; and select one candidate query path from the several candidate query paths as the query path of the target entity in the knowledge graph.
[0015] Optionally, before traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, the path determination module is further configured to: determine the mentioned entity based on structured entity elements; traverse each entity in the ontology adjacency list of the knowledge graph, and when the degree of the current entity is less than or equal to 1 and it is neither the target entity nor the mentioned entity, add the current entity to the tail of a preset candidate queue; repeat the marking step until the candidate queue is empty; wherein, the marking step includes: selecting the first entity in the candidate queue as the marked entity, setting the degree of the marked entity to 0 and marking it as unnecessary in the ontology adjacency list, traversing the entities connected to the marked entity in the ontology adjacency list to obtain several connected entities, deleting adjacent entities with a degree of 0, and decrementing the degree of each remaining connected entity by one, and when the degree of the current connected entity is 1 and it is neither the mentioned entity nor the target entity, add the current connected entity to the tail of the candidate queue; wherein, when traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, entities marked as unnecessary are excluded.
[0016] Optionally, the path determination module is specifically used to: start from the target entity in the ontology adjacency list of the knowledge graph and traverse each entity in the ontology adjacency list using a depth-first traversal algorithm.
[0017] Optionally, the path determination module is specifically used to: reverse several traversal paths to obtain several candidate query paths; obtain the priority of the starting entity of each candidate query path, and select the candidate query path with the highest priority of the starting entity as the query path of the target entity in the knowledge graph; wherein, the higher the number of entities, the lower the priority of the entities.
[0018] Optionally, the statement concatenation module is specifically used to: take the starting entity of the query path as the starting point and perform a concatenation operation based on the ontology adjacency table; wherein, the concatenation operation includes: concatenating the entity condition statement corresponding to the current entity to the query statement trunk; determining the neighboring entities of the current entity according to the ontology adjacency table, and removing visited entities and entities marked as unnecessary from the neighboring entities, and taking the entity located in the query path among the remaining neighboring entities as the main entity, and the remaining neighboring entities as branch entities; for each branch entity, concatenating the entity condition statement and relational condition statement corresponding to the branch entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the query statement trunk; for the main entity, when the main entity is the target entity, concatenating the entity condition statement and relational condition statement corresponding to the main entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the query statement trunk and ending the concatenation operation, and after ending the concatenation operation, concatenating the target statement at the end of the final query statement trunk to obtain a structured knowledge graph query statement for the query question; otherwise, continuing the recursive concatenation operation with the main entity.
[0019] In a third aspect, the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the knowledge graph query statement generation method described above.
[0020] In a fourth aspect, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the knowledge graph query statement generation method described above.
[0021] Compared with the prior art, the present invention has the following beneficial effects: This invention presents a knowledge graph query statement generation method. By utilizing a large language model to parse the query question into elements and populate them into a pre-defined structured element template to obtain a structured element file, the method decouples the query construction logic from the business logic. This allows the large language model to focus solely on parsing key constraints in business requirements, without needing to understand the details of the query statement. This avoids the pain point of designing lengthy prompt word templates when directly generating query statements, thus accurately grasping the key constraints in the business scenario. By extracting structured entity elements, relational elements, and target elements from the structured element file and mapping them to structured entity conditional statements, relational conditional statements, and target statements according to pre-defined mapping rules, combined with a real-time database schema awareness mechanism, the method ensures the executability and relevance of the generated query statement. This eliminates the problem of unexecutable or irrelevant results caused by the lack of real-time awareness of the target database schema when the large language model directly generates query statements. By determining the target entity based on the structured target elements and obtaining the query path using the ontology adjacency list of the knowledge graph, and then concatenating the structured element entity conditional statements, relational conditional statements, and target statements according to the query path to obtain the knowledge graph query statement, the method possesses adaptive path adjustment capabilities, significantly improving query performance. Meanwhile, this invention, through a flexible adaptation architecture of predefined templates and large language model parsing, reduces the cost of fine-tuning the large language model and the investment in prompt word engineering during business adjustments. Adding new entity types only requires predefined templates and corresponding mapping rules to complete functional iteration, possessing horizontal scalability. It supports smooth migration of query language and rapid adaptation by adjusting mapping rules when business logic changes, without requiring reconstruction of the large language model prompt word engineering system, significantly reducing system maintenance complexity and cost. This invention's method can be widely applied to knowledge graph question-answering systems in various fields, providing users with an efficient, accurate, and easy-to-maintain knowledge graph query platform. Attached Figure Description
[0022] Figure 1 This is a flowchart of a knowledge graph query statement generation method according to an embodiment of the present invention.
[0023] Figure 2 The following is a block diagram of the knowledge graph query statement generation system in this embodiment of the invention. Detailed Implementation
[0024] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. 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 should fall within the scope of protection of the present invention.
[0025] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0026] The present invention will now be described in further detail with reference to the accompanying drawings: See Figure 1 In one embodiment of the present invention, a method for generating query statements for a knowledge graph is provided, which automatically converts natural language query questions input by users into structured query statements that can be executed on a knowledge graph, in order to solve technical problems such as syntactic and semantic deviations, database schema awareness lag, and insufficient query performance optimization that exist when large language models directly generate query statements.
[0027] Specifically, the knowledge graph query statement generation method of this invention includes the following steps: S1: Obtain the query question, perform element parsing on the query question using a large language model, and fill the element parsing results into a preset structured element template to obtain a structured element file.
[0028] S2: Extract structured entity elements, relational elements, and target elements from the structured element file, and map them into structured entity conditional statements, relational conditional statements, and target statements according to preset mapping rules.
[0029] S3: Determine the target entity based on the structured target elements, and combine it with the ontology adjacency list of the knowledge graph to obtain the query path of the target entity in the knowledge graph.
[0030] S4: Based on the query path, the structured entity condition statement, relation condition statement, and target statement are concatenated to obtain the structured knowledge graph query statement for the query question.
[0031] Interpretive large language models, upon receiving natural language-based queries, perform semantic understanding and element parsing to identify various key information elements involved in the query. After parsing, the large language model fills the corresponding field positions in the structured element template with the parsed element values according to the predefined field definitions and format requirements, thereby generating a structured element file. This structured element file expresses the semantic information of the query in a unified format, providing a standardized data foundation for subsequent automated processing. The predefined structured element template serves as the output format specification for the large language model during element parsing. Through predefined field structures and data types, it forces the large language model to output parsing results in a unified structure, thus avoiding the syntax errors and semantic ambiguities that may occur when the large language model directly outputs unstructured text or free-format code in traditional methods.
[0032] Explanatory, entity elements refer to the specific entity types and their attribute constraints mentioned in the query. Relationship elements refer to the types of associations between entities, and target elements refer to the target entity type and target return type that the user wants to query. After extraction, the field values in the structured feature file are transformed according to preset mapping rules. These preset mapping rules are predefined correspondences between feature descriptions in natural language and graph traversal statements in knowledge graph query statements, generally including entity label mapping, attribute field mapping, operator mapping, and relation type mapping. After mapping transformation, structured entity condition statements, relation condition statements, and target statements are generated.
[0033] Interpretively, the ontology adjacency list of a knowledge graph predefines the connections between various entities within the knowledge graph, storing them in dictionary form. The dictionary keys are entity type names, and the values are lists of entity types directly connected to that entity type, along with corresponding relational query statements. Based on structured target elements, the target entity is determined. Then, by combining this with the entity connections defined in the ontology adjacency list, the query path for the target entity within the knowledge graph can be obtained. Different query paths correspond to different query execution efficiencies. By using the ontology adjacency list for path planning, redundant traversals or invalid paths that might occur when a large language model directly generates query statements can be avoided, thereby improving the execution efficiency of the query statements.
[0034] Explanatoryly, the query path determines the order and direction of entity traversal, entity condition statements provide type constraints and attribute filtering conditions for each entity, relational condition statements provide association constraints between entities, and the destination statement specifies the final data format to be returned. During the concatenation process, following the traversal order of the query path, the entity condition statements and relational condition statements corresponding to each entity are sequentially connected to the main body of the query statement, and the destination statement is appended at the end, ultimately generating a complete structured knowledge graph query statement. This knowledge graph query statement can be directly submitted to the knowledge graph database for execution, returning structured results that match the query intent.
[0035] This invention provides a method for generating knowledge graph query statements. Steps S1 to S4 constitute a complete conversion process from natural language query questions to structured knowledge graph query statements. Step S1 separates semantic parsing from structured expression, allowing large language models to focus solely on element recognition without needing to master query syntax. Step S2 standardizes the mapping from structured elements to query statements, ensuring consistency between the query statements and the database schema. Step S3 optimizes query execution efficiency by determining the query path using the ontology adjacency list of the knowledge graph. Step S4 ensures the correctness and executability of the generated knowledge graph query statements through structured concatenation. These four steps work synergistically to achieve accurate, efficient, and maintainable automatic generation of knowledge graph query statements.
[0036] In one possible implementation, the preset structured element template adopts JSON format; the element parsing includes parsing entity elements, relationship elements, and target elements; the preset structured element template includes entity element templates, relationship element templates, and target element templates; wherein, the relationship element template is embedded in the entity element template.
[0037] For example, this embodiment uses a knowledge graph in the field of science and technology project management as an example for illustration.
[0038] Specifically, the entity design of the knowledge graph in the field of science and technology project management is as follows: The entity map includes projects, organizations, personnel, and achievements. Projects are specifically divided into national projects, company projects, and self-managed projects, and include project name, project type name, technical field name, keywords, total funding, start time, end time, and year of project initiation. Organizations include organization name and organization code attributes, and are linked to projects, with relationships including lead and participation. Personnel are specifically divided into experts and general staff, and include name attributes, and are linked to projects, with relationships including acceptance and participation. Achievements are specifically divided into patents, software copyrights, papers, and monographs. Patents include patent name attributes, papers include paper title attributes, software copyrights include copyright name attributes, and monographs include monograph title attributes. All of them are linked to projects, with a relationship of affiliation.
[0039] For knowledge graphs in the field of science and technology project management, a large language model is set up to parse natural language queries into five core elements: Outcome elements: Identify whether an outcome is mentioned and its type (patent / software copyright / paper / monograph / achievement), and extract the outcome name. Purpose elements: Clarify the target entity (outcome / project / personnel / institution) and target type (list / quantity / name / funding). Project elements: Extract project name, type (country / company / self-managed), stage (initiation / implementation / completion), direction (technical field / keywords), time range (recent x years / xxxx-yyyy years, etc.), and funding conditions (>, <, >=, <=, =). Institution elements: Identify the institution's participation type (lead / participant / administrator) and institution name. Personnel elements: Determine whether the personnel are experts and extract their names.
[0040] Based on the above settings, a predefined structured feature template is used, typically in JSON format. This predefined JSON template structure ensures that the structured feature files output by the large language model have a unified format specification, facilitating automated parsing and processing by subsequent modules.
[0041] In this embodiment, the specific structured element templates are shown in Table 1.
[0042] Table 1
[0043] In addition, the completeness and consistency of the template output can be ensured by using logical constraints in the prompts (if the project type is not mentioned, the default is country and company project), avoiding the omission of key entity attributes.
[0044] Explained in this embodiment, the relation element template is embedded within the entity element template. This design is because in knowledge graph queries, relation constraints are typically attached to entity constraints; that is, when querying an entity, it is necessary to specify the relationship through which that entity is associated with another entity. For example, when the query involves "unit" and "project," it is necessary to clarify whether the unit is associated with the project through a "leading" or "participating" relationship. Embedding the relation element template within the entity element template makes the template structure closer to the actual semantic logic of knowledge graph queries, facilitating the large language model to identify relationships while parsing entity elements, and also benefiting the collaborative concatenation of relational and entity statements during subsequent query generation.
[0045] This interpretive approach extracts structured entity, relation, and target elements from structured feature files and maps them to structured entity, relation, and target conditional statements according to predefined mapping rules. Its core design decouples business semantic expression from the underlying graph database query syntax. By constructing a structured mapping intermediary layer, it achieves a deterministic transformation from natural language elements to executable query statement fragments. Its design principle follows declarative configuration and rule-driven principles. Specifically, it predefines the corresponding graph database syntax template and operator mapping relationships for each semantic element type based on the knowledge graph ontology model. This allows the system to automatically assemble syntactically correct and semantically equivalent conditional statement fragments by simply looking up and matching the element's field paths when processing any structured feature file, without relying on real-time reasoning and generation from a large language model. For example, in a specific implementation, the mapping rule is encapsulated as an independent rule engine component. This component internally maintains a mapping configuration table with structured feature template field paths as keys and graph database query statement fragment templates as values. When the rule engine receives a structured feature file, it uses recursive traversal to extract all non-empty leaf node fields and their values from the file. Then, it retrieves the corresponding syntax templates and operators from the mapping configuration table using the field paths as indexes. Finally, it fills the templates with the field values as parameters to generate independent query statements.
[0046] For example, this section uses the generation of project condition statements, unit condition statements, outcome condition statements, and personnel condition statements as examples to illustrate the knowledge graph in the field of science and technology project management.
[0047] The generation of project condition statements includes: 1. Project type mapping: Converting text types such as "National Project", "Company Project", and "Self-managed Project" into graph database label constraints, such as "hasLabel('National Project')". If the project type is not mentioned, it defaults to National and Company Projects, using the statement "hasLabel('National Project', 'Company Project')". 2. Project feature identification: Binding project features to project type names through the fields "Whether it is a National Natural Science Foundation of China" and "Whether it is a National Key R&D Program", using the statement "has('Project Type Name', 'National Key R&D Program')". 3. Project direction identification: Project directions include "Artificial Intelligence", "New Energy", etc. If the text mentions fields such as "Field", "Direction", or "Keywords", constraints on the project direction need to be applied using the statement ".or(has('Project Technical Field Name', containing('Artificial Intelligence')), has('Keywords', containing('Artificial Intelligence')), has('Requirement Technical Field Name', containing('Artificial Intelligence')), has('Project Name', containing('Artificial Intelligence')))". 4. Time Range Handling: Supports multiple time parsing types (last x years, xxxx-yyyy years, etc.), dynamically calculating the time range based on the current year (e.g., 2025). Matches different time fields (initiation year / end date) based on project stage (initiation / implementation / completion). If the project stage is mentioned but the time is not, the project stage is converted into a database-stored encoding for querying; for example, ".has('project stage',0)" is used for the initiation stage, and ".has('project stage',1)" is used for the implementation stage. To query projects in the completion or acceptance phase, use ".has('project phase', gte(2))". If the time is mentioned but the project phase is not, for the query conditions "recent x years" or "past x years", use ".has('project initiation year', gte(2025 - x))". For the query condition "xxxx year", use ".has('project initiation year', xxxx)". For the query condition "xxxx year to present", use ".has('project initiation year', gte(xxxx))". For the query condition "xxxx-yyyy year", use ".has('project initiation year', gte(xxxx)).has('project initiation year', lte(yyyy))". Furthermore, if Chinese characters are used to represent numbers, they need to be converted to Arabic numerals; if the year is a two-digit number, such as "24 years", it needs to be converted to a four-digit number "2024 years". If both time and project phase are mentioned, and the time phase is the project initiation phase, the construction of the time statement is the same as in the previous paragraph.During the implementation phase, ensure that the project start and end dates overlap with the query period. For query conditions "recent x years" or "past x years", use the statement ".has('end date', gte('{2025 - x}-01-01'))"; for query conditions "xxxx year", use ".has('end date', gte('{xxxx}-01-01')).has('start date', lte('{xxxx}-12-31'))"; for query conditions "xxxx year to present", use ".has('end date', gte('{xxxx}-01-01'))"; for query conditions "xxxx-yyyy year", use ".has('end date', gte('{xxxx}-01-01')).has('start date', lte('{yyyy}-12-31'))" If it's the project completion phase, ensure the project end date is within the query timeframe. For query conditions like "recent x years" or "past x years", use the statement ".has('end date', gte('{2025 - x}-01-01'))"; for query conditions like "xxxx year", use ".has('end date', gte('{xxxx}-01-01')).has('end date', lte('{xxxx}-12-31'))"; for query conditions like "xxxx year to present", use ".has('end date', gte('{xxxx}-01-01'))"; for query conditions like "xxxx-yyyy year", use ".has('end date', gte('{xxxx}-01-01')).has('end date', lte('{yyyy}-12-31'))" 5. Standardize funding conditions: Convert “greater than 10 million” in natural language to “has('total funding', gt(1000))”, and handle the conversion of numerical values in units of magnitude (ten thousand / hundred million). If no magnitude is specified, the default value is “ten thousand yuan”.
[0048] The generation of unit condition statements includes: Participation type mapping: Converting the text "Leader" and "Participant" into edge label constraints connecting units and projects, using the relational statement "bothE().has('r', within('Leader', 'Participant')).bothV()". If both "Leader" and "Participant" exist simultaneously, or if the text "Accountant" appears, use the relational statement "bothE().has('r', within('Leader', 'Participant')).bothV()". Unit name matching uses the statement "hasLabel('Unit').has('Unit Name', 'Unit Name 1')". If a "Responsible Unit" relationship exists, then ".has('Responsible Unit Name', 'Unit Name 1')" needs to be added to the project entity condition statement.
[0049] The generation of result condition statements includes: 1. Result type mapping: Converting types such as "patent," "software copyright," "paper," and "monograph" into graph database label constraints, supporting multiple result type constraints, such as "hasLabel('patent', 'paper')". If no specific result is mentioned, the statement "hasLabel('patent', 'paper', 'software copyright', 'monograph')" is used. 2. Name fuzzy matching: Result names are used to achieve keyword fuzzy search using the "containing" operator, and the field name corresponds to the specific result type. For example, ".has('paper title', containing('paper name 1'))".
[0050] The generation of personnel condition statements includes: distinguishing between experts and ordinary personnel: based on the "whether an expert" field, associate personnel with either the "expert" or "ordinary personnel" label using the "hasLabel('expert')" or "hasLabel('ordinary personnel')" statement. When a person is an "expert," match the "acceptance" relationship using the "bothE().has('r', 'acceptance').bothV()" relationship statement; when a person is an "ordinary personnel," match the "participation" relationship using the "bothE().has('r', 'participation').bothV()" relationship statement. Personnel are also matched based on their name using the ".has('name', 'name1')" statement.
[0051] In one possible implementation, the step of determining the target entity based on structured target elements and obtaining the query path of the target entity in the knowledge graph by combining the ontology adjacency list of the knowledge graph includes: determining the target entity based on structured target elements; traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity to obtain several traversal paths; reversing the several traversal paths to obtain several candidate query paths; and selecting one candidate query path from the several candidate query paths as the query path of the target entity in the knowledge graph.
[0052] Explanatory, the ontology adjacency list of the knowledge graph is pre-constructed based on the entity design of the knowledge graph. It is generally stored in the form of a dictionary, where the key of the dictionary is the entity type name, and the value of the dictionary is the entity type associated with that entity type. It also stores relational query statements in the form of an array, as shown in Table 2.
[0053] Table 2
[0054] Specifically, first, the "Target Entity" value under the "Target" field is extracted from the structured feature file to determine the target entity type of the user's query. Then, using this target entity as the starting point, a traversal operation is performed in the ontology adjacency list of the knowledge graph. During the traversal, starting from the target entity, adjacent entity nodes are recursively visited according to the edges of the adjacency list, and the sequence of entities traversed is recorded, resulting in several traversal paths that start from the target entity and cover all mentioned entities. Each traversal path is a sequence of entity types, representing the traversal order from the target entity to each mentioned entity. Then, each traversal path is reversed, that is, the order of the entity sequences is reversed, to obtain candidate query paths starting from the mentioned entities and ending at the target entity. Finally, one of the candidate query paths is selected as the final query path. Explanatoryly, the reason for obtaining traversal paths starting from the target entity and then reversing to obtain candidate query paths is that the execution of knowledge graph query statements usually starts from entities with filtering conditions and gradually traverses to the target entity. The destination entity is the final result entity that the user expects to return. By determining the location of the destination entity first and then deriving the starting entity in reverse during path planning, the query path can be ensured to start from the entity with the strongest filtering capability, thereby optimizing query performance.
[0055] In one possible implementation, before traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, the method further includes: determining the mentioned entity based on structured entity elements; traversing each entity in the ontology adjacency list of the knowledge graph, and adding the current entity to the tail of a preset candidate queue when the degree of the current entity is less than or equal to 1 and it is neither the target entity nor the mentioned entity; repeating the marking step until the candidate queue is empty; wherein the marking step includes: selecting the first entity in the candidate queue as the marked entity, setting the degree of the marked entity to 0 and marking it as unnecessary in the ontology adjacency list, traversing the entities connected to the marked entity in the ontology adjacency list to obtain several connected entities, deleting adjacent entities with a degree of 0, and decrementing the degree of each remaining connected entity by one, and adding the current connected entity to the tail of the candidate queue when the degree of the current connected entity is 1 and it is neither the mentioned entity nor the target entity.
[0056] When traversing the entities in the ontology adjacency list of the knowledge graph starting from the target entity, entities marked as unnecessary are excluded.
[0057] Specifically, this implementation performs an irrelevant node filtering step before traversing the ontology adjacency table to remove unnecessary entity nodes from the query path. First, all mentioned entity types are extracted from the structured feature file, i.e., the entity types explicitly appearing in the query question, resulting in a set of mentioned entities. Then, the degree of each entity type in the knowledge graph ontology adjacency table (i.e., the number of entity types directly connected to that entity type) is counted. A candidate queue is initialized, and all entity types in the ontology adjacency table are traversed. When an entity type meets the following conditions simultaneously, it is added to the tail of the candidate queue: the degree of the entity type is less than or equal to 1, and the entity type is neither the destination entity nor a mentioned entity. The marking step is then executed repeatedly until the candidate queue is empty. In each marking step, an entity is taken from the head of the candidate queue as a marked entity, its degree is set to 0, and it is marked as "unnecessary" in the ontology adjacency table. All entities connected to the marked entity in the ontology adjacency table are traversed to obtain a number of connected entities. For each connected entity, if its degree is 0, it is removed from the connected entity list. For each remaining connected entity, its degree is decremented by one, and then it is checked whether the connected entity meets the following conditions: its degree becomes 1, and it is neither a mention entity nor a destination entity. If the above conditions are met, the connected entity is added to the tail of the candidate queue. Through the above iterative process, all redundant entity nodes that are irrelevant to the query and are at the end of the path are gradually marked as "unnecessary". When traversing the ontology adjacency list from the destination entity laterally, all entities marked as "unnecessary" are excluded, thereby achieving query path pruning optimization.
[0058] Explained, this implementation method removes unmentioned and non-core path entities through undirected graph topological sorting, effectively reducing the search space of query paths and avoiding the generation of invalid query paths containing redundant nodes. The principle is as follows: In a connected graph, a node with a degree of 1 is at the end of a path. If this node is not an entity involved in the query, it does not participate in any query constraints and can be safely removed. After removal, the degree of its adjacent nodes decreases by one, potentially triggering new removable nodes. Through iterative pruning, the remaining entity nodes are all necessary for the query path. This filtering mechanism significantly reduces the complexity of query paths and improves the efficiency of query statement generation.
[0059] In one possible implementation, the step of traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity includes: traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity using a depth-first traversal algorithm.
[0060] Specifically, Depth-First Search (DFS) is a graph traversal algorithm. Its basic idea is to start from the starting node and visit nodes as deep as possible along a path until no further progress is possible, at which point it backtracks and visits other branches. In this implementation, starting from the destination entity, it recursively visits the next entity connected to the current entity according to the entity connection relationships defined in the ontology's adjacency list, until the end of the path is reached or all reachable entities have been visited. DFS can generate complete traversal paths from the destination entity to each terminal entity. Explained, the advantage of using DFS to generate traversal paths is that it can systematically explore all possible entity connection paths, ensuring that no candidate query paths are missed. Simultaneously, the recursive nature of DFS naturally aligns with the nested structure of knowledge graph query statements, facilitating subsequent path reversal and query statement concatenation. Compared to Breadth-First Search (BFS), DFS has stronger expressive power in generating complete paths and can cover complex query scenarios with multi-hop associations.
[0061] In one possible implementation, the step of reversing several traversal paths to obtain several candidate query paths, and selecting a candidate query path from the several candidate query paths as the query path of the target entity in the knowledge graph, includes: reversing several traversal paths to obtain several candidate query paths; obtaining the priority of the starting entity of each candidate query path, and selecting the candidate query path with the highest priority of the starting entity as the query path of the target entity in the knowledge graph; wherein, the higher the number of entities, the lower the priority of the entities.
[0062] Specifically, the traversal paths obtained from the depth-first search are reversed, meaning the entity sequence in each path is reversed to obtain several candidate query paths. The first entity of each candidate query path is the starting entity of that path. Then, statistical data on the number of entities of each type in the knowledge graph are obtained, and the priority of each entity type is determined based on the number of entities: the fewer the number of entities, the higher the priority. For example, the priority sequence can be defined as "personnel:1, unit:2, project:3, result:4", where the smaller the value, the higher the priority. For each candidate query path, the priority value corresponding to its starting entity is obtained, and the candidate query path with the highest priority (smallest value) is selected as the final query path.
[0063] Explained in this embodiment, the entity-based priority strategy effectively improves query execution efficiency. In knowledge graph queries, queries are typically performed in the form of paths, and the performance of queries corresponding to different paths may vary significantly. Starting the query with an entity type that has fewer entities (i.e., lower cardinality) allows for earlier filtering out a large number of candidate nodes that do not meet the criteria, reducing the amount of data that needs to be processed during subsequent traversal, thereby reducing query execution overhead. For example, in a knowledge graph in the field of science and technology project management, the number of "personnel" entities is usually much smaller than that of "project" entities. Starting the query with "personnel" can narrow the search scope more quickly than starting with "project". By introducing an entity priority mechanism, the query optimizer can be guided to select entity types with lower cardinality and stronger filtering capabilities as the query starting point, thereby improving overall query performance.
[0064] In one possible implementation, the step of concatenating structured element entity condition statements, relation condition statements, and target statements according to the query path to obtain a structured knowledge graph query statement for the query question includes: starting from the initial entity of the query path, performing a concatenation operation based on the ontology adjacency table; wherein, the concatenation operation includes: concatenating the entity condition statement corresponding to the current entity to the main body of the query statement; determining the neighboring entities of the current entity according to the ontology adjacency table, removing visited entities and entities marked as unnecessary from the neighboring entities, and taking the entity located in the query path among the remaining neighboring entities as the main entity, and the remaining neighboring entities as the main entities. The main entity is used as a branch entity. For each branch entity, the entity condition statement and relation condition statement corresponding to the branch entity are concatenated into a nested statement and attached to the entity condition statement corresponding to the current entity in the main body of the query statement. For the main entity, when the main entity is the target entity, the entity condition statement and relation condition statement corresponding to the main entity are concatenated into a nested statement and attached to the entity condition statement corresponding to the current entity in the main body of the query statement, and the concatenation operation ends. After the concatenation operation ends, the target statement is concatenated at the end of the final main body of the query statement to obtain the structured knowledge graph query statement for the query question. Otherwise, the recursive concatenation operation continues with the main entity.
[0065] Specifically, this implementation uses a depth-first search dynamic splicing graph traversal statement to ensure that the main path is expanded first, and other paths are processed as filtering conditions (such as WHERE clauses). The specific steps are as follows: Step 1: Select the candidate query path with the highest priority of the starting entity among all candidate query paths as the target query path, and use the starting entity of the target query path as the starting entity of the splicing operation. Step 2: According to the ontology adjacency table, starting from the current entity, perform a depth-first traversal of the neighboring entities connected to the current entity, and prioritize traversing neighboring entities with higher priority. Step 3: For the neighboring entity with the highest priority among the neighboring entities connected to the current entity, set it as the master node, as the next hop entity on the main path. Step 4: For other neighboring entities connected to the current entity, skip visited entities, entities marked as unnecessary, and entities on the main path, recursively construct the query statement, including relational statements and conditional statements, and nest the constructed result in the WHERE clause, as a filtering condition attached to the main body of the current query statement. Step 5: For nodes on the main path, exclude nodes marked as unnecessary, recursively construct the query statement, including relational statements and conditional statements; if the current node is the end node (i.e., the target entity), then embed the constructed result in the WHERE clause; otherwise, directly connect at the end of the main clause of the query statement and continue traversing downwards.
[0066] Explanatoryly, by separating the main path from the branch paths, the system ensures both the clarity of the query structure and constraint coverage of all mentioned entities. Furthermore, the recursive concatenation method naturally handles complex query scenarios with multi-hop associations, generating nested query statements that conform to graph database syntax.
[0067] Specifically, after the concatenation operation is completed, the target statement is concatenated to the end of the final query statement trunk to obtain a structured knowledge graph query statement for the query question.
[0068] Explanatory statements are generally set according to the type of purpose.
[0069] For example, if the purpose type is a quantity question, the purpose statement is ".dedup().count()"; if the purpose type is a question about total funding, the purpose statement is ".dedup().values('total funding').sum()"; if the purpose type is to return the entity itself, the purpose statement is generated according to the configured return fields. For example, the purpose statement for a project entity is "valueMap('project name','leading unit name','total funding','project initiation year','project technical field name','keywords','start date','end date')".
[0070] The following are embodiments of the apparatus of the present invention, which can be used to execute embodiments of the method of the present invention. For details not disclosed in the apparatus embodiments, please refer to the embodiments of the method of the present invention.
[0071] See Figure 2 In another embodiment of the present invention, a knowledge graph query statement generation system is provided, which can be used to implement the above-mentioned knowledge graph query statement generation method. Specifically, the knowledge graph query statement generation system includes an element parsing module, a statement generation module, a path determination module, and a statement concatenation module.
[0072] The system comprises the following modules: an element parsing module to obtain the query question, a large language model to parse the query question into elements, and the result to fill a preset structured element template to obtain a structured element file; a statement generation module to extract structured entity elements, relational elements, and target elements from the structured element file, and map them into structured entity conditional statements, relational conditional statements, and target statements according to preset mapping rules; a path determination module to determine the target entity based on the structured target elements and, in conjunction with the ontology adjacency list of the knowledge graph, to obtain the query path of the target entity in the knowledge graph; and a statement concatenation module to concatenate the structured entity conditional statements, relational conditional statements, and target statements according to the query path to obtain a structured knowledge graph query statement for the query question.
[0073] In one possible implementation, the preset structured element template adopts JSON format; the element parsing includes parsing entity elements, relationship elements, and target elements; the preset structured element template includes entity element templates, relationship element templates, and target element templates; wherein, the relationship element template is embedded in the entity element template.
[0074] In one possible implementation, the path determination module is specifically used to: determine the target entity based on the structured target elements; traverse each entity in the ontology adjacency table of the knowledge graph starting from the target entity to obtain several traversal paths; reverse the several traversal paths to obtain several candidate query paths; and select one candidate query path from the several candidate query paths as the query path of the target entity in the knowledge graph.
[0075] In one possible implementation, before traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, the path determination module is further configured to: determine the mentioned entity based on structured entity elements; traverse each entity in the ontology adjacency list of the knowledge graph, and when the degree of the current entity is less than or equal to 1 and it is neither the target entity nor the mentioned entity, add the current entity to the tail of a preset candidate queue; repeat the marking step until the candidate queue is empty; wherein, the marking step includes: selecting the first entity in the candidate queue as the marked entity, setting the degree of the marked entity to 0 and marking it as unnecessary in the ontology adjacency list, traversing the entities connected to the marked entity in the ontology adjacency list to obtain several connected entities, deleting adjacent entities with a degree of 0, and after decrementing the degree of each remaining connected entity by one, when the degree of the current connected entity is 1 and it is neither the mentioned entity nor the target entity, add the current connected entity to the tail of the candidate queue; when traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, entities marked as unnecessary are excluded.
[0076] In one possible implementation, the path determination module is specifically used to: start from the target entity in the ontology adjacency list of the knowledge graph and traverse each entity in the ontology adjacency list using a depth-first traversal algorithm.
[0077] In one possible implementation, the path determination module is specifically used to: reverse several traversal paths to obtain several candidate query paths; obtain the priority of the starting entity of each candidate query path, and select the candidate query path with the highest priority of the starting entity as the query path of the target entity in the knowledge graph; wherein, the higher the number of entities, the lower the priority of the entities.
[0078] In one possible implementation, the statement concatenation module is specifically used to: perform a concatenation operation based on the ontology adjacency table, starting from the initial entity of the query path; wherein the concatenation operation includes: concatenating the entity condition statement corresponding to the current entity to the query statement trunk; determining the neighboring entities of the current entity according to the ontology adjacency table, and removing visited entities and entities marked as unnecessary from the neighboring entities, and taking the entity located in the query path among the remaining neighboring entities as the main entity, and the remaining neighboring entities as branch entities; for each branch entity, concatenating the entity condition statement and relational condition statement corresponding to the branch entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the query statement trunk; for the main entity, when the main entity is the target entity, concatenating the entity condition statement and relational condition statement corresponding to the main entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the query statement trunk and ending the concatenation operation, and after ending the concatenation operation, concatenating the target statement at the end of the final query statement trunk to obtain a structured knowledge graph query statement for the query question; otherwise, continuing the recursive concatenation operation with the main entity.
[0079] All relevant content of each step involved in the aforementioned embodiments of the knowledge graph query statement generation method can be referenced to the functional description of the corresponding functional module of the knowledge graph query statement generation system in the embodiments of the present invention, and will not be repeated here.
[0080] The module division in this embodiment of the invention is illustrative and represents only one logical functional division. In actual implementation, other division methods may be used. Furthermore, the functional modules in the various embodiments of the invention can be integrated into a single processor, exist as separate physical entities, or be integrated into a single module. The integrated modules described above can be implemented in hardware or as software functional modules.
[0081] In another embodiment of the present invention, a computer device is provided, comprising a processor and a memory. The memory stores a computer program, which includes program instructions. The processor executes the program instructions stored in the computer storage medium. The processor may be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. It is the computing and control core of the terminal, suitable for implementing one or more instructions, specifically suitable for loading and executing one or more instructions from the computer storage medium to achieve a corresponding method flow or corresponding function. The processor described in this embodiment of the present invention can be used for the operation of a knowledge graph query statement generation method.
[0082] In another embodiment of the present invention, a storage medium is provided, specifically a computer-readable storage medium (Memory). This computer-readable storage medium is a memory device within a computer device used to store programs and data. It is understood that the computer-readable storage medium here can include both the built-in storage medium of the computer device and extended storage media supported by the computer device. The computer-readable storage medium provides storage space containing the terminal's operating system. Furthermore, this storage space also contains one or more instructions suitable for loading and execution by a processor. These instructions can be one or more computer programs (including program code). It should be noted that the computer-readable storage medium here can be high-speed RAM or non-volatile memory, such as at least one disk storage device. The processor can load and execute one or more instructions stored in the computer-readable storage medium to implement the corresponding steps of the knowledge graph query statement generation method in the above embodiments.
[0083] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0084] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0085] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0086] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0087] 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 it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the specific implementation of the present invention. Any modifications or equivalent substitutions that do not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A method for generating query statements for a knowledge graph, characterized in that, include: The query question is obtained, and the query question is parsed into elements using a large language model. The parsed element results are then populated into a preset structured element template to obtain a structured element file. Based on the structured feature file, extract structured entity features, relational features and target features, and map them into structured entity conditional statements, relational conditional statements and target statements respectively according to the preset mapping rules; The target entity is determined based on the structured target elements, and the query path of the target entity in the knowledge graph is obtained by combining the ontology adjacency list of the knowledge graph. Based on the query path, the structured entity condition statement, relation condition statement, and target statement are concatenated to obtain the structured knowledge graph query statement for the query question.
2. The knowledge graph query statement generation method according to claim 1, characterized in that, The preset structured element template adopts JSON format; The element parsing process includes parsing entity elements, relational elements, and target elements. The preset structured element template includes an entity element template, a relationship element template, and a destination element template; wherein the relationship element template is embedded in the entity element template.
3. The knowledge graph query statement generation method according to claim 1, characterized in that, The process of determining the target entity based on structured target elements and combining it with the ontology adjacency list of the knowledge graph to obtain the query path of the target entity in the knowledge graph includes: The target entity is determined based on the structured target elements. Starting from the target entity, the entities in the ontology adjacency list of the knowledge graph are traversed to obtain several traversal paths. Several traversal paths are reversed to obtain several candidate query paths, and one candidate query path is selected from these candidate query paths as the query path for the target entity in the knowledge graph.
4. The knowledge graph query statement generation method according to claim 3, characterized in that, Before traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity, the method further includes: The mentioned entities are determined based on the structured entity elements; Traverse each entity in the ontology adjacency list of the knowledge graph. When the degree of the current entity is less than or equal to 1 and it is neither the target entity nor the mentioned entity, add the current entity to the tail of the preset candidate queue. Repeat the marking step until the candidate queue is empty; wherein, the marking step includes: selecting the first entity in the candidate queue as the marked entity, setting the degree of the marked entity to 0 and marking it as unnecessary in the ontology adjacency list, traversing the entities connected to the marked entity in the ontology adjacency list to obtain several connected entities, deleting adjacent entities with a degree of 0, and decrementing the degree of each remaining connected entity by one, and when the degree of the current connected entity is 1 and it is neither the mentioned entity nor the target entity, adding the current connected entity to the tail of the candidate queue; When traversing the entities in the ontology adjacency list of the knowledge graph starting from the target entity, entities marked as unnecessary are excluded.
5. The knowledge graph query statement generation method according to claim 3 or 4, characterized in that, The step of traversing each entity in the ontology adjacency list of the knowledge graph starting from the target entity includes: starting from the target entity in the ontology adjacency list of the knowledge graph, traversing each entity in the ontology adjacency list using a depth-first traversal algorithm.
6. The knowledge graph query statement generation method according to claim 3 or 4, characterized in that, The process of reversing several traversal paths to obtain several candidate query paths, and selecting one candidate query path from among the candidate query paths as the query path for the target entity in the knowledge graph, includes: Reverse several traversal paths to obtain several candidate query paths; Obtain the priority of the starting entity of each candidate query path, and select the candidate query path with the highest priority of the starting entity as the query path of the target entity in the knowledge graph. The higher the number of entities, the lower the priority of those entities.
7. The knowledge graph query statement generation method according to claim 4, characterized in that, The process of concatenating structured entity condition statements, relational condition statements, and target statements according to the query path to obtain a structured knowledge graph query statement for the query question includes: Starting from the initial entity of the query path, perform a concatenation operation based on the ontology adjacency table; The concatenation operation includes: concatenating the entity condition statement corresponding to the current entity to the main body of the query statement; determining the neighboring entities of the current entity according to the ontology adjacency table, removing visited entities and entities marked as unnecessary from the neighboring entities, and designating the entities located in the query path among the remaining neighboring entities as the main entity and the remaining neighboring entities as branch entities; for each branch entity, concatenating the entity condition statement and relational condition statement corresponding to the branch entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the main body of the query statement; for the main entity, when the main entity is the target entity, concatenating the entity condition statement and relational condition statement corresponding to the main entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the main body of the query statement and ending the concatenation operation; and after ending the concatenation operation, concatenating the target statement at the end of the final main body of the query statement to obtain a structured knowledge graph query statement for the query question; otherwise, continuing the recursive concatenation operation with the main entity.
8. A knowledge graph query statement generation system, characterized in that, include: The feature parsing module is used to obtain the query question, parse the query question into features using a large language model, and populate the feature parsing results into a preset structured feature template to obtain a structured feature file. The statement generation module is used to extract structured entity elements, relational elements and target elements based on structured element files, and map them into structured entity condition statements, relational condition statements and target statements respectively according to preset mapping rules. The path determination module is used to determine the target entity based on the structured target elements, and combine it with the ontology adjacency list of the knowledge graph to obtain the query path of the target entity in the knowledge graph. The statement concatenation module is used to concatenate the structured entity condition statement, relation condition statement, and target statement according to the query path to obtain a structured knowledge graph query statement for the query question.
9. The knowledge graph query statement generation system according to claim 8, characterized in that, The preset structured element template adopts JSON format; The element parsing process includes parsing entity elements, relational elements, and target elements. The preset structured element template includes an entity element template, a relationship element template, and a destination element template; wherein the relationship element template is embedded in the entity element template.
10. The knowledge graph query statement generation system according to claim 8, characterized in that, The path determination module is specifically used for: The target entity is determined based on the structured target elements. Starting from the target entity, the entities in the ontology adjacency list of the knowledge graph are traversed to obtain several traversal paths. Several traversal paths are reversed to obtain several candidate query paths, and one candidate query path is selected from these candidate query paths as the query path for the target entity in the knowledge graph.
11. The knowledge graph query statement generation system according to claim 10, characterized in that, Before the path determination module traverses each entity in the ontology adjacency list of the knowledge graph starting from the destination entity, it is also used for: The mentioned entities are determined based on the structured entity elements; Traverse each entity in the ontology adjacency list of the knowledge graph. When the degree of the current entity is less than or equal to 1 and it is neither the target entity nor the mentioned entity, add the current entity to the tail of the preset candidate queue. Repeat the marking step until the candidate queue is empty; wherein, the marking step includes: selecting the first entity in the candidate queue as the marked entity, setting the degree of the marked entity to 0 and marking it as unnecessary in the ontology adjacency list, traversing the entities connected to the marked entity in the ontology adjacency list to obtain several connected entities, deleting adjacent entities with a degree of 0, and decrementing the degree of each remaining connected entity by one, and when the degree of the current connected entity is 1 and it is neither the mentioned entity nor the target entity, adding the current connected entity to the tail of the candidate queue; When traversing the entities in the ontology adjacency list of the knowledge graph starting from the target entity, entities marked as unnecessary are excluded.
12. The knowledge graph query statement generation system according to claim 10 or 11, characterized in that, The path determination module is specifically used to: start from the target entity in the ontology adjacency list of the knowledge graph and traverse each entity in the ontology adjacency list using a depth-first traversal algorithm.
13. The knowledge graph query statement generation system according to claim 10 or 11, characterized in that, The path determination module is specifically used for: Reverse several traversal paths to obtain several candidate query paths; Obtain the priority of the starting entity of each candidate query path, and select the candidate query path with the highest priority of the starting entity as the query path of the target entity in the knowledge graph. The higher the number of entities, the lower the priority of those entities.
14. The knowledge graph query statement generation system according to claim 11, characterized in that, The statement concatenation module is specifically used for: Starting from the initial entity of the query path, perform a concatenation operation based on the ontology adjacency table; The concatenation operation includes: concatenating the entity condition statement corresponding to the current entity to the main body of the query statement; determining the neighboring entities of the current entity according to the ontology adjacency table, removing visited entities and entities marked as unnecessary from the neighboring entities, and designating the entities located in the query path among the remaining neighboring entities as the main entity and the remaining neighboring entities as branch entities; for each branch entity, concatenating the entity condition statement and relational condition statement corresponding to the branch entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the main body of the query statement; for the main entity, when the main entity is the target entity, concatenating the entity condition statement and relational condition statement corresponding to the main entity into a nested statement and attaching it to the entity condition statement corresponding to the current entity in the main body of the query statement and ending the concatenation operation; and after ending the concatenation operation, concatenating the target statement at the end of the final main body of the query statement to obtain a structured knowledge graph query statement for the query question; otherwise, continuing the recursive concatenation operation with the main entity.
15. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the knowledge graph query statement generation method as described in any one of claims 1 to 7.
16. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the knowledge graph query statement generation method as described in any one of claims 1 to 7.
