A method and system for analyzing text sentiment reasons based on prompts

By using the propt method and BERT model in emotional cause analysis, the problem of failure to fully utilize the pre-trained model in the prior art is solved, and higher robustness and applicability are achieved, and it is suitable for a variety of emotional cause analysis tasks.

CN114416991BActive Publication Date: 2025-08-05SUN YAT SEN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210055865.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-01-18
Publication Date
2025-08-05
Estimated Expiration
2042-01-18

AI Technical Summary

Technical Problem

The prior art fails to fully utilize the ability of pre-trained language models in emotional cause analysis, there is bias phenomenon, and ignores important indicator information, resulting in insufficient robustness and universality.

Method used

The propt method is used to convert the emotional cause analysis task into a pre-training task form. By adding text prompt words and words to be predicted in the text, combining BERT pre-trained model for feature vector encoding and softmax function calculation, the pointing constraint module and sequence learning module are designed to improve model performance.

Benefits of technology

Effectively utilize the knowledge of pre-trained models to solve the bias phenomenon, improve the robustness and applicability of the algorithm on the balanced dataset, and is suitable for a variety of emotional cause analysis tasks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114416991B_ABST
    Figure CN114416991B_ABST
Patent Text Reader

Abstract

The present invention discloses a prompt-based text sentiment analysis method and system. The method comprises the following steps: S1: collecting text data and preprocessing it; S2: sequentially adding text prompt words and text words to be predicted to the preprocessed text, and setting a target candidate word set for the text words to be predicted; S3: adding clause segmentation symbols and text start and end symbols to the text; S4: using the BERT pre-trained model to perform feature vector encoding on the text and the target candidate word set, obtaining a text feature vector and a target candidate word set vector; S5: calculating the encoding distance between the text feature vector and the target candidate word set vector, and using the softmax function to calculate the probability of the encoding distance vector for each word to be predicted, obtaining a prediction result for the word to be predicted; S6: combining prediction modules based on specific tasks to obtain a method suitable for specific text sentiment analysis tasks. The present invention introduces prompts to address the differences between fine-tuning tasks and pre-training tasks.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of text emotion cause analysis, and more specifically, to a prompt-based text emotion cause analysis method and system. Background Art

[0002] Sentiment cause analysis aims to identify emotional information and the causes of emotion in sentimental text. Specific subtasks within this field include sentiment cause extraction, sentiment cause matching pair extraction, and conditional causal relationship classification. Sentiment cause extraction, as a subtask, was first proposed and defined as a word-level sequence labeling problem, aiming to explore the reasons behind a particular emotional expression within a clause. However, in some cases, the sentiment or cause information in a text spans the entire clause sequence. To address this issue, sentiment cause extraction was redefined as a clause-level classification problem. However, in this sentiment cause extraction task, the extraction of causes must be based on existing sentiment annotations, resulting in limited application of techniques for sentiment cause extraction in real-world scenarios. To address this issue, the sentiment cause matching pair extraction task was proposed. Using the techniques and methods in this task, all emotions and corresponding causes can be directly identified from unlabeled sentiment text. Additionally, within the field of sentiment cause analysis, other tasks, such as conditional relationship classification, have been proposed to further explore causal relationships within sentiment text.

[0003] With the development of the internet, textual information—from social media comments, post-sales reviews, and messages on WeChat Moments—is ubiquitous. This text often reflects underlying emotions and underlying reasons. Analyzing and leveraging textual information generated across various platforms is crucial for positioning and controlling public opinion on social platforms, analyzing the causes of post-sales customer reviews and improving services, and even adapting decisions based on the current situation and underlying reasons. This places significant demands on technologies related to emotional analysis.

[0004] Existing work on sentiment analysis primarily relies on building novel deep neural network models, achieving impressive results across diverse datasets. Most of these methods employ a unified fine-tuning architecture, which first obtains word representations of the input text sequence from a pre-trained model. Then, using an attention mechanism, the word-level feature encodings are used to derive clause-level features. An interaction module is then employed to generate contextual feature representations of the clauses for final classification. However, these methods suffer from significant drawbacks. First, they utilize only pre-trained language models as word embedding layers, failing to fully leverage the capabilities of the pre-trained language models. Second, the introduction of positional information leads to bias in these methods, neglecting important indicative information. Furthermore, designing a feature fusion module suitable for interactive learning of context and text is difficult, and these algorithmic modules lack versatility and robustness.

[0005] To address the shortcomings of existing work on sentiment analysis, we introduce the prompt method to specifically address this issue. More specifically, the prompt method converts the specific fine-tuning task format into the same format as the pre-training task. Specifically for sentiment analysis, the prompt method converts the classification, matching, and recognition tasks involved in sentiment analysis into pre-training tasks, allowing the pre-trained model's performance to be fully utilized during task training. Accordingly, the prompt method is also known as the fourth training paradigm after the fine-tuning paradigm.

