Skip to content

Commit 1a514ca

Browse files
author
Rebecca Taylor
committed
Language samples: Sentiment, Entities
language_entities_gcs_test.py::test_language_entities_text PASSED language_entities_text_test.py::test_language_entities_text PASSED language_sentiment_gcs_test.py::test_language_sentiment_gcs_positive_sentiment PASSED language_sentiment_gcs_test.py::test_language_sentiment_gcs_negative_sentiment PASSED language_sentiment_text_test.py::test_language_sentiment_text_positive_sentiment PASSED language_sentiment_text_test.py::test_language_sentiment_text_negative_sentiment PASSED
1 parent 49d5851 commit 1a514ca

File tree

6 files changed

+296
-0
lines changed

6 files changed

+296
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
# To install the latest published package dependency, execute the following:
18+
# pip install google-cloud-language
19+
20+
# [START language_entities_gcs]
21+
from google.cloud import language
22+
from google.cloud.language import enums
23+
from google.cloud.language import types
24+
25+
import six
26+
27+
def analyze_entities(
28+
gcs_uri='gs://cloud-samples-data/language/california.txt'):
29+
"""Analyze entities from a text file in Google Cloud Storage
30+
Args:
31+
gcs_uri: Path to file in GCS, e.g. gs://bucket/file.txt
32+
"""
33+
34+
client = language.LanguageServiceClient()
35+
36+
if isinstance(gcs_uri, six.binary_type):
37+
gcs_uri = gcs_uri.decode('utf-8')
38+
39+
document = types.Document(
40+
gcs_content_uri=gcs_uri,
41+
type=enums.Document.Type.PLAIN_TEXT)
42+
43+
response = client.analyze_entities(document)
44+
45+
entities = response.entities
46+
47+
for entity in entities:
48+
entity_type = enums.Entity.Type(entity.type)
49+
print('Name: {}'.format(entity.name))
50+
print('Type: {}'.format(entity_type.name))
51+
print('Salience: {}'.format(entity.salience))
52+
print('Wikipedia URL: {}'.format(entity.metadata.get('wikipedia_url', '-')))
53+
print('Knowledge Graph MID: {}'.format(entity.metadata.get('mid', '-')))
54+
# [END language_entities_gcs]
55+
56+
def main():
57+
import argparse
58+
59+
parser = argparse.ArgumentParser()
60+
parser.add_argument(
61+
'--gcs_uri',
62+
type=str,
63+
default='gs://cloud-samples-data/language/california.txt')
64+
args = parser.parse_args()
65+
66+
analyze_entities(args.gcs_uri)
67+
68+
if __name__ == '__main__':
69+
main()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2019, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from __future__ import absolute_import
15+
16+
import os
17+
import re
18+
19+
from language_entities_gcs import analyze_entities
20+
21+
def test_language_entities_text(capsys):
22+
analyze_entities(
23+
gcs_uri='gs://cloud-samples-data/language/california.txt')
24+
out, _ = capsys.readouterr()
25+
26+
assert 'Name: California' in out
27+
assert 'Type: LOCATION' in out
28+
assert re.search('Salience: \d\.\d', out)
29+
assert 'Wikipedia URL: https://en.wikipedia.org/wiki/California' in out
30+
assert 'Knowledge Graph MID: /m/' in out
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
# To install the latest published package dependency, execute the following:
18+
# pip install google-cloud-language
19+
20+
# [START language_entities_text]
21+
22+
from google.cloud import language
23+
from google.cloud.language import enums
24+
from google.cloud.language import types
25+
26+
import six
27+
28+
def analyze_entities(
29+
text_content='California is a state.'):
30+
"""Analyze entities of text
31+
Args:
32+
text: Text to analyze, e.g. 'California is a state.'
33+
"""
34+
35+
client = language.LanguageServiceClient()
36+
37+
if isinstance(text_content, six.binary_type):
38+
text_content = text_content.decode('utf-8')
39+
40+
document = types.Document(
41+
content=text_content,
42+
type=enums.Document.Type.PLAIN_TEXT)
43+
44+
response = client.analyze_entities(document)
45+
46+
entities = response.entities
47+
48+
for entity in entities:
49+
entity_type = enums.Entity.Type(entity.type)
50+
print('Name: {}'.format(entity.name))
51+
print('Type: {}'.format(entity_type.name))
52+
print('Salience: {}'.format(entity.salience))
53+
print('Wikipedia URL: {}'.format(entity.metadata.get('wikipedia_url', '-')))
54+
print('Knowledge Graph MID: {}'.format(entity.metadata.get('mid', '-')))
55+
# [END language_entities_text]
56+
57+
def main():
58+
import argparse
59+
60+
parser = argparse.ArgumentParser()
61+
parser.add_argument(
62+
'--text_content',
63+
type=str,
64+
default='California is a state.')
65+
args = parser.parse_args()
66+
67+
analyze_entities(args.text_content)
68+
69+
if __name__ == '__main__':
70+
main()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2019, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from __future__ import absolute_import
15+
16+
import os
17+
import re
18+
19+
from language_entities_text import analyze_entities
20+
21+
def test_language_entities_text(capsys):
22+
analyze_entities(
23+
text_content='California is a state.')
24+
out, _ = capsys.readouterr()
25+
26+
assert 'Name: California' in out
27+
assert 'Type: LOCATION' in out
28+
assert re.search('Salience: \d\.\d', out)
29+
assert 'Wikipedia URL: https://en.wikipedia.org/wiki/California' in out
30+
assert 'Knowledge Graph MID: /m/' in out
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
# To install the latest published package dependency, execute the following:
18+
# pip install google-cloud-language
19+
20+
# [START language_sentiment_gcs]
21+
from google.cloud import language
22+
from google.cloud.language import enums
23+
from google.cloud.language import types
24+
25+
import six
26+
27+
def analyze_sentiment(
28+
gcs_uri='gs://cloud-samples-data/language/hello.txt'):
29+
"""Analyze sentiment from a text file in Google Cloud Storage
30+
Args:
31+
gcs_uri: Path to file in GCS, e.g. gs://bucket/file.txt
32+
"""
33+
34+
client = language.LanguageServiceClient()
35+
36+
if isinstance(gcs_uri, six.binary_type):
37+
gcs_uri = gcs_uri.decode('utf-8')
38+
39+
document = types.Document(
40+
gcs_content_uri=gcs_uri,
41+
type=enums.Document.Type.PLAIN_TEXT)
42+
43+
response = client.analyze_sentiment(document)
44+
45+
sentiment = response.document_sentiment
46+
print('Score: {}'.format(sentiment.score))
47+
print('Magnitude: {}'.format(sentiment.magnitude))
48+
# [END language_sentiment_gcs]
49+
50+
def main():
51+
import argparse
52+
53+
parser = argparse.ArgumentParser()
54+
parser.add_argument(
55+
'--gcs_uri',
56+
type=str,
57+
default='gs://cloud-samples-data/language/hello.txt')
58+
args = parser.parse_args()
59+
60+
analyze_sentiment(args.gcs_uri)
61+
62+
if __name__ == '__main__':
63+
main()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2019, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from __future__ import absolute_import
15+
16+
import os
17+
18+
from language_sentiment_gcs import analyze_sentiment
19+
20+
def test_language_sentiment_gcs_positive_sentiment(capsys):
21+
analyze_sentiment(
22+
gcs_uri='gs://cloud-samples-data/language/positive.txt')
23+
out, _ = capsys.readouterr()
24+
25+
assert 'Score: 0.' in out
26+
assert 'Magnitude: 0.' in out
27+
28+
def test_language_sentiment_gcs_negative_sentiment(capsys):
29+
analyze_sentiment(
30+
gcs_uri='gs://cloud-samples-data/language/negative.txt')
31+
out, _ = capsys.readouterr()
32+
33+
assert 'Score: -0.' in out
34+
assert 'Magnitude: 0.' in out

0 commit comments

Comments
 (0)