Disclosure of Invention
The invention aims to provide a target-oriented emotion classification method, which solves the technical problems that a target sequence to be analyzed is extracted and target-oriented emotion analysis is completed aiming at the target sequence.
In order to achieve the purpose, the invention adopts the following technical scheme:
a target-oriented emotion classification method comprises the following steps:
step 1: establishing a client server and a central server, wherein the client server is used for collecting text information and sending the text information to the central server;
establishing a preprocessing module, a GloVe model module, a position information coding module, an attention coder and a classifier module in a central server;
step 2: after the central server acquires the text information, the central server preprocesses the text data with subjective emotional colors in the text information through a preprocessing module, and respectively shows text sentences and target sequences in the text data, and the method specifically comprises the following steps:
step A1: establishing a Chinese stop word dictionary, deleting stop words contained in the text data according to the Chinese stop word dictionary, and deleting incomplete text data contained in the text data according to the Chinese stop word dictionary to obtain original sentence data;
step A2: taking the sentences with emotional colors in the original sentence data as targets to be detected, establishing target sequences for the targets to be detected, and extracting the target sequences to obtain subsequences of the target sequences corresponding to the original sentence data;
step A3: carrying out serialization operation on the original sentence data and the target sequence to complete the serialization operation of the text data;
and step 3: the GloVe model module pre-trains a language model by using a GloVe word representation tool, obtains the feature representation of the word vector of the original sentence data and the target sequence by using the language model, and captures the wide semantic features among words;
and 4, step 4: the position information coding module codes the position information of the context words in the original sentence data relative to the target sequence, and calculates the position weight of each word in the original sentence data, and the method specifically comprises the following steps:
step B1: specifying that words closer to the target sequence contribute more to their computation of their sentiment values, and words further from the target sequence contribute less to their sentiment values;
step B2: calculating the position distance of each word in the context relative to the target sequence to obtain position distance information, if a target sequence consists of a plurality of words and a certain context belongs to the target sequence, the position distance between the context and the target sequence is 0, and calculating the position weight of all the context words relative to the target sequence through the position distance information;
and 5: respectively encoding the word vectors of the original sentence data and the target sequence by using an attention encoder, and specifically comprising the following steps:
step C1: combining the position distance information with the original sentence data to update word vectors, so that each word vector in the context coded by the Glove word representation tool can embody the position distance information between the word vector and the target sequence;
step C2: the learning of text semantics is completed by using a long-short term network and an attention mechanism, and the process comprises the following steps:
step Y1: using Bi-LSTM to learn the meaning of the text words from the forward direction and the reverse direction respectively, and combining word vectors obtained by the forward direction and the reverse direction respectively to form a final text word vector;
step Y2: using an attention encoder to further learn the interrelation between each word in the text sentence and the target sequence respectively to obtain a final text feature vector;
step 6: the classifier module learns a classifier for the final text feature vector and calculates the emotion classification of the original sentence data, and the specific steps are as follows:
step D1: and (3) respectively calculating the positive emotion scores, the neutral emotion scores and the negative emotion scores of the text aiming at the target sequence through a layer of fully-connected neural network, and taking one item with the highest probability as an emotion classification result, wherein the specific calculation formula is as follows:
scorej=Wp·F+bp,j∈[1,3];
wherein,
and
is a parameter of a neuron between an input layer and an output layer of a neural network, and needs to be continuously changed in the training process of a model to finally reach a convergence state, score
jA score indicating that the text belongs to tag j, whereinJ takes the value 1, 2, 3 respectively represents the emotion value: positive, neutral, negative;
step D2: text emotion categories aiming at the target sequence are calculated through Softmax normalization, the emotion label with the maximum probability is extracted to serve as a text emotion value of the target sequence, and the formula is as follows:
preferably, when step a3 is executed, first, character statistics is performed on the original sentence data and the target sequence, a dictionary library is established, the dictionary library contains all words of the corpus, subscript indexes of the words in the original sentence data and the target sequence in the dictionary library are searched, and the serialization operation of the text data is completed.
Preferably, when step 3 is executed, the method comprises the following steps:
step S1: presetting a corpus, constructing a co-occurrence matrix according to the corpus, wherein each element in the co-occurrence matrix represents the co-occurrence frequency of a word in a context window with a specific size in context words, and specifically, defining an attenuation function for calculating weight according to the distance d between two words in the context window;
step S2: and constructing an approximate relation between the word vector and the co-occurrence matrix, wherein the relation can be represented by the following formula:
wherein,
and
is the word vector to be solved finally; b
iAnd
is the offset of two word vectorsAn item; i and j represent the number of word vectors, X, respectively
ijIs an output result;
step S3: the loss function J is constructed according to the following formula:
the loss function J uses the mean square error, while adding a weight function f (X)ij);
Weight function f (X)ij) The formula of (1) is as follows:
obtaining a word vector table of the corpus after being trained by a GloVe word representation tool, and setting the word vector table to be represented as
Wherein d isvIs the dimension of the word vector, | V | is the size of the entire dictionary library constructed above;
after words in the original sentence data are mapped into vectors by searching the word vector table, the text sentence is represented as
Similarly, words in the target sequence are searched in the word vector table to obtain a vectorized target sequence:
the target-oriented emotion classification method solves the technical problems that a target sequence to be analyzed is extracted and target-oriented emotion analysis is completed aiming at the target sequence, is suitable for scenes needing fine-grained emotion analysis, can realize emotion classification on texts with a plurality of targets to be analyzed and different targets having different emotion colors in sentences, and is more accurate and effective in extraction.
Detailed Description
1-3, the emotion classification method for objects includes the following steps:
step 1: establishing a client server and a central server, wherein the client server is used for collecting text information and sending the text information to the central server;
establishing a preprocessing module, a GloVe model module, a position information coding module, an attention coder and a classifier module in a central server;
step 2: after the central server acquires the text information, the central server preprocesses the text data with subjective emotional colors in the text information through a preprocessing module, and respectively shows text sentences and target sequences in the text data, and the method specifically comprises the following steps:
step A1: establishing a Chinese stop word dictionary, deleting stop words contained in the text data according to the Chinese stop word dictionary, and deleting incomplete text data contained in the text data according to the Chinese stop word dictionary to obtain original sentence data;
the established Chinese stop word dictionary is represented as D
s={w
d1,w
d2,...,w
dtAnd f, stopping word list filtering on the text, deleting incomplete text data with missing values, and obtaining an original sentence W ═ W { (W)
1,w
2,...,w
nAnd simultaneously extracting a target sequence to be analyzed, wherein the target sequence is expressed as
Wherein W
tIs a subsequence of W, and the goal of this embodiment is to predict the emotional tendency of the target sequence to be analyzed; whereint refers to target and represents the target sequence.
Step A2: taking the sentences with emotional colors in the original sentence data as targets to be detected, establishing target sequences for the targets to be detected, and extracting the target sequences to obtain subsequences of the target sequences corresponding to the original sentence data;
step A3: carrying out serialization operation on the original sentence data and the target sequence to complete the serialization operation of the text data;
firstly, carrying out character statistics on original sentence data and a target sequence, establishing a dictionary library, wherein the dictionary library contains all words of a training corpus, searching subscript indexes of the original sentence data and the words in the target sequence in the dictionary library, and finishing serialization operation of text data, for example, the serialized original sentence data is represented as I ═ I
1,i
2,...,i
nThen the target sequence is represented as
And step 3: the GloVe model module pre-trains a language model by using a GloVe word representation tool, obtains the feature representation of the word vector of the original sentence data and the target sequence by using the language model, and captures the wide semantic features among words;
when step 3 is executed, the method comprises the following steps:
step S1: presetting a corpus, constructing a co-occurrence matrix according to the corpus, wherein each element in the co-occurrence matrix represents the co-occurrence frequency of a word in a context window with a specific size in context words, and specifically, defining an attenuation function for calculating weight according to the distance d between two words in the context window;
step S2: and constructing an approximate relation between the word vector and the co-occurrence matrix, wherein the relation can be represented by the following formula:
wherein,
and
is the word vector that is to be solved at the end,
is w
iTransposing; b
iAnd
is a bias term for two word vectors; i and j represent the number of word vectors, X, respectively
ijIs an output result;
step S3: the loss function J is constructed according to the following formula:
v represents the whole dictionary base, the loss function J uses the mean square error, and a weight function f (x) is added;
the formula of the weight function f (x) is as follows:
wherein x ismaxRepresenting the highest frequency of appearance in the context of a certain word and another word, wherein the value is set as 120, alpha is 0.76, a word vector table of a corpus is obtained after being trained by a Glove word representation tool, and the word vector table is set to be represented as
Wherein d isvIs the dimension of the word vector, | V | is the size of the entire dictionary library constructed above;
after words in the original sentence data are mapped into vectors by searching the word vector table, the text sentence is represented as
Similarly, words in the target sequence are searched in the word vector table to obtain a vectorized target sequence:
and 4, step 4: the position information coding module codes the position information of the context words in the original sentence data relative to the target sequence, and calculates the position weight of each word in the original sentence data, and the method specifically comprises the following steps:
step B1: specifying that words closer to the target sequence contribute more to their computation of their sentiment values, and words further from the target sequence contribute less to their sentiment values;
step B2: calculating the position distance of each word in the context relative to the target sequence to obtain position distance information, if a target sequence consists of a plurality of words and a certain context belongs to the target sequence, the position distance between the context and the target sequence is 0, and calculating the position weight of all the context words relative to the target sequence through the position distance information;
as shown in fig. 2, which is a flow chart of position information encoding, first, the position of the first character in the target sequence in the context in the whole original sentence data is calculated, the index number is recorded as k, then, the distance between each word in the calculation context and the position of the first character in the target sequence is calculated as l, assuming that the total length of the target sequence is m, the distance calculation formula is as follows:
wherein liIndicating the distance of the ith word in the current context from the target sequence.
As shown in the formula, the distance between the contexts on the left side of the target sequence is less than 0, the distance between the contexts on the right side of the target sequence is greater than 0, and the distance between all the words in the middle of the target sequence is set to 0, because it needs to be calculated that the influence of the upper and lower words on the emotion value of the target sequence, so that the context farther away from the target sequence contributes less to the emotion of the target sequence, and the context closer to the target sequence contributes more to the emotion of the target sequence, and the position weight of each word in the text sentence from the target sequence can be calculated by the following formula:
where n is the total length of a text, m is the total length of the target sequence, | liI is the absolute value of the distance, wiAnd the position weight of the ith word in the original sentence from the target sequence is represented.
And 5: respectively encoding the word vectors of the original sentence data and the target sequence by using an attention encoder, and specifically comprising the following steps:
step C1: combining the position distance information with the original sentence data to update word and word vectors, so that each word and word vector x in the context coded by the GloVe word representation tooliThe position distance information between the target sequence and the target sequence can be embodied, and the concrete implementation formula is as follows:
X=(x1·w1,x2·w2,...,xn·wn) (ii) a Wherein wiRepresenting the position weight of the ith word in the original sentence from the target sequence;
and X is an updated text sentence word vector, and the vector represented by the vector already contains the position distance information of the target sequence.
Step C2: the learning of text semantics is completed by using a long-short term network and an attention mechanism, and the process comprises the following steps:
step Y1: using Bi-LSTM to learn the meaning of the text words from the forward direction and the reverse direction respectively, and combining word vectors obtained by the forward direction and the reverse direction respectively to form a final text word vector;
Bi-LSTM (Bi-directional Long-Short Term Memory) also called bidirectional Long-Short Term Memory network can learn the meaning of text words from the forward direction and the reverse direction respectively, and combines word vectors obtained by the forward direction and the reverse direction learning respectively to form a final text word vector to finish the preliminary learning of sequence texts, wherein the following process is the process of single-direction text word vector learning:
fi=σ(Wf[xi,hi]+bf);
ii=σ(Wi[xi,hi]+bi);
oi=σ(Wo[xi,hi]+bo);
hi=tanh(Ci)*oi;
wherein h is
iIs a text word expression form obtained after one-time learning,
as a result of the forward learning,
for the result of backward learning, the bi-directional text word vector can be represented as:
wherein d is the number of neurons in the hidden layer in Bi-LSTM, from which it can be seen that
The text feature vector representing all words is obtained by carrying out Bi-LSTM coding on the target sequence in the same way
In this way, it can be seen that,
a text feature vector representation representing all sequences of the eye, wherein d
tNumber of hidden layer neurons in LSTM for target sequence.
Step Y2: using an attention encoder to further learn the interrelation between each word in the text sentence and the target sequence respectively to obtain a final text feature vector;
fig. 3 is a flowchart of attention learning for target-oriented text, which includes the following steps:
and step 201, fine-grained calculation from the target sequence to the text statement. In order to incorporate the information of the target sequence into the text sentence, the relationship between the text sentence and the target sequence words can be calculated, and for each word in the text sentence, the attention coefficient between the word and all the words in the target sequence can be calculated, and the calculation formula is as follows:
wherein,
is a parameter of a linear transformation in order to transform the target sequence
Is converted into a sum text statement h
iIn order to calculate a relevance score, followed by an attention coefficient:
wherein alpha is
i,jWhich represents the attention coefficient between the ith word in the text sentence and the jth word in the target sequence, it follows that,
and finally, multiplying the attention coefficient and the text statement to obtain a context word vector based on the target sequence:
and 202, performing fine-grained calculation on the text sentence to the target sequence. In order to merge the text sentence information into the target sequence, the relationship between the target sequence and the text sentence words may be calculated, and for each word in the target sequence, the attention coefficient between the word and all words in the text sentence may be calculated, and the calculation formula is as follows:
wherein,
is a parameter of a linear transformation for transforming a text sentence h
jConversion to and target sequence
In order to calculate a relevance score, followed by an attention coefficient:
wherein, beta
i,jRepresenting the attention coefficient between the ith word in the target sequence and the jth word in the text sentence, it follows that,
and finally, multiplying the attention coefficient by the target sequence to obtain a target sequence word vector based on the text sentence:
step 203, coarse-grained calculation of the target sequence to the text statement. After the attention calculation of the original text based on the target sequence and the target sequence based on the text sentence is completed, the two-way information between the original text based on the target sequence and the target sequence based on the text sentence is learned on the basis of the primary Bi-LSTM, the text sentence comprises the position and the target sequence information, the target sequence comprises the text sentence and the position information, and then the relation between the text sentence with the coarse granularity and the target sequence is learned on the basis of the fine granularity. The attention coefficient between the words with the maximum emotion contribution of each word in the text sentence to the target sequence is reserved, and the text features extracted by the LSTM are updated through the attention coefficients to obtain a new text sentence word vector ZacThe concrete formula is as follows:
αi=max(αi,:);
wherein alpha isi,:Representing the attention coefficient between the ith word in the text sentence and all the words in the target sequence, and the max () function represents the maximum attention coefficient.
And step 204, coarse-grained calculation of the text sentence to the target sequence. The attention coefficient of each word in the target sequence relative to the word which is most related to the emotion in the text sentence is reserved, and then the target sequence characteristics extracted by Bi-LSTM are updated through the attention coefficients to obtain a new target sequence word vector ZcaThe specific formula is as follows:
βi=max(βi,:);
and 205, performing feature synthesis on the fine-grained and coarse-grained processed word features of the text sentence based on the target sequence and the word features of the target sequence based on the original text to obtain final text features. Performing feature splicing on the word features of the text sentence based on the target sequence after the fine-grained processing and the word features of the target sequence based on the text sentence after the coarse-grained processing, and similarly, performing feature splicing on the word features of the target sequence based on the text sentence obtained after the fine-grained processing and the word features of the text sentence based on the target sequence obtained after the coarse-grained processing, wherein before the feature splicing, the four feature vectors need to be subjected to dimension reduction:
respectively obtaining text sentence word characteristics H ' and target sequence word characteristics H ' which are combined in coarse granularity and fine granularity 'tAnd finally, completing feature mixing between the two to obtain a final text feature vector, wherein the final text feature vector is expressed as F:
step 6: the classifier module learns a classifier for the final text feature vector and calculates the emotion classification of the original sentence data, and the specific steps are as follows:
step D1: and (3) respectively calculating the positive emotion scores, the neutral emotion scores and the negative emotion scores of the text aiming at the target sequence through a layer of fully-connected neural network, and taking one item with the highest probability as an emotion classification result, wherein the specific calculation formula is as follows:
scorej=Wp·F+bp,j∈[1,3];
wherein,
and
is a parameter of a neuron between an input layer and an output layer of a neural network, and needs to be continuously changed in the training process of a model to finally reach a convergence state, score
jAnd expressing the score of the text belonging to the label j, wherein j takes the value of 1, 2, and 3 respectively express the emotion value: positive, neutral, negative;
step D2: text emotion categories aiming at the target sequence are calculated through Softmax normalization, the emotion label with the maximum probability is extracted to serve as a text emotion value of the target sequence, and the formula is as follows:
the target-oriented emotion classification method solves the technical problems that a target sequence to be analyzed is extracted and target-oriented emotion analysis is completed aiming at the target sequence, is suitable for scenes needing fine-grained emotion analysis, can realize emotion classification on texts with a plurality of targets to be analyzed and different targets having different emotion colors in sentences, and is more accurate and effective in extraction.