Orange: Sentimen Analysis Bahasa Indonesia: Difference between revisions

From OnnoCenterWiki
Jump to navigationJump to search
Onnowpurbo (talk | contribs)
No edit summary
Onnowpurbo (talk | contribs)
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Sumber: https://www.andrijohandri.id/2019/10/orange3-menambahkan-sentiment-analysis.html
Sumber: https://www.andrijohandri.id/2019/10/orange3-menambahkan-sentiment-analysis.html
Oleh: Andri Johandri




Line 10: Line 12:
Untuk menambahkan kata tersebut  adalah sebagai berikut :
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.
* 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:
* 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)
import os
        autocommit = settings.Setting(True)
class opinion_lexicon_ina:
        language = settings.Setting('English')
    resources_folder = os.path.dirname(__file__)
        want_main_area = False
    @classmethod
        resizing_enabled = False
    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 :


         METHODS = [
import numpy as np
             Liu_Hu_Sentiment,
from nltk.corpus import opinion_lexicon
            Vader_Sentiment
from nltk.sentiment import SentimentIntensityAnalyzer
         ]
  from orangecontrib.text import Corpus
         LANG = ['English', 'Indonesia']
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])


        def __init__(self):
* Agar Widget Sentiment Analysis terdapat pilihan bahasa Indonesia , selanjutnya edit file /usr/local/lib/python3.7/dist-packages/orangecontrib/text/widgets/owsentimentanalysis.py
            super().__init__()
            self.corpus = None


            form = QGridLayout()
from AnyQt.QtCore import Qt
            self.method_box = box = gui.radioButtonsInBox(
from AnyQt.QtWidgets import QApplication, QGridLayout, QLabel
                self.controlArea, self, "method_idx", [], box="Method",
                orientation=form, callback=self._method_changed)
from Orange.widgets import gui, settings
            self.liu_hu = gui.appendRadioButton(box, "Liu Hu", addToLayout=False)
from Orange.widgets.utils.signals import Input, Output
            self.liu_lang = gui.comboBox(None, self, 'language',
from Orange.widgets.widget import OWWidget
                                        sendSelectedValue=True,
from orangecontrib.text import Corpus
                                        items=self.LANG,
from orangecontrib.text.sentiment import Vader_Sentiment, Liu_Hu_Sentiment
                                        callback=self._method_changed)
            self.vader = gui.appendRadioButton(box, "Vader", addToLayout=False)
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),
        ))


            form.addWidget(self.liu_hu, 0, 0, Qt.AlignLeft)
def main():
            form.addWidget(QLabel("Language:"), 0, 1, Qt.AlignRight)
     app = QApplication([])
            form.addWidget(self.liu_lang, 0, 2, Qt.AlignRight)
    widget = OWSentimentAnalysis()
            form.addWidget(self.vader, 1, 0, Qt.AlignLeft)
    corpus = Corpus.from_file('book-excerpts')
 
    corpus = corpus[:3]
            ac = gui.auto_commit(self.controlArea, self, 'autocommit', 'Commit',
    widget.set_corpus(corpus)
                                'Autocommit is on')
    widget.show()
            ac.layout().insertSpacing(1, 8)
    app.exec()
 
        @Inputs.corpus
if __name__ == '__main__':
        def set_corpus(self, data=None):
    main()
            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  
Selanjutnya anda kini sudah bisa menggunakan Sentiment Analysis dengan menggunakan analisis NLTK dengan menggunakan bahasa indonesia  


==Referensi==
==Referensi==

Latest revision as of 00:49, 28 January 2020

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

Oleh: Andri Johandri


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