8000 Add sentiment analysis samples · mmmarklu/python-docs-samples@77fc490 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77fc490

Browse files
committed
Add sentiment analysis samples
1 parent d331bc8 commit 77fc490

6 files changed

+285
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def create_dataset(project_id, display_name):
19+
"""Create a dataset."""
20+
# [START automl_language_sentiment_analysis_create_dataset]
21+
from google.cloud import automl
22+
23+
# TODO(developer): Uncomment and set the following variables
24+
# project_id = 'YOUR_PROJECT_ID'
25+
# display_name = 'YOUR_DATASET_NAME'
26+
27+
client = automl.AutoMlClient()
28+
29+
# A resource that represents Google Cloud Platform location.
30+
project_location = client.location_path(project_id, 'us-central1')
31+
metadata = automl.types.TextSentimentDatasetMetadata(
32+
sentiment_max=4) # Possible max sentiment score: 1-10
33+
dataset = automl.types.Dataset(
34+
display_name=display_name,
35+
text_sentiment_dataset_metadata=metadata)
36+
37+
# Create a dataset with the dataset metadata in the region.
38+
response = client.create_dataset(project_location, dataset)
39+
40+
created_dataset = response.result()
41+
42+
# Display the dataset information
43+
print(u'Dataset name: {}'.format(created_dataset.name))
44+
print(u'Dataset id: {}'.format(created_dataset.name.split('/')[-1]))
45+
# [END automl_language_sentiment_analysis_create_dataset]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def create_model(project_id, dataset_id, display_name):
19+
"""Create a model."""
20+
# [START automl_language_sentiment_analysis_create_model]
21+
from google.cloud import automl
22+
23+
# TODO(developer): Uncomment and set the following variables
24+
# project_id = 'YOUR_PROJECT_ID'
25+
# dataset_id = 'YOUR_DATASET_ID'
26+
# display_name = 'YOUR_MODEL_NAME'
27+
28+
client = automl.AutoMlClient()
29+
30+
# A resource that represents Google Cloud Platform location.
31+
project_location = client.location_path(project_id, 'us-central1')
32+
# Leave model unset to use the default base model provided by Google
33+
metadata = automl.types.TextSentimentModelMetadata()
34+
model = automl.types.Model(
35+
display_name=display_name,
36+
dataset_id=dataset_id,
37+
text_sentiment_model_metadata=metadata)
38+
39+
# Create a model with the model metadata in the region.
40+
response = client.create_model(project_location, model)
41+
42+
print(u'Training operation name: {}'.format(response.operation.name))
43+
print('Training started...')
44+
# [END automl_language_sentiment_analysis_create_model]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import datetime
18+
import os
19+
20+
import pytest
21+
22+
import language_sentiment_analysis_create_dataset
23+
import import_dataset
24+
import delete_dataset
25+
import list_datasets
26+
import get_dataset
27+
import export_dataset
28+
29+
PROJECT_ID = os.environ['GCLOUD_PROJECT']
30+
BUCKET_ID = '{}-lcm'.format(PROJECT_ID)
31+
DATASET_ID = 'TST3960250460385409610'
32+
33+
34+
@pytest.mark.slow
35+
def test_create_import_delete_dataset(capsys):
36+
# create dataset
37+
dataset_name = 'test_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S')
38+
language_sentiment_analysis_create_dataset.create_dataset(
39+
PROJECT_ID, dataset_name)
40+
out, _ = capsys.readouterr()
41+
assert 'Dataset id: ' in out
42+
43+
# import data
44+
dataset_id = out.splitlines()[1].split()[2]
45+
data = 'gs://{}/do_not_delete_sentiment_dataset/sentiment_dataset.csv'\
46+
.format(BUCKET_ID)
47+
import_dataset.import_dataset(PROJECT_ID, dataset_id, data)
48+
out, _ = capsys.readouterr()
49+
assert 'Data imported.' in out
50+
51+
# delete dataset
52+
delete_dataset.delete_dataset(PROJECT_ID, dataset_id)
53+
out, _ = capsys.readouterr()
54+
assert 'Dataset deleted.' in out
55+
56+
57+
def test_list_dataset(capsys):
58+
# list datasets
59+
list_datasets.list_datasets(PROJECT_ID)
60+
out, _ = capsys.readouterr()
61+
assert 'Dataset id: {}'.format(DATASET_ID) in out
62+
63+
64+
def test_get_dataset(capsys):
65+
get_dataset.get_dataset(PROJECT_ID, DATASET_ID)
66+
out, _ = capsys.readouterr()
67+
assert 'Dataset name: ' in out
68+
69+
70+
def test_export_dataset(capsys):
71+
export_dataset.export_dataset(
72+
PROJECT_ID,
73+
DATASET_ID,
74+
'gs://{}/TEST_EXPORT_OUTPUT/'.format(BUCKET_ID))
75+
76+
out, _ = capsys.readouterr()
77+
assert 'Dataset exported' in out
78+
79+
from google.cloud import storage
80+
storage_client = storage.Client()
81+
bucket = storage_client.get_bucket(BUCKET_ID)
82+
if len(list(bucket.list_blobs(prefix='TEST_EXPORT_OUTPUT'))) > 0:
83+
for blob in bucket.list_blobs(prefix='TEST_EXPORT_OUTPUT'):
84+
blob.delete()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
19+
import pytest
20+
21+
import deploy_model
22+
import undeploy_model
23+
24+
PROJECT_ID = os.environ['GCLOUD_PROJECT']
25+
MODEL_ID = 'TST8532792392862639819'
26+
27+
28+
@pytest.mark.slow
29+
def test_deploy_undeploy_model(capsys):
30+
undeploy_model.undeploy_model(PROJECT_ID, MODEL_ID)
31+
out, _ = capsys.readouterr()
32+
assert 'Model undeployment finished.' in out
33+
34+
deploy_model.deploy_model(PROJECT_ID, MODEL_ID)
35+
out, _ = capsys.readouterr()
36+
assert 'Model deployment finished.' in out
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google LLC
4+
#
5+
# Lic 10000 ensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def predict(project_id, model_id, content):
19+
"""Predict."""
20+
# [START automl_language_sentiment_analysis_predict]
21+
from google.cloud import automl
22+
23+
# TODO(developer): Uncomment and set the following variables
24+
# project_id = 'YOUR_PROJECT_ID'
25+
# model_id = 'YOUR_MODEL_ID'
26+
# content = 'text to predict'
27+
28+
prediction_client = automl.PredictionServiceClient()
29+
30+
# Get the full path of the model.
31+
model_full_id = prediction_client.model_path(
32+
project_id, 'us-central1', model_id
33+
)
34+
35+
text_snippet = automl.types.TextSnippet(
36+
content=content,
37+
mime_type='text/plain') # Types: 'text/plain', 'text/html'
38+
payload = automl.types.ExamplePayload(text_snippet=text_snippet)
39+
40+
response = prediction_client.predict(model_full_id, payload)
41+
42+
for annotation_payload in response.payload:
43+
print(u'Predicted class name: {}'.format(
44+
annotation_payload.display_name))
45+
print(u'Predicted sentiment score: {}'.format(
46+
annotation_payload.text_sentiment.sentiment))
47+
# [END automl_language_sentiment_analysis_predict]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
19+
import language_sentiment_analysis_predict
20+
21+
PROJECT_ID = os.environ['GCLOUD_PROJECT']
22+
MODEL_ID = 'TST8532792392862639819'
23+
24+
25+
def test_predict(capsys):
26+
text = 'Hopefully this Claritin kicks in soon'
27+
language_sentiment_analysis_predict.predict(PROJECT_ID, MODEL_ID, text)
28+
out, _ = capsys.readouterr()
29+
assert 'Predicted sentiment score: ' in out

0 commit comments

Comments
 (0)
0