A legal context intelligent retrieval and generation method based on natural language processing

By combining a pre-trained language model and a legal knowledge graph into a multi-dimensional indexing system, the problem of inconsistency between complex semantic understanding and generated content in existing legal information processing systems has been solved. This has enabled accurate retrieval and structured generation, improving the accuracy of legal queries and the reliability of generated content.

CN121278075BActive Publication Date: 2026-05-29BEIJING SANLIUWU LEGAL CULTURE DEVELOPMENT CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIJING SANLIUWU LEGAL CULTURE DEVELOPMENT CO LTD
Filing Date
2025-09-29
Publication Date
2026-05-29

AI Technical Summary

Technical Problem

Existing legal information processing systems struggle to accurately understand user intent in natural language expressions with complex semantics and long-distance dependencies. Furthermore, the generated content lacks structured output and fails to effectively capture the key alignment relationship between queries and legal texts, resulting in inaccurate search results and inconsistent generated content.

Method used

By employing the pre-trained language model RoBERTa and a legal knowledge graph, combined with a multi-dimensional indexing system, and through semantic vector recall, keyword recall, cross-encoder ranking, and LLaMA model generation, a closed-loop processing from natural language query to legal analysis is achieved, including semantic understanding, evidence retrieval, and structure generation.

Benefits of technology

It achieves accurate understanding and authoritative retrieval of legal information, generates content with a complete and structured format, and possesses interpretability and reliability, thereby improving the accuracy of search results and the compliance of generated content.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121278075B_ABST
    Figure CN121278075B_ABST
Patent Text Reader

Abstract

The application discloses a kind of legal context intelligent retrieval and generation method based on natural language processing, comprising the following steps: step one: collection legal text;Step two: construct multidimensional index system;Step three: form query element set;Step four: in the first stage retrieval, obtain the first stage candidate set;Step five: in the second stage retrieval, first stage candidate set and natural language query input cross-coder, obtain target evidence set, when element coverage is insufficient, generate incremental sub-query and restart retrieval, until meet requirements or reach maximum round;Step six: target evidence set and natural language query are jointly input LLaMA model, output structured result.The application has the advantages of multi-source information fusion, element alignment precision, reference verification completeness and risk prompt clear.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of natural language processing and legal artificial intelligence, and in particular to a method for intelligent retrieval and generation of legal context based on natural language processing. Background Technology

[0002] Existing legal information processing systems typically rely on keyword matching and rule templates to parse and retrieve natural language queries input by users. These methods primarily depend on manually constructed rule bases, thesauruses, and retrieval templates for matching operations. While they can support the connection between user intent and relevant legal provisions to some extent, they often struggle to accurately understand the true intent of users' queries when faced with complex semantics, long-distance dependencies, or ambiguous natural language expressions. Traditional retrieval methods, often centered on inverted indexes, can only perform literal text matching, failing to achieve deep semantic understanding and relevance assessment. This results in narrow recall coverage, poor ranking performance, and an inability to meet users' precise query needs in complex legal scenarios.

[0003] In recent years, with the development of pre-trained language models, some studies have begun to introduce models such as BERT and RoBERTa to perform semantic modeling and representation learning of legal texts, attempting to improve semantic matching capabilities through vectorized representation. Existing methods often only use single vector similarity for coarse ranking, lacking deep interaction modeling mechanisms with user queries, and failing to effectively capture the key alignment relationships between queries and legal texts. Existing systems generally lack the ability to systematically model and apply legal knowledge such as the structure, citation logic, and scope of application of legal documents, neglecting the complex logical relationships within legal documents and the citations, conflicts, and applicability dependencies between higher and lower-level regulations. This results in a lack of effective judgment regarding the legal validity, applicability, and timeliness of search results, affecting the credibility of the final analysis results.

[0004] At the generation level, existing legal text generation methods based on large language models mostly remain at the stage of generating general content. They lack the ability to output structured information on core legal scenarios, such as citation basis, applicable conditions, and risk warnings, and have not established a collaborative mechanism with structured legal knowledge. This easily leads to problems such as generated content that does not conform to the facts, inconsistent citations, or incorrect judgments of validity. How to integrate natural language processing models, legal knowledge graphs, and multi-dimensional index structures to achieve a closed-loop process from query understanding and evidence retrieval to legal analysis and generation has become a key technical problem that current legal intelligent systems urgently need to solve. Summary of the Invention

[0005] One objective of this invention is to propose a legal context intelligent retrieval and generation method based on natural language processing. This invention fully integrates pre-trained language models, legal knowledge graphs, and structured feature indexing systems, and describes in detail the closed-loop processing flow from natural language query parsing, legal evidence fine-checking, element alignment modeling to legal analysis generation. It has the advantages of accurate query understanding, authoritative retrieval results, interpretable generated content, and complete structured output.

[0006] A method for intelligent retrieval and generation of legal context based on natural language processing according to an embodiment of the present invention includes the following steps:

[0007] Step 1: Collect legal texts and construct a legal knowledge graph;

[0008] Step 2: Construct a multi-dimensional index system. Input legal text units into the RoBERTa model to generate semantic vectors and write them into the vector index. Construct an inverted index based on the BM25 algorithm and generate an element index based on structured annotation information.

