Skip to content

Commit b71e639

Browse files
authored
Targeted emotion (#1)
* add targeted emotion * fix examples
1 parent 38bd90a commit b71e639

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

examples/alchemy_language_v1.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88

99
print(json.dumps(alchemy_language.targeted_sentiment(text='I love cats! Dogs are smelly.',
1010
targets=['cats', 'dogs'], language='english'), indent=2))
11+
# print(json.dumps(alchemy_language.targeted_emotion(text='I love apples. I hate bananas',
12+
# targets=['apples', 'bananas'], language='english'), indent=2))
13+
1114
# print(json.dumps(alchemy_language.author(url=url), indent=2))
12-
# print(json.dumps(alchemy_language.keywords(max_items=5, url=url), indent=2))
1315
# print(json.dumps(alchemy_language.concepts(max_items=2, url=url), indent=2))
14-
# print(json.dumps(alchemy_language.entities(url=url), indent=2))
1516
# print(json.dumps(alchemy_language.dates(url=url, anchor_date='2016-03-22 00:00:00'), indent=2))
17+
# print(json.dumps(alchemy_language.emotion(url=url), indent=2))
18+
# print(json.dumps(alchemy_language.entities(url=url), indent=2))
19+
# print(json.dumps(alchemy_language.keywords(max_items=5, url=url), indent=2))
1620
# print(json.dumps(alchemy_language.category(url=url), indent=2))
1721
# print(json.dumps(alchemy_language.typed_relations(url=url), indent=2))
1822
# print(json.dumps(alchemy_language.relations(url=url), indent=2))
@@ -26,3 +30,7 @@
2630
# print(json.dumps(alchemy_language.taxonomy(url=url), indent=2))
2731
combined_operations = ['page-image', 'entity', 'keyword', 'title', 'author', 'taxonomy', 'concept', 'doc-emotion']
2832
print(json.dumps(alchemy_language.combined(url=url, extract=combined_operations), indent=2))
33+
34+
# Get sentiment and emotion information results for detected entities/keywords:
35+
# print(json.dumps(alchemy_language.entities(url=url, sentiment=True, emotion=True), indent=2))
36+
# print(json.dumps(alchemy_language.keywords(max_items=5, url=url, sentiment=True, emotion=True), indent=2))

watson_developer_cloud/alchemy_language_v1.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def authors(self, html=None, url=None, language=None):
3333
params = {'language': language}
3434
return self._alchemy_html_request('GetAuthors', html=html, url=url, params=params)
3535

36-
def keywords(self, html=None, text=None, url=None, strict_extract_mode=False, sentiment=False,
36+
def keywords(self, html=None, text=None, url=None, strict_extract_mode=False, sentiment=False, emotion=False,
3737
show_source_text=False, max_items=None, language=None, max_keywords=50):
3838
"""
3939
:param html: HTML input
@@ -47,6 +47,7 @@ def keywords(self, html=None, text=None, url=None, strict_extract_mode=False, se
4747
max_items = max_keywords
4848
params = {'keywordExtractMode': 'strict' if strict_extract_mode else 'normal',
4949
'sentiment': sentiment,
50+
'emotion': emotion,
5051
'showSourceText': show_source_text,
5152
'maxRetrieve': max_items,
5253
'language': language}
@@ -67,12 +68,13 @@ def dates(self, html=None, text=None, url=None, anchor_date=None, show_source_te
6768
return self._alchemy_html_request('ExtractDates', html=html, text=text, url=url, params=params)
6869

6970
def entities(self, html=None, text=None, url=None, disambiguate=True, linked_data=True, coreference=True,
70-
quotations=False, sentiment=False, show_source_text=False, max_items=50, language=None, model=None):
71+
quotations=False, sentiment=False, emotion=False, show_source_text=False, max_items=50, language=None, model=None):
7172
params = {'disambiguate': disambiguate,
7273
'linkedData': linked_data,
7374
'coreference': coreference,
7475
'quotations': quotations,
7576
'sentiment': sentiment,
77+
'emotion': emotion,
7678
'showSourceText': show_source_text,
7779
'maxRetrieve': max_items,
7880
'language': language,
@@ -88,8 +90,22 @@ def emotion(self, html=None, text=None, url=None, show_source_text=False, source
8890
'language': language}
8991
return self._alchemy_html_request('GetEmotion', html=html, text=text, url=url, params=params)
9092

91-
def typed_relations(self, html=None, text=None, url=None, model=None):
92-
params = {'model': model}
93+
def targeted_emotion(self, targets, html=None, text=None, url=None, language=None, constraint_query=None,
94+
xpath_query=None, show_source_text=False, source_text_type=None):
95+
if isinstance(targets, list):
96+
targets = '|'.join(targets)
97+
98+
params = {'targets': targets,
99+
'language': language,
100+
'cquery': constraint_query,
101+
'xpath': xpath_query,
102+
'showSourceText': show_source_text,
103+
'sourceText': source_text_type}
104+
return self._alchemy_html_request('GetTargetedEmotion', html=html, text=text, url=url, params=params)
105+
106+
def typed_relations(self, html=None, text=None, url=None, model=None, show_source_text=False):
107+
params = {'model': model,
108+
'showSourceText': show_source_text}
93109
return self._alchemy_html_request('GetTypedRelations', html=html, text=text, url=url, params=params)
94110

95111
def relations(self, html=None, text=None, url=None, sentiment=False, keywords=False, entities=False,
@@ -164,7 +180,7 @@ def taxonomy(self, html=None, text=None, url=None, show_source_text=False, sourc
164180
'sourceText': source_text_type,
165181
'cquery': constraint_query,
166182
'xpath': xpath_query,
167-
'base_url': base_url,
183+
'baseUrl': base_url,
168184
'language': language}
169185
return self._alchemy_html_request('GetRankedTaxonomy', html=html, text=text, url=url, params=params)
170186

@@ -236,9 +252,15 @@ def sentiment(self, html=None, text=None, url=None, language=None):
236252
params = {'language': language}
237253
return self._alchemy_html_request('GetTextSentiment', html=html, text=text, url=url, params=params)
238254

239-
def targeted_sentiment(self, targets, html=None, text=None, url=None, language=None):
255+
def targeted_sentiment(self, targets, html=None, text=None, url=None, language=None, constraint_query=None,
256+
xpath_query=None, show_source_text=False, source_text_type=None):
240257
if isinstance(targets, list):
241258
targets = '|'.join(targets)
242259

243-
params = {'targets': targets, 'language': language}
260+
params = {'targets': targets,
261+
'language': language,
262+
'cquery': constraint_query,
263+
'xpath': xpath_query,
264+
'showSourceText': show_source_text,
265+
'sourceText': source_text_type}
244266
return self._alchemy_html_request('GetTargetedSentiment', html=html, text=text, url=url, params=params)

0 commit comments

Comments
 (0)