Knowledge Graph-Based Enhanced Document Generation and Retrieval Method
Through dynamic segmentation and Graph-RAG model combined with knowledge graph, the problems of insufficient semantic understanding and low knowledge correlation in the existing document generation and retrieval system are solved, high-quality document generation and precise retrieval are achieved, and the semantic integrity and knowledge correlation of the document are ensured.
Patent Information
- Application Number
- CN202411702394.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-26
- Publication Date
- 2025-08-05
- Estimated Expiration
- 2044-11-26
AI Technical Summary
The existing document generation and search system has insufficient semantic understanding, low knowledge correlation, and poor generation quality, which is difficult to adapt to the personalized needs of different scenarios. The fixed segmentation strategy affects the accuracy of knowledge extraction and the correlation of search results.
By dynamically segmented processing of heterogeneous documents, a Graph-RAG model is built, and combined with the knowledge graph-document index extraction module and the design document generation module, the feature extraction model is used to establish the mapping relationship between document segmented and knowledge graph entities, so as to achieve accurate retrieval and high-quality document generation.
It improves the semantic understanding ability and retrieval accuracy of document generation, ensures the semantic integrity and rationality of segments, and realizes cross-document knowledge correlation. The generated documents meet both structured characteristics and personalized needs.
Smart Images

Figure CN119646178B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of knowledge graph technology, and in particular to an enhanced document generation and retrieval method based on knowledge graphs. Background Art
[0002] With the rapid development of artificial intelligence (AI) technology, knowledge graph-based document generation and retrieval systems have gained widespread application in fields such as intelligent question answering, search engines, and decision support. These systems organize and manage massive amounts of document information by building knowledge graphs and leverage natural language processing techniques for intelligent retrieval and automatic document generation. Currently, mainstream document generation and retrieval systems primarily employ keyword-matching retrieval methods and utilize pre-set templates to assist in document generation.
[0003] Existing document generation and retrieval methods typically treat knowledge graph construction and document processing as separate steps. In knowledge graph construction, the system extracts structured knowledge from documents through techniques such as entity recognition and relationship extraction. In document processing, a fixed segmentation strategy is used to segment documents, and retrieval indexes are established based on word frequency statistics or simple semantic similarity calculations. Furthermore, the document generation process primarily relies on predefined templates and rules, lacking in-depth utilization of the retrieval content.
[0004] However, this fragmented approach has significant shortcomings: First, fixed document segmentation strategies fail to ensure the semantic integrity of segmented content, affecting the accuracy of knowledge extraction; second, simple retrieval methods fail to fully utilize the semantic association information in the knowledge graph, resulting in low relevance of retrieval results; finally, document generation based on fixed templates lacks flexibility and is difficult to adapt to the personalized needs of different scenarios, and the quality and consistency of generated content need to be improved. These issues seriously restrict the practical application of document generation and retrieval systems. Summary of the Invention
[0005] In view of this, the present invention proposes an enhanced document generation and retrieval method based on knowledge graph. By dynamically segmenting heterogeneous documents, combining the Graph-RAG model to build and update the knowledge graph, a mapping relationship between document segments and knowledge graph entities is established, and accurate retrieval and high-quality document generation based on semantic understanding are achieved, thereby solving technical problems existing in traditional document processing methods such as insufficient semantic understanding, low knowledge relevance, and poor generation quality.
[0006] The technical solution of the present invention is implemented as follows: The present invention provides an enhanced document generation and retrieval method based on knowledge graph, comprising:
[0007] S1. Receive heterogeneous document input and dynamically segment the heterogeneous documents to obtain document segments with semantic integrity;
[0008] S2. Build the Graph-RAG model, including the knowledge graph-document index extraction module and the design document generation module;
[0009] S3. Build and update the knowledge graph based on document segmentation through the knowledge graph-document index extraction module, and organize entities and relationships into triples;
[0010] S4. Extract feature vectors of document segments using a feature extraction model, establish mapping relationships between document segments and entities in the knowledge graph through a matching strategy, and construct a retrieval index based on the mapping relationships between document segments and entities in the knowledge graph;
[0011] S5. Receive the user's query sequence, and in the design document generation module, retrieve relevant entities and document segments through the retrieval index, and generate the target design document in combination with the preset template.
[0012] Preferably, step S1 includes:
[0013] S11. Receive heterogeneous documents and convert them into a unified XML or JSON intermediate format to obtain a standardized document structure; based on the standardized document structure, parse the document hierarchical structure, extract structured features, and obtain document content with structural annotations; process the document content with structural annotations, identify and standardize special tags, and obtain a structured document that retains key tags; uniformly encode and normalize the structured document that retains key tags, remove redundant characters, and obtain a pre-processed standard document;
[0014] S12. Use a bidirectional long short-term memory (LSTM) network to extract text features. Based on the preprocessed standard document, set the sliding window size n and step size k to obtain a series of overlapping text segments. For each text segment, encode it through the LSTM network to obtain a feature vector sequence with a dimension of d. Based on the feature vector sequence, calculate the cosine similarity between adjacent windows to obtain the semantic relevance score between the text segments.
[0015] S13. Using the semantic relevance scores between text segments, potential segmentation points are marked based on a first threshold θ1 to obtain a preliminary set of segmentation boundaries. The segmentation points are verified and confirmed by combining the structured features of the standard document and a second threshold θ2 to obtain structure-aware segmentation boundaries. The information entropy H(w) of the text in the current window is calculated. When the information entropy exceeds a third threshold θ3, the window size is adjusted to obtain an adaptive segmentation granularity.
[0016] S14. Based on the adaptive segmentation granularity, optimization is performed according to similarity and segment length, adjacent segments with high semantic correlation are merged, and segments with too large semantic span are split to obtain the optimized final document segmentation.
[0017] Preferably, step S14 includes:
[0018] S141, extract the topic vector T of each segment based on the adaptive segmentation granularity i , obtain the segmented semantic representation;
[0019] S142. Calculate the length distribution characteristics of all segments, including the average length μ and standard deviation σ; identify segments whose length exceeds μ+1.5σ and mark them as the set to be split P; identify segments whose length is less than the minimum threshold L min The segments are marked as the set to be merged Q;
[0020] S143. For the segments in the split set P, recursively perform the following operations:
[0021] Execute steps S12-S13 for each segment to re-segment, re-execute S142 for length analysis on the newly generated segments, update the set to be split P and the set to be merged Q, and when the set to be split P is empty, the recursion ends and the set to be merged Q after the recursion is output;
[0022] S144. For the segments in the recursively merged set Q, define the adjacent segments of each segment as the segments before and after it:
[0023] Calculate the cosine similarity SIM(T i ,T i+1 ), judging whether the merging condition is met based on the fourth threshold θ4; if only one adjacent segment meets the condition, adding the segment pair to the set to be merged M; if both adjacent segments meet the condition, selecting the segment pair with the highest cosine similarity and adding it to the set to be merged M; outputting the final set of segment pairs to be merged M;
[0024] S145 . Perform a segment merging operation on the segment pairs to be merged in the final set M of segment pairs to be merged to obtain optimized final document segments.
[0025] Preferably, if the length of the first or last segment of the document is less than the minimum threshold L min , then determine the cosine similarity between it and the topic vector of the only adjacent segment. If the cosine similarity meets the fourth threshold θ4, the first segment of the document or the last segment of the document and its corresponding adjacent segment are added to the set M as a segment pair to be merged; if not, keep it as it is.
[0026] Preferably, the steps of constructing a knowledge graph based on document segmentation include:
[0027] Use the Transformer encoder to represent the features of the segmented text; identify the entity set E in the text based on the feature representation; analyze the semantic dependency between entities and obtain the relationship set R;
[0028] Align the identified entities with the external knowledge base; use the word embedding model to calculate the semantic similarity between entities; perform entity disambiguation and synonym normalization based on semantic similarity;
[0029] The entity pair (e i ,e j ) and its relationship r ij Organized into triples (e i ,r ij ,e j ); verify the semantic rationality of the triple; complete the missing entity attributes based on external knowledge;
[0030] Integrate the normalized triple set into a knowledge graph g(h); establish an index structure for entities and relationships; and save the semantic vector representations of entities and relationships.
[0031] The formula expression of the knowledge graph g(h) is as follows:
[0032] g(h)={(e i ,r ij ,e j )|e i ,e j ∈E,r ij ∈R}
[0033] Where g(h) is the knowledge graph generated by the Transformer model; E is the entity set; R is the relationship set; (e i ,r ij ,e j ) represents entity e i and e j Through the relationship ij The triplet composed of .
[0034] Preferably, the knowledge graph update strategy is:
[0035] Perform a multi-dimensional credibility assessment on the knowledge units extracted from the newly added document segments. By calculating the TF-IDF weight, the semantic consistency score based on the pre-trained word embedding model, and the source reliability score, a comprehensive credibility score (Score1) is calculated for each attribute value and relationship triple.
[0036] Calculate the entity name similarity S using the character-level edit distance algorithm c , calculate semantic similarity S based on pre-trained word embedding model s , calculate the structural similarity S by the attribute set overlap and the relationship triple overlap r , the similarities of these three dimensions are combined according to the weights to obtain the comprehensive matching score Score2,
[0037] Based on the comprehensive matching score Score2, two thresholds τ1 and τ2 are set, τ1>τ2, and the following judgment strategy is used for processing:
[0038] When Score2 is greater than τ1, it is considered a complete match, and the Score1 scores of each attribute value and relationship triple are compared respectively; the attribute value and relationship triple with the higher Score1 score are retained, and the replaced knowledge is saved as a historical version;
[0039] When Score2 is between τ1 and τ2, it is considered a partial match. The alignable attribute values and relationship triples are compared one by one, and the attribute values and relationship triples with higher Score1 scores are retained. For newly added attribute values and relationship triples, they are allowed to be added when their Score1 exceeds the preset matching threshold.
[0040] When Score2 is less than or equal to τ2, it is determined to be a new entity. The comprehensive credibility score Score1 of the new entity is checked. When Score1 exceeds the preset new threshold, it is allowed to be added to the knowledge graph and the initial credibility benchmark of the new entity is recorded.
[0041] At the same time, the timestamp, operation type, change content, and related Score1 and Score2 scores are recorded for all update operations.
[0042] Preferably, step S4 includes:
[0043] S41, record the document segments of the input heterogeneous documents as a segment set {s1, s2, ..., s n}, and assign a unique index identifier z={1,2,...,n} to each segment; use the BERT model to extract feature vectors for each document segment, and obtain the feature vector set d(z)={d1,d2,...,d n}, where d i ∈R d represents the eigenvector of segment i;
[0044] S42. At the same time, the BERT model is used to vectorize the entities in the knowledge graph to obtain the entity vector e k ∈P d ;
[0045] S43. Calculate the semantic relevance between entities and document segments through maximum inner product search:
[0046] Sim(e k ,d i )=e k T ·d i
[0047] Where, e k Represents the entity vector in the knowledge graph, d i Feature vector representing the document segment;
[0048] S44. For each entity e k , based on the semantic relevance calculation results, determine the most relevant document segment index:
[0049]
[0050] Where Z k * Represents entity e k The most relevant document segment index;
[0051] S45. Construct a mapping set of entity-document segments:
[0052] A={(e k ,z k * )|e k ∈g(h),z k * ∈d(z)}
[0053] Where A represents the set of mapping relationships between entities and document segment indexes.
[0054] Preferably, the design document generation module includes a retriever and a generator, wherein:
[0055] The retriever includes a query encoding unit, an index retrieval unit, and a sorting output unit. Its retrieval process is expressed as follows:
[0056] q(h,z|x)=P(h,z|x)=TopK[sim(q(x),I(A))]
[0057] Where q(h,z|x) represents the retriever, P(h,z|x) represents the conditional probability distribution of the retrieved entity h and document segment z based on the input query x, x represents the query sequence, q(x) is the query vector, A represents the set of mapping relationships between entities and document segment indexes, I(A) represents the retrieval index built based on the mapping relationship between entities and document segment indexes, sim represents the similarity calculation function, and TopK represents the function that returns the top K results with the highest similarity.
[0058] The generator includes a context encoding unit, an autoregressive decoding unit, and a document organization unit. Its generation process is expressed as:
[0059] p(y i |h,z,t,y {i-1})=P(y i |x,h,z,t,y {i-1} ;φ)=Decoder(c(h,z,t),y {i-1} )
[0060] In the formula, p(y i |h,z,t,y {i-1} ) represents the generator, P(y i |x,h,z,t,y {i-1} ;φ) represents the generation of the next tag y under given conditions i , φ is the parameter of the generator, h is the retrieved entity, z is the document segment related to entity h, t is the preset document generation template, y i is the target tag to be generated at the current time step, y {i-1} For the generated target sequence {y1,y2,...,y i-1}, represents all target tags that have been generated before the current time step i, c(h,z,t) represents the context vector obtained by encoding the entity h, document segment z and the preset document generation template t, and Decoder represents the decoder function, which is used to convert the context vector and the generated sequence into the probability distribution of the next tag.
[0061] Preferably, step S5 includes:
[0062] S51, receiving a query sequence x input by the user, encoding the query sequence into a query vector through the Transformer model, and normalizing the query vector;
[0063] S52. Use the retriever to search in the pre-built search index I(A), calculate the similarity between the query vector and the entity-segment pairs in the index, and return the top-K most relevant entity-segment pairs (h, z) as the search results;
[0064] S53, combining the retrieved entity h, document segment z, and the preset document generation template t into context information; initializing the generated target sequence y to be an empty sequence; and generating the target sequence token by token in an autoregressive manner:
[0065] At each time step i, based on the current context and the generated sequence y {i-1} Predict the next token y i ; Add the predicted tokens to the generated target sequence; Repeat the process until the complete document is generated or the termination condition is reached;
[0066] S54. Organize the generated content according to the structural constraints of the preset document generation template t to form a final target design document that meets the specifications.
[0067] Preferably, the retriever and generator include a pre-training process, and the objective function during the pre-training is:
[0068]
[0069] Where, represents the expectation on the training dataset D; ψ, φ are the parameters of the retriever and generator respectively; P(y|x) is the joint probability representation of the retriever and generator; x is the input query sequence; y is the target generated sequence; q(h,z|x) is the conditional probability of the retriever output; Π represents multiplication; n is the length of the generated sequence.
[0070] The present invention has the following beneficial effects compared to the prior art:
[0071] (1) The present invention organically combines the knowledge graph with the document index extraction module by constructing the Graph-RAG model, thereby realizing the intelligent processing and knowledge association of heterogeneous documents. The model uses the Transformer encoder to represent the document features, establishes the triple form of entities and relationships, and uses the BERT model to extract document segment feature vectors, thus realizing deep semantic understanding of the document content. This dual feature extraction mechanism improves the semantic understanding ability and retrieval accuracy of document generation, so that the generated design documents not only have good structural features, but also realize cross-document knowledge association;
[0072] (2) The present invention adopts a dynamic segmentation technology based on bidirectional LSTM, and adaptively adjusts the segmentation granularity by setting the sliding window and step size parameters, combined with the semantic relevance score and information entropy threshold. The system marks potential segmentation points based on the first threshold θ1, verifies the rationality of the segmentation boundary through the second threshold θ2, and dynamically adjusts the window size using the third threshold θ3. On this basis, the segments are merged and split through a recursive optimization algorithm, and the length distribution characteristics of the segments are calculated, including the average length μ and the standard deviation σ, and the overlong and overshort segments are optimized. This multi-level segmentation optimization mechanism ensures the semantic integrity and rationality of document segmentation, and effectively solves the semantic fragmentation problem caused by the traditional fixed window segmentation method;
[0073] (3) The present invention updates the knowledge graph through a multi-dimensional credibility evaluation strategy, comprehensively considers the TF-IDF weight, the semantic consistency score based on the pre-trained word embedding model, and the source reliability score, and calculates the comprehensive credibility score Score1. At the same time, the entity name similarity S is calculated by the character-level edit distance algorithm. c , calculate semantic similarity S based on pre-trained word embedding model s , calculate the structural similarity S by the attribute set overlap and the relationship triple overlap r, synthesized to obtain the comprehensive matching score Score2. Based on these two scoring indicators, a dual-threshold judgment mechanism of τ1 and τ2 was established to achieve accurate updating and version management of the knowledge graph. This update strategy based on multi-dimensional evaluation ensures the accuracy and timeliness of the knowledge base, while preserving the historical record of knowledge evolution through the version management mechanism;
[0074] (4) The document generation module of the present invention adopts a collaborative working mode of a retriever and a generator, wherein the retriever realizes accurate retrieval through three functional units: query encoding, index retrieval and sorting output, and the generator includes three processing units: context encoding, autoregressive decoding and document organization. The system calculates the semantic relevance between entities and document segments through the maximum inner product search technology, establishes the mapping relationship between entities and document segments, and performs autoregressive decoding generation in combination with the structural constraints of the preset template. This generation method based on the collaboration of two modules not only improves the standardization and reliability of the generated documents, but also ensures the integrity and consistency of the generated content through template constraints, so that the generated documents not only maintain the accuracy of the original knowledge, but also meet the standardized structural requirements. BRIEF DESCRIPTION OF THE DRAWINGS
[0075] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0076] Figure 1 is a flow chart of a method according to an embodiment of the present invention;
[0077] Figure 2 A technical implementation diagram of an embodiment of the present invention;
[0078] Figure 3 Schematic diagram of dynamic segmentation according to an embodiment of the present invention. DETAILED DESCRIPTION
[0079] The following will be combined with the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.
[0080] like Figure 1 and Figure 2 As shown, the present invention provides an enhanced document generation and retrieval method based on knowledge graph, including:
[0081] S1. Receive heterogeneous document input and dynamically segment the heterogeneous documents to obtain document segments with semantic integrity;
[0082] S2. Build the Graph-RAG model, including the knowledge graph-document index extraction module and the design document generation module;
[0083] S3. Build and update the knowledge graph based on document segmentation through the knowledge graph-document index extraction module, and organize entities and relationships into triples;
[0084] S4. Extract feature vectors of document segments using a feature extraction model, establish mapping relationships between document segments and entities in the knowledge graph through a matching strategy, and construct a retrieval index based on the mapping relationships between document segments and entities in the knowledge graph;
[0085] S5. Receive the user's query sequence, and in the design document generation module, retrieve relevant entities and document segments through the retrieval index, and generate the target design document in combination with the preset template.
[0086] Specifically, if Figure 3 As shown, in one embodiment of the present invention, step S1 includes:
[0087] S11. Receive heterogeneous documents and convert them into a unified XML or JSON intermediate format to obtain a standardized document structure; based on the standardized document structure, parse the document hierarchical structure, extract structured features, and obtain document content with structural annotations; process the document content with structural annotations, identify and standardize special tags, and obtain a structured document that retains key tags; uniformly encode and normalize the structured document that retains key tags, remove redundant characters, and obtain a pre-processed standard document.
[0088] Specifically, heterogeneous documents may be technical documents, product documents, research reports, professional literature, etc. These documents can be manually uploaded by the administrator, and the update frequency can be set to automatically capture the latest documents and perform subsequent processing on them to ensure that the knowledge base can be updated in a timely manner.
[0089] In this embodiment, the XML conversion uses a DOM parser to build a document object model, constructs an XML tree structure through a depth-first traversal method, retains the hierarchical relationship and attribute information of the document, and outputs a standardized XML document structure; the JSON conversion uses a recursive parser to build a three-layer basic structure: metadata, content, and references. The hierarchical relationship of the document is maintained through recursive parsing, attribute mapping is established to ensure data integrity, and a standardized JSON format output is generated.
[0090] The format conversion rule set is as follows: Word document conversion: extract structured elements such as paragraphs, tables, and pictures, and retain style attributes; PDF document conversion: extract text blocks based on layout analysis, and identify title and body structure; HTML document conversion: parse the DOM tree structure and extract semantic tag content; plain text conversion: identify document structure based on delimiters and indentation relationships.
[0091] Structural feature extraction includes: title hierarchy recognition: analyzing font size, style features, and numbering; paragraph relationship analysis: identifying indentation relationships, paragraph spacing, and markers; list structure recognition: processing ordered lists, unordered lists, and definition lists; and table structure analysis: identifying table boundaries, merged cells, and header information. Feature extraction methods can use feature vector space models to extract document hierarchical relationship features, analyze text semantic coherence, and construct a structured feature matrix.
[0092] Feature tag processing: Formula tagging: Recognizes mathematical formulas in LaTeX and MathML formats and unifies the formula representation format; Reference tagging: Processes literature citation and cross-reference information and standardizes the reference tag style; Chart tagging: Extracts image captions and table titles and standardizes the chart description format; Code blocks: Recognizes program code and pseudocode paragraphs and unifies the code block representation method.
[0093] S12. Use a bidirectional long short-term memory network (LSTM) to extract text features. Based on the preprocessed standard document, set the sliding window size n and step size k to obtain a series of overlapping text fragments. For each text fragment, encode it through the LSTM network to obtain a feature vector sequence with a dimension of d. Based on the feature vector sequence, calculate the cosine similarity between adjacent windows to obtain the semantic relevance score between text fragments.
[0094] In this embodiment, the bidirectional LSTM adopts a two-layer structure with a hidden layer dimension of 256. The input layer uses pre-trained Word2Vec with a dimension of 300. Each LSTM unit contains a forget gate, an input gate, and an output gate. Residual connections are added to alleviate the gradient vanishing problem.
[0095] Text feature extraction is represented as: h t =BiLSTM(x t ,h {t-1} );feature vector =concat([forward ht ,backward ht ]). The forward and backward LSTM output vectors are concatenated to obtain the final feature vector.
[0096] The sliding window size n is initially set to 50 tokens, and the step size k is set to 1 / 4 of the window size. Adjacent windows are ensured to have 75% overlap, and a padding strategy is used for the beginning and end of the document.
[0097] S13. Using the semantic relevance scores between text fragments, potential segmentation points are marked based on the first threshold θ1 to obtain a preliminary set of segmentation boundaries; combining the structured features of the standard document and the second threshold θ2, the segmentation points are verified and confirmed to obtain structure-aware segmentation boundaries; the information entropy H(w) of the current window text is calculated, and when it exceeds the third threshold θ3, the window size is adjusted to obtain an adaptive segmentation granularity.
[0098] In this embodiment, a preliminary segmentation point set is first established based on the first threshold θ1, and the position and score of each potential segmentation point is recorded. Then, combined with the hierarchical structure information of the document, paragraph tags and format identifiers are analyzed, chapter titles and list structures are identified, and the logical organizational features of the document are extracted. The segmentation boundaries are confirmed based on the second threshold θ2, and the boundaries that do not meet the conditions are adjusted to generate a structure-aware segmentation boundary set.
[0099] Statistics the word frequency distribution in the window, calculate the standardized information entropy H(w), establish an entropy value evaluation benchmark, identify abnormal information density areas, and adjust the window based on the third threshold θ3. When H(w)>θ3, reduce the window size. When H(w)<θ3, increase the window size. Set upper and lower limit constraints on the window size to achieve smooth window transition. The adjustment formula is: n new =min(max(n current +a t ,n min ),n max ), where n current Indicates the current window size, n min Indicates the lower limit, n max Indicates the upper limit, a t Specifically, the adjustment step size can be set to k / 2.
[0100] S14. Based on the adaptive segmentation granularity, optimization is performed according to similarity and segment length, adjacent segments with high semantic correlation are merged, and segments with too large semantic span are split to obtain the optimized final document segmentation.
[0101] Step S14 includes:
[0102] S141, extract the topic vector T of each segment based on the adaptive segmentation granularity i , obtain the segmented semantic representation;
[0103] S142. Calculate the length distribution characteristics of all segments, including the average length μ and standard deviation σ; identify segments whose length exceeds μ+1.5σ and mark them as the set to be split P; identify segments whose length is less than the minimum threshold L min The segments are marked as the set to be merged Q;
[0104] S143. For the segments in the split set P, recursively perform the following operations:
[0105] Execute steps S12-S13 for each segment to re-segment, re-execute S142 for length analysis on the newly generated segments, update the set to be split P and the set to be merged Q, and when the set to be split P is empty, the recursion ends and the set to be merged Q after the recursion is output;
[0106] S144. For the segments in the recursively merged set Q, define the adjacent segments of each segment as the segments before and after it:
[0107] Calculate the cosine similarity SIM(T i ,T i+1 ), judging whether the merging condition is met based on the fourth threshold θ4; if only one adjacent segment meets the condition, adding the segment pair to the set to be merged M; if both adjacent segments meet the condition, selecting the segment pair with the highest cosine similarity and adding it to the set to be merged M; outputting the final set of segment pairs to be merged M;
[0108] S145 . Perform a segment merging operation on the segment pairs to be merged in the final set M of segment pairs to be merged to obtain optimized final document segments.
[0109] If the length of the first or last segment of the document is less than the minimum threshold L min , then determine the cosine similarity between it and the topic vector of the only adjacent segment. If the cosine similarity meets the fourth threshold θ4, the first segment of the document or the last segment of the document and its corresponding adjacent segment are added to the set M as a segment pair to be merged; if not, keep it as it is.
[0110] In this embodiment, the LDA model can be used to extract topic vectors. When the length distribution parameters are set, the average length μ and the standard deviation σ are determined based on the statistical analysis of the domain documents. min Can be set to μ / 3.
[0111] Specifically, this embodiment achieves adaptive segmentation optimization through the steps of topic vector extraction, length distribution feature analysis, recursive segmentation processing, merging condition determination, and final merging optimization. This ensures both the semantic integrity of the segments and the rationality of the segment lengths. This method can effectively reduce overly long or short segments, improve the balance and readability of the document structure, and maintain semantic coherence.
[0112] Specifically, in one embodiment of the present invention, the Graph-RAG model mainly includes two parts: a knowledge graph-document index extraction module and a design document generation module. The knowledge graph-document index extraction module is used to construct and update the knowledge graph and establish a retrieval index. The design document generation module performs retrieval based on the user's query sequence input, and then generates the target design document based on the retrieval results.
[0113] Specifically, the steps of constructing a knowledge graph based on document segmentation include:
[0114] Use the Transformer encoder to represent the features of the segmented text; identify the entity set E in the text based on the feature representation; analyze the semantic dependency between entities and obtain the relationship set R;
[0115] Align the identified entities with the external knowledge base; use the word embedding model to calculate the semantic similarity between entities; perform entity disambiguation and synonym normalization based on semantic similarity;
[0116] The entity pair (e i ,e j ) and its relationship r ij Organized into triples (e i ,r ij ,e j ); verify the semantic rationality of the triple; complete the missing entity attributes based on external knowledge;
[0117] Integrate the normalized triple set into a knowledge graph g(h); establish an index structure for entities and relationships; and save the semantic vector representations of entities and relationships.
[0118] The formula expression of the knowledge graph g(h) is as follows:
[0119] g(h)={(e i ,r ij ,e j )|e i ,e j ∈E,r ij ∈R}
[0120] Where g(h) is the knowledge graph generated by the Transformer model; E is the entity set; R is the relationship set; (e i ,r ij ,e j ) represents entity e i and e j Through the relationship ij The triplet composed of .
[0121] In this embodiment, the Transformer model adopts a BERT-base or RoBERTa-base pre-trained model, including 12 layers of standard Transformer layers. Its feature extraction process is as follows: 1. Use the WordPiece algorithm to perform subword segmentation, add special tags [CLS] and [SEP], and generate position encoding; 2. Capture contextual relationships through a multi-head self-attention mechanism, use a feedforward neural network to process features, apply residual connections and layer normalization; 3. Merge feature representations of different layers, and weightedly fuse the outputs of multiple attention heads to generate the final text representation.
[0122] The knowledge graph construction process of this embodiment is as follows:
[0123] 1. Use pre-trained models to identify professional terms, extract key concepts and proper nouns, and recognize numerical entities and time expressions; analyze descriptive text of entities, extract key features of entities, and identify entity qualifications.
[0124] 2. Construct a syntactic dependency tree, analyze the semantic relationship between words, and extract the subject-verb-object ternary relationship; identify the hierarchical relationship between entities, extract causal relationships, analyze temporal relationships, and identify attribute relationships.
[0125] 3. Perform entity matching with external knowledge bases, calculate semantic similarity between entities, and establish entity mapping relationships; perform entity disambiguation based on context, use entity attributes to distinguish, and apply rule constraints to eliminate ambiguity.
[0126] 4. Organize entity pairs and relationships, build (e i ,r ij ,e j ) in the form of triples, adding attribute information; checking semantic rationality, verifying the validity of the relationship, and supplementing missing attributes.
[0127] After constructing the knowledge graph, you can also build a knowledge graph index to quickly locate related entities in the knowledge graph. The specific process is as follows:
[0128] 1. Design the index structure, including entity index and relationship index. Entity index: establish a mapping from entity ID to entity information, build entity attribute index, and create entity alias index. Relationship index: establish relationship type index, build relationship direction index, and create relationship attribute index.
[0129] 2. Store vector representations, including entity vectors and relationship vectors. Entity vectors: store the semantic vector representation of entities, preserve the contextual features of entities, and maintain the evolutionary history of entities; relationship vectors: store the semantic vector representation of relationships, preserve the direction information of relationships, and maintain the confidence of relationships.
[0130] Specifically, in one embodiment of the present invention, the update strategy of the knowledge graph is:
[0131] Perform a multi-dimensional credibility assessment on the knowledge units extracted from the newly added document segments. By calculating the TF-IDF weight, the semantic consistency score based on the pre-trained word embedding model, and the source reliability score, a comprehensive credibility score (Score1) is calculated for each attribute value and relationship triple.
[0132] Calculate the entity name similarity S using the character-level edit distance algorithm c , calculate semantic similarity S based on pre-trained word embedding model s , calculate the structural similarity S by the attribute set overlap and the relationship triple overlap r , the similarities of these three dimensions are combined according to the weights to obtain the comprehensive matching score Score2,
[0133] Based on the comprehensive matching score Score2, two thresholds τ1 and τ2 are set, τ1>τ2, and the following judgment strategy is used for processing:
[0134] When Score2 is greater than τ1, it is considered a complete match, and the Score1 scores of each attribute value and relationship triple are compared respectively; the attribute value and relationship triple with the higher Score1 score are retained, and the replaced knowledge is saved as a historical version;
[0135] When Score2 is between τ1 and τ2, it is considered a partial match. The alignable attribute values and relationship triples are compared one by one, and the attribute values and relationship triples with higher Score1 scores are retained. For newly added attribute values and relationship triples, they are allowed to be added when their Score1 exceeds the preset matching threshold.
[0136] When Score2 is less than or equal to τ2, it is determined to be a new entity. The comprehensive credibility score Score1 of the new entity is checked. When Score1 exceeds the preset new threshold, it is allowed to be added to the knowledge graph and the initial credibility benchmark of the new entity is recorded.
[0137] At the same time, the timestamp, operation type, change content, and related Score1 and Score2 scores are recorded for all update operations.
[0138] Dynamic updates to knowledge graphs are crucial for maintaining knowledge timeliness and accuracy. Traditional update methods often suffer from issues such as delayed updates, improper conflict resolution, and lost historical versions. This embodiment achieves high-quality dynamic updates to knowledge graphs through multi-dimensional evaluation and a hierarchical update strategy.
[0139] In this embodiment, the calculation process of score Score1 is as follows:
[0140] TF-IDF weight calculation: TF(t,d) = number of occurrences of word t in document d / total number of words in document d; IDF(t) = log(total number of documents / (number of documents containing word t + 1)); TF-IDF(t,d) = TF(t,d) × IDF(t).
[0141] Semantic consistency score calculation: Use the pre-trained word embedding model to generate vector representation Vec know =Embedding(knowledge unit ); Vec context =Embedding(context); Calculate cosine similarity Semantic Score =sim(Vec know ,Vecc ontext ).
[0142] Source reliability score calculation: Source Score =w1*DocType+w2*Author+w3*Time+w4*Citation.
[0143] Score1 comprehensive calculation: Score1 = α*TF-IDF+β*Semantic Score +γ*Source Score .
[0144] Then, the multi-dimensional similarity between the newly added document segment and the existing knowledge graph is calculated, including entity name similarity, semantic similarity, and structural similarity. The entity name similarity is calculated using the character-level edit distance algorithm. c =1-(Edit Dis / max(len(e1),len(e2))); semantic similarity first uses the pre-trained word embedding model to generate vectors, and then calculates the cosine similarity, S s =cosine similarity (Vec1, Vec2); Structural similarity is calculated based on the attribute set overlap A = |A1∩A2| / |A1∪A2| and the relationship triple overlap R = |R1∩R2| / |R1∪R2|, S r =(A+R) / 2; finally, the entity name similarity, semantic similarity, and structural similarity are weighted and summed to obtain the comprehensive matching score Score2.
[0145] Specifically, in one embodiment of the present invention, step S4 includes:
[0146] S41, record the document segments of the input heterogeneous documents as a segment set {s1, s2, ..., s n}, and assign a unique index identifier z={1,2,...,n} to each segment; use the BERT model to extract feature vectors for each document segment, and obtain the feature vector set d(z)={d1,d2,...,d n}, where d i ∈R d represents the eigenvector of segment i;
[0147] S42. At the same time, the BERT model is used to vectorize the entities in the knowledge graph to obtain the entity vector e k ∈R d ;
[0148] S43. Calculate the semantic relevance between entities and document segments through maximum inner product search:
[0149] Sim(e k ,d i )=e k T ·d i
[0150] Where, e k Represents the entity vector in the knowledge graph, d i Feature vector representing the document segment;
[0151] S44. For each entity e k , based on the semantic relevance calculation results, determine the most relevant document segment index:
[0152]
[0153] Where z k * Represents entity e k The most relevant document segment index;
[0154] S45. Construct a mapping set of entity-document segments:
[0155] A={(e k ,z k * )|e k ∈g(h),z k * ∈d(z)}
[0156] Where A represents the set of mapping relationships between entities and document segment indexes.
[0157] Specifically, in this embodiment, the BERT-base pre-trained model can still be used to extract features for each segment. The model adopts a 12-layer Transformer structure with a 768-dimensional hidden layer. At the input end, the segmented text is word segmented by WordPiece, and [CLS] and [SEP] tags are added. The sequence length is uniformly padded to 512. The output of the [CLS] position is extracted as the feature vector d of the segment. i And normalize it by L2 norm to ensure the comparability of vectors. At the same time, assign a unique digital index z to each segment and establish a mapping relationship from segment ID to feature vector. Get the feature vector set d(z)={d1,d2,...,d n}, in this case, d(z) represents the set of all eigenvectors corresponding to the index set z, and also implies the index z to the eigenvector d i The mapping relationship.
[0158] Next, the same BERT model is used to vectorize the entities in the knowledge graph. To enhance the semantic representation of the entity, the entity name is combined with its attribute description and relationship information to construct the input sequence. The different features are weighted and fused through the attention mechanism to generate an entity vector e with the same dimension as the segment vector. k This ensures that the entity vector and segment vector can be calculated similarity in the same feature space.
[0159] Then, the maximum inner product search is used to calculate the semantic relevance between the entity and the document segment. The inner product of the entity vector and the segment vector is calculated by batch matrix multiplication, i.e. Sim(e k ,d i )=e k T ·d i To improve computational efficiency, the N entity vectors are constructed as a matrix E∈R N×768 , n segment vectors are constructed as a matrix D∈R n×768 , through E.D. T An N×n similarity matrix is obtained at once. A temperature coefficient τ (default 0.07) is introduced to adjust the similarity distribution, and the Softmax function is used for normalization.
[0160] For each entity e k , find the segment index z with the highest score in the similarity matrix through the argmax function k * , the most relevant document segment, To handle one-to-many mappings, we set a similarity threshold θ (recommended value 0.7) and retain the top-3 candidate segments whose similarity exceeds the threshold. For instances where multiple entities are mapped to the same segment, we prioritize them based on similarity scores and entity importance to ensure the rationality of the mapping.
[0161] Finally, a mapping set A of entity-document segments is constructed. An inverted index structure is used, where the index item is the entity ID, and the index value contains the mapped segment ID, similarity score, timestamp, and other information. The Skip List data structure is used to implement the index, supporting O(logn) query efficiency. The real-time nature of the index is maintained through an asynchronous update mechanism, and mapping relationships with similarity below the threshold or expired are regularly cleaned up. A balance is struck between memory and performance, with hot data cached in memory and cold data stored on disk. To support incremental updates, when a new document segment is added, it is only necessary to calculate the similarity between the new segment and the existing entity and update the affected index items.
[0162] Specifically, in one embodiment of the present invention, the retriever consists of three core functional units: a query encoding unit, an index retrieval unit, and a ranking output unit. The query encoding unit adopts a Transformer encoder structure, which includes a multi-layer self-attention mechanism and a feedforward neural network. The input query sequence x is first converted into a vector representation through a word embedding layer, then the contextual relationship within the query is captured through a multi-head self-attention layer, and finally the query vector q(x) is obtained through a feedforward neural network. The mathematical expression of the retrieval process is:
[0163] q(h,z|x)=P(h,z|x)=topK[sim(q(x),I(A))]
[0164] Where q(h,z|x) represents the retriever, P(h,z|x) represents the conditional probability distribution of the retrieved entity h and document segment z based on the input query x, x represents the query sequence, q(x) is the query vector, A represents the set of mappings between entities and document segment indices, I(A) represents the retrieval index constructed based on these mappings, sim represents the similarity calculation function, and TopK represents the function that returns the top K most similar results. The index retrieval unit uses an inverted index structure to support efficient vector similarity retrieval.
[0165] The generator adopts an encoder-decoder architecture, which includes a context encoding unit, an autoregressive decoding unit, and a document organization unit. The context encoding unit uses a Transformer encoder to encode the retrieved entity h, document segment z, and preset template t into a context vector c(h, z, t). The autoregressive decoding unit uses a Transformer decoder with a masked attention mechanism to generate a context vector c(h, z, t) at each time step i based on the context vector and the generated sequence y. {i-1} Predict the next token y i The mathematical expression of the generation process is:
[0166] p(y i |h,z,t,y {i-1} )=P(y i |x,h,z,t,y {i-1} ;φ)=Decoder(c(h,z,t),y {i-1} )
[0167] In the formula, p(y i |h,z,t,y {i-1} ) represents the generator, P(y i |x,h,z,t,y {i-1} ;φ) represents the generation of the next tag y under given conditions i , φ is the parameter of the generator, h is the retrieved entity, z is the document segment related to entity h, t is the preset document generation template, y i is the target tag to be generated at the current time step, y {i-1} For the generated target sequence {y1,y2,...,y i-1}, represents all target tags that have been generated before the current time step i, c(h,z,t) represents the context vector obtained by encoding the entity h, document segment z and the preset document generation template t, and Decoder represents the decoder function, which is used to convert the context vector and the generated sequence into the probability distribution of the next tag.
[0168] The training of the retriever and generator adopts an end-to-end joint optimization method, and the training objective function is:
[0169]
[0170] The calculation formula of the joint probability P(y|x) is:
[0171]
[0172] Where, represents the expectation on the training dataset D; ψ, φ are the parameters of the retriever and generator respectively; P(y|x) is the joint probability representation of the retriever and generator; x is the input query sequence; y is the target generated sequence; q(h,z|x) is the conditional probability of the retriever output; ∏ represents multiplication; n is the length of the generated sequence.
[0173] The specific training process is as follows:
[0174] 1. Data preprocessing: Construct a training set D, where each sample contains a query sequence x and a target generated sequence y.
[0175] 2. Parameter initialization: Randomly initialize the retriever parameters ψ and generator parameters φ.
[0176] 3. Forward propagation:
[0177] The retriever calculates q(h,z|x) to obtain the probability distribution of entity-segment pairs;
[0178] The generator generates target sequences token by token based on the retrieval results.
[0179] 4. Back Propagation:
[0180] Calculate the joint probability P(y|x);
[0181] Calculate the log-likelihood loss - logP(y|x; ψ, φ);
[0182] Update the parameters ψ, φ via gradient descent.
[0183] 5. Iterative optimization: Repeat steps 3-4 until convergence.
[0184] The teacherforcing strategy is used during training, that is, the real pre-order label sequence y is used during training. {i-1} Rather than the sequence generated by the model. At the same time, gradient clipping is used to prevent gradient explosion, and techniques such as dropout and layer normalization are used to improve the generalization ability of the model.
[0185] Specifically, in one embodiment of the present invention, step S5 includes:
[0186] S51, receiving a query sequence x input by the user, encoding the query sequence into a query vector through the Transformer model, and normalizing the query vector;
[0187] S52. Use the retriever to search in the pre-built search index I(A), calculate the similarity between the query vector and the entity-segment pairs in the index, and return the top-K most relevant entity-segment pairs (h, z) as the search results;
[0188] S53, combining the retrieved entity h, document segment z, and the preset document generation template t into context information; initializing the generated target sequence y to be an empty sequence; and generating the target sequence token by token in an autoregressive manner:
[0189] At each time step i, based on the current context and the generated sequence y {i-1} Predict the next token y i ; Add the predicted tokens to the generated target sequence; Repeat the process until the complete document is generated or the termination condition is reached;
[0190] S54. Organize the generated content according to the structural constraints of the preset document generation template t to form a final target design document that meets the specifications.
[0191] In a specific example, the implementation process of step S5 is as follows:
[0192] First, the query sequence x is input by the user and processed using a 12-layer Transformer encoder. The encoder uses a multi-head self-attention mechanism (12 attention heads) with a hidden layer dimension of 768. The input sequence is segmented using WordPiece, with special tags [CLS] and [SEP] added. The sequence length is uniformly padded to 512. The output at the [CLS] position is extracted as the query vector q(x) and normalized using the L2 norm to ensure comparability with the vector in the index.
[0193] Perform a maximum inner product search in the pre-built retrieval index I(A). Specifically, calculate the cosine similarity between the normalized query vector q(x) and all entity-segment pair vectors in the index:
[0194] σ(q(x),I(A))=q(x) T I(A);
[0195] A temperature coefficient τ (default 0.07) is introduced to adjust the similarity distribution, and the Softmax function is used for normalization. A heap sort algorithm is used to maintain the top-K candidate set, returning the K most similar entity-segment pairs (h, z). For each search result, its similarity score and corresponding entity and segment information are obtained simultaneously.
[0196] The retrieved entity h, document segment z, and pre-set template t are combined to construct the input sequence. Using a Transformer encoder-decoder architecture, the encoder processes the input sequence to produce a context vector c(h, z, t). The decoder uses a masked multi-head attention mechanism to ensure that only generated tokens are accessible during the generation process.
[0197] At each time step i:
[0198] 1. The generated sequence y {i-1} Enter the decoder.
[0199] 2. Fuse the context vector c(h,z,t) through the cross attention mechanism.
[0200] 3. Get the next tag y through the fully connected layer and the Softmax layer i The probability distribution of .
[0201] 4. Use beam search (beam width b=4) to maintain multiple candidate sequences.
[0202] 5. Determine whether to terminate generation based on the stop flag or maximum length limit.
[0203] The probability of the generative process is calculated as:
[0204] P(y i |h,z,t,y {i-1} )=Softmax(W·h i +b);
[0205] where h i is the hidden state of the corresponding position in the last layer of the decoder, and W and b are learnable parameters.
[0206] Organize the generated content based on the structural constraints of the preset template t: parse the placeholders and format requirements in the template t, map the generated content to the corresponding position according to the template structure, process special tags and formatting characters, verify the integrity and consistency of the document structure, apply post-processing rules (such as paragraph indentation, line breaks, etc.), and output the final design document that meets the specifications.
[0207] Throughout the generation process, attention weight visualization and decoding probability threshold control are used to ensure the quality of generated content. At the same time, an alignment relationship between generated content and retrieved content is established to support traceability and explanation.
[0208] In summary, the present invention first dynamically segments heterogeneous documents through a bidirectional LSTM network and a multi-threshold adaptive strategy to ensure the semantic integrity of the segments; then uses a Transformer encoder to extract entities and relationships from the segments, constructs a normalized knowledge graph, and realizes dynamic updates of the knowledge graph through multi-dimensional credibility evaluation and a similarity-based matching strategy; then uses a BERT model to vectorize the document segments and knowledge graph entities, and establishes entity-segment mapping relationships and retrieval indexes through maximum inner product search; finally, a document generation module based on a retriever-generator architecture is designed, in which the retriever retrieves relevant content by matching the similarity between the query vector and the index, and the generator uses an encoder-decoder structure and combines preset templates for autoregressive generation. This solution improves the accuracy and semantic relevance of document generation through knowledge graph enhancement, while supporting efficient retrieval and dynamic updates of documents, and realizing intelligent processing and knowledge reuse of heterogeneous documents.
[0209] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. Enhanced document generation and retrieval method based on knowledge graph, characterized by: include: S1. Receive heterogeneous document input and dynamically segment the heterogeneous documents to obtain document segments with semantic integrity; Step S1 includes: S11, receiving heterogeneous documents and preprocessing them to obtain a preprocessed standard document; S12. Use a bidirectional long short-term memory (LSTM) network to extract text features. Based on the preprocessed standard document, set the sliding window size n and step size k to obtain a series of overlapping text segments. For each text segment, encode it through the LSTM network to obtain a feature vector sequence with a dimension of d. Based on the feature vector sequence, calculate the cosine similarity between adjacent windows to obtain the semantic relevance score between the text segments. S13. Using the semantic relevance scores between text segments, potential segmentation points are marked based on a first threshold θ1 to obtain a preliminary set of segmentation boundaries. The segmentation points are verified and confirmed by combining the structured features of the standard document and a second threshold θ2 to obtain structure-aware segmentation boundaries. The information entropy H(w) of the text in the current window is calculated. When the information entropy exceeds a third threshold θ3, the window size is adjusted to obtain an adaptive segmentation granularity. S14. Based on the adaptive segmentation granularity, optimization is performed according to similarity and segment length, adjacent segments with high semantic correlation are merged, and segments with too large semantic span are split to obtain the optimized final document segmentation; Step S14 includes: S141, extract the topic vector T of each segment based on the adaptive segmentation granularity i , obtain the segmented semantic representation; S142. Calculate the length distribution characteristics of all segments, including the average length μ and standard deviation σ; identify segments whose length exceeds μ+1.5σ and mark them as the set to be split P; identify segments whose length is less than the minimum threshold L min The segments are marked as the set to be merged Q; S143. For the segments in the split set P, recursively perform the following operations: Execute steps S12-S13 for each segment to re-segment, re-execute S142 for length analysis on the newly generated segments, update the set to be split P and the set to be merged Q, and when the set to be split P is empty, the recursion ends and the set to be merged Q after the recursion is output; S144. For the segments in the recursively merged set Q, define the adjacent segments of each segment as the segments before and after it: Calculate the cosine similarity SIM(T i ,T i+1 ), judging whether the merging condition is met based on the fourth threshold θ4; if only one adjacent segment meets the condition, adding the segment pair to the set to be merged M; if both adjacent segments meet the condition, selecting the segment pair with the highest cosine similarity and adding it to the set to be merged M; outputting the final set of segment pairs to be merged M; S145, performing a segment merging operation on the segment pairs to be merged in the final set M of segment pairs to be merged to obtain optimized final document segments; S2. Build the Graph-RAG model, including the knowledge graph-document index extraction module and the design document generation module; S3. Build and update the knowledge graph based on document segmentation through the knowledge graph-document index extraction module, and organize entities and relationships into triples; S4. Extract feature vectors of document segments using a feature extraction model, establish mapping relationships between document segments and entities in the knowledge graph through a matching strategy, and construct a retrieval index based on the mapping relationships between document segments and entities in the knowledge graph; S5. Receive the user's query sequence, and in the design document generation module, retrieve relevant entities and document segments through the search index, and generate the target design document in combination with the preset template; The design document generation module includes a retriever and a generator, where: The retriever includes a query encoding unit, an index retrieval unit, and a sorting output unit. Its retrieval process is expressed as follows: q(h,z|x)=P(h,z|x)=TopK[sim(q(x),I(A))] Where q(h,z|x) represents the retriever, P(h,z|x) represents the conditional probability distribution of the retrieved entity h and document segment z based on the input query x, x represents the query sequence, q(x) is the query vector, A represents the set of mapping relationships between entities and document segment indexes, I(A) represents the retrieval index built based on the mapping relationship between entities and document segment indexes, sim represents the similarity calculation function, and TopK represents the function that returns the top K results with the highest similarity. The generator includes a context encoding unit, an autoregressive decoding unit, and a document organization unit. Its generation process is expressed as: p(y i |h,z,t,y {i-1} )=P(y i |x,h,z,t,y {i-1} ;φ)=Decoder(c(h,z,t),y {i-1} ) In the formula, p(y i |h,z,t,y {i-1} ) represents the generator, P(y i |x,h,z,t,y {i-1} ;φ) represents the generation of the next tag y under given conditions i , φ is the parameter of the generator, h is the retrieved entity, z is the document segment related to entity h, t is the preset document generation template, y i is the target tag to be generated at the current time step, y {i-1} For the generated target sequence {y1,y2,...,y i-1 }, represents all target tags that have been generated before the current time step i, c(h,z,t) represents the context vector obtained by encoding the entity h, document segment z and the preset document generation template t, and Decoder represents the decoder function, which is used to convert the context vector and the generated sequence into the probability distribution of the next tag; Step S5 includes: S51, receiving a query sequence x input by the user, encoding the query sequence into a query vector through the Transformer model, and normalizing the query vector; S52. Use the retriever to search in the pre-built search index I(A), calculate the similarity between the query vector and the entity-segment pairs in the index, and return the top-K most relevant entity-segment pairs (h, z) as the search results; S53, combining the retrieved entity h, document segment z, and the preset document generation template t into context information; initializing the generated target sequence y to be an empty sequence; and generating the target sequence token by token in an autoregressive manner: At each time step i, based on the current context and the generated sequence y {i-1} Predict the next token y i ; Add the predicted tokens to the generated target sequence; Repeat the process until the complete document is generated or the termination condition is reached; S54. Organize the generated content according to the structural constraints of the preset document generation template t to form a final target design document that meets the specifications.
2. The enhanced document generation and retrieval method based on knowledge graph according to claim 1, characterized in that: Step S11 includes: Receive heterogeneous documents and convert them into a unified XML or JSON intermediate format to obtain a standardized document structure; based on the standardized document structure, parse the document hierarchical structure, extract structured features, and obtain document content with structural annotations; process the document content with structural annotations, identify and standardize special tags, and obtain structured documents that retain key tags; uniformly encode and normalize the structured documents that retain key tags, remove redundant characters, and obtain pre-processed standard documents.
3. The enhanced document generation and retrieval method based on knowledge graph according to claim 2, characterized in that: If the length of the first or last segment of the document is less than the minimum threshold L min , then determine the cosine similarity between it and the topic vector of the only adjacent segment. If the cosine similarity meets the fourth threshold θ4, the first segment of the document or the last segment of the document and its corresponding adjacent segment are added to the set M as a segment pair to be merged; if not, keep it as it is.
4. The enhanced document generation and retrieval method based on knowledge graph according to claim 1, characterized in that: The steps to build a knowledge graph based on document segmentation include: Use the Transformer encoder to represent the features of the segmented text; identify the entity set E in the text based on the feature representation; analyze the semantic dependency between entities and obtain the relationship set R; Align the identified entities with the external knowledge base; use the word embedding model to calculate the semantic similarity between entities; perform entity disambiguation and synonym normalization based on semantic similarity; The entity pair (e i ,e j ) and its relationship r ij Organized into triples (e i ,r ij ,e j ); verify the semantic rationality of the triple; complete the missing entity attributes based on external knowledge; Integrate the normalized triple set into a knowledge graph g(h); establish an index structure for entities and relationships; and save the semantic vector representations of entities and relationships. The formula expression of the knowledge graph g(h) is as follows: g(h)={(e i ,r ij ,e j )|e i ,e j ∈E,r ij ∈R} Where g(h) is the knowledge graph generated by the Transformer model; E is the entity set; R is the relationship set; (e i ,r ij ,e j ) represents entity e i and e j Through the relationship ij The triplet composed of .
5. The enhanced document generation and retrieval method based on knowledge graph according to claim 1, characterized in that: The update strategy of the knowledge graph is: Perform a multi-dimensional credibility assessment on the knowledge units extracted from the newly added document segments. By calculating the TF-IDF weight, the semantic consistency score based on the pre-trained word embedding model, and the source reliability score, a comprehensive credibility score (Score1) is calculated for each attribute value and relationship triple. Calculate the entity name similarity S using the character-level edit distance algorithm c , calculate semantic similarity S based on pre-trained word embedding model s , calculate the structural similarity S by the attribute set overlap and the relationship triple overlap r , the similarities of these three dimensions are combined according to the weights to obtain the comprehensive matching score Score2, Based on the comprehensive matching score Score2, two thresholds τ1 and τ2 are set, τ1>τ2, and the following judgment strategy is used for processing: When Score2 is greater than τ1, it is considered a complete match, and the Score1 scores of each attribute value and relationship triple are compared respectively; the attribute value and relationship triple with the higher Score1 score are retained, and the replaced knowledge is saved as a historical version; When Score2 is between τ1 and τ2, it is considered a partial match. The alignable attribute values and relationship triples are compared one by one, and the attribute values and relationship triples with higher Score1 scores are retained. For newly added attribute values and relationship triples, they are allowed to be added when their Score1 exceeds the preset matching threshold. When Score2 is less than or equal to τ2, it is determined to be a new entity. The comprehensive credibility score Score1 of the new entity is checked. When Score1 exceeds the preset new threshold, it is allowed to be added to the knowledge graph and the initial credibility benchmark of the new entity is recorded. At the same time, the timestamp, operation type, change content, and related Score1 and Score2 scores are recorded for all update operations.
6. The enhanced document generation and retrieval method based on knowledge graph according to claim 4, characterized in that: Step S4 includes: S41, record the document segments of the input heterogeneous documents as a segment set {s1, s2, ..., s n }, and assign a unique index identifier z={1,2,...,n} to each segment; use the BERT model to extract feature vectors for each document segment, and obtain the feature vector set d(z)={d1,d2,...,d n }, where d i ∈R d represents the eigenvector of segment i; S42. At the same time, the BERT model is used to vectorize the entities in the knowledge graph to obtain the entity vector e k ∈R d ; S43. Calculate the semantic relevance between entities and document segments through maximum inner product search: Yes(and k ,d i )=and k T ·d i Where, e k Represents the entity vector in the knowledge graph, d i Feature vector representing the document segment; S44. For each entity e k , based on the semantic relevance calculation results, determine the most relevant document segment index: Where z k * Represents entity e k The most relevant document segment index; S45. Construct a mapping set of entity-document segments: A={(e k ,with k * )|e k ∈g(h),z k * ∈d(z)} Where A represents the set of mapping relationships between entities and document segment indexes.
7. The enhanced document generation and retrieval method based on knowledge graph according to claim 1, characterized in that: The retriever and generator include a pre-training process, and the objective function during pre-training is: Where, represents the expectation on the training dataset D; ψ, φ are the parameters of the retriever and generator respectively; P(y|x) is the joint probability representation of the retriever and generator; x is the input query sequence; y is the target generated sequence; q(h,z|x) is the conditional probability of the retriever output; ∏ represents multiplication; n is the length of the generated sequence.
Citation Information
Patent Citations
Construction method of RAG system based on Graph
CN118503407A
Apparatus and method for automatic object segmentation for background composition
KR1020130075636A
Cited By
Artificial intelligence data base vector graph combined index and consistency storage method and system
CN122757297A