[0009] Step 3: Receive the natural language query input from the user and form a set of query elements;

[0010] Step 4: In the first stage of retrieval, semantic vectors are used to perform semantic recall in the vector index, and query elements are used to perform keyword recall in the inverted index to obtain the first stage candidate set;

[0011] Step 5: In the second stage of retrieval, the candidate set from the first stage and the natural language query are input into the cross encoder and sorted by combining the element alignment features of the legal knowledge graph to obtain the target evidence set. When the element coverage is insufficient, an incremental subquery is generated and the retrieval is restarted until the requirements are met or the maximum round is reached.

[0012] Step 6: Input the target evidence set and the natural language query into the LLaMA model and output structured results.

[0013] Optionally, the collection of legal texts and the construction of a legal knowledge graph specifically involves:

[0014] Collect legal texts, including legal provisions, judicial cases, and contract terms;

[0015] The legal text is preprocessed, including cleaning, segmentation and structured annotation, to generate legal text units. Each legal text unit carries structured annotation information, including number, level of validity, legal domain, effective period and citation identifier.

[0016] A legal knowledge graph is constructed based on legal text units. The legal knowledge graph includes clause nodes, case nodes, and contract clause nodes, and establishes reference relationships, application relationships, conflict relationships, and subordinate relationships between nodes.

[0017] Optionally, the construction of the multidimensional index system specifically includes:

[0018] Construct positive and negative sample pairs. Positive sample pairs consist of legal text units that are semantically relevant to natural language queries, while negative sample pairs consist of legal text units that are semantically irrelevant to natural language queries.

[0019] The natural language query is a query statement entered by the user;

[0020] Based on the positive and negative sample pairs, a contrastive learning method incorporating the InfoNCE loss function is used to...

[0021] The RoBERTa model was trained.

[0022] The legal text units are input into the trained RoBERTa model, and the legal text units are segmented and encoded using a word segmenter. The segmentation results are then input into the RoBERTa model for forward propagation. The output vector corresponding to the first and second positions of the segmentation results in the last layer of the RoBERTa model is extracted as the semantic vector of the legal text unit.

[0023] Write the semantic vector into the vector index;

[0024] An inverted index is built for legal text units based on the BM25 algorithm;

[0025] Based on the structured annotation information carried by legal text units, an element index is established in the legal knowledge graph. The element index includes subject, object, behavior, remedy, temporal effect and legal domain.

[0026] The multidimensional indexing system includes a vector index generated based on the RoBERTa model, an inverted index constructed based on the BM25 algorithm, and a feature index constructed based on structured annotation information.

[0027] Optionally, receiving the natural language query input by the user to form a query element set specifically involves:

[0028] Receive natural language queries from users;

[0029] The natural language query is segmented and encoded. The segmentation result is input into the trained RoBERTa model for forward propagation. The output vector corresponding to the first and second positions of the segmentation result in the last layer of the RoBERTa model is extracted to generate a semantic embedding vector.

[0030] The semantic embedding vector is input into the fully connected classifier in the RoBERTa model to perform a classification operation and output the query intent.

[0031] Sequence annotation is performed on the semantic embedding vectors corresponding to each word in the natural language query, and the query element labels corresponding to the word are predicted. The query element labels are arranged into a label sequence according to the word segmentation order. The query elements, including subject, object, behavior, time and legal domain, are restored from the label sequence according to the BIO annotation strategy.

[0032] Based on the preset legal terminology mapping relationship, the keywords contained in the natural language query are normalized and expanded to generate a normalized keyword set;

[0033] By combining the query elements with the normalized keyword set, a query element set is constructed.

[0034] Optionally, the step of performing semantic recall using semantic vectors in the vector index and keyword recall using query elements in the inverted index to obtain the first-stage candidate set is as follows:

[0035] Semantic recall is performed on the vector index based on semantic embedding vectors to obtain the first candidate set;

[0036] The semantic recall calculates the cosine similarity between the semantic embedding vector of the natural language query and the semantic vector of the legal text unit in the vector index, and returns the legal text unit with the highest similarity to form the first candidate set;

[0037] Based on normalized keywords, keyword recall is performed in the inverted index to obtain a second candidate set;

[0038] The keyword recall is an operation that performs a matching search in the inverted index based on normalized keywords to retrieve legal text units containing normalized keywords;

[0039] Merge the first candidate set and the second candidate set, and remove duplicates based on their numbers to form a candidate set;

[0040] The candidate set is filtered based on time validity and legal domain conditions to obtain the first-stage candidate set.

[0041] Optionally, the first-stage candidate set is cross-encoded with the natural language query input, and sorted using the element alignment features of the legal knowledge graph to obtain the target evidence set. When the element coverage is insufficient, an incremental subquery is generated and the retrieval is restarted until the requirements are met or the maximum number of rounds is reached. Specifically:

[0042] Each legal text unit in the first-stage candidate set is concatenated with a natural language query to form an input sequence, which is then input into a cross-encoder.