[0006] The prior art discloses a method, apparatus, device, and storage medium for analyzing text sentiment content. The method includes: analyzing a text to be analyzed using a BERT model to obtain a word vector for the text to be analyzed; adding a global attention mechanism to the clause information corresponding to the word vector to obtain global text information; analyzing the clause information corresponding to the word vector using a preset attention mechanism to obtain clause information combined with attention information; combining the global text information and the clause information combined with attention information using a self-attention mechanism to obtain a target text; analyzing the relationship between clauses in the target text using a classifier to obtain sentiment reason pairs, and obtaining a sentiment analysis result for the text to be analyzed based on the sentiment reasons. This method also fails to fully utilize the capabilities of the pre-trained language model and ignores important indicator information, resulting in insufficient versatility and robustness. Summary of the Invention

[0007] The primary purpose of this invention is to provide a prompt-based text emotion reason analysis method, which effectively utilizes the indicative information between emotion and reason in the text, solves the bias phenomenon in the existing technology, and has better performance.

[0008] A further object of the present invention is to provide a prompt-based text sentiment analysis system.

[0009] In order to solve the above technical problems, the technical solutions of the present invention are as follows:

[0010] A prompt-based text sentiment analysis method includes the following steps:

[0011] S1: collect text data and preprocess it;

[0012] S2: adding text prompt words and text words to be predicted to the preprocessed text, and setting a target candidate word set for the text words to be predicted;

[0013] S3: adding clause separation symbols and text start and end symbols to the text processed in step S2;

[0014] S4: Use the BERT pre-trained model to perform feature vector encoding on the text processed in step S3 and the target candidate word set set in step S2 to obtain the text feature vector and the target candidate word set vector;

[0015] S5: Calculate the encoding distance between the text feature vector and the target candidate word set vector, use the softmax function to calculate the probability of the encoding distance vector of each word to be predicted, and obtain the prediction result of the word to be predicted, that is, obtain the text sentiment reason prediction result.

[0016] S6: Combine prediction modules based on specific tasks to obtain a method suitable for specific text sentiment analysis tasks.

[0017] Furthermore, the preprocessing in step S1 includes removing punctuation marks, merging text clauses, word segmentation, and removing coded error words.

[0018] Furthermore, the text prompt words in step S2 include:

[0019] Emotional cues added to the original text to guide the BERT pre-trained model to understand the emotion recognition task;

[0020] When the BERT pre-trained model is used to understand the cause recognition task, the cause prompt words are added to the original text;

[0021] Matching hints added to the original text when guiding the BERT pre-trained model to understand the matching task between sentiment reasons.

[0022] Furthermore, in step S2, a text word to be predicted is added, and a target candidate word set is set for the text word to be predicted, which is specifically divided into:

[0023] 1) Based on the emotion recognition task, emotion words to be predicted are set for emotion clause recognition to obtain an emotion indication module. The text construction template and candidate word set of the emotion indication module are:

[0024]

[0025]

[0026] in, Function represents the text construction template of the emotion indication module, The function represents the candidate word set of the words to be predicted in the sentiment indication module, c I Indicates the i-th clause in the text, <·> indicates the emotional prompt words added to the text, [MASK]emo Indicates the sentiment word to be predicted;

[0027] 2) Based on the requirements of the sentiment reason analysis task, the reason clause is identified and the reason words to be predicted are set to obtain a reason indication module. The text construction template and candidate word set of the reason indication module are:

[0028]

[0029]

[0030] in Function represents the text construction template of the cause indication module, The function represents the candidate word set of the word to be predicted in the cause indication module, c i Indicates the i-th clause in the text, <·> indicates the reason prompt word added to the text, [MASK] cau Indicates the word to be predicted for the cause;

[0031] 3) Based on the matching task requirements between emotional reasons, matching words to be predicted are set for the matching work between emotional reason clauses to obtain a pointing constraint module. The text construction template and candidate word set of the pointing constraint module are:

[0032]

[0033]

[0034] in Function represents a text construction template pointing to the constraint module, The function represents the candidate word set pointing to the word to be predicted in the constraint module, c i Indicates the i-th clause in the text, <·> indicates adding matching prompt words to the text, [MASK] dir Indicates matching the word to be predicted, n indicates the number of clauses in the current text, and “None” indicates that there are no other clauses associated with the current clause.

[0035] Furthermore, in step S2, in order to enable the pre-trained model to complete the learning task of clause sequence features, a sequence learning module is constructed by adding sequence words to be predicted into the text. The text construction template and candidate word set of the sequence learning module are:

[0036]

[0037]

