|
| 1 | +/* |
| 2 | + * Copyright 2023 Basis Technology Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.basistech.rosette.examples; |
| 17 | + |
| 18 | +import com.basistech.rosette.api.HttpRosetteAPI; |
| 19 | +import com.basistech.rosette.api.MorphologicalFeature; |
| 20 | +import com.basistech.rosette.api.RosetteRequest; |
| 21 | +import com.basistech.rosette.apimodel.DocumentRequest; |
| 22 | +import com.basistech.rosette.apimodel.EntitiesOptions; |
| 23 | +import com.basistech.rosette.apimodel.EntitiesResponse; |
| 24 | +import com.basistech.rosette.apimodel.LanguageOptions; |
| 25 | +import com.basistech.rosette.apimodel.LanguageResponse; |
| 26 | +import com.basistech.rosette.apimodel.MorphologyOptions; |
| 27 | +import com.basistech.rosette.apimodel.MorphologyResponse; |
| 28 | +import com.basistech.rosette.apimodel.Name; |
| 29 | +import com.basistech.rosette.apimodel.NameDeduplicationRequest; |
| 30 | +import com.basistech.rosette.apimodel.Response; |
| 31 | +import com.basistech.rosette.apimodel.TokensResponse; |
| 32 | +import com.basistech.rosette.apimodel.TransliterationResponse; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.List; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | +import java.util.concurrent.Future; |
| 40 | + |
| 41 | +import static com.basistech.rosette.api.common.AbstractRosetteAPI.ENTITIES_SERVICE_PATH; |
| 42 | +import static com.basistech.rosette.api.common.AbstractRosetteAPI.LANGUAGE_SERVICE_PATH; |
| 43 | +import static com.basistech.rosette.api.common.AbstractRosetteAPI.MORPHOLOGY_SERVICE_PATH; |
| 44 | +import static com.basistech.rosette.api.common.AbstractRosetteAPI.NAME_DEDUPLICATION_SERVICE_PATH; |
| 45 | +import static com.basistech.rosette.api.common.AbstractRosetteAPI.TOKENS_SERVICE_PATH; |
| 46 | +import static com.basistech.rosette.api.common.AbstractRosetteAPI.TRANSLITERATION_SERVICE_PATH; |
| 47 | + |
| 48 | +/** |
| 49 | + * Example which demonstrates the usage of concurrent requests |
| 50 | + */ |
| 51 | +public final class ConcurrencyExample extends ExampleBase { |
| 52 | + public static void main(String[] args) { |
| 53 | + try { |
| 54 | + new ConcurrencyExample().run(); |
| 55 | + } catch (Exception e) { |
| 56 | + e.printStackTrace(); |
| 57 | + System.exit(1); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void run() throws IOException, ExecutionException, InterruptedException { |
| 62 | + //Setting up the Api |
| 63 | + int maximumConcurrency = 3; |
| 64 | + HttpRosetteAPI rosetteApi = new HttpRosetteAPI.Builder() |
| 65 | + .key("getApiKeyFromSystemProperty") |
| 66 | + .url("http://localhost:8181/rest/v1") |
| 67 | + .connectionConcurrency(maximumConcurrency) |
| 68 | + .build(); |
| 69 | + |
| 70 | + List<RosetteRequest<? extends Response>> threads = new ArrayList<>(); |
| 71 | + // Setting up entities request |
| 72 | + String entitiesTextData = |
| 73 | + "The Securities and Exchange Commission today announced the leadership of the agency’s trial unit. " |
| 74 | + + "Bridget Fitzpatrick has been named Chief Litigation Counsel of the SEC " |
| 75 | + + "and David Gottesman will continue to serve as the agency’s Deputy Chief Litigation Counsel. " |
| 76 | + + "Since December 2016, Ms. Fitzpatrick and Mr. Gottesman have served as Co-Acting Chief Litigation Counsel. " |
| 77 | + + "In that role, they were jointly responsible for supervising the trial unit at the agency’s Washington D.C. headquarters " |
| 78 | + + "as well as coordinating with litigators in the SEC’s 11 regional offices around the country."; |
| 79 | + threads.add( |
| 80 | + rosetteApi.createRosetteRequest(ENTITIES_SERVICE_PATH, |
| 81 | + DocumentRequest.<EntitiesOptions>builder().content(entitiesTextData).build(), |
| 82 | + EntitiesResponse.class) |
| 83 | + ); |
| 84 | + // Setting up language request |
| 85 | + String languageData = "Por favor Señorita, says the man."; |
| 86 | + threads.add( |
| 87 | + rosetteApi.createRosetteRequest(LANGUAGE_SERVICE_PATH, |
| 88 | + DocumentRequest.<LanguageOptions>builder().content(languageData).build(), |
| 89 | + LanguageResponse.class) |
| 90 | + ); |
| 91 | + // Setting up morphology request |
| 92 | + String morphologyCompleteData = "The quick brown fox jumped over the lazy dog. 👍🏾 Yes he did. B)"; |
| 93 | + threads.add( |
| 94 | + rosetteApi.createRosetteRequest(MORPHOLOGY_SERVICE_PATH + "/" + MorphologicalFeature.COMPLETE, |
| 95 | + DocumentRequest.<MorphologyOptions>builder().content(morphologyCompleteData).build(), |
| 96 | + MorphologyResponse.class) |
| 97 | + ); |
| 98 | + //Setting up names deduplication request |
| 99 | + String nameDedupeData = "John Smith,Johnathon Smith,Fred Jones"; |
| 100 | + List<String> listOfNames = new ArrayList<>(Arrays.asList(nameDedupeData.split(","))); |
| 101 | + |
| 102 | + ArrayList<Name> names = new ArrayList<>(); |
| 103 | + for (String name: listOfNames) { |
| 104 | + names.add(Name.builder().text(name).build()); |
| 105 | + } |
| 106 | + double threshold = 0.75; |
| 107 | + threads.add( |
| 108 | + rosetteApi.createRosetteRequest(NAME_DEDUPLICATION_SERVICE_PATH, |
| 109 | + NameDeduplicationRequest.builder().names(names).threshold(threshold).build(), |
| 110 | + MorphologyResponse.class) |
| 111 | + ); |
| 112 | + //Setting up the tokens request |
| 113 | + String tokensData = "北京大学生物系主任办公室内部会议"; |
| 114 | + threads.add( |
| 115 | + rosetteApi.createRosetteRequest(TOKENS_SERVICE_PATH, |
| 116 | + DocumentRequest.builder().content(tokensData).build(), |
| 117 | + TokensResponse.class) |
| 118 | + ); |
| 119 | + //Setting up the transliteration request |
| 120 | + String transliterationData = "ana r2ye7 el gam3a el sa3a 3 el 3asr"; |
| 121 | + threads.add( |
| 122 | + rosetteApi.createRosetteRequest(TRANSLITERATION_SERVICE_PATH, |
| 123 | + DocumentRequest.builder().content(transliterationData).build(), |
| 124 | + TransliterationResponse.class) |
| 125 | + ); |
| 126 | + |
| 127 | + // start the threads |
| 128 | + List<Future<? extends Response>> futures = rosetteApi.submitRequests(threads); |
| 129 | + |
| 130 | + // wait for the threads to finish |
| 131 | + for (int i = 0; i < threads.size(); i++) { |
| 132 | + futures.get(i).get(); |
| 133 | + } |
| 134 | + |
| 135 | + for (int i = 0; i < threads.size(); i++) { |
| 136 | + System.out.println(responseToJson(threads.get(i).getResponse())); |
| 137 | + } |
| 138 | + rosetteApi.close(); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +} |
0 commit comments