[0043] Perform correlation calculation on the encoding results output by the cross encoder to generate a first correlation score;

[0044] The correlation calculation involves extracting the vector corresponding to the first and second positions of the input sequence from the encoding result output by the cross encoder, performing a linear transformation on the fully connected layer, and combining it with an activation function to obtain the first correlation score.

[0045] Extract element information corresponding to the first-stage candidate set from the legal knowledge graph to form element alignment features;

[0046] The corresponding element information is based on the structured annotation information and legal terminology mapping relationship carried by the legal text unit; when the number, validity level, legal domain and element index of the legal text unit in the first stage candidate set matches the node or attribute in the legal knowledge graph, it is determined that the legal text unit in the first stage candidate set corresponds to the corresponding node.

[0047] The first relevance score is fused with the feature alignment feature to generate a fused ranking score;

[0048] The candidate set for the first stage is sorted according to the fusion ranking score to obtain the target evidence set;

[0049] When the feature coverage of the target evidence set is detected to be less than a preset threshold, an incremental subquery is generated based on the uncovered query features; the feature coverage is the ratio of the number of query features involved in the target evidence set to the total number of query features in the set.

[0050] The incremental subquery will be re-executed for the first and second stages of retrieval until the feature coverage is greater than or equal to the preset threshold or the preset maximum number of retrieval rounds is reached.

[0051] Optionally, the step of inputting the target evidence set and the natural language query into the LLaMA model and outputting structured results specifically involves:

[0052] Assign citation identifiers to each legal text unit in the target evidence set and concatenate them with natural language queries to form a generated context sequence;

[0053] The generated context sequence is input into the LLaMA model to perform autoregressive decoding, generating legal analysis text containing citation identifiers;

[0054] Perform citation consistency verification on legal analysis text: parse citation identifiers from legal analysis text and compare them with citation identifiers of the target evidence set to obtain a set of citations that pass the verification;

[0055] The validity of the cited set is reviewed: the validity is compared based on the number, level of validity, legal jurisdiction and effective period of the corresponding legal text unit, and a set of validity review results is formed.

[0056] Map the set of validity review results to knowledge graph features;

[0057] Perform word segmentation encoding on the legal analysis text generated by the LLaMA model and extract the analysis representation vector;

[0058] The knowledge graph features and analysis representation vectors are concatenated and then input into a graph attention network to perform forward propagation and predict risk categories.

[0059] The legal analysis text, the validated set of citations, and the risk categories are combined to output a structured result, which includes conclusions, citation basis, applicability descriptions, and risk warnings. Attached Figure Description

[0060] The accompanying drawings are provided to further illustrate the invention and form part of the specification. They are used in conjunction with embodiments of the invention to explain the invention and do not constitute a limitation thereof. In the drawings:

[0061] Figure 1 This is an overall flowchart of a legal context intelligent retrieval and generation method based on natural language processing proposed in this invention;

[0062] Figure 2 This is a schematic diagram illustrating the construction of a multidimensional index for a legal context intelligent retrieval and generation method based on natural language processing proposed in this invention.

[0063] Figure 3 This is a diagram illustrating the intelligent retrieval structure of a legal context intelligent retrieval and generation method based on natural language processing proposed in this invention. Detailed Implementation

[0064] The present invention will now be described in further detail with reference to the accompanying drawings. These drawings are simplified schematic diagrams, illustrating only the basic structure of the invention, and therefore only show the components relevant to the invention.

[0065] refer to Figure 1-3 A method for intelligent retrieval and generation of legal context based on natural language processing includes the following steps:

[0066] Step 1: Collect legal texts and construct a legal knowledge graph;

[0067] Step 2: Construct a multi-dimensional index system. Input legal text units into the RoBERTa model to generate semantic vectors and write them into the vector index. Construct an inverted index based on the BM25 algorithm and generate an element index based on structured annotation information.

[0068] Step 3: Receive the natural language query input from the user and form a set of query elements;

[0069] Step 4: In the first stage of retrieval, semantic vectors are used to perform semantic recall in the vector index, and query elements are used to perform keyword recall in the inverted index to obtain the first stage candidate set;

[0070] Step 5: In the second stage of retrieval, the candidate set from the first stage and the natural language query are input into the cross encoder and sorted by combining the element alignment features of the legal knowledge graph to obtain the target evidence set. When the element coverage is insufficient, an incremental subquery is generated and the retrieval is restarted until the requirements are met or the maximum round is reached.

[0071] Step 6: Input the target evidence set and the natural language query into the LLaMA model and output structured results.

[0072] This invention proposes a method for intelligent retrieval and generation of legal context based on natural language processing. The method sequentially involves legal text collection and knowledge graph construction, multi-dimensional index system generation, natural language query parsing, first-stage semantic and keyword joint recall, and second-stage relevance ranking and knowledge graph element alignment optimization based on cross-encoders to form a target evidence set. When element coverage is insufficient, incremental subqueries are generated to restart the retrieval. Finally, the target evidence set and user query are input into an LLaMA model to generate legal analysis text, outputting structured results including conclusions, citations, applicability explanations, and risk warnings. This achieves efficient, accurate, and highly interpretable intelligent legal services for legal retrieval scenarios.