[0038] in Function represents the text construction template of the sequence learning module, The function represents the candidate word set of the word to be predicted in the sequence learning module, d represents the input text, c i Indicates the i-th clause in the text, [MASK] dir Represents the sequence of words to be predicted, and n represents the number of clauses in the current text.

[0039] Furthermore, the step S3 specifically includes the following steps:

[0040] S3.1: For the text processed in step S2, add a clause segmentation symbol to each constructed clause in the text;

[0041] S3.2: Add a start symbol and an end symbol at the beginning and end of the constructed complete text respectively.

[0042] Furthermore, the step S4 specifically includes the following steps:

[0043] S4.1: Convert text words into corresponding word embedding vectors by searching the dictionary and the corresponding word embedding matrix;

[0044] S4.2: Input the word embedding vector into the BERT pre-trained model to obtain the output text feature vector;

[0045] S4.3: By searching the dictionary and the corresponding word vector matrix, the text of the target candidate word set is converted into the corresponding word embedding vector to obtain the target candidate word set vector.

[0046] Furthermore, the step S5 specifically includes the following steps:

[0047] S5.1: Multiply the text feature vector of the word to be predicted in the text by the target candidate word set vector matrix to obtain the encoding distance between the vector of the word to be predicted and the corresponding target candidate word set vector matrix;

[0048] S5.2: Then use the softmax function to get the final predicted probability value:

[0049] P * =softmax(V * T * )

[0050] In the formula, the subscript * refers to four prediction modules, including the emotion indication module, the cause indication module, the pointing constraint module and the sequence learning module. * Represents the text feature vector of the word to be predicted, T * Represents the target candidate word set vector matrix.

[0051] Furthermore, the method further includes step S6: combining different modules to be predicted according to the specific tasks of the emotion cause analysis direction to complete the prediction of the specific task, which specifically includes:

[0052] 1) For the emotion-cause matching pair extraction task, the emotion indication module, the cause indication module, the pointing constraint module, and the sequence learning module are combined to complete the three subtasks of emotion clause recognition, cause clause recognition, and emotion-cause clause matching. The complete construction method is shown in the following formula:

[0053]

[0054] The prediction of sentiment clauses and reason clauses can be completed by the indicator module, as shown in the following formula:

[0055]

[0056]

[0057] Among them, P emo Function and P cau Functions represent the prediction results of sentiment clauses and reason clauses respectively, f emo Function and f cau The functions represent the prediction results of the emotion to be predicted words and the reason to be predicted words respectively;

[0058] At the same time, the matching prediction between the sentiment clause and the cause clause can be completed jointly by the pointing constraint module and the cause indication module, as shown in the formula below.

[0059]

[0060] Where (i, j) indicates that the i-th clause and the j-th clause form a sentiment reason matching pair relationship, null indicates that no clause is associated with the current clause, f dir Indicates the prediction result of matching the word to be predicted;

[0061] 2) For the emotional cause extraction task, the cause indication module, the pointing constraint module, and the sequence learning module are combined at the same time. Based on the prior conditions, the emotional indication module is set to complete the recognition of the cause clause under the premise of knowing the emotional clause. The complete construction method is shown in the formula below.

[0062]

[0063]

[0064] Among them, [MASK] cau The prediction of is the prediction result of the final cause clause, and the corresponding function formula is as follows;

[0065] 3) For the conditional causal relationship classification task, the pointing constraint module and the sequence learning module are combined. Based on prior conditions, the sentiment indication module and the cause indication module are set up to determine whether the given sentiment clause and cause clause group still form a causal relationship in a specific text context. The complete construction method is shown in the formula below.

[0066]

[0067] Set up a voting mechanism to predict the category of the sample. The voting mechanism is shown in the following function:

[0068]

[0069]

[0070] where c emo represents a sentiment clause, and X represents a set of sentiment clauses.

[0071] A prompt-based text sentiment analysis system, including:

[0072] A data processing module, which is used to collect text data and perform preprocessing;

[0073] A word adding module, wherein the word adding module successively adds text prompt words and text words to be predicted to the preprocessed text, and sets a target candidate word set for the text words to be predicted;

[0074] A symbol adding module, wherein the symbol adding module adds clause segmentation symbols and text start and end symbols to the text processed by the word adding module;

[0075] An encoding module, which uses the BERT pre-trained model to perform feature vector encoding on the text processed by the symbol adding module and the target candidate word set set by the word adding module to obtain a text feature vector and a target candidate word set vector;

[0076] The prediction module is used to calculate the encoding distance between the text feature vector and the target candidate word set vector, and use the softmax function to calculate the probability of the encoding distance vector of each word to be predicted to obtain the prediction result of the word to be predicted, that is, to obtain the text emotion reason prediction result.

