Difference between revisions of "Orange: Sentimen Analysis Bahasa Indonesia"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://www.andrijohandri.id/2019/10/orange3-menambahkan-sentiment-analysis.html Bagi pengguna aplikasi Text Mining Orange3, tentu saja akan mengalami kesulitan saat...")
(No difference)

Revision as of 16:22, 7 October 2019

Sumber: https://www.andrijohandri.id/2019/10/orange3-menambahkan-sentiment-analysis.html


Bagi pengguna aplikasi Text Mining Orange3, tentu saja akan mengalami kesulitan saat akan melakukan penghitungan Sentiment Analysis , dkarenakan Orange3 hanya menyediakan dua bahasa dalam proses Sentimen Analysis yaitu bahasa Inggris dan Slovenia dalam method Liu Hiu .

Anda dapat menambahkan Bahasa Indonesia dalam metode Liu Hiu ini dengan sedikit modifikasi dan penambahan script python pada proses Sentiment Analysisnya yaitu dengan menambahkan file yang berisi kumpulan kata yang memiliki makna sentimen negatif dan sentimen positif dalam bahasa Indonesia.

Untuk menambahkan kata tersebut adalah sebagai berikut :

   Buka folder /usr/local/lib/python3.7/site-packages/orangecontrib/text/sentiment/resources , dalam folder tersebut terdapat dua file yaitu negatif_words_Slolex.txt yang berisi kata negatif dalam bahasa slovenian dan positive_words_Slolex.txt . Selanjutnya copy negatif_words_Slolex.txt menjadi negatif_words_Ina.txt dan selanjutnya file negatif_words_Ina.txt diedit dengan menghapuskan seluruh isi kata dari bahasa slovenia dan menambahkan kata dengan bahasa indonesai yang memiliki nilai negatif , demikain juga dengan file positive_words_Slolex.txt di copy menjadi file positive_words_Ina.txt, sehingga terdapat 4 file pada folder tersebut.
   Selanjutnya  buka folder /usr/local/lib/python3.7/site-packages/orangecontrib/text/sentiment , copy  file opinion_lexicon_lso.py menjadi file  opinion_lexicon_ina.py selanjutnya edit sperti script berikut ini:
   import os
   class opinion_lexicon_ina:
       resources_folder = os.path.dirname(__file__)
       @classmethod
       def positive(cls):
           with open(os.path.join(cls.resources_folder,
                                  'resources/positive_words_Ina.txt'),
                     'r') as f:
               return f.read().split('\n')
       @classmethod
       def negative(cls):
           with open(os.path.join(cls.resources_folder,
                                  'resources/negative_words_Ina.txt'),
                     'r') as f:
               return f.read().split('\n')
    Selanjutnya edit file __init__.py :
   import numpy as np
   from nltk.corpus import opinion_lexicon
   from nltk.sentiment import SentimentIntensityAnalyzer
   from orangecontrib.text import Corpus
   from orangecontrib.text.misc import wait_nltk_data
   from orangecontrib.text.preprocess import WordPunctTokenizer
   from orangecontrib.text.vectorization.base import SharedTransform, \
       VectorizationComputeValue
   from orangecontrib.text.sentiment.opinion_lexicon_ina import opinion_lexicon_ina


   class Liu_Hu_Sentiment:
       sentiments = ('sentiment',)
       name = 'Liu Hu'
       methods = {'English': opinion_lexicon,
                  'Indonesia': opinion_lexicon_ina
   }
       @wait_nltk_data
       def __init__(self, language):
           self.language = language
           self.positive = set(self.methods[language].positive())
           self.negative = set(self.methods[language].negative())
       def transform(self, corpus, copy=True):
           scores = []
           tokenizer = WordPunctTokenizer()
           tokens = tokenizer(corpus.documents)
           for doc in tokens:
               pos_words = sum(word in self.positive for word in doc)
               neg_words = sum(word in self.negative for word in doc)
               scores.append([100*(pos_words - neg_words)/max(len(doc), 1)])
           X = np.array(scores).reshape((-1, len(self.sentiments)))
           # set  compute values
           shared_cv = SharedTransform(self)
           cv = [VectorizationComputeValue(shared_cv, col)
                 for col in self.sentiments]
           if copy:
               corpus = corpus.copy()
           corpus.extend_attributes(X, self.sentiments, compute_values=cv)
           return corpus


   class Vader_Sentiment:
       sentiments = ('pos', 'neg', 'neu', 'compound')
       name = 'Vader'
       @wait_nltk_data
       def __init__(self):
           self.vader = SentimentIntensityAnalyzer()
       def transform(self, corpus, copy=True):
           scores = []
           for text in corpus.documents:
               pol_sc = self.vader.polarity_scores(text)
               scores.append([pol_sc[x] for x in self.sentiments])
           X = np.array(scores).reshape((-1, len(self.sentiments)))
           # set  compute values
           shared_cv = SharedTransform(self)
           cv = [VectorizationComputeValue(shared_cv, col)
                 for col in self.sentiments]
           if copy:
               corpus = corpus.copy()
           corpus.extend_attributes(X, self.sentiments, compute_values=cv)
           return corpus
   if __name__ == "__main__":
       corpus = Corpus.from_file('deerwester')
       liu = Liu_Hu_Sentiment('Indonesia')
       corpus2 = liu.transform(corpus[:5])
   Agar Widget Sentiment Analysis terdapat pilihan bahasa Indonesia , selanjutnya edit file /usr/local/lib/python3.7/dist-packages/orangecontrib/text/widgets/owsentimentanalysis.py
   from AnyQt.QtCore import Qt
   from AnyQt.QtWidgets import QApplication, QGridLayout, QLabel
   from Orange.widgets import gui, settings
   from Orange.widgets.utils.signals import Input, Output
   from Orange.widgets.widget import OWWidget
   from orangecontrib.text import Corpus
   from orangecontrib.text.sentiment import Vader_Sentiment, Liu_Hu_Sentiment
   class OWSentimentAnalysis(OWWidget):
       name = "Sentiment Analysis"
       description = "Predict sentiment from text."
       icon = "icons/SentimentAnalysis.svg"
       priority = 320
       class Inputs:
           corpus = Input("Corpus", Corpus)
       class Outputs:
           corpus = Output("Corpus", Corpus)
       method_idx = settings.Setting(1)
       autocommit = settings.Setting(True)
       language = settings.Setting('English')
       want_main_area = False
       resizing_enabled = False
       METHODS = [
           Liu_Hu_Sentiment,
           Vader_Sentiment
       ]
       LANG = ['English', 'Indonesia']
       def __init__(self):
           super().__init__()
           self.corpus = None
           form = QGridLayout()
           self.method_box = box = gui.radioButtonsInBox(
               self.controlArea, self, "method_idx", [], box="Method",
               orientation=form, callback=self._method_changed)
           self.liu_hu = gui.appendRadioButton(box, "Liu Hu", addToLayout=False)
           self.liu_lang = gui.comboBox(None, self, 'language',
                                        sendSelectedValue=True,
                                        items=self.LANG,
                                        callback=self._method_changed)
           self.vader = gui.appendRadioButton(box, "Vader", addToLayout=False)
           form.addWidget(self.liu_hu, 0, 0, Qt.AlignLeft)
           form.addWidget(QLabel("Language:"), 0, 1, Qt.AlignRight)
           form.addWidget(self.liu_lang, 0, 2, Qt.AlignRight)
           form.addWidget(self.vader, 1, 0, Qt.AlignLeft)
           ac = gui.auto_commit(self.controlArea, self, 'autocommit', 'Commit',
                                'Autocommit is on')
           ac.layout().insertSpacing(1, 8)
       @Inputs.corpus
       def set_corpus(self, data=None):
           self.corpus = data
           self.commit()
       def _method_changed(self):
           self.commit()
       def commit(self):
           if self.corpus is not None:
               method = self.METHODS[self.method_idx]
               if self.method_idx == 0:
                   out = method(language=self.language).transform(self.corpus)
               else:
                   out = method().transform(self.corpus)
               self.Outputs.corpus.send(out)
           else:
               self.Outputs.corpus.send(None)
       def send_report(self):
           self.report_items((
               ('Method', self.METHODS[self.method_idx].name),
           ))
   def main():
       app = QApplication([])
       widget = OWSentimentAnalysis()
       corpus = Corpus.from_file('book-excerpts')
       corpus = corpus[:3]
       widget.set_corpus(corpus)
       widget.show()
       app.exec()
   if __name__ == '__main__':
       main()


Selanjutnya anda kini sudah bisa menggunakan Sentiment Analysis dengan menggunakan analisis NLTK dengan menggunakan bahasa indonesia




Referensi

Pranala Menarik