<jupyter_start><jupyter_text>All models got `get_proba` parameters.If True, it will returned probability every classes. Else, it will return highest probability class. **Default is True.**I suggest you to use deep learning models to understand emotion, these models are better for detecting emotions from social media.  Load multinomial model<jupyter_code>model = malaya.emotion.multinomial()
print(model.predict(anger_text))
print(model.predict(anger_text,get_proba=True))
model.predict_batch([anger_text,
                    fear_text,
                    joy_text,
                    love_text,
                    sadness_text,
                    surprise_text])<jupyter_output>joy
{'anger': 0.11733952772643724, 'fear': 0.15199690452609127, 'joy': 0.2589124946219144, 'love': 0.2024854510784686, 'sadness': 0.2211704631949802, 'surprise': 0.048095158852106114}<jupyter_text>Load xgb model<jupyter_code>model = malaya.emotion.xgb()
print(model.predict(anger_text))
print(model.predict(anger_text,get_proba=True))
model.predict_batch([anger_text,
                    fear_text,
                    joy_text,
                    love_text,
                    sadness_text,
                    surprise_text],get_proba=True)<jupyter_output>anger
{'anger': 0.37050414, 'fear': 0.17237191, 'joy': 0.21709572, 'love': 0.040401105, 'sadness': 0.16886166, 'surprise': 0.030765474}<jupyter_text>List available deep learning models<jupyter_code>malaya.emotion.available_deep_model()<jupyter_output><empty_output><jupyter_text>Load deep learning modelsGood thing about deep learning models from Malaya, it returns `Attention` result, means, which part of words give the high impact to the results. But to get `Attention`, you need to set `get_proba=True`.<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
sns.set()<jupyter_output><empty_output><jupyter_text>Load bahdanau model<jupyter_code>model = malaya.emotion.deep_model('bahdanau')<jupyter_output><empty_output><jupyter_text>Predict single string<jupyter_code>model.predict(anger_text)
model.predict(fear_text)
result = model.predict(sadness_text,get_proba=True)
result
plt.figure(figsize = (15, 5))
keys = result['attention'].keys()
values = result['attention'].values()
aranged = [i for i in range(len(keys))]
plt.bar(aranged, values)
plt.xticks(aranged, keys, rotation = 'vertical')
plt.show()<jupyter_output><empty_output><jupyter_text>Open emotion visualization dashboardDefault when you call `predict_words` it will open a browser with visualization dashboard, you can disable by `visualization=False`.<jupyter_code>model.predict_words(sadness_text)
from IPython.core.display import Image, display

display(Image('emotion-bahdanau.png', width=800))<jupyter_output><empty_output><jupyter_text>I tried to put the html and javascript inside a notebook cell, pretty hard you know and a lot of weird bugs. Let stick to HTTP serving ya.`predict_words` only accept a single string. You can't predict multiple texts.  Predict batch of strings<jupyter_code>model.predict_batch([anger_text, fear_text, joy_text,
                    love_text, sadness_text, surprise_text],get_proba=True)<jupyter_output><empty_output><jupyter_text>**You might want to try `luong` and `self-attention` by yourself.**  BERT modelBERT is the best emotion model in term of accuracy, you can check emotion accuracy here, https://malaya.readthedocs.io/en/latest/Accuracy.htmlemotion-analysis. But warning, the model size is 700MB! Make sure you have enough resources to use BERT, and installed `bert-tensorflow` first,```bashpip3 install bert-tensorflow```<jupyter_code>model = malaya.emotion.bert()
model.predict_batch([anger_text, fear_text, joy_text,
                    love_text, sadness_text, surprise_text],get_proba=True)<jupyter_output><empty_output><jupyter_text>Stacking modelsMore information, you can read at [https://malaya.readthedocs.io/en/latest/Stack.html](https://malaya.readthedocs.io/en/latest/Stack.html)<jupyter_code>multinomial = malaya.emotion.multinomial()
xgb = malaya.emotion.xgb()
malaya.stack.predict_stack([multinomial, xgb, model], anger_text)<jupyter_output><empty_output><jupyter_text>Load Sparse deep learning models What happen if a word not included in the dictionary of the models? like `setan`, what if `setan` appeared in text we want to classify? We found this problem when classifying social media texts / posts. Words used not really a vocabulary-based contextual.Malaya will treat **unknown words** as ``, so, to solve this problem, we need to use N-grams character based. Malaya chose tri-grams until fifth-grams.```pythonsetan = ['set', 'eta', 'tan']```Sklearn provided easy interface to use n-grams, problem is, it is very sparse, a lot of zeros and not memory efficient. Sklearn returned sparse matrix for the result, lucky Tensorflow already provided some sparse function.<jupyter_code>malaya.emotion.available_sparse_deep_model()<jupyter_output><empty_output><jupyter_text>Right now Malaya only provide 1 sparse model, `fast-text-char`. We will try to evolve it.<jupyter_code>sparse_model = malaya.emotion.sparse_deep_model()
sparse_model.predict(love_text)
sparse_model.predict_batch([anger_text,
                    fear_text,
                    joy_text,
                    love_text,
                    sadness_text,
                    surprise_text])
sparse_model.predict_batch([anger_text,
                    fear_text,
                    joy_text,
                    love_text,
                    sadness_text,
                    surprise_text], get_proba = True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09641135511515801, 'fraction_numerical': 0.030887341546152473, 'mean_word_length': 4.486777668952008, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '319433', 'license': 'MIT', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1714, 'n_words': 443, 'path': 'example/emotion/load-emotion.ipynb', 'repo_name': 'leowmjw/Malaya', 'size': '5601'}