[0073] In this embodiment, the collection of legal texts and the construction of a legal knowledge graph specifically refers to:

[0074] Collect legal texts, including legal provisions, judicial cases, and contract terms;

[0075] The legal text is preprocessed, including cleaning, segmentation and structured annotation, to generate legal text units. Each legal text unit carries structured annotation information, including number, level of validity, legal domain, effective period and citation identifier.

[0076] A legal knowledge graph is constructed based on legal text units. The legal knowledge graph includes clause nodes, case nodes, and contract clause nodes, and establishes reference relationships, application relationships, conflict relationships, and subordinate relationships between nodes.

[0077] A legal knowledge graph is constructed based on legal text units. The construction process includes:

[0078] Each unit of legal text is mapped to a corresponding clause node, case node, or contract clause node according to its source type.

[0079] Between the nodes, based on explicit reference markers, applicable descriptions, logical pointing statements, and structural hierarchy relationships in the text content, reference relationships, applicable relationships, conflict relationships, and subordinate relationships are extracted;

[0080] The nodes and their relationships are written into the graph structure to form a legal knowledge graph containing entity nodes, relationship edges, and structured attributes.

[0081] This invention performs cleaning, segmentation, and structured annotation operations on legal texts such as legal provisions, judicial cases, and contract clauses to extract legal text units carrying attributes such as number, level of validity, legal domain, effective period, and citation identifier. Based on the citation identifiers, applicable descriptions, logical pointing statements, and structural hierarchical relationships in the text content, a legal knowledge graph containing provision nodes, case nodes, and contract clause nodes is constructed. Citation relationships, applicable relationships, conflict relationships, and subordinate relationships are established between nodes, forming a legal knowledge network foundation with entity nodes, relationship edges, and structured attributes.

[0082] In this embodiment, the construction of the multidimensional index system specifically refers to:

[0083] Construct positive and negative sample pairs. Positive sample pairs consist of legal text units that are semantically relevant to natural language queries, while negative sample pairs consist of legal text units that are semantically irrelevant to natural language queries.

[0084] The natural language query is a query statement entered by the user;

[0085] Based on the positive and negative sample pairs, a contrastive learning method incorporating the InfoNCE loss function is used to...

[0086] The RoBERTa model was trained.

[0087] The legal text units are input into the trained RoBERTa model, and the legal text units are segmented and encoded using a word segmenter. The segmentation results are then input into the RoBERTa model for forward propagation. The output vector corresponding to the first and second positions of the segmentation results in the last layer of the RoBERTa model is extracted as the semantic vector of the legal text unit.

[0088] Write the semantic vector into the vector index;

[0089] An inverted index is built for legal text units based on the BM25 algorithm;

[0090] Based on the structured annotation information carried by legal text units, an element index is established in the legal knowledge graph. The element index includes subject, object, behavior, remedy, temporal effect and legal domain.

[0091] The multidimensional indexing system includes a vector index generated based on the RoBERTa model, an inverted index constructed based on the BM25 algorithm, and a feature index constructed based on structured annotation information, which are used to support semantics respectively.

[0092] Search, keyword search, and legal element alignment operations.

[0093] The multidimensional indexing system includes a vector index generated based on the RoBERTa model, an inverted index constructed based on the BM25 algorithm, and an element index constructed based on structured annotation information, which are used to support semantic retrieval, keyword retrieval, and legal element alignment operations, respectively.

[0094] This invention constructs positive and negative sample pairs, trains a RoBERTa model using a contrastive learning method incorporating the InfoNCE loss function, extracts semantic vectors from legal text units, and writes them into a vector index to achieve semantic retrieval support. Simultaneously, it establishes an inverted index based on the BM25 algorithm to support keyword retrieval, and combines the structured annotation information carried by legal text units to construct an element index in the legal knowledge graph, including content such as subject, object, behavior, remedy, temporal effect, and legal domain. Ultimately, this forms a multi-dimensional index system encompassing vector indexes, inverted indexes, and element indexes to support subsequent multi-modal retrieval and element alignment operations.

[0095] In this embodiment, receiving the natural language query input by the user and forming a query element set specifically involves:

[0096] Receive natural language queries from users;

[0097] The natural language query is segmented and encoded. The segmentation result is input into the trained RoBERTa model for forward propagation. The output vector corresponding to the first and second positions of the segmentation result in the last layer of the RoBERTa model is extracted to generate a semantic embedding vector.

[0098] The semantic embedding vector is input into the fully connected classifier in the RoBERTa model to perform a classification operation and output the query intent.

[0099] Sequence annotation is performed on the semantic embedding vectors corresponding to each word in the natural language query, and the query element labels corresponding to the word are predicted. The query element labels are arranged into a label sequence according to the word segmentation order. The query elements, including subject, object, behavior, time and legal domain, are restored from the label sequence according to the BIO annotation strategy.

[0100] The prediction involves performing a linear transformation and Softmax calculation on each word segmentation vector in the natural language query to obtain the predicted probability distribution of each tag category; the tag with the highest probability is selected as the query element tag corresponding to that word segmentation.

