# Topic-Specific Emotion Mining Model for Online Comments
## 3 Proposed Model
### Gibbs Sampling for the Topic-Specific Emotion Mining Model  
Topic-Specific Emotion Mining Model (TSEM), as an extension model of LDA [10], also applies Gibbs sampling algorithm [28] to the latent parameter estimation. Gibbs sampling will sequentially sample \(s_{i}\) and \(z_{i}\) from the distribution of variables given current values of all other variables and the data. In our model, there are three types of distribution, which are \(\theta_{t}\), \(\Pi_{tk}\), and \(\varphi_{tk}\). \(\theta_{t}\) means topic distribution of the time \(t\). \(\Pi_{tk}\) implies the time-specific and topic-specific emotion distribution. \(\varphi_{tk}\) refers to the aspect-opinion pair distribution under sentiment \(e\) and topic \(k\). In order to obtain the three above-mentioned distributions, we firstly have to deduce a joint posterior distribution of emotion and topic for each word pair. The following is the description of the formula derivation result. The symbols in the formulas are defined in Table 1.  
\[ \begin{array}{l}P(s_{i}=j,z_{i}=a|_{-i}^{\rightarrow},\overset{\rightarrow} {\rightarrow}_{-i},w\overset{\rightarrow}{\rightarrow}_{-i}t_{-i}^{ \rightarrow},w_{i}=m,t_{i}=y,\alpha,\beta,\gamma)\\ \propto\frac{\{N_{aim}\}_{-i}+\beta_{m}}{\{N_{aj}\}_{-i}+\sum_{\
u=1}^{V} \beta_{\
u}\}\cdot\frac{\{N_{wi}\}_{-i}+\gamma_{\
u}}{\{N_{w}\}_{-i}+\sum_{ \
u=1}^{V}\gamma_{\
u}}\cdot\frac{\{N_{w}\}_{-i}+a_{a}}{\{N_{y}\}_{-i}+\sum_{ \
u=1}^{V}\alpha_{\
u}}\end{array}  \tag{1}\]  
Formula (1) is the conditional posterior for \(s_{i}\) and \(z_{i}\), which is the conditional probability derived by marginalizing out the random variables \(\theta_{t}\), \(\Pi_{tk}\), and \(\varphi_{ke}\). The subscript \(-i\) denotes a quantity that does not include data from \(i\)th position. According to Formula (1), the [PERSON] sampling algorithm respectively samples an emotional label and a topic label for each word pair of each comment. The sampling process is executed iteratively until the sample results are stable. As for the Gibbs sampling algorithm of our model, we make a generalization in Algorithm 2. After getting the results of the sampling, we can use the sample obtained from the Markov chain to estimate the required posterior distribution. The three distributions can be estimated separately on the basis of Formulas (2)-(4). The calculation formula of aspect-opinion pair distribution under sentiment \(e\) and topic \(k\) is:  
\[\varphi_{kev}=\frac{N_{kev}+\beta_{\
u}}{N_{ke}+\sum_{\
u=1}^{V} \beta_{\
u}} \tag{2}\] \begin{table}
\begin{tabular}{c c} \hline
**Matched Pattern** & **Examples** \\ \hline n.+a. & Taste (noun) is very (adv.) good (adj.) \\ \hline n.+v. & Price (noun) increases (verb) \\ \hline a.+n. & Lovely (adj.) package (noun) \\ \hline v.+n. & Like (verb) screen(noun) \\ \hline n.+a.+v. & Delivery (noun) fast (adj.) transport (verb) \\ \hline n.+v.+a. & Sellers (noun) deliver (verb) fast (adj.) \\ \hline \end{tabular}
\end{table} 
Table 2: Simplified matching mode. Formula (2) is the approximate estimated distribution of word pairs in topics and sentiment labels. Where \(N_{k\epsilon}\) is the number of word pairs assigned to the topic \(k\) and the emotional \(e\). \(\beta_{v}\) means the \(v\)th element of the vector \(\beta\). \(k\) is a topic token and \(e\) is an emotion token. ```
Input: Prior parameters \(\alpha\), \(\beta\), \(\gamma\), the number of topic \(K\), the number of sentiment \(E=2\), Time-words matrix \(W\) Output: Multinomial distribution \(\theta\), \(\varphi\), \(\Pi\), label matrix \(S\), \(Z\)
1. Initialize topic label matrix \(Z\) and sentiment label matrix \(S\) randomly
2. Calculate the value of \(K^{*}E^{*}V\) matrix \(\Omega\), \(T^{*}K^{*}E\) matrix \(\Psi\) and \(T^{*}K\) matrix \(\Phi\) based on matrix \(Z\) and matrix
3. For iteration = 1 to Niter do
4. for \(i\) in T do
5. for \(j\) in len(word[\(i\)]) do
6. Calculate the possibility \(P(s\),\(z)\) based on Formula (1), which word[\(i\)][\(j\)] is assigned
7. simultaneously to a sentiment label s and a topic label z
8.