An aspect-level sentiment analysis method and device based on contrastive learning
By generating high-quality semantic information based on contrastive learning, this method solves the problem of sparse data labeling in existing technologies, achieves efficient joint detection of target, aspect, and sentiment, and improves the effectiveness of sentiment analysis.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHONGQING UNIV
- Filing Date
- 2023-02-16
- Publication Date
- 2026-04-28
AI Technical Summary
Existing aspect-level sentiment analysis methods perform poorly when dealing with sparse data labeling, are unable to generate high-quality text representations, have weak transferability, and their complex network models result in insufficient practicality.
We employ a contrastive learning-based approach. By generating multiple prompt sentence pairs, we enhance the sentences to be analyzed using question and answer prompt sentences. We combine pre-trained encoding layers and activation function layers for sentiment analysis and use contrastive learning to train and generate high-quality semantic information, thus alleviating the problem of sparse labeled data.
It improves the performance of joint target-aspect-sentiment detection, generates high-quality text representations, effectively alleviates the problem of sparse labeled data, and improves the effect of sentiment analysis.
Smart Images

Figure CN116257630B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of natural language processing and artificial intelligence, and in particular to an aspect-level sentiment analysis method and apparatus based on contrastive learning. Background Technology
[0002] With the interdisciplinary development of machine learning and deep learning, text sentiment analysis has become a research hotspot and challenge in the field of natural language processing. Among these, aspect-level sentiment analysis (ABSA) has received increasing attention due to its ability to provide fine-grained sentiment analysis. Traditional ABSA aims to detect the sentiment of a corresponding target in a sentence. For example, given the sentence "Saul is the best restaurant on Smith Street and in Brooklyn," which contains a target "Saul" and a predefined aspect "RESTAURANT#GENERAL," although both the target "Saul" and the aspect "RESTAURANT#GENERAL" correspond to positive sentiment, traditional ABSA can only detect two pairs: (Saul, positive) or (RESTAURANT#GENERAL, positive), failing to provide more fine-grained sentiment analysis. To further analyze the sentiment of different targets in different aspects of a sentence, a task called Target-Aspect-Sentence Detection (TASD) has been proposed. This task requires detecting (target, aspect, sentiment) triplets to complete the sentiment analysis.
[0003] Specifically, the target is a continuous sequence of entities appearing in the comments, the aspect is a predefined aspect category, and the sentiment polarity mainly includes three cases: Positive, Negative, and Neutral. For example, in the previous sentence, to satisfy the TASD task, the triple (Saul, RESTAURANT#GENERAL, positive) should be detected. Observation shows that sentiment depends not only on the target or aspect, but on both simultaneously to obtain accurate sentiment analysis. Therefore, modeling the dependency relationship between the target, aspect, and sentiment is a major challenge for the TASD task. Currently, the TASD task is mainly accomplished using pre-trained language models, which convert text data into computer-recognizable word vectors to build neural networks. For example, patent CN114298011A discloses a sentiment analysis method and device that integrates topic information, using existing topic models to generate topic information and integrating it into a pre-trained language model to improve classification ability. Although this method can complete the TASD task, it requires the extraction of topic information from the text data in advance, which leads to weak transferability of the method. For different scenarios, topic information needs to be extracted again, which is time-consuming. Most other methods rely on complex network models to perform ABSA. For example, patent CN113535904A uses graph neural networks to model individual words in a sentence and extract semantic features. Therefore, most currently available aspect-level sentiment analysis patents and literature are quite complex and lack practicality.
[0004] The Task-Aspect-Sentence Detection (TASD) is an important task in Aspect-Level Sentiment Analysis (ABSA), aiming to detect (object, aspect, sentiment) triples from sentences. However, most existing methods do not handle sparse data labeling well and cannot generate high-quality text representations, resulting in poor performance on the TASD task. Summary of the Invention
[0005] The present invention aims to at least solve the technical problems existing in the prior art, and provide a simple and effective aspect-level sentiment analysis method and device based on contrastive learning.
[0006] To achieve the above-mentioned objectives of the present invention, according to a first aspect of the present invention, the present invention provides an aspect-level sentiment analysis method based on contrastive learning, comprising: step S1, generating multiple prompt sentence pairs based on multiple preset aspect sentiment pairs, wherein a prompt sentence pair includes a question prompt sentence and an answer prompt sentence containing the same aspect sentiment pair; step S2, obtaining the statement to be analyzed, inputting the statement to be analyzed into an aspect-level sentiment analysis model based on contrastive learning to obtain analysis results, wherein the aspect-level sentiment analysis model based on contrastive learning includes: an enhancement module configured to combine the statement to be analyzed with question prompt sentences or answer prompt sentences from different prompt sentence pairs to obtain different enhanced statements to be analyzed; and a pre-trained encoding layer configured to perform word embedding processing on the enhanced statements to be analyzed. The word embedding processing result is encoded to obtain the sentence representation vector of the sentence to be analyzed and enhanced and the word vectors of all words; a first activation function layer is configured to obtain a first analysis result based on the sentence representation vector of the sentence to be analyzed and enhanced, the first analysis result being the judgment result of whether the sentence to be analyzed in the sentence to be analyzed and enhanced matches the aspect sentiment pair contained in the sentence to be analyzed and enhanced; a second activation function layer is configured to decode all word vectors of the sentence to be analyzed and enhanced to obtain a decoding sequence when the first analysis result is that the sentence to be analyzed in the sentence to be analyzed and enhanced matches the aspect sentiment pair contained in the sentence to be analyzed and enhanced, and mark the position of the target word that matches the aspect sentiment pair in the decoding sequence to obtain a marked sequence, and record the marked sequence as the second analysis result.
[0007] The above technical solution achieves joint detection of target-aspect-sentiment by using the first and second analysis results output by the aspect-level sentiment analysis model based on contrastive learning. The aspect-level sentiment analysis model based on contrastive learning is a model trained by contrastive learning, which can generate high-quality text representations to improve the performance of joint detection of target-aspect-sentiment. Furthermore, after the sentence to be analyzed is enhanced by using interrogative or answer prompts, high-quality semantic information (i.e., sentence representation vectors and word vectors) can be generated through a pre-trained encoding layer, which can effectively alleviate the problem of sparse labeled data and improve the sentiment analysis effect.
[0008] In a preferred embodiment of the present invention, the contrastive training process of the aspect-level sentiment analysis model based on contrastive learning includes: Step A, acquiring multiple training statements and aspect sentiment pairs matching the training statements, generating prompt sentence pairs based on the matching aspect sentiment pairs, combining the training statements with the question prompts and answer prompts of the generated prompt sentence pairs to obtain question-enhanced sentences and prompt-enhanced sentences, and forming a positive sample pair; Step B, inputting the positive sample pair into a pre-training encoding layer, performing word embedding processing on the positive sample pair, encoding the word embedding processing results to obtain the sentence representation vector of the question-enhanced sentence and the word vectors of all words in the positive sample pair, and obtaining the sentence representation vector of the answer-enhanced sentence and the word vectors of all words in the positive sample pair; Step C, inputting the sentence representation vector of the positive sample pair into... The first activation function layer obtains the first analysis results of the question-enhanced sentence and the first analysis results of the answer-enhanced sentence, respectively. The first task loss is obtained by comparing and learning the sentence representation vectors of the question-enhanced sentence and the answer-enhanced sentence in the positive sample pair through the first contrastive learning layer. Simultaneously, the word vectors of the positive sample pair are input into the second function activation module to obtain the second analysis results of the question-enhanced sentence and the second analysis results of the answer-enhanced sentence, respectively. The second task loss is obtained by comparing and learning the second analysis results of the question-enhanced sentence and the second analysis results of the answer-enhanced sentence in the positive sample pair through the second contrastive learning layer. In step D, the first task loss and the second task loss of all positive sample pairs participating in training are fused to obtain the total loss. If the total loss reaches the preset target, training is stopped. If the total loss does not reach the preset target, the next positive sample pair is input and steps B to D are repeated.
[0009] The above technical solution involves first establishing multiple positive sample pairs during model training. These pairs are then used for comparative learning to improve the similarity between positive samples, thereby obtaining better semantic features. During comparative learning, as the model optimizes, the feature distribution between the two samples in a positive sample pair decreases, resulting in better feature representation. To construct positive sample pairs that meet the requirements of comparative learning, this application proposes a data augmentation method based on dual prompts. A text sample is transformed into a question-enhanced sentence with a question prompt and an answer-enhanced sentence with an answer prompt. The model uses these two sentences as positive sample pairs for comparative learning. During the Dolby Learning model training process, by simultaneously optimizing the loss functions of the sentiment classification subtask (the task that obtains the first analysis result) and the sequence labeling subtask (the task that obtains the second analysis result), the model balances the output results of the two tasks, thereby effectively extracting aspect-level sentiment analysis triples.
[0010] In a preferred embodiment of the present invention, the first task loss of the i-th positive sample pair is expressed as: in, Let i represent the classification loss for the i-th positive sample pair. P1[CLS] represents the sentence representation vector of the question enhancement sentence x1 of the i-th positive sample pair, and P2[CLS] represents the sentence representation vector of the answer enhancement sentence x2 of the i-th positive sample pair; This represents the minimum two-way KL divergence between P1[CLS] and P2[CLS]. α represents the adjustable parameter for the first task.
[0011] The above technical solution: The first task loss function is obtained through the sentence representation vector output by the pre-trained encoding layer. The minimum bidirectional KL divergence of the two sentence representation vectors is added. The minimum bidirectional KL divergence can measure the distance between positive sample pairs, thereby enabling comparative learning. The smaller the bidirectional KL divergence, the more similar the features are. As the model is optimized, the feature distribution between the two samples becomes smaller and smaller, thus obtaining better feature representation.
[0012] In a preferred embodiment of the present invention, the second task loss for the i-th positive sample pair is expressed as: in, This represents the sequence labeling loss for the i-th positive sample pair. This indicates that the word vector of the question-enhanced sentence x1 predicts the sequence probability distribution of the true sequence T. β represents the sequence probability distribution of the predicted true sequence T from the word vectors of the enhanced response x2; β represents the adjustable parameter of the second task. express and The minimum two-way KL divergence between them
[0013] The above technical solution: The loss function for the second task is obtained through the sequence probability distribution. The minimum bidirectional KL divergence of the sequence probability distribution of positive sample pairs is added. The bidirectional KL divergence can measure the distance between positive sample pairs, thereby enabling comparative learning. The smaller the bidirectional KL divergence, the more similar the features are. As the model is optimized, the feature distribution between the two samples becomes smaller and smaller, thus obtaining better labeling results.
[0014] In a preferred embodiment of the present invention, the total loss is defined as: Where N represents the number of positive sample pairs participating in training.
[0015] The above technical solution: The total loss function can simultaneously optimize the loss functions of the sentiment classification subtask and the sequence labeling subtask, so that the model can balance the output results of the two tasks, thereby effectively extracting aspect-level sentiment analysis triples.
[0016] To achieve the above-mentioned objectives of the present invention, according to a second aspect of the present invention, an aspect-level sentiment analysis device based on contrastive learning is provided, comprising: a prompt sentence pair generation module, which generates multiple prompt sentence pairs based on multiple preset aspect sentiment pairs, wherein a prompt sentence pair includes a question prompt sentence and an answer prompt sentence containing the same aspect sentiment pair; a sentiment analysis module, which acquires a sentence to be analyzed and inputs the sentence to be analyzed into an aspect-level sentiment analysis model based on contrastive learning to obtain analysis results; wherein the aspect-level sentiment analysis model based on contrastive learning includes: an enhancement module, configured to combine the sentence to be analyzed with question prompt sentences or answer prompt sentences from different prompt sentence pairs to obtain different enhanced sentences to be analyzed; and a pre-trained encoding layer, configured to perform word embedding on the enhanced sentences to be analyzed. The process involves: encoding the word embedding results to obtain the sentence representation vector of the sentence to be analyzed and enhanced, and the word vectors of all words; a first activation function layer configured to obtain a first analysis result based on the sentence representation vector of the sentence to be analyzed and enhanced, wherein the first analysis result is a judgment result on whether the sentence to be analyzed in the sentence to be analyzed and enhanced matches the aspect sentiment pair contained in the sentence to be analyzed and enhanced; and a second activation function layer configured to decode all word vectors of the sentence to be analyzed and enhanced to obtain a decoding sequence when the first analysis result indicates that the sentence to be analyzed and enhanced matches the aspect sentiment pair contained in the sentence to be analyzed and enhanced, thereby obtaining a labeled sequence, and recording the labeled sequence as the second analysis result. Attached Figure Description
[0017] Figure 1 This is a flowchart illustrating an aspect-level sentiment analysis method in a preferred embodiment of the present invention.
[0018] Figure 2 This is a schematic diagram of the structure of an aspect-level sentiment analysis model based on contrastive learning in a preferred embodiment of the present invention;
[0019] Figure 3 This is a schematic diagram of a data augmentation method based on dual prompts in a preferred embodiment of the present invention;
[0020] Figure 4 This is a schematic diagram of the training of an aspect-level sentiment analysis model based on contrastive learning in a preferred embodiment of the present invention. Detailed Implementation
[0021] Embodiments of the present invention are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention.
[0022] In the description of this invention, it should be understood that the terms "longitudinal", "lateral", "up", "down", "front", "rear", "left", "right", "vertical", "horizontal", "top", "bottom", "inner", "outer", etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are only for the convenience of describing this invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this invention.
[0023] In the description of this invention, unless otherwise specified and limited, it should be noted that the terms "installation", "connection" and "linking" should be interpreted broadly. For example, they can refer to mechanical or electrical connections, or internal connections between two components. They can be direct connections or indirect connections through an intermediate medium. Those skilled in the art can understand the specific meaning of the above terms according to the specific circumstances.
[0024] This invention discloses an aspect-level sentiment analysis method based on contrastive learning. In a preferred embodiment, such as... Figure 1 As shown, this sentiment analysis method includes:
[0025] Step S1: Generate multiple prompt sentence pairs based on preset multiple aspect sentiment pairs. A prompt sentence pair includes a question prompt sentence and an answer prompt sentence that contain the same aspect sentiment pair.
[0026] An aspect refers to the collective term for multiple user-defined objectives. Multiple aspects can be pre-defined, such as environment general, service general, food quality, and food prices. Each aspect includes multiple objectives. For example, the environment general aspect could include objectives such as flooring, tables and chairs, sound, noise, and lighting. The actual words for each objective may take various forms; for example, the objective for flooring could be tile, wood flooring, or cement flooring. Service general could include objectives such as waiter attitude, promotional offers, and speed of service. Affectiveness can include multiple categories, with more categories allowed for higher granularity requirements. For example, if affectiveness includes three categories, they could be positive, negative, and neutral. An aspect-affectiveness pair is a combination of aspect and affectiveness, such as good environment, good service, poor food quality, etc. An aspect-affectiveness pair is aspect + affectiveness, such as service general positive, food quality negative, etc.
[0027] Preferably, multiple sentiment pairs are pre-defined and are related to the domain of the statement to be analyzed. Multiple prompt pairs are generated based on these pre-defined sentiment pairs. Specifically, one sentiment pair corresponds to one prompt pair, which includes a question prompt and an answer prompt containing that sentiment pair.
[0028] Preferably, the question prompt sentence is generated through a question prompt function. Specifically, the execution process of generating the question prompt sentence using the question prompt function is as follows: The aspect and sentiment in the aspect-sentiment pair are substituted into the corresponding positions in the question prompt sentence template, which is: Does the sentiment {} belong to the aspect {}. In the question prompt sentence template, brackets {} represent positions, where the first bracket {} from left to right represents the sentiment position, and the second bracket {} represents the aspect position. For example, given a sentence: "Skip this restaurant, it is a big disappointment", its predefined aspect is "RESTAURANT#GENERAL", and its sentiment is "negative". The generated question prompt sentence would then be: "Does the sentiment negative belong to the aspect RESTAURANT#GENERAL".
[0029] And / or, preferably, generate response prompts using a response prompt function. Specifically, the execution process of generating response prompts using the response prompt function is as follows: Substitute the aspect and sentiment from the aspect-sentiment pair into the corresponding aspect and sentiment positions in the response prompt template, respectively. The response prompt template is: The sentiment of the aspect {} is {}. In the response prompt template, brackets {} represent positions, where the first bracket {} from left to right represents the aspect position, and the second bracket {} represents the sentiment position. For example, given a sentence: "Skip this restaurant, it is a big disappointment," with a predefined aspect of "RESTAURANT#GENERAL" and a sentiment of "negative," the generated response prompt is "The sentiment of the aspect RESTAURANT#GENERAL is negative." The interrogative prompt "Does the sentiment negative belong to the aspect RESTAURANT#GENERAL" and the response prompt "The sentiment of the aspect RESTAURANT#GENERAL is negative" form a prompt pair.
[0030] Step S2: Obtain the statement to be analyzed. Input the statement to be analyzed into an aspect-level sentiment analysis model based on contrastive learning to obtain the analysis results, such as... Figure 2 As shown, aspect-level sentiment analysis models based on contrastive learning include:
[0031] The enhancement module is configured to combine the statement to be analyzed with interrogative or response prompts from different prompt pairs to obtain different enhanced statements. After combining the statement to be analyzed with different prompt pairs, multiple enhanced statements are obtained. Subsequent pre-trained encoding layers and first activation function layers perform sentiment classification and sequence labeling on each enhanced statement. The statement to be analyzed can be combined with interrogative prompts or response prompts from prompt pairs. For example, the statement to be analyzed is: "Skip this restaurant, it is a big disappointment." Combined with an interrogative prompt, it becomes: "[CLS]Skip this restaurant, it is a big disappointment.[SEP]Does the sentiment negative belong to the aspect RESTAURANT#GENERAL.[SEP]"; combined with a response prompt, it becomes: "[CLS]Skip this restaurant, it is a big disappointment.[SEP]The sentiment of the aspect RESTAURANT#GENERAL is negative.[SEP]". [CLS] and [SEP] are special labels in the BERT pre-trained encoding model. [CLS] represents the overall semantic representation of the text, used for sentiment classification, while [SEP] indicates that when the input consists of two texts, this symbol is used to separate the two texts. This transforms the sentence to be analyzed into a sentence with hints, enhancing the semantic feature expression of the sentence.
[0032] The pre-trained encoding layer is configured to perform word embedding processing on the sentence to be analyzed and enhanced, and then encode the word embedding results to obtain the sentence representation vector and word vectors of all words in the sentence to be analyzed and enhanced. The pre-trained encoding layer preferably uses, but is not limited to, the BERT word embedding encoding model.
[0033] The first activation function layer is configured to obtain a first analysis result based on the sentence representation vector of the sentence to be analyzed and enhanced. The first analysis result is a judgment result indicating whether the sentence to be analyzed matches the aspect sentiment pair contained in the sentence to be analyzed and enhanced. The first activation function layer preferably uses, but is not limited to, the softmax function. The first activation function layer is used to detect whether there is a target in the sentence to be analyzed that corresponds to the aspect sentiment pair contained in the enhanced sentence. The specific output format can be "Yes / No" labels, such as... Figure 4 As shown.
[0034] The second activation function layer is configured to decode all word vectors of the sentence to be analyzed when the first analysis result indicates that the sentence to be analyzed matches the sentiment pair contained in the sentence to be analyzed (i.e., after the first activation function layer outputs the "Yes" label), thereby obtaining a decoded sequence. The layer then marks the positions of target words matching the sentiment pair within the decoded sequence to obtain a labeled sequence. This labeled sequence is recorded as the second analysis result. The labeled sequence is used to mark the targets to be extracted in the sentence to be analyzed, such as... Figure 4 In this context, T represents the target word position, and 0 represents a non-target word position. The second activation function layer preferably uses, but is not limited to, the softmax function.
[0035] In this embodiment, more preferably, when the judgment result output by the first activation function layer is a match between the statement to be analyzed and the aspect sentiment pair, it further includes outputting a triple consisting of the target word, aspect, and sentiment; wherein, the target word is the word in the statement to be analyzed whose position corresponds to the position of the target word marked in the marker sequence.
[0036] In a preferred embodiment, the aspect-level sentiment analysis model based on contrastive learning uses positive sample pairs for contrastive learning to improve the similarity between positive samples and thus obtain better semantic features. The contrastive training process of the aspect-level sentiment analysis model based on contrastive learning includes:
[0037] Step A involves acquiring multiple training statements and their matching sentiment pairs. Based on these sentiment pairs, prompt pairs are generated. The training statements are then combined with the question and answer prompts from the generated pairs to obtain question-enhanced and prompt-enhanced sentences. These two sentences form a positive sample pair. Specifically, for example... Figure 3 As shown, data augmentation is performed based on dual prompts. Each piece of text data (i.e., the training sentence) is combined with two types of prompts (question prompts and answer prompts) as model input to obtain two types of prompts, such as... Figure 3 The illustrated cue view uses the two augmented views as positive sample pairs. Each augmented data entry is encoded using the commonly used pre-trained model BERT, transforming the text into a vector form that can be processed by a computer. Compared to existing technologies, the data augmentation method based on dual cues proposed in this application can generate high-quality semantic information and alleviate the problem of sparse labeled data.
[0038] Step B: Input positive sample pairs into the pre-trained encoding layer. The pre-trained encoding layer performs word embedding processing on the positive sample pairs. The word embedding processing results are encoded to obtain the sentence representation vector of the question enhancement sentence and the word vector of all words in the positive sample pair, as well as the sentence representation vector of the answer enhancement sentence and the word vector of all words in the positive sample pair.
[0039] Step C involves inputting the sentence representation vectors of the positive sample pairs into the first activation function layer to obtain the first analysis results for the question-enhanced sentence and the answer-enhanced sentence, respectively. A first task loss is obtained by comparing and learning the sentence representation vectors of the question-enhanced sentence and the answer-enhanced sentence in the positive sample pair through a first contrastive learning layer. Simultaneously, the word vectors of the positive sample pairs are input into the second activation function module to obtain the second analysis results for the question-enhanced sentence and the answer-enhanced sentence, respectively. A second task loss is obtained by comparing and learning the second analysis results of the question-enhanced sentence and the answer-enhanced sentence in the positive sample pair through a second contrastive learning layer. Bidirectional KL divergence is used to measure the distance between positive samples for contrastive learning; a smaller bidirectional KL divergence indicates greater feature similarity between the positive sample pairs.
[0040] Step D involves fusing the first task loss and the second task loss of all positive sample pairs participating in training to obtain the total loss. If the total loss reaches a preset target, training stops. If the total loss does not reach the preset target, the next positive sample pair is input, and steps B through D are repeated. The preset target is preferably, but is not limited to, the increment of the total loss being less than a preset increment threshold.
[0041] This application transforms the TASD task into a binary classification sentiment classification subtask and a sequence labeling subtask. The former is used to detect whether there are (Yes and No) targets corresponding to aspect-sentiment in the sentence to be analyzed, and the latter is used to label the targets to be extracted after outputting the Yes label. Therefore, after obtaining x1 and x2, this application directly minimizes the bidirectional KL divergence between them. Preferably, the first task loss for the i-th positive sample pair is expressed as:
[0042]
[0043] in, Let i represent the classification loss for the i-th positive sample pair. P1[CLS] represents the sentence representation vector of the question enhancement sentence x1 of the i-th positive sample pair, and P2[CLS] represents the sentence representation vector of the answer enhancement sentence x2 of the i-th positive sample pair; This represents the minimum two-way KL divergence between P1[CLS] and P2[CLS]. α represents the adjustable parameter for the first task, which is a preset parameter, preferably in the range of [0, 1]. This application continues to use the two sentence representation vectors P1[CLS] and P2[CLS] to calculate the original loss function for the sentiment classification subtask. Finally, this application will and These are combined to form the final loss function for the sentiment classification subtask. D KL(P1[CLS]||P2[CLS]) represents the KL divergence from P1[CLS] to P2[CLS], D KL (P2[CLS]||P1[CLS]) represents the KL divergence from P2[CLS] to P1[CLS].
[0044] In this embodiment, for the sequence labeling subtask, after the interrogative and cue-enhanced sentences x1 and x2 are input into the BERT encoder, the word vector representation of each word can be obtained, defined as follows: and Where n represents the number of words. Then, define... and As the sequence probability distributions predicting the same true labeled sequence T, therefore, minimizing their bidirectional KL divergence is crucial. Defined as: express arrive KL divergence, express arrive The KL divergence. Continuing to utilize these two probability distributions... and To calculate the original loss function of the sequence labeling subtask
[0045]
[0046] Will and Together, they form the final loss function for the sequence labeling subtask. That is, the second task loss function. The second task loss for the i-th positive sample pair is expressed as:
[0047]
[0048] in, This represents the sequence labeling loss for the i-th positive sample pair. This indicates that the word vector of the question-enhanced sentence x1 predicts the sequence probability distribution of the true sequence T. β represents the sequence probability distribution of the predicted true sequence T by the word vector of the enhanced response sentence x2; β represents the adjustable parameter of the second task, which is a preset parameter, and the preferred value range is [0, 1]. express and The minimum two-way KL divergence between them
[0049] In this embodiment, multi-task learning is preferably employed, using the Adam optimization algorithm to simultaneously optimize the two sub-tasks, thereby balancing the model's output from both tasks and effectively extracting aspect-level sentiment analysis results. The total loss is defined as:
[0050] Where N represents the number of positive sample pairs participating in training.
[0051] This application proposes an aspect-level sentiment analysis model based on contrastive learning. Specifically, the model utilizes positive sample pairs in the data for contrastive learning, aiming to improve the similarity between positive samples and thus obtain better semantic features. KL divergence is used to measure the distance between positive samples; a smaller KL divergence indicates greater feature similarity. As the model optimizes, the feature distribution between two samples becomes smaller, resulting in better feature representation. Simultaneously, to construct positive sample pairs to meet the requirements of contrastive learning, this application proposes a data augmentation method based on dual prompts, transforming a text data into a sentence with a question prompt and a sentence with an answer prompt. The model uses these two sentences as positive sample pairs for contrastive learning.
[0052] The TASD task is an important part of ABSA, aiming to detect (object, aspect, sentiment) triples from sentences. However, most existing methods fail to generate high-quality text representations, resulting in poor performance on TASD tasks. Furthermore, handling high-quality labeled data lacking defects is a significant challenge. This application proposes a data augmentation method based on dual prompts, which generates different sentences from a question prompt view and an answer prompt view. A contrastive learning model for joint object-aspect-sentiment detection is also proposed, maximizing the consistency between the two prompt views to obtain high-quality text representations. Compared to existing techniques, this dual-prompt-based data augmentation method, proposed a decade ago, generates high-quality semantic information, mitigating the problem of sparse labeled data. Moreover, the contrastive learning model for joint object-aspect-sentiment detection proposed in this application detects (object, aspect, sentiment) triples more effectively than other methods.
[0053] This application also discloses an aspect-level sentiment analysis device based on contrastive learning, comprising: a prompt pair generation module, which generates multiple prompt pairs based on multiple preset aspect sentiment pairs, wherein a prompt pair includes a question prompt and an answer prompt containing the same aspect sentiment pair; a sentiment analysis module, which acquires the statement to be analyzed and inputs the statement to be analyzed into an aspect-level sentiment analysis model based on contrastive learning to obtain analysis results; the aspect-level sentiment analysis model based on contrastive learning includes: an enhancement module, configured to combine the statement to be analyzed with question prompts or answer prompts from different prompt pairs to obtain different enhanced statements to be analyzed; and a pre-trained encoding layer, configured to perform word embedding processing on the enhanced statements to be analyzed, and to process the word embedding processing results. The process involves encoding the sentence representation vector of the sentence to be analyzed and enhanced, as well as the word vectors of all its words. A first activation function layer is configured to obtain a first analysis result based on the sentence representation vector of the sentence to be analyzed and enhanced. This first analysis result is a judgment of whether the sentence to be analyzed in the sentence to be analyzed matches the sentiment pair contained in that sentence. A second activation function layer is configured to decode all word vectors of the sentence to be analyzed when the first analysis result indicates that the sentence to be analyzed in the sentence to be analyzed matches the sentiment pair contained in that sentence. This decoded sequence is then used to mark the positions of target words that match the sentiment pair in the decoded sequence, thus obtaining a marked sequence. This marked sequence is recorded as the second analysis result.
[0054] In the description of this specification, references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of the invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.
[0055] Although embodiments of the invention have been shown and described, those skilled in the art will understand that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the claims and their equivalents.
Claims
1. An aspect-level sentiment analysis method based on contrastive learning, characterized in that, include: Step S1: Generate multiple prompt sentence pairs based on preset multiple aspect sentiment pairs. Each prompt sentence pair includes a question prompt sentence and an answer prompt sentence that contain the same aspect sentiment pair. The multiple aspect sentiment pairs are related to the domain of the statement to be analyzed. Step S2: Obtain the statement to be analyzed, input the statement to be analyzed into an aspect-level sentiment analysis model based on contrastive learning to obtain the analysis results. The aspect-level sentiment analysis model based on contrastive learning includes: The enhancement module is configured to combine the statement to be analyzed with question prompts or answer prompts in different prompt pairs to obtain different enhanced statements to be analyzed; The pre-trained encoding layer is configured to perform word embedding processing on the sentence to be analyzed and enhanced, and to encode the word embedding processing results to obtain the sentence representation vector of the sentence to be analyzed and enhanced and the word vectors of all words. The first activation function layer is configured to obtain a first analysis result based on the sentence representation vector of the statement to be analyzed and enhanced. The first analysis result is a judgment result on whether the statement to be analyzed in the statement to be analyzed and enhanced matches the aspect sentiment pair contained in the statement to be analyzed and enhanced. The second activation function layer is configured to decode all word vectors of the statement to be analyzed to obtain a decoding sequence when the first analysis result is that the statement to be analyzed in the statement to be analyzed matches the aspect sentiment pair contained in the statement to be analyzed. The target word position that matches the aspect sentiment pair is marked in the decoding sequence to obtain a label sequence, and the label sequence is recorded as the second analysis result. The contrastive training process of the aspect-level sentiment analysis model based on contrastive learning includes: Step A: Obtain multiple training statements and the aspect sentiment pairs that match the training statements. Generate prompt sentence pairs based on the matched aspect sentiment pairs. Combine the training statements with the question prompts and answer prompts of the generated prompt sentence pairs to obtain question enhancement sentences and prompt enhancement sentences. The question enhancement sentences and prompt enhancement sentences form a positive sample pair. Step B: Input positive sample pairs into the pre-trained encoding layer. The pre-trained encoding layer performs word embedding processing on the positive sample pairs. The word embedding processing results are encoded to obtain the sentence representation vector of the question enhancement sentence and the word vector of all words in the positive sample pair, as well as the sentence representation vector of the answer enhancement sentence and the word vector of all words in the positive sample pair. Step C: Input the sentence representation vectors of the positive sample pairs into the first activation function layer to obtain the first analysis results of the question-enhanced sentence and the answer-enhanced sentence respectively; obtain the first task loss by comparing and learning the sentence representation vectors of the question-enhanced sentence and the answer-enhanced sentence in the positive sample pairs through the first contrastive learning layer. Simultaneously, the word vectors of the positive sample pairs are input into the second function activation module to obtain the second analysis results of the question-enhanced sentence and the second analysis results of the answer-enhanced sentence, respectively; the second task loss is obtained by comparing and learning the second analysis results of the question-enhanced sentence and the answer-enhanced sentence in the positive sample pairs through the second contrastive learning layer. Step D: Combine the first task loss and the second task loss of all positive sample pairs participating in training to obtain the total loss. If the total loss reaches the preset target, stop training. If the total loss does not reach the preset target, input the next positive sample pair and repeat steps B to D.
2. The aspect-level sentiment analysis method based on contrastive learning as described in claim 1, characterized in that, Generate question prompts using a question prompt function; And / or, generate answer prompts using an answer prompt function.
3. The aspect-level sentiment analysis method based on contrastive learning as described in claim 2, characterized in that, The execution process of generating question prompt sentences using the question prompt function is as follows: Substitute the aspect and sentiment in the aspect-sentiment pair into the corresponding positions of the aspect and sentiment in the question prompt sentence template, respectively. The question prompt sentence template is: Does the sentiment {} belong to the aspect {}.
4. The aspect-level sentiment analysis method based on contrastive learning as described in claim 2 or 3, characterized in that, The execution process of generating answer prompt sentences using the answer prompt function is as follows: Substitute the aspect and sentiment in the aspect-sentiment pair into the corresponding positions of the aspect and sentiment in the answer prompt sentence template, respectively. The answer prompt sentence template is: The sentiment of theaspect {} is {}.
5. The aspect-level sentiment analysis method based on contrastive learning as described in claim 4, characterized in that, When the judgment result output by the first activation function layer is that the statement to be analyzed matches the aspect sentiment pair, it also includes outputting a triple consisting of the target word, aspect, and sentiment; wherein, the target word is the word in the statement to be analyzed whose position corresponds to the position of the target word marked in the marker sequence.
6. The aspect-level sentiment analysis method based on contrastive learning as described in claim 1, characterized in that, No. The first task loss for a pair of positive samples is expressed as: ; in, Indicates the first The classification loss for a positive sample pair, , Indicates the first Enhanced interrogative sentences for each positive sample pair The sentence is represented by a vector. Indicates the first Enhancement sentences for responses to positive sample pairs The sentence represents a vector; express and The minimum two-way KL divergence between them , This indicates the adjustable parameters for the first task.
7. The aspect-level sentiment analysis method based on contrastive learning as described in claim 6, characterized in that, No. The second task loss for each positive sample pair is expressed as: ; in, Indicates the first Sequence labeling loss for a positive sample pair , Indicating an interrogative sentence Word vector prediction of true sequences The sequence probability distribution, Indicates an enhanced response sentence Word vector prediction of true sequences The sequence probability distribution; This indicates the adjustable parameters for the second task; express and The minimum two-way KL divergence between them .
8. The aspect-level sentiment analysis method based on contrastive learning as described in claim 7, characterized in that, Total loss is defined as: ; in, This indicates the number of positive sample pairs participating in the training.
9. An aspect-level sentiment analysis device based on contrastive learning, characterized in that, include: The prompt pair generation module generates multiple prompt pairs based on preset multiple aspect sentiment pairs. A prompt pair includes a question prompt and an answer prompt that contain the same aspect sentiment pair. The multiple aspect sentiment pairs are related to the domain of the statement to be analyzed. The sentiment analysis module acquires the statement to be analyzed and inputs it into an aspect-level sentiment analysis model based on contrastive learning to obtain the analysis results. The aspect-level sentiment analysis model based on contrastive learning includes: The enhancement module is configured to combine the statement to be analyzed with question prompts or answer prompts in different prompt pairs to obtain different enhanced statements to be analyzed; The pre-trained encoding layer is configured to perform word embedding processing on the sentence to be analyzed and enhanced, and to encode the word embedding processing results to obtain the sentence representation vector of the sentence to be analyzed and enhanced and the word vectors of all words. The first activation function layer is configured to obtain a first analysis result based on the sentence representation vector of the statement to be analyzed and enhanced. The first analysis result is a judgment result on whether the statement to be analyzed in the statement to be analyzed and enhanced matches the aspect sentiment pair contained in the statement to be analyzed and enhanced. The second activation function layer is configured to decode all word vectors of the statement to be analyzed to obtain a decoding sequence when the first analysis result is that the statement to be analyzed in the statement to be analyzed matches the aspect sentiment pair contained in the statement to be analyzed. The target word position that matches the aspect sentiment pair is marked in the decoding sequence to obtain a label sequence, and the label sequence is recorded as the second analysis result. The comparative training process of the aspect-level sentiment analysis model based on comparative learning includes: Step A: Obtain multiple training statements and the aspect sentiment pairs that match the training statements. Generate prompt sentence pairs based on the matched aspect sentiment pairs. Combine the training statements with the question prompts and answer prompts of the generated prompt sentence pairs to obtain question enhancement sentences and prompt enhancement sentences. The question enhancement sentences and prompt enhancement sentences form a positive sample pair. Step B: Input positive sample pairs into the pre-trained encoding layer. The pre-trained encoding layer performs word embedding processing on the positive sample pairs. The word embedding processing results are encoded to obtain the sentence representation vector of the question enhancement sentence and the word vector of all words in the positive sample pair, as well as the sentence representation vector of the answer enhancement sentence and the word vector of all words in the positive sample pair. Step C: Input the sentence representation vectors of the positive sample pairs into the first activation function layer to obtain the first analysis results of the question-enhanced sentence and the answer-enhanced sentence respectively; obtain the first task loss by comparing and learning the sentence representation vectors of the question-enhanced sentence and the answer-enhanced sentence in the positive sample pairs through the first contrastive learning layer. Simultaneously, the word vectors of the positive sample pairs are input into the second function activation module to obtain the second analysis results of the question-enhanced sentence and the second analysis results of the answer-enhanced sentence, respectively; the second task loss is obtained by comparing and learning the second analysis results of the question-enhanced sentence and the answer-enhanced sentence in the positive sample pairs through the second contrastive learning layer. Step D: Combine the first task loss and the second task loss of all positive sample pairs participating in training to obtain the total loss. If the total loss reaches the preset target, stop training. If the total loss does not reach the preset target, input the next positive sample pair and repeat steps B to D.
Citation Information
Patent Citations
Aspect-level sentiment analysis method based on graph neural network
CN113535904A
Neural network, training method, aspect-level sentiment analysis method and device and storage medium
CN114298011A