27
27
from google .cloud .gapic .language .v1beta2 import enums
28
28
from google .cloud .gapic .language .v1beta2 import language_service_client
29
29
from google .cloud .proto .language .v1beta2 import language_service_pb2
30
+ import six
30
31
31
32
32
33
def sentiment_text (text ):
33
34
"""Detects sentiment in the text."""
34
35
language_client = language .Client (api_version = 'v1beta2' )
35
36
37
+ if isinstance (text , six .binary_type ):
38
+ text = text .decode ('utf-8' )
39
+
36
40
# Instantiates a plain text document.
37
41
document = language_client .document_from_text (text )
38
42
39
43
# Detects sentiment in the document. You can also analyze HTML with:
40
44
# document.doc_type == language.Document.HTML
41
45
sentiment = document .analyze_sentiment ().sentiment
42
46
43
- print ('Score: {}' .format (sentiment .score ))
44
- print ('Magnitude: {}' .format (sentiment .magnitude ))
47
+ print (u 'Score: {}' .format (sentiment .score ))
48
+ print (u 'Magnitude: {}' .format (sentiment .magnitude ))
45
49
46
50
47
51
def sentiment_file (gcs_uri ):
@@ -55,14 +59,17 @@ def sentiment_file(gcs_uri):
55
59
# document.doc_type == language.Document.HTML
56
60
sentiment = document .analyze_sentiment ().sentiment
57
61
58
- print ('Score: {}' .format (sentiment .score ))
59
- print ('Magnitude: {}' .format (sentiment .magnitude ))
62
+ print (u 'Score: {}' .format (sentiment .score ))
63
+ print (u 'Magnitude: {}' .format (sentiment .magnitude ))
60
64
61
65
62
66
def entities_text (text ):
63
67
"""Detects entities in the text."""
64
68
language_client = language .Client (api_version = 'v1beta2' )
65
69
70
+ if isinstance (text , six .binary_type ):
71
+ text = text .decode ('utf-8' )
72
+
66
73
# Instantiates a plain text document.
67
74
document = language_client .document_from_text (text )
68
75
@@ -71,12 +78,12 @@ def entities_text(text):
71
78
entities = document .analyze_entities ().entities
72
79
73
80
for entity in entities :
74
- print ('=' * 20 )
75
- print ('{:<16}: {}' .format ('name' , entity .name ))
76
- print ('{:<16}: {}' .format ('type' , entity .entity_type ))
77
- print ('{:<16}: {}' .format ('metadata' , entity .metadata ))
78
- print ('{:<16}: {}' .format ('salience' , entity .salience ))
79
- print ('{:<16}: {}' .format ('wikipedia_url' ,
81
+ print (u '=' * 20 )
82
+ print (u '{:<16}: {}' .format ('name' , entity .name ))
83
+ print (u '{:<16}: {}' .format ('type' , entity .entity_type ))
84
+ print (u '{:<16}: {}' .format ('metadata' , entity .metadata ))
85
+ print (u '{:<16}: {}' .format ('salience' , entity .salience ))
86
+ print (u '{:<16}: {}' .format ('wikipedia_url' ,
80
87
entity .metadata .get ('wikipedia_url' , '-' )))
81
88
82
89
@@ -105,6 +112,9 @@ def syntax_text(text):
105
112
"""Detects syntax in the text."""
106
113
language_client = language .Client (api_version = 'v1beta2' )
107
114
115
+ if isinstance (text , six .binary_type ):
116
+ text = text .decode ('utf-8' )
117
+
108
118
# Instantiates a plain text document.
109
119
document = language_client .document_from_text (text )
110
120
@@ -113,7 +123,7 @@ def syntax_text(text):
113
123
tokens = document .analyze_syntax ().tokens
114
124
115
125
for token in tokens :
116
- print ('{}: {}' .format (token .part_of_speech , token .text_content ))
126
+ print (u '{}: {}' .format (token .part_of_speech , token .text_content ))
117
127
118
128
119
129
def syntax_file (gcs_uri ):
@@ -128,14 +138,17 @@ def syntax_file(gcs_uri):
128
138
tokens = document .analyze_syntax ().tokens
129
139
130
140
for token in tokens :
131
- print ('{}: {}' .format (token .part_of_speech , token .text_content ))
141
+ print (u '{}: {}' .format (token .part_of_speech , token .text_content ))
132
142
133
143
134
144
def entity_sentiment_text (text ):
135
145
"""Detects entity sentiment in the provided text."""
136
146
language_client = language_service_client .LanguageServiceClient ()
137
147
document = language_service_pb2 .Document ()
138
148
149
+ if isinstance (text , six .binary_type ):
150
+ text = text .decode ('utf-8' )
151
+
139
152
document .content = text .encode ('utf-8' )
140
153
document .type = enums .Document .Type .PLAIN_TEXT
141
154
@@ -144,15 +157,15 @@ def entity_sentiment_text(text):
144
157
145
158
for entity in result .entities :
146
159
print ('Mentions: ' )
147
- print ('Name: "{}"' .format (entity .name ))
160
+ print (u 'Name: "{}"' .format (entity .name ))
148
161
for mention in entity .mentions :
149
- print (' Begin Offset : {}' .format (mention .text .begin_offset ))
150
- print (' Content : {}' .format (mention .text .content ))
151
- print (' Magnitude : {}' .format (mention .sentiment .magnitude ))
152
- print (' Sentiment : {}' .format (mention .sentiment .score ))
153
- print (' Type : {}' .format (mention .type ))
154
- print ('Salience: {}' .format (entity .salience ))
155
- print ('Sentiment: {}\n ' .format (entity .sentiment ))
162
+ print (u ' Begin Offset : {}' .format (mention .text .begin_offset ))
163
+ print (u ' Content : {}' .format (mention .text .content ))
164
+ print (u ' Magnitude : {}' .format (mention .sentiment .magnitude ))
165
+ print (u ' Sentiment : {}' .format (mention .sentiment .score ))
166
+ print (u ' Type : {}' .format (mention .type ))
167
+ print (u 'Salience: {}' .format (entity .salience ))
168
+ print (u 'Sentiment: {}\n ' .format (entity .sentiment ))
156
169
157
170
158
171
def entity_sentiment_file (gcs_uri ):
@@ -167,15 +180,15 @@ def entity_sentiment_file(gcs_uri):
167
180
document , enums .EncodingType .UTF8 )
168
181
169
182
for entity in result .entities :
170
- print ('Name: "{}"' .format (entity .name ))
183
+ print (u 'Name: "{}"' .format (entity .name ))
171
184
for mention in entity .mentions :
172
- print (' Begin Offset : {}' .format (mention .text .begin_offset ))
173
- print (' Content : {}' .format (mention .text .content ))
174
- print (' Magnitude : {}' .format (mention .sentiment .magnitude ))
175
- print (' Sentiment : {}' .format (mention .sentiment .score ))
176
- print (' Type : {}' .format (mention .type ))
177
- print ('Salience: {}' .format (entity .salience ))
178
- print ('Sentiment: {}\n ' .format (entity .sentiment ))
185
+ print (u ' Begin Offset : {}' .format (mention .text .begin_offset ))
186
+ print (u ' Content : {}' .format (mention .text .content ))
187
+ print (u ' Magnitude : {}' .format (mention .sentiment .magnitude ))
188
+ print (u ' Sentiment : {}' .format (mention .sentiment .score ))
189
+ print (u ' Type : {}' .format (mention .type ))
190
+ print (u 'Salience: {}' .format (entity .salience ))
191
+ print (u 'Sentiment: {}\n ' .format (entity .sentiment ))
179
192
180
193
181
194
if __name__ == '__main__' :
0 commit comments