Skip to content

Commit

Permalink
Sentiment - Store ISO language in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Jan 26, 2024
1 parent 71c8bea commit 2241d4d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
56 changes: 26 additions & 30 deletions orangecontrib/text/widgets/owsentimentanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from AnyQt.QtCore import Qt
from AnyQt.QtWidgets import QGridLayout, QLabel

from Orange.widgets import gui, settings
from Orange.widgets import gui
from Orange.widgets.utils.concurrent import ConcurrentWidgetMixin, TaskState
from Orange.widgets.utils.signals import Input, Output
from Orange.widgets.widget import OWWidget, Msg
from orangewidget.settings import Setting

from orangecontrib.text import Corpus, preprocess
from orangecontrib.text.language import ISO2LANG, LANG2ISO
from orangecontrib.text.language import LanguageModel, LANG2ISO
from orangecontrib.text.sentiment import (
VaderSentiment,
LiuHuSentiment,
Expand Down Expand Up @@ -37,24 +39,16 @@ class Inputs:
class Outputs:
corpus = Output("Corpus", Corpus)

settings_version = 1
settings_version = 2
want_main_area = False
resizing_enabled = False

method_idx: int = settings.Setting(1)
autocommit: bool = settings.Setting(True)
liu_language: str = settings.Setting(
ISO2LANG[LiuHuSentiment.DEFAULT_LANG], schema_only=True
)
multi_language: str = settings.Setting(
ISO2LANG[MultiSentiment.DEFAULT_LANG], schema_only=True
)
senti_language: str = settings.Setting(
ISO2LANG[SentiArt.DEFAULT_LANG], schema_only=True
)
lilah_language: str = settings.Setting(
ISO2LANG[LilahSentiment.DEFAULT_LANG], schema_only=True
)
method_idx: int = Setting(1)
autocommit: bool = Setting(True)
liu_language: str = Setting(LiuHuSentiment.DEFAULT_LANG, schema_only=True)
multi_language: str = Setting(MultiSentiment.DEFAULT_LANG, schema_only=True)
senti_language: str = Setting(SentiArt.DEFAULT_LANG, schema_only=True)
lilah_language: str = Setting(LilahSentiment.DEFAULT_LANG, schema_only=True)

METHODS = [
LiuHuSentiment,
Expand Down Expand Up @@ -99,9 +93,8 @@ def __init__(self):
None,
self,
"liu_language",
sendSelectedValue=True,
contentsLength=10,
items=[ISO2LANG[lg] for lg in LiuHuSentiment.LANGUAGES],
model=LanguageModel(languages=LiuHuSentiment.LANGUAGES),
callback=self._method_changed,
)
self.vader = gui.appendRadioButton(box, "Vader", addToLayout=False)
Expand All @@ -112,9 +105,8 @@ def __init__(self):
None,
self,
"multi_language",
sendSelectedValue=True,
contentsLength=10,
items=[ISO2LANG[lg] for lg in MultiSentiment.LANGUAGES],
model=LanguageModel(languages=MultiSentiment.LANGUAGES),
callback=self._method_changed,
)
self.senti_art = gui.appendRadioButton(box, "SentiArt", addToLayout=False)
Expand All @@ -124,7 +116,7 @@ def __init__(self):
"senti_language",
sendSelectedValue=True,
contentsLength=10,
items=[ISO2LANG[lg] for lg in SentiArt.LANGUAGES],
model=LanguageModel(languages=SentiArt.LANGUAGES),
callback=self._method_changed,
)
self.lilah_sent = gui.appendRadioButton(
Expand All @@ -134,9 +126,8 @@ def __init__(self):
None,
self,
"lilah_language",
sendSelectedValue=True,
contentsLength=10,
items=[ISO2LANG[lg] for lg in LilahSentiment.LANGUAGES],
model=LanguageModel(languages=LilahSentiment.LANGUAGES),
callback=self._method_changed,
)
self.custom_list = gui.appendRadioButton(
Expand Down Expand Up @@ -228,10 +219,10 @@ def __set_language_settings(self):

for l_pending, l_setting, model in settings_:
if self.pp_corpus and self.pp_corpus.language in model.LANGUAGES:
setattr(self, l_setting, ISO2LANG[self.pp_corpus.language])
setattr(self, l_setting, self.pp_corpus.language)
else:
# if Corpus's language not supported use default language
setattr(self, l_setting, ISO2LANG[model.DEFAULT_LANG])
setattr(self, l_setting, model.DEFAULT_LANG)

# when workflow loaded use language saved in workflow
if l_pending is not None:
Expand All @@ -249,13 +240,13 @@ def _compute_sentiment(self):
method = self.METHODS[self.method_idx]
kwargs = {}
if method.name == "Liu Hu":
kwargs = dict(language=LANG2ISO[self.liu_language])
kwargs = dict(language=self.liu_language)
elif method.name == "Multilingual Sentiment":
kwargs = dict(language=LANG2ISO[self.multi_language])
kwargs = dict(language=self.multi_language)
elif method.name == "SentiArt":
kwargs = dict(language=LANG2ISO[self.senti_language])
kwargs = dict(language=self.senti_language)
elif method.name == "LiLaH Sentiment":
kwargs = dict(language=LANG2ISO[self.lilah_language])
kwargs = dict(language=self.lilah_language)
elif method.name == "Custom Dictionaries":
kwargs = dict(pos=self.pos_file, neg=self.neg_file)
if bool(self.pos_file) != bool(self.neg_file): # xor: one of them None
Expand Down Expand Up @@ -313,6 +304,11 @@ def migrate_settings(cls, settings, version):
method_idx = settings["method_idx"]
if method_idx == 4:
settings["metric_idx"] = 5
if version is None or version < 2:
s = ("liu_language", "lilah_language", "multi_language", "senti_language")
for lang_set in s:
if lang_set in settings:
settings[lang_set] = LANG2ISO[settings[lang_set]]


if __name__ == '__main__':
Expand Down
39 changes: 28 additions & 11 deletions orangecontrib/text/widgets/tests/test_owsentimentanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from orangecontrib.text import preprocess
from orangecontrib.text.corpus import Corpus
from orangecontrib.text.language import ISO2LANG
from orangecontrib.text.sentiment import DictionaryNotFound
from orangecontrib.text.sentiment import (
DictionaryNotFound, LiuHuSentiment, MultiSentiment, SentiArt, LilahSentiment
)
from orangecontrib.text.widgets.owsentimentanalysis import OWSentimentAnalysis

MS_FILES = [
Expand Down Expand Up @@ -164,6 +166,21 @@ def test_migrates_settings(self):
OWSentimentAnalysis.migrate_settings(settings, version=None)
self.assertTrue(settings.get("method_idx", 5))

def test_migrate_language_settings(self):
methods = (
("liu_language", LiuHuSentiment),
("multi_language", MultiSentiment),
("senti_language", SentiArt),
("lilah_language", LilahSentiment),
)
for setting, method in methods:
if hasattr(method, "LANGUAGES"):
print(method)
for lang in getattr(method, "LANGUAGES"):
se = {setting: ISO2LANG[lang], "__version__": 1}
widget = self.create_widget(OWSentimentAnalysis, stored_settings=se)
self.assertEqual(lang, getattr(widget, setting))

def test_preprocessed(self):
widget = self.create_widget(OWSentimentAnalysis)
corpus = self.corpus.copy()
Expand All @@ -184,7 +201,7 @@ def test_language_from_corpus(self):
w = self.widget
settings = [
w.liu_language,
"English",
"en",
w.multi_language,
w.senti_language,
w.lilah_language,
Expand All @@ -198,7 +215,7 @@ def test_language_from_corpus(self):
self.send_signal(self.widget.Inputs.corpus, self.corpus)
self.widget.findChildren(QRadioButton)[i].click()
self.assertIsNotNone(self.get_output(self.widget.Outputs.corpus))
self.assertEqual(ISO2LANG[s], sett)
self.assertEqual(s, sett)

# try with unsupported language - use default language istead
self.corpus.attributes["language"] = ns
Expand All @@ -213,18 +230,18 @@ def test_language_from_settings(self):
simulate.combobox_activate_item(self.widget.senti_box, "German")
simulate.combobox_activate_item(self.widget.lilah_box, "Croatian")

self.assertEqual("Slovenian", self.widget.liu_language)
self.assertEqual("Spanish", self.widget.multi_language)
self.assertEqual("German", self.widget.senti_language)
self.assertEqual("Croatian", self.widget.lilah_language)
self.assertEqual("sl", self.widget.liu_language)
self.assertEqual("es", self.widget.multi_language)
self.assertEqual("de", self.widget.senti_language)
self.assertEqual("hr", self.widget.lilah_language)
settings = self.widget.settingsHandler.pack_data(self.widget)

widget = self.create_widget(OWSentimentAnalysis, stored_settings=settings)
self.send_signal(widget.Inputs.corpus, self.corpus, widget=widget)
self.assertEqual("Slovenian", widget.liu_language)
self.assertEqual("Spanish", widget.multi_language)
self.assertEqual("German", widget.senti_language)
self.assertEqual("Croatian", widget.lilah_language)
self.assertEqual("sl", widget.liu_language)
self.assertEqual("es", widget.multi_language)
self.assertEqual("de", widget.senti_language)
self.assertEqual("hr", widget.lilah_language)

def test_dictionary_offline(self):
"""Test case when offline and dictionary not found locally"""
Expand Down

0 comments on commit 2241d4d

Please sign in to comment.