Sentiment analysis is one of the most useful Natural Language Processing (NLP) functionalities that can determine the tone (positive, negative) of the text (e.g., product reviews, comments, etc.).
This Machine Learning (ML) model was built using a dataset published on Kaggle and combines 100k Arabic reviews from hotels, books, movies, products, and a few airlines. Text (reviews) were cleaned by removing Arabic diacritics and non-Arabic characters. Predictions are calculated using the log-odds statistics, and method accuracy exceeds 75% which is not a bad performance for a model sized less than 30 KB.
For simplicity, we assumed that all the words in the first language spoken by the Semitic peoples consisted of bi-radicals (i.e., two sounds/letters). Therefore, we can handle the majority of Arabic word roots as being expanded by the addition of a third letter, with the resulting meaning having a semantic relation to the original bi-radical.
Arabic Review (sample input) | Sentiment (auto generated) |
الخدمة كانت بطيئة | Negative |
الإطلالة رائعة والطعام لذيذ | Positive |
التبريد لا يعمل والواي فاي ضعيفة | Negative |
النظافة مميزة وموظفي الاستقبال متعاونين | Positive |
جاءت القطعة مكسورة والعلبة مفتوحة | Negative |
المنتج مطابق للمواصفات والتسليم سريع | Positive |
<?php
$Arabic = new \ArPHP\I18N\Arabic();
$reviews = array('الخدمة كانت بطيئة',
'الإطلالة رائعة والطعام لذيذ',
'التبريد لا يعمل والواي فاي ضعيفة',
'النظافة مميزة وموظفي الاستقبال متعاونين',
'جاءت القطعة مكسورة والعلبة مفتوحة',
'المنتج مطابق للمواصفات والتسليم سريع');
echo <<< END
<center>
<table border="0" cellspacing="2" cellpadding="5" width="60%">
<tr>
<td bgcolor="#27509D" align="center" width="70%">
<b><font color="#ffffff">Arabic Review (sample input)</font></b>
</td>
<td bgcolor="#27509D" align="center" width="30%">
<b><font color="#ffffff">Sentiment (auto generated)</font></b>
</td>
</tr>
END;
foreach ($reviews as $review) {
if ($Arabic->arSentiment($review) > 0) {
$sentiment = 'Positive';
$bgcolor = '#E0F0FF';
} else {
$sentiment = 'Negative';
$bgcolor = '#FFF0FF';
}
echo '<tr><td bgcolor="'.$bgcolor.'" align="right">';
echo '<font face="Tahoma">'.$review.'</font></td>';
echo '<td bgcolor="'.$bgcolor.'" align="center">'.$sentiment.'</td></tr>';
}
echo '</table></center>';