forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.py
More file actions
129 lines (97 loc) · 4.03 KB
/
Copy pathmain_test.py
File metadata and controls
129 lines (97 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright 2016 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import six
import main
def test_get_request_body():
text = 'hello world'
body = main.get_request_body(text, syntax=True, entities=True,
sentiment=False)
assert body.get('document').get('content') == text
assert body.get('features').get('extract_syntax') is True
assert body.get('features').get('extract_entities') is True
assert body.get('features').get('extract_document_sentiment') is False
def test_get_sentiment_label():
assert main.get_sentiment_label(20.50) == 1
assert main.get_sentiment_label(-42.34) == -1
def test_to_sentiment_json():
doc_id = '12345'
sentiment = 23.344564
label = 1
sentiment_json = json.loads(
main.to_sentiment_json(doc_id, sentiment, label)
)
assert sentiment_json.get('doc_id') == doc_id
assert sentiment_json.get('sentiment') == 23.345
assert sentiment_json.get('label') == label
def test_process_movie_reviews():
service = main.get_service()
doc1 = main.Document('Top Gun was awesome and Tom Cruise rocked!', 'doc1',
'doc1')
doc2 = main.Document('Tom Cruise is a great actor.', 'doc2', 'doc2')
reader = [doc1, doc2]
swriter = six.StringIO()
ewriter = six.StringIO()
main.process_movie_reviews(service, reader, swriter, ewriter)
sentiments = swriter.getvalue().strip().split('\n')
entities = ewriter.getvalue().strip().split('\n')
sentiments = [json.loads(sentiment) for sentiment in sentiments]
entities = [json.loads(entity) for entity in entities]
# assert sentiments
assert sentiments[0].get('sentiment') == 0.9
assert sentiments[0].get('label') == 1
assert sentiments[1].get('sentiment') == 0.9
assert sentiments[1].get('label') == 1
# assert entities
assert len(entities) == 1
assert entities[0].get('name') == 'Tom Cruise'
assert (entities[0].get('wiki_url') ==
'http://en.wikipedia.org/wiki/Tom_Cruise')
assert entities[0].get('sentiment') == 1.8
def test_rank_positive_entities(capsys):
reader = [
('{"avg_sentiment": -12.0, '
'"name": "Patrick Macnee", "sentiment": -12.0}'),
('{"avg_sentiment": 5.0, '
'"name": "Paul Rudd", "sentiment": 5.0}'),
('{"avg_sentiment": -5.0, '
'"name": "Martha Plimpton", "sentiment": -5.0}'),
('{"avg_sentiment": 7.0, '
'"name": "Lucy (2014 film)", "sentiment": 7.0}')
]
main.rank_entities(reader, 'pos', topn=1, reverse_bool=False)
out, err = capsys.readouterr()
expected = ('{"avg_sentiment": 5.0, '
'"name": "Paul Rudd", "sentiment": 5.0}')
expected = ''.join(sorted(expected))
out = ''.join(sorted(out.strip()))
assert out == expected
def test_rank_negative_entities(capsys):
reader = [
('{"avg_sentiment": -12.0, '
'"name": "Patrick Macnee", "sentiment": -12.0}'),
('{"avg_sentiment": 5.0, '
'"name": "Paul Rudd", "sentiment": 5.0}'),
('{"avg_sentiment": -5.0, '
'"name": "Martha Plimpton", "sentiment": -5.0}'),
('{"avg_sentiment": 7.0, '
'"name": "Lucy (2014 film)", "sentiment": 7.0}')
]
main.rank_entities(reader, 'neg', topn=1, reverse_bool=True)
out, err = capsys.readouterr()
expected = ('{"avg_sentiment": -5.0, '
'"name": "Martha Plimpton", "sentiment": -5.0}')
expected = ''.join(sorted(expected))
out = ''.join(sorted(out.strip()))
assert out == expected