[0101] Based on the preset legal terminology mapping relationship, the keywords contained in the natural language query are normalized and expanded to generate a normalized keyword set;

[0102] By combining the query elements with the normalized keyword set, a query element set is constructed.

[0103] This invention receives natural language queries input by users, performs forward propagation on the segmented results using a trained RoBERTa model, extracts semantic representation vectors, and outputs the query intent. It then combines a fully connected classifier to classify the queries and identifies the corresponding query element tags through sequence labeling. Based on the BIO labeling strategy, it reconstructs query elements such as subject, object, behavior, time, and legal jurisdiction. Furthermore, it performs normalization expansion on keywords based on a pre-defined legal terminology mapping relationship, generating a normalized keyword set. Finally, it merges the query elements and normalized keywords to construct a query element set, providing semantic support for subsequent multimodal retrieval and element alignment.

[0104] In this embodiment, the step of performing semantic recall using semantic vectors in the vector index and keyword recall using query elements in the inverted index to obtain the first-stage candidate set is as follows:

[0105] Semantic recall is performed on the vector index based on semantic embedding vectors to obtain the first candidate set;

[0106] The semantic recall calculates the cosine similarity between the semantic embedding vector of the natural language query and the semantic vector of the legal text unit in the vector index, and returns the legal text unit with the highest similarity to form the first candidate set;

[0107] Based on normalized keywords, keyword recall is performed in the inverted index to obtain a second candidate set;

[0108] The keyword recall is an operation that performs a matching search in the inverted index based on normalized keywords to retrieve legal text units containing normalized keywords;

[0109] Merge the first candidate set and the second candidate set, and remove duplicates based on their numbers to form a candidate set;

[0110] The candidate set is filtered based on time validity and legal domain conditions to obtain the first-stage candidate set.

[0111] In this embodiment, the first-stage candidate set and the natural language query input are cross-encoded and sorted using the element alignment features of the legal knowledge graph to obtain the target evidence set. When the element coverage is insufficient, an incremental subquery is generated and the retrieval is restarted until the requirements are met or the maximum round is reached. Specifically:

[0112] Each legal text unit in the first-stage candidate set is concatenated with a natural language query to form an input sequence, which is then input into a cross-encoder. The cross-encoder is an interactive encoding model based on the RoBERTa model, which receives the input sequence, models the interaction between the natural language query and the legal text unit through a multi-head attention mechanism, and outputs the overall encoding result.

[0113] Perform correlation calculation on the encoding results output by the cross encoder to generate a first correlation score;

[0114] The correlation calculation involves extracting the vector corresponding to the first and second positions of the input sequence from the encoding result output by the cross encoder, performing a linear transformation on the fully connected layer, and combining it with an activation function to obtain the first correlation score.

[0115] Extract element information corresponding to the first-stage candidate set from the legal knowledge graph to form element alignment features;

[0116] The corresponding element information is based on the structured annotation information and legal terminology mapping relationship carried by the legal text unit; when the number, validity level, legal domain and element index of the legal text unit in the first stage candidate set matches the node or attribute in the legal knowledge graph, it is determined that the legal text unit in the first stage candidate set corresponds to the corresponding node.

[0117] The first relevance score is fused with the feature alignment feature to generate a fused ranking score;

[0118] The candidate set for the first stage is sorted according to the fusion ranking score to obtain the target evidence set;

[0119] When the feature coverage of the target evidence set is detected to be less than a preset threshold, an incremental subquery is generated based on the uncovered query features; the feature coverage is the ratio of the number of query features involved in the target evidence set to the total number of query features in the set.

[0120] The incremental subquery will be re-executed for the first and second stages of retrieval until the feature coverage is greater than or equal to the preset threshold or the preset maximum number of retrieval rounds is reached.

[0121] This invention constructs a two-stage retrieval mechanism. First, semantic recall is performed in the vector index using semantic vectors generated from natural language queries, and keyword recall is performed in the inverted index using normalized keywords to obtain a first-stage candidate set. Then, the first-stage candidate set is concatenated with the natural language query and input into a search engine based on...

[0122] The cross-encoder built by the RoBERTa model models the interaction between queries and legal text units through a multi-head attention mechanism, and outputs a relevance score. It further combines the element alignment features extracted from the legal knowledge graph for fusion and sorting to obtain the target evidence set. When the element coverage of the target evidence set is insufficient, it dynamically generates incremental subqueries and iteratively executes a two-stage retrieval, ultimately achieving efficient retrieval of legal evidence texts that are semantically relevant, structurally matched, and have sufficient element coverage.

[0123] In this embodiment, the step of inputting the target evidence set and the natural language query into the LLaMA model and outputting structured results specifically involves:

[0124] Assign citation identifiers to each legal text unit in the target evidence set and concatenate them with natural language queries to form a generated context sequence;

[0125] The generated context sequence is input into the LLaMA model to perform autoregressive decoding, generating legal analysis text containing citation identifiers;

[0126] The process of generating the legal analysis text specifically includes: arranging the generated context sequence according to a preset template, combining the natural language query and the text in the target evidence set, adding citation markers to each piece of evidence text according to the order of the evidence, and inserting separator markers;

