@@ -128,18 +128,18 @@ def sentiment_analysis_example(client):
128
128
document = [" I had the best day of my life. I wish you were there with me." ]
129
129
response = client.analyze_sentiment(inputs = document)[0 ]
130
130
print (" Document Sentiment: {} " .format(response.sentiment))
131
- print (" Overall scores: positive={0:.3f } ; neutral={1:.3f } ; negative={2:.3f } \n " .format(
132
- response.sentiment_scores .positive,
133
- response.sentiment_scores .neutral,
134
- response.sentiment_scores .negative,
131
+ print (" Overall scores: positive={0:.2f } ; neutral={1:.2f } ; negative={2:.2f } \n " .format(
132
+ response.confidence_scores .positive,
133
+ response.confidence_scores .neutral,
134
+ response.confidence_scores .negative,
135
135
))
136
136
for idx, sentence in enumerate (response.sentences):
137
- print (" [Offset: {} , Length: {} ]" .format(sentence.offset, sentence.length ))
137
+ print (" [Length: {} ]" .format(sentence.grapheme_length ))
138
138
print (" Sentence {} sentiment: {} " .format(idx+ 1 , sentence.sentiment))
139
- print (" Sentence score:\n Positive={0:.3f } \n Neutral={1:.3f } \n Negative={2:.3f } \n " .format(
140
- sentence.sentiment_scores .positive,
141
- sentence.sentiment_scores .neutral,
142
- sentence.sentiment_scores .negative,
139
+ print (" Sentence score:\n Positive={0:.2f } \n Neutral={1:.2f } \n Negative={2:.2f } \n " .format(
140
+ sentence.confidence_scores .positive,
141
+ sentence.confidence_scores .neutral,
142
+ sentence.confidence_scores .negative,
143
143
))
144
144
145
145
@@ -150,21 +150,21 @@ sentiment_analysis_example(client)
150
150
151
151
``` console
152
152
Document Sentiment: positive
153
- Overall scores: positive=1.000 ; neutral=0.000 ; negative=0.000
153
+ Overall scores: positive=1.00 ; neutral=0.00 ; negative=0.00
154
154
155
- [Offset: 0, Length: 30]
155
+ [Length: 30]
156
156
Sentence 1 sentiment: positive
157
157
Sentence score:
158
- Positive=1.000
159
- Neutral=0.000
160
- Negative=0.000
158
+ Positive=1.00
159
+ Neutral=0.00
160
+ Negative=0.00
161
161
162
- [Offset: 31, Length: 30]
162
+ [Length: 30]
163
163
Sentence 2 sentiment: neutral
164
164
Sentence score:
165
- Positive=0.210
166
- Neutral=0.770
167
- Negative=0.020
165
+ Positive=0.21
166
+ Neutral=0.77
167
+ Negative=0.02
168
168
```
169
169
170
170
#### [ Version 2.1] ( #tab/version-2 )
@@ -249,9 +249,8 @@ def entity_recognition_example(client):
249
249
250
250
print (" Named Entities:\n " )
251
251
for entity in result.entities:
252
- print (" \t Text: \t " , entity.text, " \t Category: \t " , entity.category, " \t SubCategory: \t " , entity.subcategory,
253
- " \n\t Offset: \t " , entity.offset, " \t Length: \t " , entity.offset,
254
- " \t Confidence Score: \t " , round (entity.score, 3 ), " \n " )
252
+ print (" \t Text: \t " , entity.text, " \t Category: \t " , entity.category, " \t SubCategory: \t " , entity.subcategory,
253
+ " \n\t Length: \t " , entity.grapheme_length, " \t Confidence Score: \t " , round (entity.score, 2 ), " \n " )
255
254
256
255
except Exception as err:
257
256
print (" Encountered exception. {} " .format(err))
@@ -263,11 +262,11 @@ entity_recognition_example(client)
263
262
``` console
264
263
Named Entities:
265
264
266
- Text: Seattle Category: Location SubCategory: GPE
267
- Offset : 26 Length: 26 Confidence Score: 0.92
265
+ Text: Seattle Category: Location SubCategory: GPE
266
+ Length : 7 Confidence Score: 0.92
268
267
269
- Text: last week Category: DateTime SubCategory: DateRange
270
- Offset : 34 Length: 34 Confidence Score: 0.8
268
+ Text: last week Category: DateTime SubCategory: DateRange
269
+ Length : 9 Confidence Score: 0.8
271
270
```
272
271
273
272
## Using NER to detect personal information
@@ -285,17 +284,17 @@ def entity_pii_example(client):
285
284
print (" Personally Identifiable Information Entities: " )
286
285
for entity in result.entities:
287
286
print (" \t Text: " ,entity.text," \t Category: " , entity.category," \t SubCategory: " , entity.subcategory)
288
- print (" \t\t Offset: " , entity.offset, " \ t Length: " , entity.length , " \t Score: {0:.3f } " .format(entity.score), " \n " )
287
+ print (" \t\t Length: " , entity.grapheme_length , " \t Score: {0:.2f } " .format(entity.score), " \n " )
289
288
290
289
entity_pii_example(client)
291
290
```
292
291
293
292
### Output
294
293
295
294
``` console
296
- Personally Identifiable Information Entities:
297
- Text: 123-12-1234 Category: U.S. Social Security Number (SSN) SubCategory: None
298
- Offset: 33 Length: 11 Score: 0.850
295
+ Personally Identifiable Information Entities:
296
+ Text: 123-12-1234 Category: U.S. Social Security Number (SSN) SubCategory: None
297
+ Length: 11 Score: 0.85
299
298
```
300
299
301
300
@@ -316,13 +315,12 @@ def entity_linking_example(client):
316
315
317
316
print (" Linked Entities:\n " )
318
317
for entity in result.entities:
319
- print (" \t Name: " , entity.name, " \t Id: " , entity.id , " \t Url: " , entity.url,
318
+ print (" \t Name: " , entity.name, " \t Id: " , entity.data_source_entity_id , " \t Url: " , entity.url,
320
319
" \n\t Data Source: " , entity.data_source)
321
320
print (" \t Matches:" )
322
321
for match in entity.matches:
323
322
print (" \t\t Text:" , match.text)
324
- print (" \t\t Score: {0:.3f } " .format(match.score), " \t Offset: " , match.offset,
325
- " \t Length: {} \n " .format(match.length))
323
+ print (" \t\t Score: {0:.2f } " .format(match.score), " \t Length: {} \n " .format(match.grapheme_length))
326
324
327
325
except Exception as err:
328
326
print (" Encountered exception. {} " .format(err))
@@ -334,47 +332,47 @@ entity_linking_example(client)
334
332
``` console
335
333
Linked Entities:
336
334
337
- Name: Altair 8800 Id: Altair 8800 Url: https://en.wikipedia.org/wiki/Altair_8800
335
+ Name: Altair 8800 Id: Altair 8800 Url: https://en.wikipedia.org/wiki/Altair_8800
338
336
Data Source: Wikipedia
339
337
Matches:
340
338
Text: Altair 8800
341
- Score: 0.777 Offset: 125 Length: 11
339
+ Score: 0.78 Length: 11
342
340
343
- Name: Bill Gates Id: Bill Gates Url: https://en.wikipedia.org/wiki/Bill_Gates
341
+ Name: Bill Gates Id: Bill Gates Url: https://en.wikipedia.org/wiki/Bill_Gates
344
342
Data Source: Wikipedia
345
343
Matches:
346
344
Text: Bill Gates
347
- Score: 0.555 Offset: 25 Length: 10
345
+ Score: 0.55 Length: 10
348
346
349
347
Text: Gates
350
- Score: 0.555 Offset: 179 Length: 5
348
+ Score: 0.55 Length: 5
351
349
352
- Name: Paul Allen Id: Paul Allen Url: https://en.wikipedia.org/wiki/Paul_Allen
350
+ Name: Paul Allen Id: Paul Allen Url: https://en.wikipedia.org/wiki/Paul_Allen
353
351
Data Source: Wikipedia
354
352
Matches:
355
353
Text: Paul Allen
356
- Score: 0.533 Offset: 40 Length: 10
354
+ Score: 0.53 Length: 10
357
355
358
- Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
356
+ Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
359
357
Data Source: Wikipedia
360
358
Matches:
361
359
Text: Microsoft
362
- Score: 0.469 Offset: 0 Length: 9
360
+ Score: 0.47 Length: 9
363
361
364
362
Text: Microsoft
365
- Score: 0.469 Offset: 168 Length: 9
363
+ Score: 0.47 Length: 9
366
364
367
- Name: April 4 Id: April 4 Url: https://en.wikipedia.org/wiki/April_4
365
+ Name: April 4 Id: April 4 Url: https://en.wikipedia.org/wiki/April_4
368
366
Data Source: Wikipedia
369
367
Matches:
370
368
Text: April 4
371
- Score: 0.248 Offset: 54 Length: 7
369
+ Score: 0.25 Length: 7
372
370
373
- Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
371
+ Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
374
372
Data Source: Wikipedia
375
373
Matches:
376
374
Text: BASIC
377
- Score: 0.281 Offset: 98 Length: 5
375
+ Score: 0.28 Length: 5
378
376
379
377
```
380
378
0 commit comments