[0077] Compared with the prior art, the beneficial effects of the technical solution of the present invention are:

[0078] 1. This paper uses the prompt method and the pre-trained model BERT to convert the sentiment analysis task into a pre-training task, resolving the formal differences between previous fine-tuning tasks and pre-training tasks, and making full use of the knowledge learned by BERT through pre-training tasks.

[0079] 2. Previous methods improve the performance of the method by introducing the relative position information between clauses. However, this invention adopts a method that combines the directional constraint module and the sequence learning module, so that the directional information between the sentiment clause and the cause clause is effectively learned, thereby improving the robustness of the algorithm on the balanced data set.

[0080] 3. The present invention designs a general algorithm that is applicable to a variety of emotion cause analysis tasks. In addition to the applicability between different tasks, the algorithm mentioned in the present invention can also learn the commonalities between different tasks, thereby further improving the efficiency and performance of the algorithm. BRIEF DESCRIPTION OF THE DRAWINGS

[0081] Figure 1 Schematic diagram of the method of the present invention.

[0082] Figure 2 Schematic diagram of the model structure of the present invention.

[0083] Figure 3 Schematic diagram of the system module of the present invention. DETAILED DESCRIPTION

[0084] The accompanying drawings are for illustrative purposes only and are not to be construed as limiting this patent;

[0085] In order to better illustrate this embodiment, some parts in the drawings may be omitted, enlarged, or reduced, and do not represent the actual product size;

[0086] It is understandable to those skilled in the art that some well-known structures and descriptions thereof may be omitted in the drawings.

[0087] The technical solution of the present invention is further described below with reference to the accompanying drawings and embodiments.

[0088] Example 1

[0089] This embodiment provides a method for analyzing text sentiment based on prompt. Figure 1 and Figure 2 As shown, the following steps are included:

[0090] S1: collect text data and preprocess it;

[0091] S2: adding text prompt words and text words to be predicted to the preprocessed text, and setting a target candidate word set for the text words to be predicted;

[0092] S3: adding clause separation symbols and text start and end symbols to the text processed in step S2;

[0093] S4: Use the BERT pre-trained model to perform feature vector encoding on the text processed in step S3 and the target candidate word set set in step S2 to obtain the text feature vector and the target candidate word set vector;

[0094] S5: Calculate the encoding distance between the text feature vector and the target candidate word set vector, use the softmax function to calculate the probability of the encoding distance vector of each word to be predicted, and obtain the prediction result of the word to be predicted, that is, obtain the text sentiment reason prediction result.

[0095] S6: Combine prediction modules based on specific tasks to obtain a method suitable for specific text sentiment analysis tasks.

[0096] The preprocessing in step S1 includes removing punctuation marks, merging text clauses, word segmentation, and removing coded error words.

[0097] The text prompt words in step S2 include:

[0098] Emotional cues added to the original text to guide the BERT pre-trained model to understand the emotion recognition task;

[0099] When the BERT pre-trained model is used to understand the cause recognition task, the cause prompt words are added to the original text;

[0100] Matching hints added to the original text when guiding the BERT pre-trained model to understand the matching task between sentiment reasons.

[0101] In step S2, a text word to be predicted is added, and a target candidate word set is set for the text word to be predicted, which is specifically divided into:

[0102] 1) Based on the emotion recognition task, emotion words to be predicted are set for emotion clause recognition to obtain an emotion indication module. The emotion indication module can complete the recognition of emotion clauses in the text. The text construction template and candidate word set of the emotion indication module are:

[0103]

[0104]

[0105] in, Function represents the text construction template of the emotion indication module, The function represents the candidate word set of the words to be predicted in the sentiment indication module, c iIndicates the i-th clause in the text, <·> indicates the emotional prompt words added to the text, [MASK] emo Indicates the sentiment word to be predicted;

[0106] 2) Based on the requirements of the sentiment reason analysis task, the reason clause identification sets the reason to be predicted words to obtain the reason indication module. The reason indication module can complete the recognition of the reason clause in the text. The text construction template and candidate word set of the reason indication module are:

[0107]

[0108]

[0109] in Function represents the text construction template of the cause indication module, The function represents the candidate word set of the word to be predicted in the cause indication module, c i Indicates the i-th clause in the text, <·> indicates the reason prompt word added to the text, [MASK] cau Indicates the word to be predicted for the cause;

[0110] 3) Based on the matching task requirements between emotional reasons, the matching work between emotional reason clauses is set to match the words to be predicted, and a pointing constraint module is obtained. The pointing constraint module can match the reason clauses and the emotional clauses in the text. The text construction template and candidate word set of the pointing constraint module are:

[0111]

[0112]