[0127] Perform word segmentation encoding on the generated context sequence to obtain the input token sequence;

[0128] The input token sequence is input into a large language model based on LLaMA fine-tuning, and the maximum generation length, stop marker and decoding parameters are set.

[0129] Decoding is performed step by step using an autoregressive approach. At each decoding step, the next token is selected based on a conditional probability distribution. The selection is performed through beam search or kernel sampling.

[0130] During the decoding process, the corresponding reference identifier is output at the reference location according to the preset format;

[0131] Decoding ends when the generation reaches the stop mark or the maximum generation length, and the output is legal analysis text containing citations.

[0132] Perform citation consistency verification on legal analysis text: parse citation identifiers from legal analysis text and compare them with citation identifiers of the target evidence set to obtain a set of citations that pass the verification;

[0133] The validity of the cited set is reviewed: the validity is compared based on the number, level of validity, legal jurisdiction and effective period of the corresponding legal text unit;

[0134] The result of the validity comparison is to generate valid, invalid, or insufficient validity annotation information for each legal text unit in the citation set, forming a validity review result set;

[0135] The validity comparison includes: comparing the effective period in the citation set with the query time; if the query time falls within the effective period, it is determined to be valid; otherwise, it is determined to be invalid. It also includes comparing the validity hierarchy of the citation set; if there is a conflict, the validity priority is determined according to the hierarchy, and the legal text unit corresponding to the lower hierarchy is determined to have insufficient validity. Furthermore, it includes comparing the legal domain attribute of the citation set with the legal domain in the query element; if they match, it is determined to be valid; otherwise, it is determined to be invalid. Finally, it includes comparing the number of the citation set with the corresponding node number in the knowledge graph; if they match, it is determined to be valid; otherwise, it is determined to be invalid.

[0136] Map the set of validity review results to knowledge graph features;

[0137] Perform word segmentation encoding on the legal analysis text generated by the LLaMA model and extract the analysis representation vector;

[0138] The knowledge graph features and analysis representation vectors are concatenated and then input into a graph attention network to perform forward propagation and predict risk categories.

[0139] The legal analysis text, the validated set of citations, and the risk categories are combined to output a structured result, which includes conclusions, citation basis, applicability descriptions, and risk warnings.

[0140] In this invention, the structured results are designed to include four parts: conclusions, cited references, applicability descriptions, and risk warnings.

[0141] The conclusion corresponds to the direct answer portion of the legal analysis text generated based on the LLaMA model. It is used to explicitly answer the legal questions raised by users and is the core output of the entire intelligent retrieval and generation process.

[0142] The basis for citation corresponds to the result of the consistency check and is used to indicate the specific legal provisions, judicial cases or contractual clauses on which the conclusion is based, ensuring that the source of the conclusion is traceable and meets the basic requirements of legal search scenarios.

[0143] The applicability statement corresponds to the logical explanation section in the legal analysis text. It explains why the cited provisions or cases are applicable to the current query, reflects the reasoning chain in the retrieval and generation process, and makes the results understandable.

[0144] Risk warnings correspond to the results of validity review and risk category prediction. They are used to alert users to potential risks when there are invalid references, insufficient validity levels, or conflicting scopes of application, thereby ensuring the reliability and compliance of system outputs.

[0145] This invention proposes an intelligent legal context generation method that combines large language models and knowledge graph reasoning. Specifically, it assigns citation identifiers to each legal text unit in the target evidence set and concatenates them with natural language queries to form a context sequence. This sequence is then input into a fine-tuned LLaMA model to perform autoregressive decoding and generate legal analysis text containing citation identifiers. The generated text undergoes citation consistency verification and validity review, outputting a validity label for each citation and mapping it to graph features. Furthermore, the graph features and analysis representation vectors are fused into an input graph attention network to predict the legal risk categories involved in the text. Finally, the analysis text, citation basis, and risk results are output in a structured manner, constructing a structured result including four elements: conclusion, citation basis, applicability explanation, and risk warning. This ensures that the generated content has legal basis, applicable interpretation, and risk reminders, meeting the requirements of compliance, traceability, and understandability.

[0146] Example 1:

[0147] To verify the feasibility of this invention in practice, it was applied to the intelligent question-and-answer module of a government and enterprise legal service platform. The goal is to improve the accuracy, interpretability, and completeness of the citation basis of the results returned by the system when users conduct natural language legal consultations, and to solve the problems existing in the current legal retrieval system, such as "misunderstanding of query intent", "lack of contextual support for search provisions", "inability to verify the validity of cited content", and "lack of risk warnings in the results".

