In [1]:
import IPython.display
IPython.display.YouTubeVideo('4m44aPkLY2k')
Out[1]:
From the same API console, choose "Dashboard" on the left-hand menu and "Enable API".
Enable the following APIs for your project (search for them) if they are not already enabled:
Finally, because we are calling the APIs from Python (clients in many other languages are available), let's install the Python package (it's not installed by default on Datalab)
In [2]:
# !pip install --upgrade google-cloud-language
In [3]:
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
In [4]:
# Imports useful library
import six, sys
In [5]:
# [Optional] Display location of service account API key if defined in GOOGLE_APPLICATION_CREDENTIALS
!echo $GOOGLE_APPLICATION_CREDENTIALS
In [6]:
##################################################################
# (1) Instantiates a client - using GOOGLE_APPLICATION_CREDENTIALS
# client_language = language.LanguageServiceClient()
#
# (2) Instantiates a client - using 'service account json' file
client_language = language.LanguageServiceClient.from_service_account_json(
"/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
In [7]:
def didi_sentiment_text(text):
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
##################################################################
# (1) Instantiates a client - using GOOGLE_APPLICATION_CREDENTIALS
# client_language = language.LanguageServiceClient()
#
# (2) Instantiates a client - using 'service account json' file
client_language = language.LanguageServiceClient.from_service_account_json(
"/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client_language.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print(' ')
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
return sentiment
In [8]:
text=u'Alvin Khoo: One of the most useful things we learnt at ISS was to be analytical and to be able to adapt, \
especially in an industry that is changing so quickly. What ISS gave us was an invaluable reference point.'
In [9]:
cloud_response = didi_sentiment_text(text)
In [10]:
print('[ cloud_response ]\n{}'.format(cloud_response))
In [11]:
def didi_entity_sentiment_text(text):
"""Detects entity sentiment in the provided text."""
##################################################################
# (1) Instantiates a client - using GOOGLE_APPLICATION_CREDENTIALS
# client_language = language.LanguageServiceClient()
#
# (2) Instantiates a client - using 'service account json' file
client_language = language.LanguageServiceClient.from_service_account_json(
"/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
document = types.Document(
content=text.encode('utf-8'),
type=enums.Document.Type.PLAIN_TEXT)
# Detect and send native Python encoding to receive correct word offsets.
encoding = enums.EncodingType.UTF32
if sys.maxunicode == 65535:
encoding = enums.EncodingType.UTF16
result = client_language.analyze_entity_sentiment(document, encoding)
for entity in result.entities:
print('Mentions: ')
print(u'Name: "{}"'.format(entity.name))
for mention in entity.mentions:
print(u' Begin Offset : {}'.format(mention.text.begin_offset))
print(u' Content : {}'.format(mention.text.content))
print(u' Magnitude : {}'.format(mention.sentiment.magnitude))
print(u' Sentiment : {}'.format(mention.sentiment.score))
print(u' Type : {}'.format(mention.type))
print(u'Salience: {}'.format(entity.salience))
print(u'Sentiment: {}\n'.format(entity.sentiment))
return result
In [12]:
text=u'Alvin Khoo: One of the most useful things we learnt at ISS was to be analytical and to be able to adapt, \
especially in an industry that is changing so quickly. What ISS gave us was an invaluable reference point.'
In [13]:
cloud_response = didi_entity_sentiment_text(text)
In [14]:
print('[ cloud_response ]\n{}'.format(cloud_response))
In [15]:
def didi_entities_text(text):
"""Detects entities in the text."""
##################################################################
# (1) Instantiates a client - using GOOGLE_APPLICATION_CREDENTIALS
# client_language = language.LanguageServiceClient()
#
# (2) Instantiates a client - using 'service account json' file
client_language = language.LanguageServiceClient.from_service_account_json(
"/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
# Instantiates a plain text document.
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects entities in the document. You can also analyze HTML with:
# document.type == enums.Document.Type.HTML
entities = client_language.analyze_entities(document).entities
# entity types from enums.Entity.Type
entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION',
'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER')
for entity in entities:
print('=' * 20)
print(u'{:<16}: {}'.format('name', entity.name))
print(u'{:<16}: {}'.format('type', entity_type[entity.type]))
print(u'{:<16}: {}'.format('metadata', entity.metadata))
print(u'{:<16}: {}'.format('salience', entity.salience))
print(u'{:<16}: {}'.format('wikipedia_url',
entity.metadata.get('wikipedia_url', '-')))
return entities
In [16]:
text=u'Alvin Khoo: One of the most useful things we learnt at ISS was to be analytical and to be able to adapt, \
especially in an industry that is changing so quickly. What ISS gave us was an invaluable reference point.'
In [17]:
cloud_response = didi_entities_text(text)
In [18]:
print('[ cloud_response ]\n{}'.format(cloud_response))
In [19]:
def didi_syntax_text(text):
"""Detects syntax in the text."""
##################################################################
# (1) Instantiates a client - using GOOGLE_APPLICATION_CREDENTIALS
# client_language = language.LanguageServiceClient()
#
# (2) Instantiates a client - using 'service account json' file
client_language = language.LanguageServiceClient.from_service_account_json(
"/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
# Instantiates a plain text document.
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects syntax in the document. You can also analyze HTML with:
# document.type == enums.Document.Type.HTML
tokens = client_language.analyze_syntax(document).tokens
# part-of-speech tags from enums.PartOfSpeech.Tag
pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM',
'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX')
for token in tokens:
print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag],
token.text.content))
return tokens
In [20]:
text=u'Alvin Khoo: One of the most useful things we learnt at ISS was to be analytical and to be able to adapt, \
especially in an industry that is changing so quickly. What ISS gave us was an invaluable reference point.'
In [21]:
cloud_response = didi_syntax_text(text)
In [22]:
print('[ cloud_response ]\n{}'.format(cloud_response))
In [23]:
def didi_classify_text(text):
"""Classifies content categories of the provided text."""
##################################################################
# (1) Instantiates a client - using GOOGLE_APPLICATION_CREDENTIALS
# client_language = language.LanguageServiceClient()
#
# (2) Instantiates a client - using 'service account json' file
client_language = language.LanguageServiceClient.from_service_account_json(
"/media/sf_vm_shared_folder/000-cloud-api-key/mtech-ai-7b7e049cf5f6.json")
##################################################################
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
document = types.Document(
content=text.encode('utf-8'),
type=enums.Document.Type.PLAIN_TEXT)
categories = client_language.classify_text(document).categories
for category in categories:
print(u'=' * 20)
print(u'{:<16}: {}'.format('name', category.name))
print(u'{:<16}: {}'.format('confidence', category.confidence))
return categories
In [24]:
text=u'Alvin Khoo: One of the most useful things we learnt at ISS was to be analytical and to be able to adapt, \
especially in an industry that is changing so quickly. What ISS gave us was an invaluable reference point.'
In [25]:
cloud_response = didi_classify_text(text)
In [26]:
print('[ cloud_response ]\n{}'.format(cloud_response))
In [27]:
text=u'GU Zhan (Sam) lectures Master of Technology programme in the areas of data science, machine intelligence, \
soft computing, and applied deep learning. Prior to joining ISS, he was in New Zealand running Kudos Data \
start-up, which focussed on training programs to democratize artificial intelligence and machine learning. \
Before that, he was a data scientist in Yokogawa Engineering, leading industrial innovation initiatives. \
Sam had also spent many years in financial sector wearing versatile hats: project manager, consultant, \
and business analyst in Barclays bank, system manager and senior software engineer at Citibank, leading \
to experience in actualizing information technology values in complex business environment. \
He devotes himself into pedagogy, and is very passionate in inspiring next generation of \
artificial intelligence lovers and leaders.'
In [28]:
cloud_response = didi_classify_text(text)
In [29]:
print('[ cloud_response ]\n{}'.format(cloud_response))
In [30]:
def didi_language_processing(text):
nlp_reply = u'[ NLP 自然语言处理结果 ]\n\n'
nlp_reply += u'[ didi_sentiment_text ]\n\n' + str(didi_sentiment_text(text)) + u'\n\n'
nlp_reply += u'[ didi_entity_sentiment_text ]\n\n' + str(didi_entity_sentiment_text(text)) + u'\n\n'
nlp_reply += u'[ didi_entities_text ]\n\n' + str(didi_entities_text(text)) + u'\n\n'
nlp_reply += u'[ didi_syntax_text ]\n\n' + str(didi_syntax_text(text)) + u'\n\n'
nlp_reply += u'[ didi_classify_text ]\n\n' + str(didi_classify_text(text)) + u'\n\n'
return nlp_reply
In [31]:
demo_text = 'Sam devotes himself into pedagogy, and is very passionate in inspiring next generation of \
artificial intelligence lovers and leaders.'
demo_nlp_reply = didi_language_processing(demo_text)
In [32]:
print(demo_nlp_reply)
In [33]:
# Comment some API which does not support Chinese language:
def didi_language_processing_chinese(text):
nlp_reply = u'[ NLP 自然语言处理结果 ]\n\n'
nlp_reply += u'[ didi_sentiment_text ]\n\n' + str(didi_sentiment_text(text)) + u'\n\n'
# nlp_reply += u'[ didi_entity_sentiment_text ]\n\n' + str(didi_entity_sentiment_text(text)) + u'\n\n'
nlp_reply += u'[ didi_entities_text ]\n\n' + str(didi_entities_text(text)) + u'\n\n'
nlp_reply += u'[ didi_syntax_text ]\n\n' + str(didi_syntax_text(text)) + u'\n\n'
# nlp_reply += u'[ didi_classify_text ]\n\n' + str(didi_classify_text(text)) + u'\n\n'
return nlp_reply
In [34]:
demo_text = '山姆致力于教育学,并且非常热衷于激励下一代人工智能爱好者和领导者。'
demo_nlp_reply = didi_language_processing_chinese(demo_text)
In [35]:
print(demo_nlp_reply)
In [36]:
# import io, os, subprocess, sys, re, codecs, time, datetime, requests, itchat
import itchat
from itchat.content import *
In [37]:
# itchat.auto_login(hotReload=True) # hotReload=True: 退出程序后暂存登陆状态。即使程序关闭,一定时间内重新开启也可以不用重新扫码。
itchat.auto_login(enableCmdQR=-2) # enableCmdQR=-2: 命令行显示QR图片
In [38]:
# Obtain my own Nick Name
MySelf = itchat.search_friends()
NickName4RegEx = '@' + MySelf['NickName'] + '\s*'
In [39]:
# 单聊模式,自动进行自然语言分析,以文本形式返回处理结果:
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
text = msg['Content']
# call NLP API:
nlp_responses = didi_language_processing(text)
# nlp_responses = didi_language_processing_chinese(text)
# Format NLP results:
nlp_reply = nlp_responses # Exercise / Workshop Enhancement: to parse and format results nicely.
print(nlp_reply)
return nlp_reply
In [40]:
# 群聊模式,如果收到 @ 自己的文字信息,会自动进行自然语言分析,以文本形式返回处理结果:
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
if msg['isAt']:
text = re.sub(NickName4RegEx, '', msg['Content'])
# call NLP API:
nlp_responses = didi_language_processing(text)
# nlp_responses = didi_language_processing_chinese(text)
# Format NLP results:
nlp_reply = nlp_responses # Exercise / Workshop Enhancement: to parse and format results nicely.
print(nlp_reply)
return nlp_reply
In [41]:
itchat.run()
In [42]:
# interupt kernel, then logout
itchat.logout() # 安全退出
Out[42]: