Skip to content

Commit

Permalink
[Text Analytics] Add support for extract summary actions (#16304)
Browse files Browse the repository at this point in the history
* [Text Analytics] Add support for extract summary actions

* add a test case

* update readme

* edit

* edit

* edit

* regenerate using latest swagger

* edit

* edit

* address feedback

* edit

* edit
  • Loading branch information
deyaaeldeen committed Jul 29, 2021
1 parent 35f86a6 commit f4730cf
Show file tree
Hide file tree
Showing 17 changed files with 737 additions and 26 deletions.
5 changes: 4 additions & 1 deletion sdk/textanalytics/ai-text-analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Release History

## 5.1.1 (Unreleased)
## 5.2.0-beta.1 (Unreleased)

### Features Added

- We are now targeting the service's v3.2-preview.1 API as the default instead of v3.1.
- `beginAnalyzeActions` now supports extract summary actions.

### Breaking Changes

### Bugs Fixed
Expand Down
2 changes: 1 addition & 1 deletion sdk/textanalytics/ai-text-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[Azure TextAnalytics](https://azure.microsoft.com/services/cognitive-services/text-analytics/) is a cloud-based service that provides advanced natural language processing over raw text, and includes six main functions:

**Note:** This SDK targets Azure Text Analytics service API version 3.1.0.
**Note:** This SDK targets Azure Text Analytics service API version 3.2.0-preview.1.

- Language Detection
- Sentiment Analysis
Expand Down
2 changes: 1 addition & 1 deletion sdk/textanalytics/ai-text-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"sdk-type": "client",
"author": "Microsoft Corporation",
"description": "An isomorphic client library for the Azure Text Analytics service.",
"version": "5.1.1",
"version": "5.2.0-beta.1",
"keywords": [
"node",
"azure",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type AnalyzeActionsPollerLike = PollerLike<AnalyzeActionsOperationState,
export interface AnalyzeActionsResult {
analyzeSentimentResults: AnalyzeSentimentActionResult[];
extractKeyPhrasesResults: ExtractKeyPhrasesActionResult[];
extractSummaryResults: ExtractSummaryActionResult[];
recognizeEntitiesResults: RecognizeCategorizedEntitiesActionResult[];
recognizeLinkedEntitiesResults: RecognizeLinkedEntitiesActionResult[];
recognizePiiEntitiesResults: RecognizePiiEntitiesActionResult[];
Expand Down Expand Up @@ -247,6 +248,42 @@ export interface ExtractKeyPhrasesSuccessResult extends TextAnalyticsSuccessResu
keyPhrases: string[];
}

// @public
export interface ExtractSummaryAction extends TextAnalyticsAction {
disableServiceLogs?: boolean;
maxSentenceCount?: number;
orderBy: string;
stringIndexType?: StringIndexType;
}

// @public
export type ExtractSummaryActionErrorResult = TextAnalyticsActionErrorResult;

// @public
export type ExtractSummaryActionResult = ExtractSummaryActionSuccessResult | ExtractSummaryActionErrorResult;

// @public
export interface ExtractSummaryActionSuccessResult extends TextAnalyticsActionSuccessState {
results: ExtractSummaryResultArray;
}

// @public
export type ExtractSummaryErrorResult = TextAnalyticsErrorResult;

// @public
export type ExtractSummaryResult = ExtractSummarySuccessResult | ExtractSummaryErrorResult;

// @public
export interface ExtractSummaryResultArray extends Array<ExtractSummaryResult> {
modelVersion: string;
statistics?: TextDocumentBatchStatistics;
}

// @public
export interface ExtractSummarySuccessResult extends TextAnalyticsSuccessResult {
sentences: SummarySentence[];
}

// @public
export interface HealthcareEntity extends Entity {
assertion?: EntityAssertion;
Expand Down Expand Up @@ -280,7 +317,7 @@ export type HealthcareEntityRelationType = string;
export type InnerErrorCodeValue = string;

// @public
export const enum KnownHealthcareEntityCategory {
export enum KnownHealthcareEntityCategory {
// (undocumented)
AdministrativeEvent = "ADMINISTRATIVE_EVENT",
// (undocumented)
Expand Down Expand Up @@ -336,7 +373,7 @@ export const enum KnownHealthcareEntityCategory {
}

// @public
export const enum KnownInnerErrorCodeValue {
export enum KnownInnerErrorCodeValue {
// (undocumented)
EmptyRequest = "EmptyRequest",
// (undocumented)
Expand All @@ -358,7 +395,10 @@ export const enum KnownInnerErrorCodeValue {
}

// @public
export const enum KnownWarningCode {
export type KnownSummarySentencesSortBy = "Offset" | "Rank";

// @public
export enum KnownWarningCode {
// (undocumented)
DocumentTruncated = "DocumentTruncated",
// (undocumented)
Expand Down Expand Up @@ -585,6 +625,14 @@ export interface SentimentConfidenceScores {
// @public
export type StringIndexType = "TextElement_v8" | "UnicodeCodePoint" | "Utf16CodeUnit";

// @public
export interface SummarySentence {
length: number;
offset: number;
rankScore: number;
text: string;
}

// @public
export interface TargetConfidenceScoreLabel {
// (undocumented)
Expand Down Expand Up @@ -618,6 +666,7 @@ export interface TextAnalyticsActionErrorResult {
export interface TextAnalyticsActions {
analyzeSentimentActions?: AnalyzeSentimentAction[];
extractKeyPhrasesActions?: ExtractKeyPhrasesAction[];
extractSummaryActions?: ExtractSummaryAction[];
recognizeEntitiesActions?: RecognizeCategorizedEntitiesAction[];
recognizeLinkedEntitiesActions?: RecognizeLinkedEntitiesAction[];
recognizePiiEntitiesActions?: RecognizePiiEntitiesAction[];
Expand Down
55 changes: 51 additions & 4 deletions sdk/textanalytics/ai-text-analytics/src/analyzeActionsResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
ExtractKeyPhrasesResultArray,
makeExtractKeyPhrasesResultArray
} from "./extractKeyPhrasesResultArray";
import {
ExtractSummaryResultArray,
makeExtractSummaryResultArray
} from "./extractSummaryResultArray";
import { AnalyzeJobState as GeneratedResponse, TextDocumentInput } from "./generated/models";
import {
makeRecognizeCategorizedEntitiesResultArray,
Expand Down Expand Up @@ -49,6 +53,10 @@ export interface AnalyzeActionsResult {
* Array of the results for each analyze sentiment action.
*/
analyzeSentimentResults: AnalyzeSentimentActionResult[];
/**
* Array of the results for each extract summary action.
*/
extractSummaryResults: ExtractSummaryActionResult[];
}

/**
Expand Down Expand Up @@ -191,6 +199,28 @@ export type AnalyzeSentimentActionResult =
| AnalyzeSentimentActionSuccessResult
| AnalyzeSentimentActionErrorResult;

/**
* The error of an extract summary action.
*/
export type ExtractSummaryActionErrorResult = TextAnalyticsActionErrorResult;

/**
* The results of a succeeded extract summary action.
*/
export interface ExtractSummaryActionSuccessResult extends TextAnalyticsActionSuccessState {
/**
* Array of the results for each extract summary action.
*/
results: ExtractSummaryResultArray;
}

/**
* The result of an extract summary action.
*/
export type ExtractSummaryActionResult =
| ExtractSummaryActionSuccessResult
| ExtractSummaryActionErrorResult;

/**
* The results of an analyze Actions operation represented as a paged iterator that
* iterates over the results of the requested actions.
Expand Down Expand Up @@ -222,7 +252,8 @@ type TextAnalyticsActionType =
| "RecognizePiiEntities"
| "ExtractKeyPhrases"
| "RecognizeLinkedEntities"
| "AnalyzeSentiment";
| "AnalyzeSentiment"
| "ExtractSummary";

/**
* The type of an action error with the type of the action that erred and its
Expand Down Expand Up @@ -271,6 +302,9 @@ function convertTaskTypeToActionType(taskType: string): TextAnalyticsActionType
case "sentimentAnalysisTasks": {
return "AnalyzeSentiment";
}
case "extractiveSummarizationTasks": {
return "ExtractSummary";
}
default: {
throw new Error(`unexpected action type from the service: ${taskType}`);
}
Expand All @@ -286,7 +320,7 @@ function convertTaskTypeToActionType(taskType: string): TextAnalyticsActionType
export function parseActionError(erredActions: TextAnalyticsError): TextAnalyticsActionError {
if (erredActions.target) {
const regex = new RegExp(
/#\/tasks\/(entityRecognitionTasks|entityRecognitionPiiTasks|keyPhraseExtractionTasks|entityLinkingTasks|sentimentAnalysisTasks)\/(\d+)/
/#\/tasks\/(entityRecognitionTasks|entityRecognitionPiiTasks|keyPhraseExtractionTasks|entityLinkingTasks|sentimentAnalysisTasks|extractiveSummarizationTasks)\/(\d+)/
);
const result = regex.exec(erredActions.target);
if (result !== null) {
Expand Down Expand Up @@ -320,7 +354,8 @@ function categorizeActionErrors(
recognizePiiEntitiesActionErrors: TextAnalyticsActionError[],
extractKeyPhrasesActionErrors: TextAnalyticsActionError[],
recognizeLinkedEntitiesActionErrors: TextAnalyticsActionError[],
analyzeSentimentActionErrors: TextAnalyticsActionError[]
analyzeSentimentActionErrors: TextAnalyticsActionError[],
extractSummarySentencesActionErrors: TextAnalyticsActionError[]
): void {
for (const error of erredActions) {
const actionError = parseActionError(error);
Expand All @@ -345,6 +380,10 @@ function categorizeActionErrors(
analyzeSentimentActionErrors.push(actionError);
break;
}
case "ExtractSummary": {
extractSummarySentencesActionErrors.push(actionError);
break;
}
}
}
}
Expand Down Expand Up @@ -425,13 +464,15 @@ export function createAnalyzeActionsResult(
const extractKeyPhrasesActionErrors: TextAnalyticsActionError[] = [];
const recognizeLinkedEntitiesActionErrors: TextAnalyticsActionError[] = [];
const analyzeSentimentActionErrors: TextAnalyticsActionError[] = [];
const extractSummarySentencesActionErrors: TextAnalyticsActionError[] = [];
categorizeActionErrors(
response?.errors ?? [],
recognizeEntitiesActionErrors,
recognizePiiEntitiesActionErrors,
extractKeyPhrasesActionErrors,
recognizeLinkedEntitiesActionErrors,
analyzeSentimentActionErrors
analyzeSentimentActionErrors,
extractSummarySentencesActionErrors
);
return {
recognizeEntitiesResults: makeActionResult(
Expand Down Expand Up @@ -463,6 +504,12 @@ export function createAnalyzeActionsResult(
makeAnalyzeSentimentResultArray,
response.tasks.sentimentAnalysisTasks ?? [],
analyzeSentimentActionErrors
),
extractSummaryResults: makeActionResult(
documents,
makeExtractSummaryResultArray,
response.tasks.extractiveSummarizationTasks ?? [],
extractSummarySentencesActionErrors
)
};
}
2 changes: 1 addition & 1 deletion sdk/textanalytics/ai-text-analytics/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/**
* @internal
*/
export const SDK_VERSION: string = "5.1.1";
export const SDK_VERSION: string = "5.2.0-beta.1";
74 changes: 74 additions & 0 deletions sdk/textanalytics/ai-text-analytics/src/extractSummaryResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import {
makeTextAnalyticsSuccessResult,
TextAnalyticsSuccessResult,
TextAnalyticsErrorResult,
makeTextAnalyticsErrorResult
} from "./textAnalyticsResult";
import {
TextAnalyticsError,
ExtractedDocumentSummary,
ExtractedSummarySentence as GeneratedSummarySentences
} from "./generated/models";

/**
* The result of the extract summary operation on a single document.
*/
export type ExtractSummaryResult = ExtractSummarySuccessResult | ExtractSummaryErrorResult;

/**
* The result of the extract summary operation on a single document,
* containing a collection of the summary identified in that document.
*/
export interface ExtractSummarySuccessResult extends TextAnalyticsSuccessResult {
/**
* A list of sentences composing a summary of the input document.
*/
sentences: SummarySentence[];
}

/**
* An extracted sentence as part of the summary of a document.
*/
export interface SummarySentence {
/** The extracted sentence text. */
text: string;
/** A double value representing the relevance of the sentence within the summary. Higher values indicate higher importance. */
rankScore: number;
/** The sentence offset from the start of the document, based on the value of the stringIndexType parameter. */
offset: number;
/** The length of the sentence. */
length: number;
}

/**
* An error result from the extract summary operation on a single document.
*/
export type ExtractSummaryErrorResult = TextAnalyticsErrorResult;

/**
* @internal
*/
export function makeExtractSummaryResult(
result: ExtractedDocumentSummary
): ExtractSummarySuccessResult {
const { id, warnings, statistics, sentences } = result;
return {
...makeTextAnalyticsSuccessResult(id, warnings, statistics),
sentences: sentences.map((sentence: GeneratedSummarySentences) => ({
...sentence
}))
};
}

/**
* @internal
*/
export function makeExtractSummaryErrorResult(
id: string,
error: TextAnalyticsError
): ExtractSummaryErrorResult {
return makeTextAnalyticsErrorResult(id, error);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import {
TextDocumentBatchStatistics,
TextDocumentInput,
ExtractiveSummarizationResult
} from "./generated/models";
import {
ExtractSummaryResult,
makeExtractSummaryResult,
makeExtractSummaryErrorResult
} from "./extractSummaryResult";
import { combineSuccessfulAndErroneousDocumentsWithStatisticsAndModelVersion } from "./textAnalyticsResult";

/**
* Array of `ExtractSummaryResult` objects corresponding to a batch of input documents, and
* annotated with information about the batch operation.
*/
export interface ExtractSummaryResultArray extends Array<ExtractSummaryResult> {
/**
* Statistics about the input document batch and how it was processed
* by the service. This property will have a value when includeStatistics is set to true
* in the client call.
*/
statistics?: TextDocumentBatchStatistics;
/**
* The version of the text analytics model used by this operation on this
* batch of input documents.
*/
modelVersion: string;
}

/**
* @internal
*/
export function makeExtractSummaryResultArray(
input: TextDocumentInput[],
response: ExtractiveSummarizationResult
): ExtractSummaryResultArray {
return combineSuccessfulAndErroneousDocumentsWithStatisticsAndModelVersion(
input,
response,
makeExtractSummaryResult,
makeExtractSummaryErrorResult
);
}
Loading

0 comments on commit f4730cf

Please sign in to comment.