-
-
Save daspecster/ed566b5a63f3a7e50c879e05d4c30f09 to your computer and use it in GitHub Desktop.
Language Snippet Results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.cloud import language | |
from google.cloud import storage | |
storage_client = storage.Client() | |
bucket = storage_client.bucket('my-bucket24356uytrew') | |
blob = storage.Blob('sentiment-data.txt', bucket) | |
blob.upload_from_string( | |
'Google, headquartered in Mountain View, unveiled the ' | |
'new Android phone at the Consumer Electronic Show. ' | |
'Sundar Pichai said in his keynote that users love ' | |
'their new Android phones.') | |
client = language.Client() | |
text_content = 'hola senior' | |
document = client.document_from_text( | |
text_content, language='es', encoding=language.Encoding.UTF16) | |
text_content = ( | |
'Google, headquartered in Mountain View, unveiled the ' | |
'new Android phone at the Consumer Electronic Show. ' | |
'Sundar Pichai said in his keynote that users love ' | |
'their new Android phones.') | |
document = client.document_from_text(text_content) | |
document.doc_type == language.Document.PLAIN_TEXT | |
""" | |
REPL Output: | |
True | |
""" | |
html_content = """\ | |
<html> | |
<head> | |
<title>El Tiempo de las Historias</time> | |
</head> | |
<body> | |
<p>La vaca saltó sobre la luna.</p> | |
</body> | |
</html> | |
""" | |
document = client.document_from_html(html_content, language='es') | |
print(document) | |
""" | |
Output: | |
<google.cloud.language.document.Document object at 0x1056ee250> | |
""" | |
gcs_url = 'gs://my-bucket24356uytrew/ssentiment-data.txt' | |
document = client.document_from_url( | |
gcs_url, doc_type=language.Document.HTML) | |
document.gcs_url == gcs_url | |
document.doc_type == language.Document.PLAIN_TEXT | |
document = client.document_from_url(gcs_url, doc_type=language.Document.HTML) | |
print(document) | |
""" | |
Output: | |
<google.cloud.language.document.Document object at 0x108352250> | |
""" | |
text_content = ("Michelangelo Caravaggio, Italian painter, is " | |
"known for 'The Calling of Saint Matthew'.") | |
document = client.document_from_text(text_content) | |
entity_response = document.analyze_entities() | |
for entity in entity_response.entities: | |
print('=' * 20) | |
print(' name: %s' % (entity.name,)) | |
print(' type: %s' % (entity.entity_type,)) | |
print(' metadata: %s' % (entity.metadata,)) | |
print(' salience: %s' % (entity.salience,)) | |
""" | |
Output: | |
==================== | |
name: Michelangelo Caravaggio | |
type: PERSON | |
metadata: {u'mid': u'/m/020bg', u'wikipedia_url': u'http://en.wikipedia.org/wiki/Caravaggio'} | |
salience: 0.81965727 | |
==================== | |
name: Italian | |
type: LOCATION | |
metadata: {u'mid': u'/m/03rjj', u'wikipedia_url': u'http://en.wikipedia.org/wiki/Italy'} | |
salience: 0.14426395 | |
==================== | |
name: The Calling of Saint Matthew | |
type: EVENT | |
metadata: {u'mid': u'/m/085_p7', u'wikipedia_url': u'http://en.wikipedia.org/wiki/The_Calling_of_St_Matthew_(Caravaggio)'} | |
salience: 0.036078777 | |
""" | |
text_content = "Jogging isn't very fun." | |
document = client.document_from_text(text_content) | |
sentiment_response = document.analyze_sentiment() | |
sentiment = sentiment_response.sentiment | |
print(sentiment.score) | |
print(sentiment.magnitude) | |
""" | |
Output: | |
0 | |
0 | |
""" | |
text_content = 'The cow jumped over the Moon.' | |
document = client.document_from_text(text_content) | |
annotations = document.annotate_text() | |
# Sentences present if include_syntax=True | |
print(annotations.sentences) | |
['The cow jumped over the Moon.'] | |
# Tokens present if include_syntax=True | |
for token in annotations.tokens: | |
msg = '%11s: %s' % (token.part_of_speech, token.text_content) | |
print(msg) | |
# Sentiment present if include_sentiment=True | |
print(annotations.sentiment.score) | |
print(annotations.sentiment.magnitude) | |
# Entities present if include_entities=True | |
for entity in annotations.entities: | |
print('=' * 20) | |
print(' name: %s' % (entity.name,)) | |
print(' type: %s' % (entity.entity_type,)) | |
print(' metadata: %s' % (entity.metadata,)) | |
print(' salience: %s' % (entity.salience,)) | |
""" | |
Output: | |
[<google.cloud.language.sentence.Sentence object at 0x108352390>] | |
DET: The | |
NOUN: cow | |
VERB: jumped | |
ADP: over | |
DET: the | |
NOUN: Moon | |
PUNCT: . | |
0 | |
0 | |
==================== | |
name: cow | |
type: OTHER | |
metadata: {} | |
salience: 0.84928745 | |
==================== | |
name: Moon | |
type: LOCATION | |
metadata: {u'mid': u'/m/0d_23', u'wikipedia_url': u'http://en.wikipedia.org/wiki/Natural_satellite'} | |
salience: 0.15071256 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment