テキスト感情分析のデータセットを一覧表示します。
コードサンプル
Go
import (
"context"
"fmt"
"io"
automl "cloud.google.com/go/automl/apiv1"
"cloud.google.com/go/automl/apiv1/automlpb"
"google.golang.org/api/iterator"
)
// listDatasets lists existing datasets.
func listDatasets(w io.Writer, projectID string, location string) error {
// projectID := "my-project-id"
// location := "us-central1"
ctx := context.Background()
client, err := automl.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewClient: %v", err)
}
defer client.Close()
req := &automlpb.ListDatasetsRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
}
it := client.ListDatasets(ctx, req)
// Iterate over all results
for {
dataset, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return fmt.Errorf("ListGlossaries.Next: %v", err)
}
fmt.Fprintf(w, "Dataset name: %v\n", dataset.GetName())
fmt.Fprintf(w, "Dataset display name: %v\n", dataset.GetDisplayName())
fmt.Fprintf(w, "Dataset create time:\n")
fmt.Fprintf(w, "\tseconds: %v\n", dataset.GetCreateTime().GetSeconds())
fmt.Fprintf(w, "\tnanos: %v\n", dataset.GetCreateTime().GetNanos())
// Language sentiment analysis
if metadata := dataset.GetTextSentimentDatasetMetadata(); metadata != nil {
fmt.Fprintf(w, "Text sentiment dataset metadata: %v\n", metadata)
}
}
return nil
}
Java
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.Dataset;
import com.google.cloud.automl.v1.ListDatasetsRequest;
import com.google.cloud.automl.v1.LocationName;
import java.io.IOException;
class ListDatasets {
static void listDatasets() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
listDatasets(projectId);
}
// List the datasets
static void listDatasets(String projectId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");
ListDatasetsRequest request =
ListDatasetsRequest.newBuilder().setParent(projectLocation.toString()).build();
// List all the datasets available in the region by applying filter.
System.out.println("List of datasets:");
for (Dataset dataset : client.listDatasets(request).iterateAll()) {
// Display the dataset information
System.out.format("\nDataset name: %s\n", dataset.getName());
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
// required for other methods.
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
String[] names = dataset.getName().split("/");
String retrievedDatasetId = names[names.length - 1];
System.out.format("Dataset id: %s\n", retrievedDatasetId);
System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
System.out.println("Dataset create time:");
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
System.out.format(
"Text sentiment dataset metadata: %s\n", dataset.getTextSentimentDatasetMetadata());
}
}
}
}
Python
from google.cloud import automl
# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
client = automl.AutoMlClient()
# A resource that represents Google Cloud Platform location.
project_location = f"projects/{project_id}/locations/us-central1"
# List all the datasets available in the region.
request = automl.ListDatasetsRequest(parent=project_location, filter="")
response = client.list_datasets(request=request)
print("List of datasets:")
for dataset in response:
print("Dataset name: {}".format(dataset.name))
print("Dataset id: {}".format(dataset.name.split("/")[-1]))
print("Dataset display name: {}".format(dataset.display_name))
print("Dataset create time: {}".format(dataset.create_time))
print(
"Text sentiment dataset metadata: {}".format(
dataset.text_sentiment_dataset_metadata
)
)
次のステップ
他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。