[0148] In actual use of this platform, users often input questions with strong semantic differences in natural language, such as "If the company doesn't pay compensation, is my non-compete agreement still valid?" or "If my landlord refuses to return my deposit, which law should I use to sue?" These questions are semantically rich and have flexible sentence structures, making it difficult for traditional keyword-based retrieval systems to accurately match legal provisions or cases. This invention employs a structured query element extraction and multi-dimensional index recall mechanism. Upon receiving a user query, the system encodes the natural language question using a RoBERTa model, extracting four types of query elements: "Subject: Company and Employee," "Behavior: Signing a Non-Compete Agreement," "Condition: Failure to Pay Compensation," and "Time: Current." The intent is identified as "Determination of the Validity of the Non-Compete Agreement." Subsequently, the system performs semantic recall and keyword recall in the vector index and inverted index respectively, fusing them to generate a first-stage candidate set. After passing through a cross-encoder and a legal knowledge graph element alignment mechanism, the candidate set is further fused and sorted to obtain a target evidence set covering the query elements.

[0149] The system concatenates the target evidence set with the original query to form a generated context sequence, which is then input into a fine-tuned LLaMA model for autoregression generation, resulting in a structurally complete and clearly cited legal analysis text. For example: "According to Articles 23 and 24 of the Labor Contract Law, employers and employees agreeing to non-competition agreements should provide economic compensation. If compensation is not paid, the non-competition agreement may not be legally valid." The system then performs consistency checks on the citations in the analysis text to confirm their existence within the target evidence set and further verifies their validity, identifying whether the article is still valid in the current time period, whether it falls within the same legal jurisdiction, and whether there are conflicting articles with higher validity. If the citation is deemed invalid or expired, the system will mark it as "invalid" and output a risk warning: "The cited article expired in July 2021. It is recommended to refer to the latest article or supplementary evidence."

[0150] The generated legal analysis text, verified cited provisions, and risk warnings after validity checks are combined into a structured output. This output not only clearly indicates the conclusion to the user but also marks the cited provisions, the reasons for their application, and potential legal risks, enabling the user to make subsequent decisions within the bounds of legality and compliance. Specific experimental data are shown in Table 1.

[0151] Table 1. Comparison of Application Performance of the Legal Question-Answer System Based on the Invention

[0152]

[0153]

[0154] During the platform's trial operation, comparing the system performance before and after the deployment of this invention, Table 1 shows that: the average user query response time improved from 3.6 seconds to 2.4 seconds; the user satisfaction score increased from 82.1% to 93.6%; the coverage of the system retrieving valid clauses and using them to generate results increased from 61.3% to 90.4%; the accuracy rate of the consistency verification of cited clauses remained above 97%; the average number of cited bases per legal analysis text increased by 1.8; and the proportion of risk warnings included in the structured results increased from 0 to 27.1%, effectively avoiding misleading results caused by the use of invalid clauses.

[0155] The results verify the effectiveness and engineering feasibility of the multidimensional index construction, knowledge graph fusion sorting, citation verification and validity review mechanism adopted in this invention in actual legal retrieval and question answer generation, especially in dealing with semantically complex and cross-legal natural language legal issues, which have significant advantages.

[0156] The above description is only a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any equivalent substitutions or modifications made by those skilled in the art within the scope of the technology disclosed in the present invention, based on the technical solution and inventive concept of the present invention, should be covered within the scope of protection of the present invention.

Claims

1. A method for intelligent retrieval and generation of legal context based on natural language processing, characterized in that, Includes the following steps: Step 1: Collect legal texts and construct a legal knowledge graph, specifically: Collect legal texts, including legal provisions, judicial cases, and contract terms; The legal text is preprocessed, including cleaning, segmentation and structured annotation, to generate legal text units. Each legal text unit carries structured annotation information, including number, level of validity, legal domain, effective period and citation identifier. A legal knowledge graph is constructed based on legal text units. The legal knowledge graph includes clause nodes, case nodes, and contract clause nodes. Each node carries a node number, node validity level, node legal domain, and node element attributes. Step 2: Construct a multi-dimensional index system. Input legal text units into the RoBERTa model to generate semantic vectors and write them into the vector index. Construct an inverted index based on the BM25 algorithm and generate an element index based on structured annotation information. Step 3: Receive the natural language query input from the user and form a set of query elements; Step 4: In the first stage of retrieval, semantic vectors are used to perform semantic recall in the vector index, and query elements are used to perform keyword recall in the inverted index to obtain the first stage candidate set; Step 5: In the second stage of retrieval, the candidate set from the first stage and the natural language query are input into the cross encoder and sorted by combining the element alignment features of the legal knowledge graph to obtain the target evidence set. When the element coverage is insufficient, an incremental subquery is generated and the retrieval is restarted until the requirements are met or the maximum round is reached. Step Six: Input the target evidence set and the natural language query into the LLaMA model to output structured results, specifically: Assign citation identifiers to each legal text unit in the target evidence set and concatenate them with natural language queries to form a generated context sequence; The generated context sequence is input into the LLaMA model to perform autoregressive decoding, generating legal analysis text containing citation identifiers; Perform citation consistency verification on legal analysis text: parse citation identifiers from legal analysis text and compare them with citation identifiers of the target evidence set to obtain a set of citations that pass the verification; The validity of the cited set is reviewed: the validity is compared based on the number, level of validity, legal jurisdiction and effective period of the corresponding legal text unit, and a set of validity review results is formed. Map the set of validity review results to knowledge graph features; Perform word segmentation encoding on the legal analysis text generated by the LLaMA model and extract the analysis representation vector; The knowledge graph features and analysis representation vectors are concatenated and then input into a graph attention network to perform forward propagation and predict risk categories. The legal analysis text, the validated set of citations, and the risk categories are combined to output a structured result, which includes conclusions, citation basis, applicability descriptions, and risk warnings.