[0113] in Function represents a text construction template pointing to the constraint module, The function represents the candidate word set pointing to the word to be predicted in the constraint module, c i Indicates the i-th clause in the text, <·> indicates adding matching prompt words to the text, [MASK] dir Indicates matching the word to be predicted, n indicates the number of clauses in the current text, and “None” indicates that there are no other clauses associated with the current clause.

[0114] In step S2, in order to enable the pre-trained model to complete the task of learning clause sequence features, a sequence learning module is constructed by adding sequence words to be predicted in the text. The sequence learning module enables the pre-trained model to learn the sequence information of specific labels during the training process. The text construction template and candidate word set of the sequence learning module are:

[0115]

[0116]

[0117] in Function represents the text construction template of the sequence learning module, The function represents the candidate word set of the word to be predicted in the sequence learning module, d represents the input text, c i Indicates the i-th clause in the text, [MASK] dir Represents the sequence of words to be predicted, and n represents the number of clauses in the current text.

[0118] The step S3 specifically includes the following steps:

[0119] S3.1: For the text processed in step S2, add a clause segmentation symbol to each constructed clause in the text;

[0120] S3.2: Add a start symbol and an end symbol at the beginning and end of the constructed complete text respectively.

[0121] The step S4 specifically includes the following steps:

[0122] S4.1: Convert text words into corresponding word embedding vectors by searching the dictionary and the corresponding word embedding matrix;

[0123] S4.2: Input the word embedding vector into the BERT pre-trained model to obtain the output text feature vector;

[0124] S4.3: By searching the dictionary and the corresponding word vector matrix, the text of the target candidate word set is converted into the corresponding word embedding vector to obtain the target candidate word set vector.

[0125] The step S5 specifically includes the following steps:

[0126] S5.1: Multiply the text feature vector of the word to be predicted in the text by the target candidate word set vector matrix to obtain the encoding distance between the vector of the word to be predicted and the corresponding target candidate word set vector matrix;

[0127] S5.2: Then use the softmax function to get the final predicted probability value:

[0128] P * =softmax(V * T * )

[0129] In the formula, the subscript * refers to four prediction modules, including the emotion indication module, the cause indication module, the pointing constraint module and the sequence learning module. * Represents the text feature vector of the word to be predicted, T *Represents the target candidate word set vector matrix.

[0130] The step S6 is also included: according to the specific tasks of the emotion cause analysis direction, different modules to be predicted are combined to complete the prediction of the specific task, which specifically includes:

[0131] 1) For the emotion-cause matching pair extraction task, the emotion indication module, the cause indication module, the pointing constraint module, and the sequence learning module are combined to complete the three subtasks of emotion clause recognition, cause clause recognition, and emotion-cause clause matching. The complete construction method is shown in the following formula:

[0132]

[0133] The prediction of sentiment clauses and reason clauses can be completed by the indicator module, as shown in the following formula:

[0134]

[0135]

[0136] Among them, P emo Function and P cau Functions represent the prediction results of sentiment clauses and reason clauses respectively, f emo Function and f cau The functions represent the prediction results of the emotion to be predicted words and the reason to be predicted words respectively;

[0137] At the same time, the matching prediction between the sentiment clause and the cause clause can be completed jointly by the pointing constraint module and the cause indication module, as shown in the formula below.

[0138]

[0139] Where (i, j) indicates that the i-th clause and the j-th clause form a sentiment reason matching pair relationship, null indicates that no clause is associated with the current clause, f dir Indicates the prediction result that matches the word to be predicted;

[0140] 2) For the emotional cause extraction task, the cause indication module, the pointing constraint module, and the sequence learning module are combined at the same time. Based on the prior conditions, the emotional indication module is set to complete the recognition of the cause clause under the premise of knowing the emotional clause. The complete construction method is shown in the formula below.

[0141]

[0142]

[0143] Among them, [MASK] cauThe prediction of is the prediction result of the final cause clause, and the corresponding function formula is as follows;

[0144] 3) For the conditional causal relationship classification task, the pointing constraint module and the sequence learning module are combined. Based on prior conditions, the sentiment indication module and the cause indication module are set up to determine whether the given sentiment clause and cause clause group still form a causal relationship in a specific text context. The complete construction method is shown in the formula below.

[0145]

[0146] Unlike the two tasks mentioned above, this task is to determine the relationship between a single sentiment clause and multiple reason clauses. Therefore, a voting mechanism is set up to finally predict the category of the sample. The voting mechanism is shown in the following function:

[0147]

[0148]

[0149] where c emo represents a sentiment clause, and X represents a set of sentiment clauses.

[0150] Example 2

[0151] This embodiment provides a specific embodiment of embodiment 1, specifically:

