Skip to content

Commit

Permalink
Merge pull request Azure#6 from samvaity/text-analytics
Browse files Browse the repository at this point in the history
Detect language API impl changes
  • Loading branch information
samvaity authored Dec 4, 2019
2 parents daa092c + 2ee11d4 commit 4df74b0
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 91 deletions.
43 changes: 43 additions & 0 deletions sdk/cognitiveservices/azure-cs-textanalytics/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# DO NOT EDIT THIS FILE
# This file is generated automatically and any changes will be lost.

resources:
repositories:
- repository: azure-sdk-build-tools
type: git
name: internal/azure-sdk-build-tools
- repository: azure-sdk-tools
type: github
name: Azure/azure-sdk-tools
endpoint: azure

trigger:
branches:
include:
- master
- feature/*
- hotfix/*
- release/*
paths:
include:
- sdk/cognitiveservices/azure-cs-textanalytics

pr:
branches:
include:
- master
- feature/*
- hotfix/*
- release/*
paths:
include:
- sdk/cognitiveservices/azure-cs-textanalytics

stages:
- template: ../../eng/pipelines/templates/stages/archetype-sdk-client.yml
parameters:
ServiceDirectory: textanalytics
Artifacts:
- name: azure-cs-text-analytics
safeName: azurecstextanalytics
stagingProfileId: 88192f04117501
17 changes: 17 additions & 0 deletions sdk/cognitiveservices/azure-cs-textanalytics/pom.service.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.azure</groupId>
<artifactId>azure-cognitiveservices-textanalytics-service</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version><!-- Need not change for every release-->
<modules>
<!-- NOTE: Commenting out becuase we are now binary composed.
-->
<module>../core/azure-core</module>
<module>azure-cognitiveservices-textanalytics</module>
</modules>
</project>
4 changes: 2 additions & 2 deletions sdk/cognitiveservices/azure-cs-textanalytics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.1.0</version>
<version>1.1.0</version> <!-- {x-version-update;com.azure:azure-core;current} -->
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.1.0</version>
<version>1.1.0</version> <!-- {x-version-update;com.azure:azure-core-http-netty;current} -->
</dependency>

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,29 @@
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.cs.textanalytics.implementation.TextAnalyticsClientImpl;
import com.azure.cs.textanalytics.implementation.models.DocumentLanguage;
import com.azure.cs.textanalytics.implementation.models.LanguageBatchInput;
import com.azure.cs.textanalytics.implementation.models.LanguageResult;
import com.azure.cs.textanalytics.implementation.models.MultiLanguageBatchInput;
import com.azure.cs.textanalytics.models.DetectLanguageInput;
import com.azure.cs.textanalytics.models.DetectLanguageResult;
import com.azure.cs.textanalytics.models.DocumentError;
import com.azure.cs.textanalytics.models.DocumentResultCollection;
import com.azure.cs.textanalytics.models.KeyPhraseResult;
import com.azure.cs.textanalytics.models.LinkedEntityResult;
import com.azure.cs.textanalytics.models.DetectLanguageInput;
import com.azure.cs.textanalytics.models.NamedEntityResult;
import com.azure.cs.textanalytics.models.TextDocumentInput;
import com.azure.cs.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.cs.textanalytics.models.TextDocumentInput;
import com.azure.cs.textanalytics.models.TextSentimentResult;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.azure.core.util.FluxUtil.monoError;
import static com.azure.core.util.FluxUtil.withContext;
Expand All @@ -34,11 +40,11 @@
public final class TextAnalyticsAsyncClient {
private final ClientLogger logger = new ClientLogger(TextAnalyticsAsyncClient.class);

private final TextAnalyticsClientImpl client;
private final TextAnalyticsClientImpl service;
private final TextAnalyticsServiceVersion serviceVersion;

TextAnalyticsAsyncClient(TextAnalyticsClientImpl client, TextAnalyticsServiceVersion serviceVersion) {
this.client = client;
TextAnalyticsAsyncClient(TextAnalyticsClientImpl service, TextAnalyticsServiceVersion serviceVersion) {
this.service = service;
this.serviceVersion = serviceVersion;
}

Expand All @@ -54,44 +60,64 @@ public TextAnalyticsServiceVersion getServiceVersion() {
// (1) language
// new user
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DetectLanguageResult> detectLanguage(String text) { return detectLanguage(text, null);}
public Mono<DetectLanguageResult> detectLanguage(String text) {
return detectLanguage(text, null);
}

@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DetectLanguageResult> detectLanguage(String text, String countryHint) {
return detectLanguageWithResponse(text, countryHint)
.flatMap(response -> Mono.justOrEmpty(response.getValue()));
.flatMap(FluxUtil::toMono);
}

@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<DetectLanguageResult>> detectLanguageWithResponse(String text, String countryHint) {
try {
return withContext(context -> detectLanguageWithResponse(text, countryHint, context));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

@ServiceMethod(returns = ReturnType.SINGLE)
Mono<Response<DetectLanguageResult>> detectLanguageWithResponse(String text, String countryHint, Context context) {
List<DetectLanguageInput> languageInputs = new ArrayList<>();
languageInputs.add(new DetectLanguageInput(Integer.toString(0), text, countryHint));
// TODO: correct it
// return detectLanguagesWithResponse(languageInputs, null).flatMap(
// response -> {
// final Iterator<DetectedLanguageResult> resultIterator = response.getValue().iterator();
// if (resultIterator.hasNext()) {
// return Mono.justOrEmpty(resultIterator.next());
// }
// return monoError(logger, new RuntimeException(""));
// });
return null;
// TODO: should this be a random number generator?
return detectBatchLanguagesWithResponse(languageInputs, null, context).flatMap(response -> {
if (response.getValue().iterator().hasNext()) {
return Mono.justOrEmpty(new SimpleResponse<>(response, response.getValue().iterator().next()));
}
return monoError(logger, new RuntimeException("Unable to retrieve language for the provided text."));
});
}

// Hackathon user
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentResultCollection<DetectLanguageResult>> detectLanguages(List<String> inputs) {
return null;
}
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentResultCollection<DetectLanguageResult>> detectLanguages(List<String> inputs) {
return detectLanguages(inputs, null);
}

@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentResultCollection<DetectLanguageResult>> detectLanguages(List<String> inputs,
String countryHint) {
try {
return withContext(context -> detectLanguagesWithResponse(inputs, countryHint, context))
.flatMap(FluxUtil::toMono);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

@ServiceMethod(returns = ReturnType.SINGLE)
Mono<Response<DocumentResultCollection<DetectLanguageResult>>> detectLanguagesWithResponse(List<String> inputs,
String countryHint, Context context) {
List<DetectLanguageInput> languageInputs = new ArrayList<>();
// TODO update/validate inputs and id assigning
for (int i = 0; i < inputs.size(); i++) {
languageInputs.add(new DetectLanguageInput(Integer.toString(i), inputs.get(i), countryHint));
}

return detectBatchLanguages(languageInputs, null);
return detectBatchLanguagesWithResponse(languageInputs, null, context);
}

// Advanced user
Expand All @@ -103,12 +129,7 @@ public Mono<DocumentResultCollection<DetectLanguageResult>> detectBatchLanguages
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentResultCollection<DetectLanguageResult>> detectBatchLanguages(
List<DetectLanguageInput> inputs, TextAnalyticsRequestOptions options) {
try {
return withContext(context -> detectBatchLanguagesWithResponse(inputs, options, context)
.flatMap(response -> Mono.justOrEmpty(response.getValue())));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
return detectBatchLanguagesWithResponse(inputs, options).flatMap(FluxUtil::toMono);
}

@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -124,15 +145,43 @@ public Mono<Response<DocumentResultCollection<DetectLanguageResult>>> detectBatc

Mono<Response<DocumentResultCollection<DetectLanguageResult>>> detectBatchLanguagesWithResponse(
List<DetectLanguageInput> inputs, TextAnalyticsRequestOptions options, Context context) {
// TODO: validate inputs
// TODO: validate inputs?
final LanguageBatchInput languageBatchInput = new LanguageBatchInput().setDocuments(inputs);
// return client.languagesWithRestResponseAsync(languageBatchInput, options.getModelVersion(),
// options.showStatistics(), context)
// .doOnSubscribe(ignoredValue -> logger.info("A batch of language input - {}", languageBatchInput))
// .doOnSuccess(response -> logger.info("A batch of detected language output - {}", languageBatchInput))
// .doOnError(error -> logger.warning("Failed to detected languages - {}", languageBatchInput))
// .map(response -> new SimpleResponse<>(response, toDocumentResultCollection(response.getValue())));
return null;
// TODO: confirm if options null is fine?
return service.languagesWithRestResponseAsync(
languageBatchInput, options == null ? null : options.getModelVersion(),
options == null ? null : options.showStatistics(), context)
.doOnSubscribe(ignoredValue -> logger.info("A batch of language input - {}", languageBatchInput))
.doOnSuccess(response -> logger.info("A batch of detected language output - {}", languageBatchInput))
.doOnError(error -> logger.warning("Failed to detected languages - {}", languageBatchInput))
.map(response -> new SimpleResponse<>(response, toDocumentResultCollection(response.getValue())));
}

private DocumentResultCollection<DetectLanguageResult> toDocumentResultCollection(
final LanguageResult languageResult) {
return new DocumentResultCollection<>(getDocumentLanguages(languageResult), languageResult.getModelVersion(),
languageResult.getStatistics());
}

private List<DetectLanguageResult> getDocumentLanguages(final LanguageResult languageResult) {
Stream<DetectLanguageResult> validDocumentList = languageResult.getDocuments().stream()
.map(this::convertToDetectLanguageResult);
Stream<DetectLanguageResult> errorDocumentList = languageResult.getErrors().stream()
.map(this::convertToErrorDetectLanguageResult);

return Stream.concat(validDocumentList, errorDocumentList).collect(Collectors.toList());
}

private DetectLanguageResult convertToErrorDetectLanguageResult(final DocumentError errorDocument) {
return new DetectLanguageResult(errorDocument.getId(), null,
errorDocument.getError(),
null, null);
}

private DetectLanguageResult convertToDetectLanguageResult(final DocumentLanguage documentLanguage) {
// TODO confirm the primary language support from service
return new DetectLanguageResult(documentLanguage.getId(), documentLanguage.getStatistics(), null,
documentLanguage.getDetectedLanguages().get(0), documentLanguage.getDetectedLanguages());
}

// (2) entities
Expand Down Expand Up @@ -193,7 +242,7 @@ public Mono<Response<DocumentResultCollection<NamedEntityResult>>> recognizeBatc

Mono<Response<DocumentResultCollection<NamedEntityResult>>> recognizeBatchEntitiesWithResponse(
List<TextDocumentInput> document, TextAnalyticsRequestOptions options, Context context) {
return client.entitiesRecognitionGeneralWithRestResponseAsync(
return service.entitiesRecognitionGeneralWithRestResponseAsync(
new MultiLanguageBatchInput().setDocuments(document), options.getModelVersion(),
options.showStatistics(), context).map(response -> new SimpleResponse<>(response, null));
}
Expand Down Expand Up @@ -259,7 +308,7 @@ public Mono<Response<DocumentResultCollection<NamedEntityResult>>> recognizeBatc
Mono<Response<DocumentResultCollection<NamedEntityResult>>> recognizeBatchPiiEntitiesWithResponse(
List<TextDocumentInput> document, TextAnalyticsRequestOptions options, Context context) {
// TODO: validate multiLanguageBatchInput
return client.entitiesRecognitionPiiWithRestResponseAsync(new MultiLanguageBatchInput().setDocuments(document),
return service.entitiesRecognitionPiiWithRestResponseAsync(new MultiLanguageBatchInput().setDocuments(document),
options.getModelVersion(), options.showStatistics(), context)
.map(response -> new SimpleResponse<>(response, null));
}
Expand Down Expand Up @@ -325,7 +374,7 @@ public Mono<Response<DocumentResultCollection<LinkedEntityResult>>> recognizeBat
Mono<Response<DocumentResultCollection<LinkedEntityResult>>> recognizeBatchLinkedEntitiesWithResponse(
List<TextDocumentInput> inputs, TextAnalyticsRequestOptions options, Context context) {
// TODO: validate multiLanguageBatchInput
return client.entitiesLinkingWithRestResponseAsync(new MultiLanguageBatchInput().setDocuments(inputs),
return service.entitiesLinkingWithRestResponseAsync(new MultiLanguageBatchInput().setDocuments(inputs),
options.getModelVersion(), options.showStatistics(), context)
.map(response -> new SimpleResponse<>(response, null));
}
Expand Down Expand Up @@ -404,7 +453,7 @@ public Mono<Response<DocumentResultCollection<KeyPhraseResult>>> extractBatchKey

Mono<Response<DocumentResultCollection<KeyPhraseResult>>> extractBatchKeyPhrasesWithResponse(
List<TextDocumentInput> document, TextAnalyticsRequestOptions options, Context context) {
return client.keyPhrasesWithRestResponseAsync(new MultiLanguageBatchInput().setDocuments(document),
return service.keyPhrasesWithRestResponseAsync(new MultiLanguageBatchInput().setDocuments(document),
options.getModelVersion(), options.showStatistics(), context).map(response ->
new SimpleResponse<>(response, null));
}
Expand Down Expand Up @@ -450,19 +499,19 @@ public Mono<Response<TextSentimentResult>> analyzeSentimentWithResponse(String i
// }

@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentResultCollection<TextSentimentResult>> analyzeSentiment(List<String> inputs) {
public Mono<DocumentResultCollection<TextSentimentResult>> analyzeSentiment(List<String> inputs) {
return null;
}

@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentResultCollection<TextSentimentResult>> analyzeSentiment(List<String> inputs, String language) {
public Mono<DocumentResultCollection<TextSentimentResult>> analyzeSentiment(List<String> inputs, String language) {
return null;
}

// advantage user
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentResultCollection<TextSentimentResult>> analyzeBatchSentiment(List<TextDocumentInput> inputs) {
return null;
return null;
}

@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -486,20 +535,8 @@ public Mono<Response<DocumentResultCollection<TextSentimentResult>>> analyzeBatc
Mono<Response<DocumentResultCollection<TextSentimentResult>>> analyzeBatchSentimentWithResponse(
List<TextDocumentInput> document, TextAnalyticsRequestOptions options, Context context) {
// TODO: validate multiLanguageBatchInput
return client.sentimentWithRestResponseAsync(
return service.sentimentWithRestResponseAsync(
new MultiLanguageBatchInput().setDocuments(document), options.getModelVersion(), options.showStatistics(),
context).map(response -> new SimpleResponse<>(response, null));
}


// private DocumentResultCollection toDocumentResultCollection(Object object) {
// // TODO: add support to
// // if (object instanceof ) {
// //
// // }
// // return new DocumentResultCollection();
// return null;
// }


}
Loading

0 comments on commit 4df74b0

Please sign in to comment.