|
3 | 3 |
|
4 | 4 | package com.azure.ai.textanalytics; |
5 | 5 |
|
6 | | -import com.azure.ai.textanalytics.implementation.AspectSentimentPropertiesHelper; |
7 | | -import com.azure.ai.textanalytics.implementation.OpinionSentimentPropertiesHelper; |
| 6 | +import com.azure.ai.textanalytics.implementation.SentenceOpinionPropertiesHelper; |
| 7 | +import com.azure.ai.textanalytics.implementation.TargetSentimentPropertiesHelper; |
| 8 | +import com.azure.ai.textanalytics.implementation.AssessmentSentimentPropertiesHelper; |
8 | 9 | import com.azure.ai.textanalytics.implementation.SentenceSentimentPropertiesHelper; |
9 | 10 | import com.azure.ai.textanalytics.implementation.TextAnalyticsClientImpl; |
10 | 11 | import com.azure.ai.textanalytics.implementation.Utility; |
11 | | -import com.azure.ai.textanalytics.implementation.models.AspectConfidenceScoreLabel; |
12 | | -import com.azure.ai.textanalytics.implementation.models.AspectRelationType; |
13 | 12 | import com.azure.ai.textanalytics.implementation.models.DocumentError; |
14 | 13 | import com.azure.ai.textanalytics.implementation.models.DocumentSentiment; |
15 | 14 | import com.azure.ai.textanalytics.implementation.models.DocumentSentimentValue; |
16 | 15 | import com.azure.ai.textanalytics.implementation.models.MultiLanguageBatchInput; |
17 | | -import com.azure.ai.textanalytics.implementation.models.SentenceAspect; |
18 | | -import com.azure.ai.textanalytics.implementation.models.SentenceOpinion; |
| 16 | +import com.azure.ai.textanalytics.implementation.models.SentenceAssessment; |
19 | 17 | import com.azure.ai.textanalytics.implementation.models.SentenceSentimentValue; |
| 18 | +import com.azure.ai.textanalytics.implementation.models.SentenceTarget; |
20 | 19 | import com.azure.ai.textanalytics.implementation.models.SentimentConfidenceScorePerLabel; |
21 | 20 | import com.azure.ai.textanalytics.implementation.models.SentimentResponse; |
| 21 | +import com.azure.ai.textanalytics.implementation.models.TargetConfidenceScoreLabel; |
| 22 | +import com.azure.ai.textanalytics.implementation.models.TargetRelationType; |
22 | 23 | import com.azure.ai.textanalytics.implementation.models.WarningCodeValue; |
23 | 24 | import com.azure.ai.textanalytics.models.AnalyzeSentimentOptions; |
24 | 25 | import com.azure.ai.textanalytics.models.AnalyzeSentimentResult; |
25 | | -import com.azure.ai.textanalytics.models.AspectSentiment; |
26 | | -import com.azure.ai.textanalytics.models.MinedOpinion; |
27 | | -import com.azure.ai.textanalytics.models.OpinionSentiment; |
| 26 | +import com.azure.ai.textanalytics.models.TargetSentiment; |
| 27 | +import com.azure.ai.textanalytics.models.SentenceOpinion; |
| 28 | +import com.azure.ai.textanalytics.models.AssessmentSentiment; |
28 | 29 | import com.azure.ai.textanalytics.models.SentenceSentiment; |
29 | 30 | import com.azure.ai.textanalytics.models.SentimentConfidenceScores; |
30 | 31 | import com.azure.ai.textanalytics.models.TextAnalyticsWarning; |
@@ -166,10 +167,10 @@ private AnalyzeSentimentResult convertToAnalyzeSentimentResult(DocumentSentiment |
166 | 167 | final SentenceSentiment sentenceSentiment1 = new SentenceSentiment(sentenceSentiment.getText(), |
167 | 168 | TextSentiment.fromString(sentenceSentimentValue == null ? null : sentenceSentimentValue.toString()), |
168 | 169 | new SentimentConfidenceScores(confidenceScorePerSentence.getNegative(), |
169 | | - confidenceScorePerSentence.getNeutral(), confidenceScorePerSentence.getPositive()), |
170 | | - toMinedOpinionList(sentenceSentiment, documentSentimentList), |
171 | | - sentenceSentiment.getOffset() |
172 | | - ); |
| 170 | + confidenceScorePerSentence.getNeutral(), confidenceScorePerSentence.getPositive())); |
| 171 | + SentenceSentimentPropertiesHelper.setOpinions(sentenceSentiment1, |
| 172 | + toSentenceOpinionList(sentenceSentiment, documentSentimentList)); |
| 173 | + SentenceSentimentPropertiesHelper.setOffset(sentenceSentiment1, sentenceSentiment.getOffset()); |
173 | 174 | SentenceSentimentPropertiesHelper.setLength(sentenceSentiment1, sentenceSentiment.getLength()); |
174 | 175 | return sentenceSentiment1; |
175 | 176 | }).collect(Collectors.toList()); |
@@ -229,112 +230,123 @@ private Mono<Response<AnalyzeSentimentResultCollection>> getAnalyzedSentimentRes |
229 | 230 | /* |
230 | 231 | * Transform SentenceSentiment's opinion mining to output that user can use. |
231 | 232 | */ |
232 | | - private IterableStream<MinedOpinion> toMinedOpinionList( |
| 233 | + private IterableStream<SentenceOpinion> toSentenceOpinionList( |
233 | 234 | com.azure.ai.textanalytics.implementation.models.SentenceSentiment sentenceSentiment, |
234 | 235 | List<DocumentSentiment> documentSentimentList) { |
235 | | - // If include opinion mining indicator is false, the service return null for the aspect list. |
236 | | - final List<SentenceAspect> sentenceAspects = sentenceSentiment.getAspects(); |
237 | | - if (sentenceAspects == null) { |
| 236 | + // If include opinion mining indicator is false, the service return null for the target list. |
| 237 | + final List<SentenceTarget> sentenceTargets = sentenceSentiment.getTargets(); |
| 238 | + if (sentenceTargets == null) { |
238 | 239 | return null; |
239 | 240 | } |
240 | | - final List<MinedOpinion> minedOpinions = new ArrayList<>(); |
241 | | - sentenceAspects.forEach(sentenceAspect -> { |
242 | | - final List<OpinionSentiment> opinionSentiments = new ArrayList<>(); |
243 | | - sentenceAspect.getRelations().forEach(aspectRelation -> { |
244 | | - final AspectRelationType aspectRelationType = aspectRelation.getRelationType(); |
245 | | - final String opinionPointer = aspectRelation.getRef(); |
246 | | - if (AspectRelationType.OPINION == aspectRelationType) { |
247 | | - opinionSentiments.add(toOpinionSentiment( |
248 | | - findSentimentOpinion(opinionPointer, documentSentimentList))); |
| 241 | + final List<SentenceOpinion> sentenceOpinions = new ArrayList<>(); |
| 242 | + sentenceTargets.forEach(sentenceTarget -> { |
| 243 | + final List<AssessmentSentiment> assessmentSentiments = new ArrayList<>(); |
| 244 | + sentenceTarget.getRelations().forEach(targetRelation -> { |
| 245 | + final TargetRelationType targetRelationType = targetRelation.getRelationType(); |
| 246 | + final String opinionPointer = targetRelation.getRef(); |
| 247 | + if (TargetRelationType.ASSESSMENT == targetRelationType) { |
| 248 | + assessmentSentiments.add(toAssessmentSentiment( |
| 249 | + findSentimentAssessment(opinionPointer, documentSentimentList))); |
249 | 250 | } |
250 | 251 | }); |
251 | | - final AspectSentiment aspectSentiment = new AspectSentiment(sentenceAspect.getText(), |
252 | | - TextSentiment.fromString(sentenceAspect.getSentiment().toString()), |
253 | | - sentenceAspect.getOffset(), |
254 | | - toSentimentConfidenceScores(sentenceAspect.getConfidenceScores())); |
255 | | - AspectSentimentPropertiesHelper.setLength(aspectSentiment, sentenceAspect.getLength()); |
256 | | - minedOpinions.add(new MinedOpinion(aspectSentiment, new IterableStream<>(opinionSentiments))); |
| 252 | + final TargetSentiment targetSentiment = new TargetSentiment(); |
| 253 | + TargetSentimentPropertiesHelper.setText(targetSentiment, sentenceTarget.getText()); |
| 254 | + TargetSentimentPropertiesHelper.setSentiment(targetSentiment, |
| 255 | + TextSentiment.fromString(sentenceTarget.getSentiment().toString())); |
| 256 | + TargetSentimentPropertiesHelper.setConfidenceScores(targetSentiment, |
| 257 | + toSentimentConfidenceScores(sentenceTarget.getConfidenceScores())); |
| 258 | + TargetSentimentPropertiesHelper.setOffset(targetSentiment, sentenceTarget.getOffset()); |
| 259 | + TargetSentimentPropertiesHelper.setLength(targetSentiment, sentenceTarget.getLength()); |
| 260 | + |
| 261 | + final SentenceOpinion sentenceOpinion = new SentenceOpinion(); |
| 262 | + SentenceOpinionPropertiesHelper.setTarget(sentenceOpinion, targetSentiment); |
| 263 | + SentenceOpinionPropertiesHelper.setAssessments(sentenceOpinion, new IterableStream<>(assessmentSentiments)); |
| 264 | + sentenceOpinions.add(sentenceOpinion); |
257 | 265 | }); |
258 | 266 |
|
259 | | - return new IterableStream<>(minedOpinions); |
| 267 | + return new IterableStream<>(sentenceOpinions); |
260 | 268 | } |
261 | 269 |
|
262 | 270 | /* |
263 | | - * Transform type AspectConfidenceScoreLabel to SentimentConfidenceScores. |
| 271 | + * Transform type TargetConfidenceScoreLabel to SentimentConfidenceScores. |
264 | 272 | */ |
265 | 273 | private SentimentConfidenceScores toSentimentConfidenceScores( |
266 | | - AspectConfidenceScoreLabel aspectConfidenceScoreLabel) { |
267 | | - return new SentimentConfidenceScores(aspectConfidenceScoreLabel.getNegative(), NEUTRAL_SCORE_ZERO, |
268 | | - aspectConfidenceScoreLabel.getPositive()); |
| 274 | + TargetConfidenceScoreLabel targetConfidenceScoreLabel) { |
| 275 | + return new SentimentConfidenceScores(targetConfidenceScoreLabel.getNegative(), NEUTRAL_SCORE_ZERO, |
| 276 | + targetConfidenceScoreLabel.getPositive()); |
269 | 277 | } |
270 | 278 |
|
271 | 279 | /* |
272 | 280 | * Transform type SentenceOpinion to OpinionSentiment. |
273 | 281 | */ |
274 | | - private OpinionSentiment toOpinionSentiment(SentenceOpinion sentenceOpinion) { |
275 | | - final OpinionSentiment opinionSentiment = new OpinionSentiment(sentenceOpinion.getText(), |
276 | | - TextSentiment.fromString(sentenceOpinion.getSentiment().toString()), |
277 | | - sentenceOpinion.getOffset(), sentenceOpinion.isNegated(), |
278 | | - toSentimentConfidenceScores(sentenceOpinion.getConfidenceScores())); |
279 | | - OpinionSentimentPropertiesHelper.setLength(opinionSentiment, sentenceOpinion.getLength()); |
280 | | - return opinionSentiment; |
| 282 | + private AssessmentSentiment toAssessmentSentiment(SentenceAssessment sentenceAssessment) { |
| 283 | + final AssessmentSentiment assessmentSentiment = new AssessmentSentiment(); |
| 284 | + AssessmentSentimentPropertiesHelper.setText(assessmentSentiment, sentenceAssessment.getText()); |
| 285 | + AssessmentSentimentPropertiesHelper.setSentiment(assessmentSentiment, |
| 286 | + TextSentiment.fromString(sentenceAssessment.getSentiment().toString())); |
| 287 | + AssessmentSentimentPropertiesHelper.setConfidenceScores(assessmentSentiment, |
| 288 | + toSentimentConfidenceScores(sentenceAssessment.getConfidenceScores())); |
| 289 | + AssessmentSentimentPropertiesHelper.setNegated(assessmentSentiment, sentenceAssessment.isNegated()); |
| 290 | + AssessmentSentimentPropertiesHelper.setOffset(assessmentSentiment, sentenceAssessment.getOffset()); |
| 291 | + AssessmentSentimentPropertiesHelper.setLength(assessmentSentiment, sentenceAssessment.getLength()); |
| 292 | + return assessmentSentiment; |
281 | 293 | } |
282 | 294 |
|
283 | 295 | /* |
284 | 296 | * Parses the reference pointer to an index array that contains document, sentence, and opinion indexes. |
285 | 297 | */ |
286 | | - int[] parseRefPointerToIndexArray(String opinionPointer) { |
287 | | - // The pattern always start with character '#', the opinion index will existing in specified sentence, which |
| 298 | + int[] parseRefPointerToIndexArray(String assessmentPointer) { |
| 299 | + // The pattern always start with character '#', the assessment index will existing in specified sentence, which |
288 | 300 | // is under specified document. |
289 | | - // example: #/documents/0/sentences/0/opinions/0 |
290 | | - final String patternRegex = "#/documents/(\\d+)/sentences/(\\d+)/opinions/(\\d+)"; |
| 301 | + // example: #/documents/0/sentences/0/assessments/0 |
| 302 | + final String patternRegex = "#/documents/(\\d+)/sentences/(\\d+)/assessments/(\\d+)"; |
291 | 303 | final Pattern pattern = Pattern.compile(patternRegex); |
292 | | - final Matcher matcher = pattern.matcher(opinionPointer); |
| 304 | + final Matcher matcher = pattern.matcher(assessmentPointer); |
293 | 305 | final boolean isMatched = matcher.find(); |
294 | 306 |
|
295 | 307 | // The first index represents the document index, second one represents the sentence index, |
296 | | - // third ond represents the opinion index. |
| 308 | + // third ond represents the assessment index. |
297 | 309 | final int[] result = new int[3]; |
298 | 310 |
|
299 | 311 | if (isMatched) { |
300 | | - String[] segments = opinionPointer.split("/"); |
| 312 | + String[] segments = assessmentPointer.split("/"); |
301 | 313 | result[0] = Integer.parseInt(segments[2]); |
302 | 314 | result[1] = Integer.parseInt(segments[4]); |
303 | 315 | result[2] = Integer.parseInt(segments[6]); |
304 | 316 | } else { |
305 | 317 | throw logger.logExceptionAsError(new IllegalStateException( |
306 | | - String.format("'%s' is not a valid opinion pointer.", opinionPointer))); |
| 318 | + String.format("'%s' is not a valid assessment pointer.", assessmentPointer))); |
307 | 319 | } |
308 | 320 |
|
309 | 321 | return result; |
310 | 322 | } |
311 | 323 |
|
312 | 324 | /* |
313 | | - * Find the specific sentence opinion in the document sentiment list by given the opinion reference pointer. |
| 325 | + * Find the specific sentence assessment in the document sentiment list by given the assessment reference pointer. |
314 | 326 | */ |
315 | | - SentenceOpinion findSentimentOpinion(String opinionPointer, List<DocumentSentiment> documentSentiments) { |
316 | | - final int[] opinionIndexes = parseRefPointerToIndexArray(opinionPointer); |
317 | | - final int documentIndex = opinionIndexes[0]; |
318 | | - final int sentenceIndex = opinionIndexes[1]; |
319 | | - final int opinionIndex = opinionIndexes[2]; |
| 327 | + SentenceAssessment findSentimentAssessment(String assessmentPointer, List<DocumentSentiment> documentSentiments) { |
| 328 | + final int[] assessmentIndexes = parseRefPointerToIndexArray(assessmentPointer); |
| 329 | + final int documentIndex = assessmentIndexes[0]; |
| 330 | + final int sentenceIndex = assessmentIndexes[1]; |
| 331 | + final int assessmentIndex = assessmentIndexes[2]; |
320 | 332 | if (documentIndex >= documentSentiments.size()) { |
321 | 333 | throw logger.logExceptionAsError(new IllegalStateException( |
322 | | - String.format("Invalid document index '%s' in '%s'.", documentIndex, opinionPointer))); |
| 334 | + String.format("Invalid document index '%s' in '%s'.", documentIndex, assessmentPointer))); |
323 | 335 | } |
324 | 336 | final DocumentSentiment documentsentiment = documentSentiments.get(documentIndex); |
325 | 337 |
|
326 | 338 | final List<com.azure.ai.textanalytics.implementation.models.SentenceSentiment> sentenceSentiments = |
327 | 339 | documentsentiment.getSentences(); |
328 | 340 | if (sentenceIndex >= sentenceSentiments.size()) { |
329 | 341 | throw logger.logExceptionAsError(new IllegalStateException( |
330 | | - String.format("Invalid sentence index '%s' in '%s'.", sentenceIndex, opinionPointer))); |
| 342 | + String.format("Invalid sentence index '%s' in '%s'.", sentenceIndex, assessmentPointer))); |
331 | 343 | } |
332 | 344 |
|
333 | | - final List<SentenceOpinion> opinions = sentenceSentiments.get(sentenceIndex).getOpinions(); |
334 | | - if (opinionIndex >= opinions.size()) { |
| 345 | + final List<SentenceAssessment> assessments = sentenceSentiments.get(sentenceIndex).getAssessments(); |
| 346 | + if (assessmentIndex >= assessments.size()) { |
335 | 347 | throw logger.logExceptionAsError(new IllegalStateException( |
336 | | - String.format("Invalid opinion index '%s' in '%s'.", opinionIndex, opinionPointer))); |
| 348 | + String.format("Invalid assessment index '%s' in '%s'.", assessmentIndex, assessmentPointer))); |
337 | 349 | } |
338 | | - return opinions.get(opinionIndex); |
| 350 | + return assessments.get(assessmentIndex); |
339 | 351 | } |
340 | 352 | } |
0 commit comments