[0152] Publicly available sentiment analysis datasets, including the ECE, ECPE, and CCRC datasets, are used. The ECE dataset, the most widely used in sentiment analysis, originates from Sina City News and contains 2,105 text data. Each text data contains only one sentiment clause and one or more reason clauses. The ECPE dataset is constructed based on the aforementioned ECE dataset. This dataset aggregates text data containing the same text but with different sentiment and reason annotations. The CCRC dataset is constructed based on the ECPE dataset. This dataset was obtained through manual annotation and negative sampling, and three experts were hired to annotate the data. Each dataset was randomly divided into ten equal parts for cross-validation.

[0153] We used an RTX 3090 graphics card with 24GB of memory and trained the algorithm on the publicly available Chinese BERT pre-trained model. Specifically, we used the AdamW optimizer, a learning rate of 1e-5, and 8 samples per iteration.

[0154] In previous methods, either regularized sentence prediction is performed based on manually designed rules, and emotional cause judgment is performed only through keywords in the text; or it is based on traditional machine learning methods, which only consider learning in the sense of mathematical statistics and ignore the language information in the text; or it is based on deep learning and adopts fine-tuning structure to train and tune the pre-trained model and downstream modules for the task of emotional cause analysis. Specifically, they can be divided into graph structure-based methods, joint learning-based methods, multi-task learning-based methods, sequence labeling-based methods, and grammar transfer-based methods, which not only lead to insufficient robustness and versatility of the methods, but also fail to fully utilize the language knowledge in the pre-trained model. While fully utilizing the capabilities of the pre-trained model, this embodiment effectively utilizes the indicative information between emotions and causes in the text, solves the bias phenomenon in existing methods, and the robustness and versatility of the method are significantly better than existing methods.

[0155] This embodiment is a prompt-based text sentiment analysis method, which is essentially a deep learning method. Figure 1 As shown in the method flowchart, this method first preprocesses the input text. Then, text prompt words and the words to be predicted are added to the text. A target candidate word set is set for the words to be predicted, and clause separators and text start and end symbols are added. Next, the text is input into the BERT pre-trained model for text feature encoding, followed by distance calculation between feature vectors. Finally, the softmax function is used to predict and classify the words to be predicted. Details are as follows:

[0156] 1. First, read the text data and perform preprocessing on the text, such as removing punctuation, merging text clauses, and word segmentation.

[0157] 2. Add emotional prompt words, cause prompt words and matching prompt words to the text.

[0158] 3. Add sentiment words to be predicted, cause words to be predicted, match words to be predicted, and sequence words to be predicted to the text, and set candidate word sets for the four types of words to be predicted.

[0159] 4. Add clause separators to the text, and add start and end symbols at the beginning and end of the text respectively.

[0160] 5. Load the Chinese pre-training parameters of the BERT model (the data set is a Chinese data set).

[0161] 6. Input the text constructed in step 4 into the BERT model loaded with pre-trained parameters in step 5, and encode it to obtain the text feature vector output.

[0162] 7. Perform word feature processing on the candidate words, and use the processed candidate word feature vector and the text feature vector output in step 6 to perform vector distance calculation to obtain a distance calculation matrix.

[0163] 8. Use softmax to calculate the distance vector of the word to be predicted in the distance calculation matrix to obtain sentiment classification, cause classification, and matching classification.

[0164] 9. Perform 20 epochs of iterative training on the model calculation part of steps 6 to 8 above, calculate the indicators for the test set, and save the model with the highest indicator value for the final sentiment cause analysis task prediction.

[0165] To compare with previous methods, the specific results are evaluated by accuracy, recall, and F1-score. The specific results on the three tasks of emotion reason matching extraction, emotion reason extraction, and conditional emotion reason classification are shown in the following table:

[0166] Table 1. Experimental comparison results with other models on the emotion reason matching extraction task

[0167]

[0168] Table 2. Experimental comparison results with other models on the emotion reason extraction task

[0169] Model F1 P R EF-BHA 0.7868 0.7938 0.7808 RTHN (Layer 3) 0.7677 0.7697 0.7662 RHNN 0.7914 0.8112 0.7725 MANN 0.7706 0.7843 0.7587 FSS-GCN 0.7861 0.7572 0.7714 LambdaMART 0.7608 0.7720 0.7499 Multi-kernel 0.6752 0.6588 0.6972 Support Vector Machine 0.4285 0.4200 0.4375 Our Model 0.8319 0.8274 0.8373

[0170] Table 3 Experimental comparison results with other models on the conditional emotion cause classification task

[0171] Model F1 P R BiLSTM+Concatenation 0.6127 0.5412 0.7119 BiLSTM+BiLSTM 0.6976 0.6606 0.7400 BiLSTM+Self-Attention 0.6605 0.5766 0.7770 Our Model 0.8074 0.7707 0.8487

