|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 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 | + * https://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 | +// DO NOT EDIT! This is a generated sample ("Request", "automl_natural_language_create_dataset") |
| 17 | +// sample-metadata: |
| 18 | +// title: Create Dataset |
| 19 | +// description: Create Dataset |
| 20 | +// usage: gradle run -PmainClass=com.google.cloud.examples.automl.v1beta1.AutomlNaturalLanguageCreateDataset [--args='[--display_name "My_Dataset_Name_123"] [--project "[Google Cloud Project ID]"]'] |
| 21 | + |
| 22 | +package com.google.cloud.examples.automl.v1beta1; |
| 23 | + |
| 24 | +import com.google.cloud.automl.v1beta1.AutoMlClient; |
| 25 | +import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType; |
| 26 | +import com.google.cloud.automl.v1beta1.CreateDatasetRequest; |
| 27 | +import com.google.cloud.automl.v1beta1.Dataset; |
| 28 | +import com.google.cloud.automl.v1beta1.LocationName; |
| 29 | +import com.google.cloud.automl.v1beta1.TextClassificationDatasetMetadata; |
| 30 | +import org.apache.commons.cli.CommandLine; |
| 31 | +import org.apache.commons.cli.DefaultParser; |
| 32 | +import org.apache.commons.cli.Option; |
| 33 | +import org.apache.commons.cli.Options; |
| 34 | + |
| 35 | +public class AutomlNaturalLanguageCreateDataset { |
| 36 | + // [START automl_natural_language_create_dataset] |
| 37 | + /* |
| 38 | + * Please include the following imports to run this sample. |
| 39 | + * |
| 40 | + * import com.google.cloud.automl.v1beta1.AutoMlClient; |
| 41 | + * import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType; |
| 42 | + * import com.google.cloud.automl.v1beta1.CreateDatasetRequest; |
| 43 | + * import com.google.cloud.automl.v1beta1.Dataset; |
| 44 | + * import com.google.cloud.automl.v1beta1.LocationName; |
| 45 | + * import com.google.cloud.automl.v1beta1.TextClassificationDatasetMetadata; |
| 46 | + */ |
| 47 | + |
| 48 | + /** |
| 49 | + * Create Dataset |
| 50 | + * |
| 51 | + * @param displayName The name of the dataset to show in the interface. The name can be up to 32 |
| 52 | + * characters long and can consist only of ASCII Latin letters A-Z and a-z, underscores (_), |
| 53 | + * and ASCII digits 0-9. Must be unique within the scope of the provided GCP Project and |
| 54 | + * Location. |
| 55 | + * @param project Required. Your Google Cloud Project ID. |
| 56 | + */ |
| 57 | + public static void sampleCreateDataset(String displayName, String project) { |
| 58 | + try (AutoMlClient autoMlClient = AutoMlClient.create()) { |
| 59 | + // displayName = "My_Dataset_Name_123"; |
| 60 | + // project = "[Google Cloud Project ID]"; |
| 61 | + LocationName parent = LocationName.of(project, "us-central1"); |
| 62 | + |
| 63 | + // User-provided description of dataset (optional) |
| 64 | + String description = "Description of this dataset"; |
| 65 | + |
| 66 | + // Type of the classification problem. |
| 67 | + // MULTICLASS: At most one label is allowed per example. |
| 68 | + // MULTILABEL: Multiple labels are allowed for one example. |
| 69 | + ClassificationType classificationType = ClassificationType.MULTILABEL; |
| 70 | + TextClassificationDatasetMetadata textClassificationDatasetMetadata = |
| 71 | + TextClassificationDatasetMetadata.newBuilder() |
| 72 | + .setClassificationType(classificationType) |
| 73 | + .build(); |
| 74 | + Dataset dataset = |
| 75 | + Dataset.newBuilder() |
| 76 | + .setDisplayName(displayName) |
| 77 | + .setDescription(description) |
| 78 | + .setTextClassificationDatasetMetadata(textClassificationDatasetMetadata) |
| 79 | + .build(); |
| 80 | + CreateDatasetRequest request = |
| 81 | + CreateDatasetRequest.newBuilder() |
| 82 | + .setParent(parent.toString()) |
| 83 | + .setDataset(dataset) |
| 84 | + .build(); |
| 85 | + Dataset response = autoMlClient.createDataset(request); |
| 86 | + System.out.println("Created Dataset."); |
| 87 | + Dataset dataset = response; |
| 88 | + // Print out the full name of the created dataset. |
| 89 | + // |
| 90 | + // This will have the format: |
| 91 | + // projects/[Google Cloud Project Number]/locations/us-central1/datasets/VOT1234567890123456789 |
| 92 | + // |
| 93 | + // The Dataset ID is the generated identifer in this path, e.g. VOT1234567890123456789 |
| 94 | + // You will need this ID to perform operations on the dataset as well as to create a model. |
| 95 | + // |
| 96 | + System.out.printf("Name: %s\n", dataset.getName()); |
| 97 | + // Print out the Display Name (the text you provided during creation) |
| 98 | + System.out.printf("Display Name: %s\n", dataset.getDisplayName()); |
| 99 | + // Print out the user-provided description (may be blank) |
| 100 | + System.out.printf("Description: %s\n", dataset.getDescription()); |
| 101 | + // The number of examples in the dataset, if any. |
| 102 | + // Added by importing data via importData |
| 103 | + // |
| 104 | + System.out.printf("Example count: %s\n", dataset.getExampleCount()); |
| 105 | + // Get the classification type |
| 106 | + System.out.printf( |
| 107 | + "Text Classification Type: %s\n", |
| 108 | + dataset.getTextClassificationDatasetMetadata().getClassificationType()); |
| 109 | + } catch (Exception exception) { |
| 110 | + System.err.println("Failed to create the client due to: " + exception); |
| 111 | + } |
| 112 | + } |
| 113 | + // [END automl_natural_language_create_dataset] |
| 114 | + |
| 115 | + public static void main(String[] args) throws Exception { |
| 116 | + Options options = new Options(); |
| 117 | + options.addOption( |
| 118 | + Option.builder("").required(false).hasArg(true).longOpt("display_name").build()); |
| 119 | + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("project").build()); |
| 120 | + |
| 121 | + CommandLine cl = (new DefaultParser()).parse(options, args); |
| 122 | + String displayName = cl.getOptionValue("display_name", "My_Dataset_Name_123"); |
| 123 | + String project = cl.getOptionValue("project", "[Google Cloud Project ID]"); |
| 124 | + |
| 125 | + sampleCreateDataset(displayName, project); |
| 126 | + } |
| 127 | +} |
0 commit comments