2. The method for intelligent retrieval and generation of legal context based on natural language processing according to claim 1, characterized in that, The construction of the multi-dimensional index system specifically involves: Construct positive and negative sample pairs. Positive sample pairs consist of legal text units that are semantically relevant to natural language queries, while negative sample pairs consist of legal text units that are semantically irrelevant to natural language queries. The natural language query is a query statement entered by the user; Based on the positive and negative sample pairs, the RoBERTa model is trained using a contrastive learning method that includes the InfoNCE loss function; The legal text units are input into the trained RoBERTa model, and the legal text units are segmented and encoded using a word segmenter. The segmentation results are then input into the RoBERTa model for forward propagation. The output vector corresponding to the first and second positions of the segmentation results in the last layer of the RoBERTa model is extracted as the semantic vector of the legal text unit. Write the semantic vector into the vector index; An inverted index is built for legal text units based on the BM25 algorithm; Based on the structured annotation information carried by legal text units, an element index is established in the legal knowledge graph. The element index includes subject, object, behavior, remedy, temporal effect and legal domain. The multidimensional indexing system includes a vector index generated based on the RoBERTa model, an inverted index constructed based on the BM25 algorithm, and a feature index constructed based on structured annotation information.

3. The method for intelligent retrieval and generation of legal context based on natural language processing according to claim 1, characterized in that, The process of receiving natural language queries input by the user and forming a set of query elements specifically includes: Receive natural language queries from users; The natural language query is segmented and encoded. The segmentation result is input into the trained RoBERTa model for forward propagation. The output vector corresponding to the first and second positions of the segmentation result in the last layer of the RoBERTa model is extracted to generate a semantic embedding vector. Sequence annotation is performed on the semantic embedding vectors corresponding to each word in the natural language query, and the query element labels corresponding to the word are predicted. The query element labels are arranged into a label sequence according to the word segmentation order. The query elements, including subject, object, behavior, time and legal domain, are restored from the label sequence according to the BIO annotation strategy. Based on the preset legal terminology mapping relationship, the keywords contained in the natural language query are normalized and expanded to generate a normalized keyword set; By combining the query elements with the normalized keyword set, a query element set is constructed.

4. The method for intelligent retrieval and generation of legal context based on natural language processing according to claim 1, characterized in that, The process involves performing semantic recall using semantic vectors in the vector index and keyword recall using query elements in the inverted index to obtain the first-stage candidate set, specifically as follows: Semantic recall is performed on the vector index based on semantic embedding vectors to obtain the first candidate set; The semantic recall calculates the cosine similarity between the semantic embedding vector of the natural language query and the semantic vector of the legal text unit in the vector index, and returns the legal text unit with the highest similarity to form the first candidate set; Based on normalized keywords, keyword recall is performed in the inverted index to obtain a second candidate set; The keyword recall is an operation that performs a matching search in the inverted index based on normalized keywords to retrieve legal text units containing normalized keywords; Merge the first candidate set and the second candidate set, and remove duplicates based on their numbers to form a candidate set; The candidate set is filtered based on time validity and legal domain conditions to obtain the first-stage candidate set.

5. The method for intelligent retrieval and generation of legal context based on natural language processing according to claim 1, characterized in that, The process involves cross-encoding the first-stage candidate set with the natural language query input, sorting them using the element alignment features of the legal knowledge graph, and obtaining the target evidence set. When the element coverage is insufficient, an incremental subquery is generated and the retrieval is restarted until the requirements are met or the maximum round is reached. Specifically: Each legal text unit in the first-stage candidate set is concatenated with a natural language query to form an input sequence, which is then input into a cross-encoder. Perform correlation calculation on the encoding results output by the cross encoder to generate a first correlation score; The correlation calculation involves extracting the vector corresponding to the first and second positions of the input sequence from the encoding result output by the cross encoder, performing a linear transformation on the fully connected layer, and combining it with an activation function to obtain the first correlation score. Extract element information corresponding to the first-stage candidate set from the legal knowledge graph to form element alignment features; The process of forming element alignment features is as follows: based on the number, validity level, legal domain and element index carried by the legal text units in the first-stage candidate set, the corresponding node number, node validity level, node legal domain and node element attributes are retrieved in the legal knowledge graph, the matching results are mapped into vectorized representations, and aligned with the first-stage candidate set one by one to form element alignment features. The first relevance score is fused with the feature alignment feature to generate a fused ranking score; The candidate set for the first stage is sorted according to the fusion ranking score to obtain the target evidence set; When the feature coverage of the target evidence set is detected to be less than a preset threshold, an incremental subquery is generated based on the uncovered query features. The element coverage is the ratio of the number of query elements involved in the target evidence set to the total number of query element sets; The incremental subquery will be re-executed for the first and second stages of retrieval until the feature coverage is greater than or equal to the preset threshold or the preset maximum number of retrieval rounds is reached.