[0172] The experimental results above demonstrate that this embodiment significantly outperforms other methods across multiple emotion analysis tasks, approaching or even exceeding the state-of-the-art on existing datasets. Furthermore, it demonstrates strong versatility across multiple tasks, making it suitable for a wide range of emotion analysis tasks.

[0173] Example 3

[0174] This embodiment provides a prompt-based text emotion analysis system. Figure 3 As shown, including:

[0175] A data processing module, which is used to collect text data and perform preprocessing;

[0176] A word adding module, wherein the word adding module successively adds text prompt words and text words to be predicted to the preprocessed text, and sets a target candidate word set for the text words to be predicted;

[0177] A symbol adding module, wherein the symbol adding module adds clause segmentation symbols and text start and end symbols to the text processed by the word adding module;

[0178] An encoding module, which uses the BERT pre-trained model to perform feature vector encoding on the text processed by the symbol adding module and the target candidate word set set by the word adding module to obtain a text feature vector and a target candidate word set vector;

[0179] The prediction module is used to calculate the encoding distance between the text feature vector and the target candidate word set vector, and use the softmax function to calculate the probability of the encoding distance vector of each word to be predicted to obtain the prediction result of the word to be predicted, that is, to obtain the text emotion reason prediction result.

[0180] The same or similar reference numerals correspond to the same or similar components;

[0181] The terms used in the drawings to describe positional relationships are for illustrative purposes only and should not be construed as limiting this patent;

[0182] Obviously, the above embodiments of the present invention are merely examples for the purpose of clearly illustrating the present invention, and are not intended to limit the embodiments of the present invention. Those skilled in the art will appreciate that other variations or modifications can be made based on the above description. It is not necessary and impossible to enumerate all embodiments here. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention shall be included within the scope of protection of the claims of the present invention.

Claims

1. A prompt-based text sentiment analysis method, characterized in that: The following steps are involved: S1: collect text data and perform preprocessing; S2: adding text prompt words and text words to be predicted to the preprocessed text, and setting a target candidate word set for the text words to be predicted; S3: adding clause separation symbols and text start and end symbols to the text processed in step S2; S4: Use the BERT pre-trained model to perform feature vector encoding on the text processed in step S3 and the target candidate word set set in step S2 to obtain the text feature vector and the target candidate word set vector; S5: Calculate the encoding distance between the text feature vector and the target candidate word set vector, calculate the probability of the encoding distance vector of each word to be predicted using the softmax function, and obtain the prediction result of the word to be predicted; S6: Combine prediction modules based on specific tasks to obtain a method suitable for specific text sentiment analysis tasks; In step S2, a text word to be predicted is added, and a target candidate word set is set for the text word to be predicted, which is specifically divided into: 1) Based on the emotion recognition task, emotion words to be predicted are set for emotion clause recognition to obtain an emotion indication module. The text construction template and candidate word set of the emotion indication module are: in, Function represents the text construction template of the emotion indication module, The function represents the candidate word set of the words to be predicted in the sentiment indication module, c i Indicates the i-th clause in the text, <·> indicates the emotional prompt words added to the text, [MASK] emo Indicates the sentiment word to be predicted; 2) Based on the requirements of the sentiment reason analysis task, the reason clause is identified and the reason words to be predicted are set to obtain a reason indication module. The text construction template and candidate word set of the reason indication module are: in Function represents the text construction template of the cause indication module, The function represents the candidate word set of the word to be predicted in the cause indication module, c i Indicates the i-th clause in the text, <·> indicates the reason prompt word added to the text, [MASK] cau Indicates the word to be predicted for the cause; 3) Based on the matching task requirements between emotional reasons, matching words to be predicted are set for the matching work between emotional reason clauses to obtain a pointing constraint module. The text construction template and candidate word set of the pointing constraint module are: in Function represents a text construction template pointing to the constraint module, The function represents the candidate word set pointing to the word to be predicted in the constraint module, c i Indicates the i-th clause in the text, <·> indicates adding matching prompt words to the text, [MASK] dir Indicates matching the word to be predicted, n indicates the number of clauses in the current text, and "None" indicates that there are no other clauses associated with the current clause; In step S2, in order to enable the pre-trained model to complete the learning task of clause sequence features, a sequence learning module is constructed by adding sequence words to be predicted into the text. The text construction template and candidate word set of the sequence learning module are: in Function represents the text construction template of the sequence learning module, The function represents the candidate word set of the word to be predicted in the sequence learning module, d represents the input text, c i Indicates the i-th clause in the text, [MASK] dir Represents the sequence of words to be predicted, and n represents the number of clauses in the current text; The step S6 specifically includes: 1) For the emotion-cause matching pair extraction task, the emotion indication module, the cause indication module, the pointing constraint module, and the sequence learning module are combined to complete the three subtasks of emotion clause recognition, cause clause recognition, and emotion-cause clause matching. The complete construction method is shown in the following formula: The prediction of sentiment clauses and reason clauses can be completed by the indicator module, as shown in the following formula: Among them, P emo Function and P cau Functions represent the prediction results of sentiment clauses and reason clauses respectively, f emo Function and f cau The functions represent the prediction results of the emotion to be predicted words and the reason to be predicted words respectively; At the same time, the matching prediction between the sentiment clause and the cause clause can be completed by the pointing constraint module and the cause indication module. The specific form is shown in the following formula: Where (i, j) indicates that the i-th clause and the j-th clause form a sentiment reason matching pair relationship, null indicates that no clause is associated with the current clause, f dir Indicates the prediction result that matches the word to be predicted; 2) For the emotional reason extraction task, the reason indication module, the pointing constraint module, and the sequence learning module are combined at the same time. Based on the prior conditions, the emotional indication module is set to complete the recognition of the reason clause under the premise of knowing the emotional clause. The complete construction method is shown in the following formula: Among them, [MASK] cau The prediction of is the prediction result of the final cause clause, and the corresponding function formula is as follows; 3) For the conditional causal relationship classification task, the pointing constraint module and the sequence learning module are combined simultaneously. Based on the prior conditions, the sentiment indication module and the cause indication module are set to determine whether the given sentiment clause and cause clause group still form a causal relationship in the specific text context. The complete construction method is shown in the following formula: Set up a voting mechanism to predict the category of the sample. The voting mechanism is shown in the following function: where c emo represents a sentiment clause, and X represents a set of sentiment clauses.

2. The prompt-based text emotion reason analysis method according to claim 1 is characterized in that: The preprocessing in step S1 includes removing punctuation marks, merging text clauses, word segmentation, and removing coded error words.

3. The prompt-based text emotion reason analysis method according to claim 2 is characterized in that: The text prompt words in step S2 include: Emotional cues added to the original text to guide the BERT pre-trained model to understand the emotion recognition task; When the BERT pre-trained model is used to understand the cause recognition task, the cause prompt words are added to the original text; Matching hints added to the original text when guiding the BERT pre-trained model to understand the matching task between sentiment reasons.

4. The prompt-based text emotion reason analysis method according to claim 3 is characterized in that: The step S3 specifically includes the following steps: S3.1: For the text processed in step S2, add a clause segmentation symbol to each constructed clause in the text; S3.2: Add a start symbol and an end symbol at the beginning and end of the constructed complete text respectively.

5. The prompt-based text emotion reason analysis method according to claim 4 is characterized in that: The step S4 specifically includes the following steps: S4.1: Convert text words into corresponding word embedding vectors by searching the dictionary and the corresponding word embedding matrix; S4.2: Input the word embedding vector into the BERT pre-trained model to obtain the output text feature vector; S4.3: By searching the dictionary and the corresponding word vector matrix, the text of the target candidate word set is converted into the corresponding word embedding vector to obtain the target candidate word set vector.

6. The prompt-based text emotion reason analysis method according to claim 5 is characterized in that: The step S5 specifically includes the following steps: S5.1: Multiply the text feature vector of the word to be predicted in the text by the target candidate word set vector matrix to obtain the encoding distance between the vector of the word to be predicted and the corresponding target candidate word set vector matrix; S5.2: Then use the softmax function to get the final predicted probability value: P * =softmax(V * T * ) In the formula, the subscript * refers to four prediction modules, including the emotion indication module, the cause indication module, the pointing constraint module and the sequence learning module. * Represents the text feature vector of the word to be predicted, T * Represents the target candidate word set vector matrix.

7. A prompt-based text emotion analysis system, characterized by: The system applies the prompt-based text sentiment analysis method according to any one of claims 1 to 6, comprising: A data processing module, which is used to collect text data and perform preprocessing; A word adding module, wherein the word adding module successively adds text prompt words and text to-be-predicted words to the preprocessed text, and sets a target candidate word set for the text to-be-predicted words; A symbol adding module, which adds clause segmentation symbols and text start and end symbols to the text processed by the word adding module; An encoding module, which uses the BERT pre-trained model to perform feature vector encoding on the text processed by the symbol adding module and the target candidate word set set by the word adding module to obtain a text feature vector and a target candidate word set vector; The prediction module is used to calculate the encoding distance between the text feature vector and the target candidate word set vector, and use the softmax function to calculate the probability of the encoding distance vector of each word to be predicted to obtain the prediction result of the word to be predicted, that is, to obtain the text emotion reason prediction result.

Citation Information

Patent Citations

  • Commodity recommendation method and commodity recommendation device based on comment text sentiment analysis

    CN110517121A

  • Chinese entity identification method based on BERT and Word2Vec vector fusion

    CN112632997A